From 0200ddb2c53c3db008db8a777ac9f20eef487871 Mon Sep 17 00:00:00 2001 From: dkoo Date: Fri, 26 Jul 2024 12:30:10 -0600 Subject: [PATCH 1/5] fix: reCAPTCHA v3 for modal checkout --- includes/class-modal-checkout.php | 20 +++++++++++++++----- src/modal-checkout/index.js | 21 +++++++++++++++++++++ 2 files changed, 36 insertions(+), 5 deletions(-) diff --git a/includes/class-modal-checkout.php b/includes/class-modal-checkout.php index 58cfbfef7..f6fcc5a0e 100644 --- a/includes/class-modal-checkout.php +++ b/includes/class-modal-checkout.php @@ -545,10 +545,16 @@ public static function enqueue_scripts() { return; } + $dependencies = [ 'jquery' ]; + // Add support reCAPTCHA dependencies, if connected. + if ( class_exists( 'Newspack\Recaptcha' ) ) { + $dependencies[] = \Newspack\Recaptcha::SCRIPT_HANDLE; + } + wp_enqueue_script( 'newspack-blocks-modal-checkout', plugins_url( 'dist/modalCheckout.js', \NEWSPACK_BLOCKS__PLUGIN_FILE ), - [ 'jquery' ], + $dependencies, \NEWSPACK_BLOCKS__VERSION, true ); @@ -1021,12 +1027,16 @@ class="button close-button" * @param string $url The URL from which the checkout originated. */ public static function recaptcha_verify_captcha( $should_verify, $url ) { + $is_validation_only = boolval( filter_input( INPUT_POST, 'is_validation_only', FILTER_SANITIZE_NUMBER_INT ) ); parse_str( \wp_parse_url( $url, PHP_URL_QUERY ), $query ); if ( - // Only in the context of a checkout request. - defined( 'WOOCOMMERCE_CHECKOUT' ) - && isset( $query['wc-ajax'] ) - && 'wc_stripe_create_order' === $query['wc-ajax'] + // Only in the context of a true checkout request. + $is_validation_only || + ( + defined( 'WOOCOMMERCE_CHECKOUT' ) + && isset( $query['wc-ajax'] ) + && 'wc_stripe_create_order' === $query['wc-ajax'] + ) ) { return false; } diff --git a/src/modal-checkout/index.js b/src/modal-checkout/index.js index f528c7c6d..e50d7d2ab 100644 --- a/src/modal-checkout/index.js +++ b/src/modal-checkout/index.js @@ -365,10 +365,22 @@ domReady( * @param {boolean} isEditingDetails */ function setEditingDetails( isEditingDetails ) { + const newspack_grecaptcha = window.newspack_grecaptcha || {}; + + // Scroll to top. + window.scroll( { top: 0, left: 0, behavior: 'smooth' } ); + // Update checkout. + $( document.body ).trigger( 'update_checkout' ); clearNotices(); // Clear checkout details. $( '#checkout_details' ).remove(); if ( isEditingDetails ) { + $form.append( '' ); + // Destroy reCAPTCHA inputs so we don't trigger validation between checkout steps. + if ( newspack_grecaptcha?.destroyV3Captchas ) { + $form.removeData( 'newspack-recaptcha' ); + newspack_grecaptcha.destroyV3Captchas( $form.get() ); + } if ( $coupon.length ) { $coupon.hide(); } @@ -385,6 +397,15 @@ domReady( } ); $form.on( 'submit', handleFormSubmit ); } else { + if ( $form.find( '[name="is_validation_only"]' ) ) { + $form.find( '[name="is_validation_only"]' ).remove(); + } + + // Initiate reCAPTCHA, if available. + if ( newspack_grecaptcha?.renderV3Captchas ) { + $form.data( 'newspack-recaptcha', 'newspack_modal_checkout' ); + newspack_grecaptcha.renderV3Captchas( $form.get() ); + } if ( $coupon.length ) { $coupon.show(); } From 8353ea1404ed1c0f8ee232b9a4a63ca40592732e Mon Sep 17 00:00:00 2001 From: dkoo Date: Fri, 26 Jul 2024 18:15:51 -0600 Subject: [PATCH 2/5] fix: reCAPTCHA v2 support --- src/modal-checkout/index.js | 9 ++++----- src/modal-checkout/templates/form-checkout.php | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/modal-checkout/index.js b/src/modal-checkout/index.js index e50d7d2ab..16af2276b 100644 --- a/src/modal-checkout/index.js +++ b/src/modal-checkout/index.js @@ -377,9 +377,8 @@ domReady( if ( isEditingDetails ) { $form.append( '' ); // Destroy reCAPTCHA inputs so we don't trigger validation between checkout steps. - if ( newspack_grecaptcha?.destroyV3Captchas ) { - $form.removeData( 'newspack-recaptcha' ); - newspack_grecaptcha.destroyV3Captchas( $form.get() ); + if ( 'v3' === newspack_grecaptcha?.version ) { + newspack_grecaptcha.destroy( $form.get() ); } if ( $coupon.length ) { $coupon.hide(); @@ -402,9 +401,9 @@ domReady( } // Initiate reCAPTCHA, if available. - if ( newspack_grecaptcha?.renderV3Captchas ) { + if ( newspack_grecaptcha?.render ) { $form.data( 'newspack-recaptcha', 'newspack_modal_checkout' ); - newspack_grecaptcha.renderV3Captchas( $form.get() ); + newspack_grecaptcha.render( $form.get() ); } if ( $coupon.length ) { $coupon.show(); diff --git a/src/modal-checkout/templates/form-checkout.php b/src/modal-checkout/templates/form-checkout.php index 94fa2ea28..ccaf05640 100644 --- a/src/modal-checkout/templates/form-checkout.php +++ b/src/modal-checkout/templates/form-checkout.php @@ -31,7 +31,7 @@ - +