Skip to content

Commit

Permalink
Fix notice behaviour & apply visual fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
inpsyde-maticluznar committed Oct 29, 2024
1 parent 1f470cd commit 6c34de0
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 20 deletions.
5 changes: 4 additions & 1 deletion src/Activation/ActivationModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,12 @@ public function run(ContainerInterface $container): bool
'init',
[$this, 'pluginInit']
);

add_action('admin_init', function () {
$this->mollieWcNoticeApiKeyMissing();
});
$this->declareCompatibleWithHPOS();
$this->handleTranslations();
$this->mollieWcNoticeApiKeyMissing();
$this->appleValidationFileRewriteRules();
return true;
}
Expand Down
119 changes: 118 additions & 1 deletion src/Settings/MollieSettingsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Mollie\WooCommerce\Settings\Page\PageNoApiKey;
use Mollie\WooCommerce\Settings\Page\PagePaymentMethods;
use Mollie\WooCommerce\Shared\Data;
use WC_Admin_Settings;
use WC_Settings_Page;

class MollieSettingsPage extends WC_Settings_Page
Expand Down Expand Up @@ -130,7 +131,7 @@ public function get_settings($currentSection = '')
$this->dataHelper
);
if ($page::slug() === $defaultSection) {
$mollieSettings = $page->settings();
$mollieSettings = $this->hideKeysIntoStars($page->settings());
break;
}
}
Expand All @@ -141,4 +142,120 @@ public function get_settings($currentSection = '')
$currentSection
);
}

protected function hideKeysIntoStars($settings): array
{
$liveKeyName = 'mollie-payments-for-woocommerce_live_api_key';
$testKeyName = 'mollie-payments-for-woocommerce_test_api_key';
$liveValue = get_option($liveKeyName);
$testValue = get_option($testKeyName);

foreach ($settings as $key => $setting) {
if (
($setting['id']
=== $liveKeyName
&& $liveValue)
|| ($setting['id']
=== $testKeyName
&& $testValue)
) {
$settings[$key]['value'] = '**********';
}
}
return $settings;
}

/**
* Save settings
*
* @since 1.0
*/
public function save()
{
global $current_section;

$settings = $this->get_settings($current_section);
$settings = $this->saveApiKeys($settings);
WC_Admin_Settings::save_fields($settings);
}

/**
* @param $settings
*
* @return array
*/
protected function saveApiKeys($settings)
{
$nonce = filter_input(INPUT_POST, '_wpnonce', FILTER_SANITIZE_SPECIAL_CHARS);
$isNonceValid = wp_verify_nonce(
$nonce,
'woocommerce-settings'
);
if (!$isNonceValid) {
return $settings;
}
$liveKeyName = 'mollie-payments-for-woocommerce_live_api_key';
$testKeyName = 'mollie-payments-for-woocommerce_test_api_key';
$liveValueInDb = get_option($liveKeyName);
$testValueInDb = get_option($testKeyName);
$postedLiveValue = isset($_POST[$liveKeyName]) ? sanitize_text_field(wp_unslash($_POST[$liveKeyName])) : '';
$postedTestValue = isset($_POST[$testKeyName]) ? sanitize_text_field(wp_unslash($_POST[$testKeyName])) : '';

foreach ($settings as $setting) {
if (
$setting['id']
=== $liveKeyName
&& $liveValueInDb
) {
if ($postedLiveValue === '**********') {
$_POST[$liveKeyName] = $liveValueInDb;
} else {
$pattern = '/^live_\w{30,}$/';
$this->validateApiKeyOrRemove(
$pattern,
$postedLiveValue,
$liveKeyName
);
}
} elseif (
$setting['id']
=== $testKeyName
&& $testValueInDb
) {
if ($postedTestValue === '**********') {
$_POST[$testKeyName] = $testValueInDb;
} else {
$pattern = '/^test_\w{30,}$/';
$this->validateApiKeyOrRemove(
$pattern,
$postedTestValue,
$testKeyName
);
}
}
}
return $settings;
}

/**
* @param $pattern
* @param $value
* @param $keyName
*
*/
protected function validateApiKeyOrRemove($pattern, $value, $keyName)
{
$nonce = filter_input(INPUT_POST, '_wpnonce', FILTER_SANITIZE_SPECIAL_CHARS);
$isNonceValid = wp_verify_nonce(
$nonce,
'woocommerce-settings'
);
if (!$isNonceValid) {
return;
}
$hasApiFormat = preg_match($pattern, $value);
if (!$hasApiFormat) {
unset($_POST[$keyName]);
}
}
}
13 changes: 13 additions & 0 deletions src/Settings/Page/Section/Advanced.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ public function config(): array
'type' => 'title',
'desc' => '<p>' . __('The following options are required to use the plugin and are used by all Mollie payment methods', 'mollie-payments-for-woocommerce') . '</p>',
],
[
'id' => $this->settings->getSettingId('debug'),
'title' => __('Debug Log', 'mollie-payments-for-woocommerce'),
'type' => 'checkbox',
'desc' => sprintf(
__(
"Log plugin events. <a href='%s'>View logs</a>",
'mollie-payments-for-woocommerce'
),
$this->settings->getLogsUrl()
),
'default' => 'yes',
],
[
'id' => $this->settings->getSettingId('order_status_cancelled_payments'),
'title' => __('Order status after cancelled payment', 'mollie-payments-for-woocommerce'),
Expand Down
16 changes: 1 addition & 15 deletions src/Settings/Page/Section/ConnectionFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,7 @@ public function config(): array
'Test API key should start with test_',
'mollie-payments-for-woocommerce'
),
],
[
'id' => $this->settings->getSettingId('debug'),
'title' => __('Debug Log', 'mollie-payments-for-woocommerce'),
'type' => 'checkbox',
'desc' => sprintf(
__(
"Log plugin events. <a href='%s'>View logs</a>",
'mollie-payments-for-woocommerce'
),
$this->settings->getLogsUrl()
),
'default' => 'yes',
],
[
], [
'id' => $this->settings->getSettingId('sectionend'),
'type' => 'sectionend',
],
Expand Down
4 changes: 2 additions & 2 deletions src/Settings/Page/Section/ConnectionStatusTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ protected function connectionStatus(Settings $settings, bool $connectionStatus):
);
}
if ($testMode) {
return __('Successfully connected with Test API &#x2713;', 'mollie-payments-for-woocommerce');
return __('Successfully connected with <strong>Test API</strong> &#x2713;', 'mollie-payments-for-woocommerce');
}
return __('Successfully connected with Live API &#x2713;', 'mollie-payments-for-woocommerce');
return __('Successfully connected with <strong>Live API</strong> &#x2713;', 'mollie-payments-for-woocommerce');
}
}
3 changes: 2 additions & 1 deletion src/Settings/SettingsModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ public function addPluginActionLinks(array $links): array
public function maybeTestModeNotice(): bool
{
$testModeEnabled = get_option('mollie-payments-for-woocommerce_test_mode_enabled', true);
$shouldShowNotice = $testModeEnabled === 'yes';
$testKeyEntered = get_option('mollie-payments-for-woocommerce_test_api_key');
$shouldShowNotice = $testModeEnabled === 'yes' && !empty($testKeyEntered);
if (!$shouldShowNotice) {
return false;
}
Expand Down

0 comments on commit 6c34de0

Please sign in to comment.