diff --git a/src/Activation/ActivationModule.php b/src/Activation/ActivationModule.php index 598bcef2..03fa454f 100644 --- a/src/Activation/ActivationModule.php +++ b/src/Activation/ActivationModule.php @@ -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; } diff --git a/src/Settings/MollieSettingsPage.php b/src/Settings/MollieSettingsPage.php index 6e225d5e..ac062536 100644 --- a/src/Settings/MollieSettingsPage.php +++ b/src/Settings/MollieSettingsPage.php @@ -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 @@ -130,7 +131,7 @@ public function get_settings($currentSection = '') $this->dataHelper ); if ($page::slug() === $defaultSection) { - $mollieSettings = $page->settings(); + $mollieSettings = $this->hideKeysIntoStars($page->settings()); break; } } @@ -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]); + } + } } diff --git a/src/Settings/Page/Section/Advanced.php b/src/Settings/Page/Section/Advanced.php index df39872b..8b3c8967 100644 --- a/src/Settings/Page/Section/Advanced.php +++ b/src/Settings/Page/Section/Advanced.php @@ -18,6 +18,19 @@ public function config(): array 'type' => 'title', 'desc' => '

' . __('The following options are required to use the plugin and are used by all Mollie payment methods', 'mollie-payments-for-woocommerce') . '

', ], + [ + 'id' => $this->settings->getSettingId('debug'), + 'title' => __('Debug Log', 'mollie-payments-for-woocommerce'), + 'type' => 'checkbox', + 'desc' => sprintf( + __( + "Log plugin events. View logs", + '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'), diff --git a/src/Settings/Page/Section/ConnectionFields.php b/src/Settings/Page/Section/ConnectionFields.php index 681c7d43..0708ba20 100644 --- a/src/Settings/Page/Section/ConnectionFields.php +++ b/src/Settings/Page/Section/ConnectionFields.php @@ -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. View logs", - 'mollie-payments-for-woocommerce' - ), - $this->settings->getLogsUrl() - ), - 'default' => 'yes', - ], [ 'id' => $this->settings->getSettingId('sectionend'), 'type' => 'sectionend', diff --git a/src/Settings/Page/Section/ConnectionStatusTrait.php b/src/Settings/Page/Section/ConnectionStatusTrait.php index ff1c0514..2639fd9b 100644 --- a/src/Settings/Page/Section/ConnectionStatusTrait.php +++ b/src/Settings/Page/Section/ConnectionStatusTrait.php @@ -29,8 +29,8 @@ protected function connectionStatus(Settings $settings, bool $connectionStatus): ); } if ($testMode) { - return __('Successfully connected with Test API ✓', 'mollie-payments-for-woocommerce'); + return __('Successfully connected with Test API ✓', 'mollie-payments-for-woocommerce'); } - return __('Successfully connected with Live API ✓', 'mollie-payments-for-woocommerce'); + return __('Successfully connected with Live API ✓', 'mollie-payments-for-woocommerce'); } } diff --git a/src/Settings/SettingsModule.php b/src/Settings/SettingsModule.php index efdbb459..61ecb663 100644 --- a/src/Settings/SettingsModule.php +++ b/src/Settings/SettingsModule.php @@ -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; }