🐛 Fix the condition to inject the settings JS

Switched to using an existing DI service to determine if the current page requires the “gateway-settings” asset files.
This commit is contained in:
Philipp Stracker 2024-06-13 11:35:05 +02:00
parent c23129cb91
commit 238adedcad
No known key found for this signature in database
4 changed files with 30 additions and 41 deletions

View file

@ -214,6 +214,7 @@ return array(
array( array(
PayPalGateway::ID, PayPalGateway::ID,
CreditCardGateway::ID, CreditCardGateway::ID,
CardButtonGateway::ID,
), ),
true true
); );

View file

@ -112,6 +112,13 @@ class SettingsPageAssets {
*/ */
private $billing_agreements_endpoint; 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. * 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_settings_page Whether it's a settings page of this plugin.
* @param bool $is_acdc_enabled Whether the ACDC gateway is enabled. * @param bool $is_acdc_enabled Whether the ACDC gateway is enabled.
* @param BillingAgreementsEndpoint $billing_agreements_endpoint Billing Agreements endpoint. * @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( public function __construct(
string $module_url, string $module_url,
@ -142,7 +150,8 @@ class SettingsPageAssets {
array $all_funding_sources, array $all_funding_sources,
bool $is_settings_page, bool $is_settings_page,
bool $is_acdc_enabled, bool $is_acdc_enabled,
BillingAgreementsEndpoint $billing_agreements_endpoint BillingAgreementsEndpoint $billing_agreements_endpoint,
bool $is_paypal_payment_method_page
) { ) {
$this->module_url = $module_url; $this->module_url = $module_url;
$this->version = $version; $this->version = $version;
@ -157,6 +166,7 @@ class SettingsPageAssets {
$this->is_settings_page = $is_settings_page; $this->is_settings_page = $is_settings_page;
$this->is_acdc_enabled = $is_acdc_enabled; $this->is_acdc_enabled = $is_acdc_enabled;
$this->billing_agreements_endpoint = $billing_agreements_endpoint; $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(); $this->register_admin_assets();
} }
if ( $this->is_paypal_payment_method_page() ) { if ( $this->is_paypal_payment_method_page ) {
$this->register_paypal_admin_assets(); $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. * Register assets for PayPal admin pages.
*/ */

View file

@ -183,7 +183,8 @@ class WCGatewayModule implements ModuleInterface {
$c->get( 'wcgateway.settings.funding-sources' ), $c->get( 'wcgateway.settings.funding-sources' ),
$c->get( 'wcgateway.is-ppcp-settings-page' ), $c->get( 'wcgateway.is-ppcp-settings-page' ),
$settings->has( 'dcc_enabled' ) && $settings->get( 'dcc_enabled' ), $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(); $assets->register_assets();
} }

View file

@ -33,7 +33,8 @@ class SettingsPagesAssetsTest extends TestCase
array(), array(),
true, true,
false, false,
$billingAgreementEndpoint $billingAgreementEndpoint,
true
); );
when('is_admin') when('is_admin')