Skip to content

Commit

Permalink
fix: correctly check gateway assets
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Dec 6, 2024
1 parent 2dd48b8 commit 312773b
Showing 1 changed file with 29 additions and 18 deletions.
47 changes: 29 additions & 18 deletions includes/class-modal-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -782,21 +782,27 @@ public static function dequeue_scripts() {
if ( ! self::is_modal_checkout() ) {
return;
}
/**
* Filters the allowed scripts to render in the modal checkout
*
* @param string[] $allowed_scripts Array of allowed assets handles.
*/
$allowed_scripts = apply_filters( 'newspack_blocks_modal_checkout_allowed_scripts', self::$allowed_scripts );

global $wp_scripts, $wp_styles;

$payment_gateways = \WC()->payment_gateways->get_available_payment_gateways();
$allowed_gateway_assets = [];
if ( ! empty( $payment_gateways ) ) {
foreach ( array_keys( $payment_gateways ) as $gateway ) {
$class = get_class( $payment_gateways[ $gateway ] );
$plugin_file = ( new \ReflectionClass( $class ) )->getFileName();
$plugin_base = \plugin_basename( $plugin_file );
$plugin_slug = explode( '/', $plugin_base )[0];
$allowed_gateway_assets[] = $plugin_slug;
}
}

/**
* Filters the allowed styles to render in the modal checkout
*
* @param string[] $allowed_styles Array of allowed assets handles.
*/
$allowed_styles = apply_filters( 'newspack_blocks_modal_checkout_allowed_styles', self::$allowed_styles );
$payment_gateways = \WC()->payment_gateways->get_available_payment_gateways();

global $wp_scripts, $wp_styles;
$allowed_styles = apply_filters( 'newspack_blocks_modal_checkout_allowed_styles', self::$allowed_styles );
foreach ( $wp_styles->registered as $handle => $wp_style ) {
$allowed = false;
foreach ( $allowed_styles as $allowed_style ) {
Expand All @@ -806,8 +812,8 @@ public static function dequeue_scripts() {
}
}
if ( ! empty( $payment_gateways ) ) {
foreach ( array_keys( $payment_gateways ) as $gateway ) {
if ( false !== strpos( $wp_style->src, $gateway->id ) ) {
foreach ( $allowed_gateway_assets as $gateway ) {
if ( false !== strpos( $wp_style->src, $gateway ) ) {
$allowed = true;
break;
}
Expand All @@ -817,6 +823,13 @@ public static function dequeue_scripts() {
wp_dequeue_style( $handle );
}
}

/**
* Filters the allowed scripts to render in the modal checkout
*
* @param string[] $allowed_scripts Array of allowed assets handles.
*/
$allowed_scripts = apply_filters( 'newspack_blocks_modal_checkout_allowed_scripts', self::$allowed_scripts );
foreach ( $wp_scripts->registered as $handle => $wp_script ) {
$allowed = false;
foreach ( $allowed_scripts as $allowed_script ) {
Expand All @@ -825,12 +838,10 @@ public static function dequeue_scripts() {
break;
}
}
if ( ! empty( $payment_gateways ) ) {
foreach ( array_keys( $payment_gateways ) as $gateway ) {
if ( false !== strpos( $wp_script->src, $gateway ) ) {
$allowed = true;
break;
}
foreach ( $allowed_gateway_assets as $gateway ) {
if ( false !== strpos( $wp_script->src, $gateway ) ) {
$allowed = true;
break;
}
}
if ( ! $allowed ) {
Expand Down

0 comments on commit 312773b

Please sign in to comment.