From 67671118e4fe291f13fcc951ee038c23df7d6396 Mon Sep 17 00:00:00 2001 From: Rasmy Nguyen Date: Fri, 6 Dec 2024 13:39:06 -0500 Subject: [PATCH] fix(modal-checkout): allow all gateway assets --- includes/class-modal-checkout.php | 150 +++++++++++++++++++++--------- 1 file changed, 105 insertions(+), 45 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index a5af32a61..59aecc8ef 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -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. */ @@ -160,6 +220,36 @@ public static function dequeue_woocommerce_styles( $enqueue_styles ) { } unset( $enqueue_styles['woocommerce-general'] ); unset( $enqueue_styles['woocommerce-smallscreen'] ); + + /** + * 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_styles; + foreach ( $wp_styles->registered as $handle => $wp_style ) { + $allowed = false; + 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 ) { + unset( $enqueue_styles[ $handle ] ); + } + } return $enqueue_styles; } @@ -723,63 +813,33 @@ public static function dequeue_scripts() { 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 ); + $payment_gateways = \WC()->payment_gateways->get_available_payment_gateways(); - global $wp_scripts, $wp_styles; - - foreach ( $wp_scripts->queue as $handle ) { + global $wp_scripts; + 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 ( ! $allowed ) { - wp_dequeue_script( $handle ); - } - } - foreach ( $wp_styles->queue as $handle ) { - $allowed = false; - foreach ( $allowed_assets as $allowed_asset ) { - if ( 0 === strpos( $handle, $allowed_asset ) ) { - $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 ); } } }