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 a245017 commit 07b89ed
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 18 deletions.
3 changes: 2 additions & 1 deletion src/Activation/ActivationModule.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public function run(ContainerInterface $container): bool
'init',
[$this, 'pluginInit']
);

add_action('admin_init', [$this, 'mollieWcNoticeApiKeyMissing']);
$this->declareCompatibleWithHPOS();
$this->handleTranslations();
$this->mollieWcNoticeApiKeyMissing();
$this->appleValidationFileRewriteRules();
return true;
}
Expand Down
124 changes: 123 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,125 @@ public function get_settings($currentSection = '')
$currentSection
);
}

/**
* @param $settings
*
* @return array
*/
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
13 changes: 0 additions & 13 deletions src/Settings/Page/Section/ConnectionFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,6 @@ public function config(): array
'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', true);
$shouldShowNotice = $testModeEnabled === 'yes' && !empty($testKeyEntered);
if (!$shouldShowNotice) {
return false;
}
Expand Down

0 comments on commit 07b89ed

Please sign in to comment.