From 0b2383e019bfa726c50c1732be0bcc4701d26ae3 Mon Sep 17 00:00:00 2001 From: carmenmaymo Date: Mon, 20 Jan 2025 08:37:22 +0100 Subject: [PATCH] Load payment fields after setup theme for translations --- src/Gateway/MolliePaymentGateway.php | 5 +++-- src/PaymentMethods/AbstractPaymentMethod.php | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/Gateway/MolliePaymentGateway.php b/src/Gateway/MolliePaymentGateway.php index 18ef627a..4ecd1386 100644 --- a/src/Gateway/MolliePaymentGateway.php +++ b/src/Gateway/MolliePaymentGateway.php @@ -127,8 +127,9 @@ public function __construct( ); $this->supports = $this->paymentMethod->getProperty('supports'); - // Load the settings. - $this->init_form_fields(); + // Load the settings when translations are ready + add_action('after_setup_theme', [$this, 'init_form_fields']); + $this->init_settings(); $this->title = $this->paymentMethod->title(); diff --git a/src/PaymentMethods/AbstractPaymentMethod.php b/src/PaymentMethods/AbstractPaymentMethod.php index 3b81cf99..d5a11d38 100644 --- a/src/PaymentMethods/AbstractPaymentMethod.php +++ b/src/PaymentMethods/AbstractPaymentMethod.php @@ -44,6 +44,9 @@ abstract class AbstractPaymentMethod implements PaymentMethodI * @var array */ private $apiPaymentMethod; + /** + * @var bool + */ protected bool $translationsInitialized = false; public function __construct( @@ -63,6 +66,7 @@ public function __construct( $this->settings = $this->getSettings(); $this->apiPaymentMethod = $apiPaymentMethod; add_action('after_setup_theme', [$this, 'initializeTranslations']); + add_action('after_setup_theme', [$this, 'updateSettingsWithDefaults']); } public function title(): string @@ -225,6 +229,20 @@ public function getProcessedDescriptionForBlock(): string * @return array */ public function getSettings(): array + { + $optionName = 'mollie_wc_gateway_' . $this->id . '_settings'; + $settings = get_option($optionName, false); + if (!$settings) { + $settings = []; + } + return $settings; + } + + /** + * Update the payment method's settings with defaults if not exist + * @return array + */ + public function updateSettingsWithDefaults(): array { $optionName = 'mollie_wc_gateway_' . $this->id . '_settings'; $settings = get_option($optionName, false);