Skip to content

Commit

Permalink
fix(modal-checkout): allow all gateway assets
Browse files Browse the repository at this point in the history
  • Loading branch information
chickenn00dle committed Dec 6, 2024
1 parent 8e250eb commit 2dd48b8
Showing 1 changed file with 94 additions and 39 deletions.
133 changes: 94 additions & 39 deletions includes/class-modal-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,66 @@ final class Modal_Checkout {
*/
private static $modal_checkout_labels = [];

/**
* Allowed assets for modal checkout.
*
* @var string[]
*/
private static $allowed_scripts = [
'jquery',
'google_gtagjs',
// Newspack.
'newspack-newsletters-',
'newspack-blocks-modal',
'newspack-blocks-modal-checkout',
'newspack-wc',
'newspack-ui',
'newspack-style',
'newspack-recaptcha',
'newspack-woocommerce-style',
// Woo.
'woocommerce',
'WCPAY',
'Woo',
'wc-',
'wc_',
'wcs-',
'stripe',
'select2',
'selectWoo',
// Metorik.
'metorik',
];

/**
* Allowed styles for modal checkout.
*
* @var string[]
*/
private static $allowed_styles = [
// Newspack.
'newspack-newsletters-',
'newspack-blocks-modal',
'newspack-blocks-modal-checkout',
'newspack-wc',
'newspack-ui',
'newspack-style',
'newspack-recaptcha',
'newspack-woocommerce-style',
// Woo.
'woocommerce',
'WCPAY',
'Woo',
'wc-',
'wc_',
'wcs-',
'stripe',
'select2',
'selectWoo',
// Metorik.
'metorik',
];

/**
* Initialize hooks.
*/
Expand Down Expand Up @@ -722,64 +782,59 @@ public static function dequeue_scripts() {
if ( ! self::is_modal_checkout() ) {
return;
}

$allowed_assets = [
'jquery',
'google_gtagjs',
// Newspack.
'newspack-newsletters-',
'newspack-blocks-modal',
'newspack-blocks-modal-checkout',
'newspack-wc',
'newspack-ui',
'newspack-style',
'newspack-recaptcha',
'newspack-woocommerce-style',
// Woo.
'woocommerce',
'WCPAY',
'Woo',
'wc-',
'wc_',
'wcs-',
'stripe',
'select2',
'selectWoo',
// Metorik.
'metorik',
];

/**
* Filters the allowed assets to render in the modal checkout
* Filters the allowed scripts to render in the modal checkout
*
* @param string[] $allowed_assets Array of allowed assets handles.
* @param string[] $allowed_scripts Array of allowed assets handles.
*/
$allowed_assets = apply_filters( 'newspack_blocks_modal_checkout_allowed_assets', $allowed_assets );
$allowed_scripts = apply_filters( 'newspack_blocks_modal_checkout_allowed_scripts', self::$allowed_scripts );
/**
* 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;

foreach ( $wp_scripts->queue as $handle ) {
foreach ( $wp_styles->registered as $handle => $wp_style ) {
$allowed = false;
foreach ( $allowed_assets as $allowed_asset ) {
if ( 0 === strpos( $handle, $allowed_asset ) ) {
foreach ( $allowed_styles as $allowed_style ) {
if ( 0 === strpos( $handle, $allowed_style ) ) {
$allowed = true;
break;
}
}
if ( ! empty( $payment_gateways ) ) {
foreach ( array_keys( $payment_gateways ) as $gateway ) {
if ( false !== strpos( $wp_style->src, $gateway->id ) ) {
$allowed = true;
break;
}
}
}
if ( ! $allowed ) {
wp_dequeue_script( $handle );
wp_dequeue_style( $handle );
}
}
foreach ( $wp_styles->queue as $handle ) {
foreach ( $wp_scripts->registered as $handle => $wp_script ) {
$allowed = false;
foreach ( $allowed_assets as $allowed_asset ) {
if ( 0 === strpos( $handle, $allowed_asset ) ) {
foreach ( $allowed_scripts as $allowed_script ) {
if ( 0 === strpos( $handle, $allowed_script ) ) {
$allowed = true;
break;
}
}
if ( ! empty( $payment_gateways ) ) {
foreach ( array_keys( $payment_gateways ) as $gateway ) {
if ( false !== strpos( $wp_script->src, $gateway ) ) {
$allowed = true;
break;
}
}
}
if ( ! $allowed ) {
wp_dequeue_style( $handle );
wp_dequeue_script( $handle );
}
}
}
Expand Down

0 comments on commit 2dd48b8

Please sign in to comment.