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: reCAPTCHA v2 + v3 for RAS-ACC flows #1811

Merged
merged 5 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
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
20 changes: 15 additions & 5 deletions includes/class-modal-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -545,10 +545,16 @@ public static function enqueue_scripts() {
return;
}

$dependencies = [ 'jquery' ];
// Add support reCAPTCHA dependencies, if connected.
if ( class_exists( 'Newspack\Recaptcha' ) && \Newspack\Recaptcha::can_use_captcha() ) {
$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
);
Expand Down Expand Up @@ -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;
}
Expand Down
21 changes: 21 additions & 0 deletions src/modal-checkout/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,10 +365,21 @@ 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( '<input name="is_validation_only" type="hidden" value="1" />' );
// Destroy reCAPTCHA inputs so we don't trigger validation between checkout steps.
if ( 'v3' === newspack_grecaptcha?.version ) {
newspack_grecaptcha.destroy( $form.get() );
}
if ( $coupon.length ) {
$coupon.hide();
}
Expand All @@ -385,6 +396,16 @@ domReady(
} );
$form.on( 'submit', handleFormSubmit );
} else {
const $validationOnlyField = $form.find( '[name="is_validation_only"]' );
if ( $validationOnlyField.length ) {
$validationOnlyField.remove();
}

// Initiate reCAPTCHA, if available.
if ( newspack_grecaptcha?.render ) {
$form.data( 'newspack-recaptcha', 'newspack_modal_checkout' );
newspack_grecaptcha.render( $form.get() );
}
if ( $coupon.length ) {
$coupon.show();
}
Expand Down
2 changes: 1 addition & 1 deletion src/modal-checkout/templates/form-checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<?php do_action( 'woocommerce_checkout_billing' ); ?>
<?php do_action( 'woocommerce_checkout_shipping' ); ?>
<?php \Newspack_Blocks\Modal_Checkout::maybe_show_wcs_gifting_fields(); ?>
<button class="newspack-ui__button newspack-ui__button--primary newspack-ui__button--wide" id="checkout_continue" type="submit"><?php esc_html_e( 'Continue', 'newspack-blocks' ); ?></button>
<button class="newspack-ui__button newspack-ui__button--primary newspack-ui__button--wide" id="checkout_continue" type="submit" data-skip-recaptcha><?php esc_html_e( 'Continue', 'newspack-blocks' ); ?></button>
</div>
<div id="after_customer_details">
<div class="order-review-wrapper hidden">
Expand Down