From 238adedcadb26cb17e6e294f42b156743f42b2c2 Mon Sep 17 00:00:00 2001 From: Philipp Stracker Date: Thu, 13 Jun 2024 11:35:05 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20the=20condition=20to=20inj?= =?UTF-8?q?ect=20the=20settings=20JS?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Switched to using an existing DI service to determine if the current page requires the “gateway-settings” asset files. --- modules/ppcp-wc-gateway/services.php | 1 + .../src/Assets/SettingsPageAssets.php | 64 ++++++++----------- .../ppcp-wc-gateway/src/WCGatewayModule.php | 3 +- .../Assets/SettingsPagesAssetsTest.php | 3 +- 4 files changed, 30 insertions(+), 41 deletions(-) diff --git a/modules/ppcp-wc-gateway/services.php b/modules/ppcp-wc-gateway/services.php index 412b1d3b2..f8541e152 100644 --- a/modules/ppcp-wc-gateway/services.php +++ b/modules/ppcp-wc-gateway/services.php @@ -214,6 +214,7 @@ return array( array( PayPalGateway::ID, CreditCardGateway::ID, + CardButtonGateway::ID, ), true ); diff --git a/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php b/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php index 50f453d9e..353d33827 100644 --- a/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php +++ b/modules/ppcp-wc-gateway/src/Assets/SettingsPageAssets.php @@ -112,6 +112,13 @@ class SettingsPageAssets { */ private $billing_agreements_endpoint; + /** + * Whether we're on a settings page for our plugin's payment methods. + * + * @var bool + */ + private $is_paypal_payment_method_page; + /** * Assets constructor. * @@ -128,6 +135,7 @@ class SettingsPageAssets { * @param bool $is_settings_page Whether it's a settings page of this plugin. * @param bool $is_acdc_enabled Whether the ACDC gateway is enabled. * @param BillingAgreementsEndpoint $billing_agreements_endpoint Billing Agreements endpoint. + * @param bool $is_paypal_payment_method_page Whether we're on a settings page for our plugin's payment methods. */ public function __construct( string $module_url, @@ -142,21 +150,23 @@ class SettingsPageAssets { array $all_funding_sources, bool $is_settings_page, bool $is_acdc_enabled, - BillingAgreementsEndpoint $billing_agreements_endpoint + BillingAgreementsEndpoint $billing_agreements_endpoint, + bool $is_paypal_payment_method_page ) { - $this->module_url = $module_url; - $this->version = $version; - $this->subscription_helper = $subscription_helper; - $this->client_id = $client_id; - $this->currency = $currency; - $this->country = $country; - $this->environment = $environment; - $this->is_pay_later_button_enabled = $is_pay_later_button_enabled; - $this->disabled_sources = $disabled_sources; - $this->all_funding_sources = $all_funding_sources; - $this->is_settings_page = $is_settings_page; - $this->is_acdc_enabled = $is_acdc_enabled; - $this->billing_agreements_endpoint = $billing_agreements_endpoint; + $this->module_url = $module_url; + $this->version = $version; + $this->subscription_helper = $subscription_helper; + $this->client_id = $client_id; + $this->currency = $currency; + $this->country = $country; + $this->environment = $environment; + $this->is_pay_later_button_enabled = $is_pay_later_button_enabled; + $this->disabled_sources = $disabled_sources; + $this->all_funding_sources = $all_funding_sources; + $this->is_settings_page = $is_settings_page; + $this->is_acdc_enabled = $is_acdc_enabled; + $this->billing_agreements_endpoint = $billing_agreements_endpoint; + $this->is_paypal_payment_method_page = $is_paypal_payment_method_page; } /** @@ -176,7 +186,7 @@ class SettingsPageAssets { $this->register_admin_assets(); } - if ( $this->is_paypal_payment_method_page() ) { + if ( $this->is_paypal_payment_method_page ) { $this->register_paypal_admin_assets(); } } @@ -184,30 +194,6 @@ class SettingsPageAssets { } - /** - * Check whether the current page is PayPal payment method settings. - * - * @return bool - */ - private function is_paypal_payment_method_page(): bool { - - if ( ! function_exists( 'get_current_screen' ) ) { - return false; - } - - $screen = get_current_screen(); - if ( ! $screen || $screen->id !== 'woocommerce_page_wc-settings' ) { - return false; - } - - // phpcs:disable WordPress.Security.NonceVerification.Recommended - $tab = wc_clean( wp_unslash( $_GET['tab'] ?? '' ) ); - $section = wc_clean( wp_unslash( $_GET['section'] ?? '' ) ); - // phpcs:enable WordPress.Security.NonceVerification.Recommended - - return 'checkout' === $tab && in_array( $section, array( PayPalGateway::ID, CardButtonGateway::ID ), true ); - } - /** * Register assets for PayPal admin pages. */ diff --git a/modules/ppcp-wc-gateway/src/WCGatewayModule.php b/modules/ppcp-wc-gateway/src/WCGatewayModule.php index 19f67ef53..76de0478c 100644 --- a/modules/ppcp-wc-gateway/src/WCGatewayModule.php +++ b/modules/ppcp-wc-gateway/src/WCGatewayModule.php @@ -183,7 +183,8 @@ class WCGatewayModule implements ModuleInterface { $c->get( 'wcgateway.settings.funding-sources' ), $c->get( 'wcgateway.is-ppcp-settings-page' ), $settings->has( 'dcc_enabled' ) && $settings->get( 'dcc_enabled' ), - $c->get( 'api.endpoint.billing-agreements' ) + $c->get( 'api.endpoint.billing-agreements' ), + $c->get( 'wcgateway.is-ppcp-settings-payment-methods-page' ) ); $assets->register_assets(); } diff --git a/tests/PHPUnit/WcGateway/Assets/SettingsPagesAssetsTest.php b/tests/PHPUnit/WcGateway/Assets/SettingsPagesAssetsTest.php index 5569c4158..931c55fcf 100644 --- a/tests/PHPUnit/WcGateway/Assets/SettingsPagesAssetsTest.php +++ b/tests/PHPUnit/WcGateway/Assets/SettingsPagesAssetsTest.php @@ -33,7 +33,8 @@ class SettingsPagesAssetsTest extends TestCase array(), true, false, - $billingAgreementEndpoint + $billingAgreementEndpoint, + true ); when('is_admin')