From 46202e83ef6b6365695cfc1d41dc975de8fc22fe Mon Sep 17 00:00:00 2001 From: Rasmy Nguyen Date: Fri, 6 Dec 2024 13:39:06 -0500 Subject: [PATCH 1/2] fix(modal-checkout): allow all gateway assets --- includes/class-modal-checkout.php | 134 +++++++++++++++++++++--------- 1 file changed, 94 insertions(+), 40 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index 032f6aefe..e13f1ce86 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. */ @@ -722,65 +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. - 'ppcp', // PayPal Payments. - 'gateway', // PayPal Payments. - ]; - /** - * 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 ); } } } From 95d58a04e3124572bbf0a504c62e1cc08a6ee8df Mon Sep 17 00:00:00 2001 From: Rasmy Nguyen Date: Fri, 6 Dec 2024 16:27:22 -0500 Subject: [PATCH 2/2] fix: correctly check gateway assets --- includes/class-modal-checkout.php | 47 +++++++++++++++++++------------ 1 file changed, 29 insertions(+), 18 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index e13f1ce86..49054d743 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -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 ) { @@ -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; } @@ -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 ) { @@ -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 ) {