From 2fc0ddda26531b26251e45983343b2fee2932e58 Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Tue, 3 Dec 2024 12:03:43 -0800 Subject: [PATCH 01/11] fix: remove reCAPTCHA for Woo from modal checkout --- includes/class-modal-checkout.php | 29 +++++++++++++++++++++++++++-- src/modal-checkout/index.js | 1 + 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index da2d97db2..245bdd271 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -132,6 +132,11 @@ public static function init() { } add_filter( 'woocommerce_subscriptions_product_limited_for_user', [ __CLASS__, 'subscriptions_product_limited_for_user' ], 10, 3 ); add_filter( 'woocommerce_get_privacy_policy_text', [ __CLASS__, 'woocommerce_get_privacy_policy_text' ], 10, 2 ); + + // Remove any hooks related to reCAPTCHA for WooCommerce. + if ( self::is_modal_checkout() ) { + add_action( 'plugins_loaded', [ __CLASS__, 'remove_recaptcha_hooks' ] ); + } } /** @@ -744,19 +749,26 @@ public static function dequeue_scripts() { 'metorik', ]; + $skip_assets = [ + // reCAPTCHA for Woo. + 'recaptcha', + 'rcfwc-js', + ]; + /** * Filters the allowed assets to render in the modal checkout * * @param string[] $allowed_assets Array of allowed assets handles. */ $allowed_assets = apply_filters( 'newspack_blocks_modal_checkout_allowed_assets', $allowed_assets ); + $skip_assets = apply_filters( 'newspack_blocks_modal_checkout_skip_assets', $skip_assets ); global $wp_scripts, $wp_styles; foreach ( $wp_scripts->queue as $handle ) { $allowed = false; foreach ( $allowed_assets as $allowed_asset ) { - if ( false !== strpos( $handle, $allowed_asset ) ) { + if ( false !== strpos( $handle, $allowed_asset ) && ! in_array( $handle, $skip_assets ) ) { $allowed = true; break; } @@ -768,7 +780,7 @@ public static function dequeue_scripts() { foreach ( $wp_styles->queue as $handle ) { $allowed = false; foreach ( $allowed_assets as $allowed_asset ) { - if ( false !== strpos( $handle, $allowed_asset ) ) { + if ( false !== strpos( $handle, $allowed_asset ) && ! in_array( $handle, $skip_assets ) ) { $allowed = true; break; } @@ -779,6 +791,19 @@ public static function dequeue_scripts() { } } + /** + * Remove hooks related to reCAPTCHA for WooCommerce + */ + public static function remove_recaptcha_hooks() { + remove_action( 'woocommerce_review_order_before_payment', 'rcfwc_field_checkout', 10 ); + remove_action( 'woocommerce_review_order_before_payment', 'rcfwc_field_checkout', 10 ); + remove_action( 'woocommerce_review_order_after_payment', 'rcfwc_field_checkout', 10 ); + remove_action( 'woocommerce_before_checkout_billing_form', 'rcfwc_field_checkout', 10 ); + remove_action( 'woocommerce_after_checkout_billing_form', 'rcfwc_field_checkout', 10 ); + remove_action( 'woocommerce_review_order_before_submit', 'rcfwc_field_checkout', 10 ); + remove_action( 'woocommerce_checkout_process', 'rcfwc_checkout_check', 10 ); + } + /** * Enqueue script for triggering modal checkout. * diff --git a/src/modal-checkout/index.js b/src/modal-checkout/index.js index 4c4527771..5e30d48fe 100644 --- a/src/modal-checkout/index.js +++ b/src/modal-checkout/index.js @@ -226,6 +226,7 @@ import { domReady } from './utils'; const handleErrorItem = $error => { // Add errors to known fields. const $field = $( '#' + $error.data( 'id' ) + '_field' ); + if ( $field?.length ) { if ( ! $fieldToFocus ) { $fieldToFocus = $field; From c3b6b6fc85828addd26aa272ad45b5cc5bbafad1 Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Tue, 3 Dec 2024 12:09:58 -0800 Subject: [PATCH 02/11] fix: don't remove recaptcha script; this seems dangerous and doesn't seem required --- includes/class-modal-checkout.php | 1 - 1 file changed, 1 deletion(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index 245bdd271..e641d2605 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -751,7 +751,6 @@ public static function dequeue_scripts() { $skip_assets = [ // reCAPTCHA for Woo. - 'recaptcha', 'rcfwc-js', ]; From d225247c855266e345371eebd46763d6c825c9d6 Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Tue, 3 Dec 2024 12:10:59 -0800 Subject: [PATCH 03/11] fix: remove formatting change --- src/modal-checkout/index.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/modal-checkout/index.js b/src/modal-checkout/index.js index 5e30d48fe..4c4527771 100644 --- a/src/modal-checkout/index.js +++ b/src/modal-checkout/index.js @@ -226,7 +226,6 @@ import { domReady } from './utils'; const handleErrorItem = $error => { // Add errors to known fields. const $field = $( '#' + $error.data( 'id' ) + '_field' ); - if ( $field?.length ) { if ( ! $fieldToFocus ) { $fieldToFocus = $field; From 9cb4f16f8e8ef6f57c2c04fb26a4d81adef6b97a Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Tue, 3 Dec 2024 13:28:08 -0800 Subject: [PATCH 04/11] fix: updating approach to removing reCAPTCHA JS --- includes/class-modal-checkout.php | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index e641d2605..df8739857 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -749,25 +749,19 @@ public static function dequeue_scripts() { 'metorik', ]; - $skip_assets = [ - // reCAPTCHA for Woo. - 'rcfwc-js', - ]; - /** * Filters the allowed assets to render in the modal checkout * * @param string[] $allowed_assets Array of allowed assets handles. */ $allowed_assets = apply_filters( 'newspack_blocks_modal_checkout_allowed_assets', $allowed_assets ); - $skip_assets = apply_filters( 'newspack_blocks_modal_checkout_skip_assets', $skip_assets ); global $wp_scripts, $wp_styles; foreach ( $wp_scripts->queue as $handle ) { $allowed = false; foreach ( $allowed_assets as $allowed_asset ) { - if ( false !== strpos( $handle, $allowed_asset ) && ! in_array( $handle, $skip_assets ) ) { + if ( false !== strpos( $handle, $allowed_asset, 0 ) ) { $allowed = true; break; } @@ -779,7 +773,7 @@ public static function dequeue_scripts() { foreach ( $wp_styles->queue as $handle ) { $allowed = false; foreach ( $allowed_assets as $allowed_asset ) { - if ( false !== strpos( $handle, $allowed_asset ) && ! in_array( $handle, $skip_assets ) ) { + if ( false !== strpos( $handle, $allowed_asset, 0 ) ) { $allowed = true; break; } From 1b8371a4666e06c195c61bbc877e31d6ad4423f4 Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Tue, 3 Dec 2024 14:05:34 -0800 Subject: [PATCH 05/11] fix: update approach to stripping out reCAPTCHA --- includes/class-modal-checkout.php | 58 +++++++++++++++++++++++-------- 1 file changed, 43 insertions(+), 15 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index df8739857..9eff2bd72 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -133,10 +133,8 @@ public static function init() { add_filter( 'woocommerce_subscriptions_product_limited_for_user', [ __CLASS__, 'subscriptions_product_limited_for_user' ], 10, 3 ); add_filter( 'woocommerce_get_privacy_policy_text', [ __CLASS__, 'woocommerce_get_privacy_policy_text' ], 10, 2 ); - // Remove any hooks related to reCAPTCHA for WooCommerce. - if ( self::is_modal_checkout() ) { - add_action( 'plugins_loaded', [ __CLASS__, 'remove_recaptcha_hooks' ] ); - } + // Remove any hooks that aren't supported by the modal checkout. + add_action( 'plugins_loaded', [ __CLASS__, 'remove_hooks' ] ); } /** @@ -761,7 +759,7 @@ public static function dequeue_scripts() { foreach ( $wp_scripts->queue as $handle ) { $allowed = false; foreach ( $allowed_assets as $allowed_asset ) { - if ( false !== strpos( $handle, $allowed_asset, 0 ) ) { + if ( 0 === strpos( $handle, $allowed_asset, 0 ) ) { $allowed = true; break; } @@ -773,7 +771,7 @@ public static function dequeue_scripts() { foreach ( $wp_styles->queue as $handle ) { $allowed = false; foreach ( $allowed_assets as $allowed_asset ) { - if ( false !== strpos( $handle, $allowed_asset, 0 ) ) { + if ( 0 === strpos( $handle, $allowed_asset, 0 ) ) { $allowed = true; break; } @@ -785,16 +783,46 @@ public static function dequeue_scripts() { } /** - * Remove hooks related to reCAPTCHA for WooCommerce + * Remove any hooks that may not work nicely with the modal checkout. */ - public static function remove_recaptcha_hooks() { - remove_action( 'woocommerce_review_order_before_payment', 'rcfwc_field_checkout', 10 ); - remove_action( 'woocommerce_review_order_before_payment', 'rcfwc_field_checkout', 10 ); - remove_action( 'woocommerce_review_order_after_payment', 'rcfwc_field_checkout', 10 ); - remove_action( 'woocommerce_before_checkout_billing_form', 'rcfwc_field_checkout', 10 ); - remove_action( 'woocommerce_after_checkout_billing_form', 'rcfwc_field_checkout', 10 ); - remove_action( 'woocommerce_review_order_before_submit', 'rcfwc_field_checkout', 10 ); - remove_action( 'woocommerce_checkout_process', 'rcfwc_checkout_check', 10 ); + public static function remove_hooks() { + if ( ! self::is_modal_checkout() ) { + return; + } + $remove_list = [ + // reCAPTCHA for WooCommerce. + [ + 'hooks' => [ + 'woocommerce_review_order_before_payment', + 'woocommerce_review_order_after_payment', + 'woocommerce_before_checkout_billing_form', + 'woocommerce_after_checkout_billing_form', + 'woocommerce_review_order_before_submit', + ], + 'callback' => 'rcfwc_field_checkout', + ], + [ + + 'hooks' => [ + 'woocommerce_checkout_process', + ], + 'callback' => 'rcfwc_checkout_check', + ], + ]; + + /** + * Filters the hooks to remove from the modal checkout. + * + * @param string[] $remove_list Array of hooks to remove. + */ + $remove_list = apply_filters( 'newspack_blocks_modal_checkout_remove_hooks', $remove_list ); + + foreach ( $remove_list as $remove ) { + foreach ( $remove['hooks'] as $hook ) { + $priority = has_action( $hook, 'callback' ); + remove_action( $hook, $remove['callback'], $priority ); + } + } } /** From b10dfd48f069fbf3af9cf914cbe60c482d77fa63 Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Tue, 3 Dec 2024 14:06:59 -0800 Subject: [PATCH 06/11] fix: remove unneeded 0s --- includes/class-modal-checkout.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index 9eff2bd72..e03555a8e 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -759,7 +759,7 @@ public static function dequeue_scripts() { foreach ( $wp_scripts->queue as $handle ) { $allowed = false; foreach ( $allowed_assets as $allowed_asset ) { - if ( 0 === strpos( $handle, $allowed_asset, 0 ) ) { + if ( 0 === strpos( $handle, $allowed_asset ) ) { $allowed = true; break; } @@ -771,7 +771,7 @@ public static function dequeue_scripts() { foreach ( $wp_styles->queue as $handle ) { $allowed = false; foreach ( $allowed_assets as $allowed_asset ) { - if ( 0 === strpos( $handle, $allowed_asset, 0 ) ) { + if ( 0 === strpos( $handle, $allowed_asset ) ) { $allowed = true; break; } From 7e0b99e8e77cc776616faab9b9d78244c7d9a16b Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Tue, 3 Dec 2024 16:16:23 -0800 Subject: [PATCH 07/11] fix: change up the formatting --- includes/class-modal-checkout.php | 36 ++++++++++++++++++------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index e03555a8e..cebe6139d 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -734,6 +734,7 @@ public static function dequeue_scripts() { 'newspack-ui', 'newspack-style', 'newspack-recaptcha', + 'newspack-woocommerce-style', // Woo. 'woocommerce', 'WCPAY', @@ -792,20 +793,27 @@ public static function remove_hooks() { $remove_list = [ // reCAPTCHA for WooCommerce. [ - 'hooks' => [ - 'woocommerce_review_order_before_payment', - 'woocommerce_review_order_after_payment', - 'woocommerce_before_checkout_billing_form', - 'woocommerce_after_checkout_billing_form', - 'woocommerce_review_order_before_submit', - ], + 'hook' => 'woocommerce_review_order_before_payment', 'callback' => 'rcfwc_field_checkout', ], [ - - 'hooks' => [ - 'woocommerce_checkout_process', - ], + 'hook' => 'woocommerce_review_order_after_payment', + 'callback' => 'rcfwc_field_checkout', + ], + [ + 'hook' => 'woocommerce_before_checkout_billing_form', + 'callback' => 'rcfwc_field_checkout', + ], + [ + 'hook' => 'woocommerce_after_checkout_billing_form', + 'callback' => 'rcfwc_field_checkout', + ], + [ + 'hook' => 'woocommerce_review_order_before_submit', + 'callback' => 'rcfwc_field_checkout', + ], + [ + 'hook' => 'woocommerce_checkout_process', 'callback' => 'rcfwc_checkout_check', ], ]; @@ -818,10 +826,8 @@ public static function remove_hooks() { $remove_list = apply_filters( 'newspack_blocks_modal_checkout_remove_hooks', $remove_list ); foreach ( $remove_list as $remove ) { - foreach ( $remove['hooks'] as $hook ) { - $priority = has_action( $hook, 'callback' ); - remove_action( $hook, $remove['callback'], $priority ); - } + $priority = has_action( $remove['hook'], $remove['callback'] ); + remove_action( $remove['hook'], $remove['callback'], $priority ); } } From f398d8c69ba1322ae0b57b28a27a5e561c3b3815 Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Tue, 3 Dec 2024 16:21:56 -0800 Subject: [PATCH 08/11] fix: add selectWoo to allowed scripts --- includes/class-modal-checkout.php | 1 + 1 file changed, 1 insertion(+) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index cebe6139d..9abbd302b 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -744,6 +744,7 @@ public static function dequeue_scripts() { 'wcs-', 'stripe', 'select2', + 'selectWoo', // Metorik. 'metorik', ]; From 79d9edb5dcb34ed9c75ba4267f4b3d2dd986afce Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Wed, 4 Dec 2024 10:29:08 -0800 Subject: [PATCH 09/11] fix: update filters to match official reCaptcha for Woo plugin --- includes/class-modal-checkout.php | 60 +++++++++++++++++-------------- 1 file changed, 33 insertions(+), 27 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index 9abbd302b..5951ef4a1 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -791,34 +791,40 @@ public static function remove_hooks() { if ( ! self::is_modal_checkout() ) { return; } - $remove_list = [ - // reCAPTCHA for WooCommerce. - [ - 'hook' => 'woocommerce_review_order_before_payment', - 'callback' => 'rcfwc_field_checkout', - ], - [ - 'hook' => 'woocommerce_review_order_after_payment', - 'callback' => 'rcfwc_field_checkout', - ], - [ - 'hook' => 'woocommerce_before_checkout_billing_form', - 'callback' => 'rcfwc_field_checkout', - ], - [ - 'hook' => 'woocommerce_after_checkout_billing_form', - 'callback' => 'rcfwc_field_checkout', - ], - [ - 'hook' => 'woocommerce_review_order_before_submit', - 'callback' => 'rcfwc_field_checkout', - ], - [ - 'hook' => 'woocommerce_checkout_process', - 'callback' => 'rcfwc_checkout_check', - ], - ]; + $remove_list = []; + + // reCaptcha for WooCommerce. + if ( method_exists( 'I13_Woo_Recpatcha', '__construct' ) ) { + global $i13_woo_recpatcha; + array_push( + $remove_list, + [ + 'hook' => 'woocommerce_review_order_before_payment', + 'callback' => array( $i13_woo_recpatcha, 'i13woo_extra_checkout_fields' ), + ], + [ + 'hook' => 'woocommerce_after_checkout_validation', + 'callback' => array( $i13_woo_recpatcha, 'i13_woocomm_validate_checkout_captcha' ), + ], + [ + 'hook' => 'woocommerce_pay_order_before_submit', + 'callback' => array( $i13_woo_recpatcha, 'i13woo_extra_checkout_fields' ), + ], + [ + 'hook' => 'woocommerce_review_order_before_submit', + 'callback' => array( $i13_woo_recpatcha, 'i13woo_extra_checkout_fields' ), + ], + [ + 'hook' => 'woocommerce_pay_order_before_submit', + 'callback' => array( $i13_woo_recpatcha, 'i13woo_extra_checkout_fields_pay_order' ), + ], + [ + 'hook' => 'woocommerce_proceed_to_checkout', + 'callback' => array( $i13_woo_recpatcha, 'i13_woocommerce_payment_request_btn_captcha' ), + ] + ); + } /** * Filters the hooks to remove from the modal checkout. * From 25d6760e210bed32838bca2a1b99f438ac568171 Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Wed, 4 Dec 2024 10:37:11 -0800 Subject: [PATCH 10/11] fix: add metadata to the list of hooks to remove --- includes/class-modal-checkout.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index 5951ef4a1..7466db5a9 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -822,6 +822,10 @@ public static function remove_hooks() { [ 'hook' => 'woocommerce_proceed_to_checkout', 'callback' => array( $i13_woo_recpatcha, 'i13_woocommerce_payment_request_btn_captcha' ), + ], + [ + 'hook' => 'wp_head', + 'callback' => array( $i13_woo_recpatcha, 'i13_add_header_metadata' ), ] ); } From ad577d6e040e72dacb494191da2166c3ec4376a3 Mon Sep 17 00:00:00 2001 From: Laurel Fulford Date: Wed, 4 Dec 2024 12:00:55 -0800 Subject: [PATCH 11/11] fix: improve class check --- includes/class-modal-checkout.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index 7466db5a9..a5af32a61 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -795,7 +795,7 @@ public static function remove_hooks() { $remove_list = []; // reCaptcha for WooCommerce. - if ( method_exists( 'I13_Woo_Recpatcha', '__construct' ) ) { + if ( class_exists( 'I13_Woo_Recpatcha' ) ) { global $i13_woo_recpatcha; array_push( $remove_list,