From 8f3fb307666fdc0ba4d1fe618171444ca291213a Mon Sep 17 00:00:00 2001 From: inpsyde-maticluznar Date: Mon, 30 Sep 2024 13:10:45 +0200 Subject: [PATCH] Basic pages for settings revamp --- public/images/logo/black.svg | 18 +++ src/Settings/MollieSettingsPage.php | 116 ++++++++++++++++++ src/Settings/Page/AbstractPage.php | 55 +++++++++ src/Settings/Page/Components.php | 68 ---------- ...ingsPage.php => MollieSettingsPageOld.php} | 0 src/Settings/Page/PageAdvancedSettings.php | 18 +++ src/Settings/Page/PageApiKeys.php | 18 +++ src/Settings/Page/PageNoApiKey.php | 32 +++++ src/Settings/Page/PagePaymentMethods.php | 18 +++ src/Settings/Page/Section/AbstractSection.php | 30 +++++ .../Page/Section/ConnectionFields.php | 86 +++++++++++++ src/Settings/Page/Section/Header.php | 84 +++++++++++++ src/Settings/Page/Section/Instructions.php | 70 +++++++++++ src/Settings/Page/Section/Notices.php | 28 +++++ src/Settings/SettingsModule.php | 16 +-- 15 files changed, 582 insertions(+), 75 deletions(-) create mode 100644 public/images/logo/black.svg create mode 100644 src/Settings/MollieSettingsPage.php create mode 100644 src/Settings/Page/AbstractPage.php delete mode 100644 src/Settings/Page/Components.php rename src/Settings/Page/{MollieSettingsPage.php => MollieSettingsPageOld.php} (100%) create mode 100644 src/Settings/Page/PageAdvancedSettings.php create mode 100644 src/Settings/Page/PageApiKeys.php create mode 100644 src/Settings/Page/PageNoApiKey.php create mode 100644 src/Settings/Page/PagePaymentMethods.php create mode 100644 src/Settings/Page/Section/AbstractSection.php create mode 100644 src/Settings/Page/Section/ConnectionFields.php create mode 100644 src/Settings/Page/Section/Header.php create mode 100644 src/Settings/Page/Section/Instructions.php create mode 100644 src/Settings/Page/Section/Notices.php diff --git a/public/images/logo/black.svg b/public/images/logo/black.svg new file mode 100644 index 000000000..76e679294 --- /dev/null +++ b/public/images/logo/black.svg @@ -0,0 +1,18 @@ + + + + + + diff --git a/src/Settings/MollieSettingsPage.php b/src/Settings/MollieSettingsPage.php new file mode 100644 index 000000000..53cd41b1d --- /dev/null +++ b/src/Settings/MollieSettingsPage.php @@ -0,0 +1,116 @@ +id = 'mollie_settings'; + $this->label = __('Mollie Settings', 'mollie-payments-for-woocommerce'); + $this->settingsHelper = $settingsHelper; + $this->pluginPath = $pluginPath; + $this->pluginUrl = $pluginUrl; + $this->gateways = $gateways; + $this->isTestModeEnabled = $isTestModeEnabled; + $this->dataHelper = $dataHelper; + $this->paymentMethods = $paymentMethods; + $this->registerContentFieldType(); + $this->outputSections(); + parent::__construct(); + } + + public function registerContentFieldType(): void + { + add_action('woocommerce_admin_field_mollie_custom_input', static function ($value): void { + ?> + + + + + + + + + + + id, + [$this, 'output_sections'] + ); + } + + protected function pages(): array + { + return [ + PageNoApiKey::class, + PageApiKeys::class, + PagePaymentMethods::class, + PageAdvancedSettings::class + ]; + } + + public function get_settings($currentSection = '') + { + $mollieSettings = null; + foreach ($this->pages() as $pageClass) { + /** @var AbstractPage $page */ + $page = new $pageClass($this->settingsHelper, $this->pluginUrl); + if ($page->slug() === $currentSection) { + $mollieSettings = $page->settings(); + break; + } + } + + if (!$mollieSettings) { + $mollieSettings = (new PageNoApiKey($this->settingsHelper, $this->pluginUrl))->settings(); + } + + return apply_filters( + 'woocommerce_get_settings_' . $this->id, + $mollieSettings, + $currentSection + ); + } +} diff --git a/src/Settings/Page/AbstractPage.php b/src/Settings/Page/AbstractPage.php new file mode 100644 index 000000000..5a9864cf3 --- /dev/null +++ b/src/Settings/Page/AbstractPage.php @@ -0,0 +1,55 @@ +settings = $settings; + $this->pluginUrl = $pluginUrl; + } + + abstract public function isTab(): bool; + + abstract public function slug(): string; + + public function tabName(): string + { + return ''; + } + + protected function sections(): array + { + return []; + } + + public function settings(): array + { + $settings = []; + $styles = []; + + foreach ($this->sections() as $sectionClass) { + /** @var AbstractSection $section */ + $section = new $sectionClass($this->settings, $this->pluginUrl); + foreach ($section->config() as $field) { + $settings[] = $field; + } + $styles[$sectionClass] = preg_replace('/\s+/', '', $section->styles()); + } + array_unshift($settings, [ + 'id' => $this->settings->getSettingId('styles'), + 'type' => 'mollie_content', + 'value' => implode($styles) + ]); + return $settings; + } +} diff --git a/src/Settings/Page/Components.php b/src/Settings/Page/Components.php deleted file mode 100644 index 59548e33f..000000000 --- a/src/Settings/Page/Components.php +++ /dev/null @@ -1,68 +0,0 @@ -id = 'mollie_components'; - $this->label = __('Mollie Components', 'mollie-payments-for-woocommerce'); - $this->pluginPath = $pluginPath; - - parent::__construct(); - } - - public function output() - { - $settings = $this->get_settings(); - WC_Admin_Settings::output_fields($settings); - } - - public function get_settings() - { - $componentsSettings = $this->componentsSettings(); - - /** - * Filter Component Settings - * - * @param array $componentSettings Default components settings for the Credit Card Gateway - */ - $componentsSettings = apply_filters(self::FILTER_COMPONENTS_SETTINGS, $componentsSettings); - - return $componentsSettings; - } - - protected function componentsSettings() - { - $componentSettingsFilePath = $this->componentsFilePath(); - - if (!file_exists($componentSettingsFilePath)) { - return []; - } - - $components = include $componentSettingsFilePath; - - if (!is_array($components)) { - $components = []; - } - - return $components; - } - - protected function componentsFilePath() - { - return $this->pluginPath . '/inc/settings/mollie_components.php'; - } -} diff --git a/src/Settings/Page/MollieSettingsPage.php b/src/Settings/Page/MollieSettingsPageOld.php similarity index 100% rename from src/Settings/Page/MollieSettingsPage.php rename to src/Settings/Page/MollieSettingsPageOld.php diff --git a/src/Settings/Page/PageAdvancedSettings.php b/src/Settings/Page/PageAdvancedSettings.php new file mode 100644 index 000000000..5f21e8b09 --- /dev/null +++ b/src/Settings/Page/PageAdvancedSettings.php @@ -0,0 +1,18 @@ +settings = $settings; + $this->pluginUrl = $pluginUrl; + } + + abstract public function config(): array; + + public function styles(): string{ + return ''; + } + + public function images(): string + { + return $this->pluginUrl . '/public/images/'; + } +} diff --git a/src/Settings/Page/Section/ConnectionFields.php b/src/Settings/Page/Section/ConnectionFields.php new file mode 100644 index 000000000..af708c3ba --- /dev/null +++ b/src/Settings/Page/Section/ConnectionFields.php @@ -0,0 +1,86 @@ + $this->settings->getSettingId('title'), + 'title' => '', + 'type' => 'title' + ], + [ + 'id' => $this->settings->getSettingId('test_mode_enabled'), + 'title' => __('Mollie Payment Mode', 'mollie-payments-for-woocommerce'), + 'default' => 'no', + 'type' => 'select', + 'options' => [ + 'no' => 'Live API', + 'yes' => 'Test API' + ], + 'desc_tip' => __( + 'Enable test mode if you want to test the plugin without using real payments.', + 'mollie-payments-for-woocommerce' + ), + ], + [ + 'id' => $this->settings->getSettingId('live_api_key'), + 'title' => __('Live API key', 'mollie-payments-for-woocommerce'), + 'default' => '', + 'type' => 'text', + 'desc' => sprintf( + /* translators: Placeholder 1: API key mode (live or test). The surrounding %s's Will be replaced by a link to the Mollie profile */ + __( + 'The API key is used to connect to Mollie. You can find your %1$s API key in your %2$sMollie account%3$s', + 'mollie-payments-for-woocommerce' + ), + 'live', + '', + '' + ), + 'css' => 'width: 350px', + 'placeholder' => __( + 'Live API key should start with live_', + 'mollie-payments-for-woocommerce' + ), + ], + [ + 'id' => $this->settings->getSettingId('test_api_key'), + 'title' => __('Test API key', 'mollie-payments-for-woocommerce'), + 'default' => '', + 'type' => 'text', + 'desc' => sprintf( + /* translators: Placeholder 1: API key mode (live or test). The surrounding %s's Will be replaced by a link to the Mollie profile */ + __( + 'The API key is used to connect to Mollie. You can find your %1$s API key in your %2$sMollie account%3$s', + 'mollie-payments-for-woocommerce' + ), + 'test', + '', + '' + ), + 'css' => 'width: 350px', + 'placeholder' => __( + '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' => __('Log plugin events.', 'mollie-payments-for-woocommerce'), + 'default' => 'yes', + ], + [ + 'id' => $this->settings->getSettingId('sectionend'), + 'type' => 'sectionend', + ], + ]; + } +} diff --git a/src/Settings/Page/Section/Header.php b/src/Settings/Page/Section/Header.php new file mode 100644 index 000000000..c6408fcea --- /dev/null +++ b/src/Settings/Page/Section/Header.php @@ -0,0 +1,84 @@ + $this->settings->getSettingId('header'), + 'type' => 'mollie_content', + 'value' => $this->content() + ] + ]; + } + + public function styles(): string + { + ob_start(); + ?> + + +
+
+ +

+ + + +

+ +
+
+ $this->settings->getSettingId('instructions'), + 'type' => 'mollie_content', + 'value' => $this->content() + ] + ]; + } + + protected function content(): string + { + ob_start(); + ?> +

+

+ +

+

+ + + +

+
    +
  1. + Mollie Dashboard", + 'mollie-payments-for-woocommerce' + ), + 'https://my.mollie.com/dashboard/login?lang=en' + ); ?> +
  2. +
  3. + Developers > API keys.", 'mollie-payments-for-woocommerce'); ?> +
  4. +
  5. + Copy next to your API key.", 'mollie-payments-for-woocommerce'); ?> +
  6. +
  7. + Live API key or Test API key fields below.", + 'mollie-payments-for-woocommerce' + ); ?> +
  8. +
+

+ +

+ $this->settings->getSettingId('notices'), + 'type' => 'mollie_content', + 'value' => $this->content() + ] + ]; + } + + protected function content(): string + { + ob_start(); + ?> + NOTICES + dataHelper = $container->get('settings.data_helper'); assert($this->dataHelper instanceof Data); $pluginPath = $container->get('shared.plugin_path'); + $pluginUrl = $container->get('shared.plugin_url'); $paymentMethods = $container->get('gateway.paymentMethods'); // Add settings link to plugins page @@ -148,7 +148,7 @@ function () use ($optionName, $defaultAdvancedOptions, $defaultComponentsOptions $gateways = $container->get('gateway.instances'); $isSDDGatewayEnabled = $container->get('gateway.isSDDGatewayEnabled'); - $this->initMollieSettingsPage($isSDDGatewayEnabled, $gateways, $pluginPath, $paymentMethods); + $this->initMollieSettingsPage($isSDDGatewayEnabled, $gateways, $pluginPath, $pluginUrl, $paymentMethods); add_action( 'woocommerce_admin_settings_sanitize_option', [$this->settingsHelper, 'updateMerchantIdOnApiKeyChanges'], @@ -247,10 +247,11 @@ public function maybeSaveDefaultSettings($optionName, $testOption, $defaultOptio * @param $isSDDGatewayEnabled * @param $gateways * @param $pluginPath + * @param $pluginUrl * @param $paymentMethods * @return void */ - protected function initMollieSettingsPage($isSDDGatewayEnabled, $gateways, $pluginPath, $paymentMethods): void + protected function initMollieSettingsPage($isSDDGatewayEnabled, $gateways, $pluginPath, $pluginUrl, $paymentMethods): void { if (!$isSDDGatewayEnabled) { //remove directdebit gateway from gateways list @@ -258,10 +259,11 @@ protected function initMollieSettingsPage($isSDDGatewayEnabled, $gateways, $plug } add_filter( 'woocommerce_get_settings_pages', - function ($settings) use ($pluginPath, $gateways, $paymentMethods) { + function ($settings) use ($pluginPath, $pluginUrl, $gateways, $paymentMethods) { $settings[] = new MollieSettingsPage( $this->settingsHelper, $pluginPath, + $pluginUrl, $gateways, $paymentMethods, $this->isTestModeEnabled,