Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(modal-checkout): allow all gateway assets #1988

Merged
merged 2 commits into from
Dec 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 105 additions & 40 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 = [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I separated these out so we can be more explicit with our hooks but wasn't sure which of these were for scripts and which were for styles. Since this is time sensitive I didn't want to let this hold up this PR.

// 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 @@ -723,64 +783,69 @@ 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.
'ppcp', // PayPal Payments.
'gateway', // PayPal Payments.
];
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 assets to render in the modal checkout
* Filters the allowed styles to render in the modal checkout
*
* @param string[] $allowed_assets Array of allowed assets handles.
* @param string[] $allowed_styles Array of allowed assets handles.
*/
$allowed_assets = apply_filters( 'newspack_blocks_modal_checkout_allowed_assets', $allowed_assets );

global $wp_scripts, $wp_styles;

foreach ( $wp_scripts->queue as $handle ) {
$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_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 ( $allowed_gateway_assets as $gateway ) {
if ( false !== strpos( $wp_style->src, $gateway ) ) {
$allowed = true;
break;
}
}
}
if ( ! $allowed ) {
wp_dequeue_script( $handle );
wp_dequeue_style( $handle );
}
}
foreach ( $wp_styles->queue as $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_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;
}
}
foreach ( $allowed_gateway_assets as $gateway ) {
if ( false !== strpos( $wp_script->src, $gateway ) ) {
$allowed = true;
break;
}
}
if ( ! $allowed ) {
wp_dequeue_style( $handle );
wp_dequeue_script( $handle );
}
}
}
Expand Down
Loading