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

feat(my-account): reCAPTCHA check on add payment method #3673

Merged
merged 1 commit into from
Jan 13, 2025
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
25 changes: 25 additions & 0 deletions includes/class-recaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,13 @@ public static function init() {
// Add reCAPTCHA to the Woo checkout form.
\add_action( 'woocommerce_review_order_before_submit', [ __CLASS__, 'add_recaptcha_v2_to_checkout' ] );
\add_action( 'woocommerce_checkout_after_customer_details', [ __CLASS__, 'add_recaptcha_v3_to_checkout' ] );
\add_action( 'woocommerce_add_payment_method_form_bottom', [ __CLASS__, 'add_recaptcha_v3_to_checkout' ] );
dkoo marked this conversation as resolved.
Show resolved Hide resolved

// Verify reCAPTCHA on checkout submission.
\add_action( 'woocommerce_checkout_process', [ __CLASS__, 'verify_recaptcha_on_checkout' ] );

// Verify reCAPTCHA when adding new payment method.
\add_filter( 'woocommerce_add_payment_method_form_is_valid', [ __CLASS__, 'verify_recaptcha_on_add_payment_method' ] );
}

/**
Expand Down Expand Up @@ -520,6 +524,27 @@ public static function verify_recaptcha_on_checkout() {
WooCommerce_Connection::add_wc_notice( $check->get_error_message(), 'error' );
}
}

/**
* Verify reCAPTCHA when adding new payment method in My Account.
*
* @param bool $is_valid Whether the form is valid.
*
* @rturn bool
*/
public static function verify_recaptcha_on_add_payment_method( $is_valid ) {
$url = \home_url( \add_query_arg( null, null ) );
$should_verify_captcha = apply_filters( 'newspack_recaptcha_verify_captcha', self::can_use_captcha(), $url );
if ( ! $should_verify_captcha ) {
return $is_valid;
}
$check = self::verify_captcha();
if ( \is_wp_error( $check ) ) {
WooCommerce_Connection::add_wc_notice( $check->get_error_message(), 'error' );
return false;
}
return $is_valid;
}
}

Recaptcha::init();
4 changes: 3 additions & 1 deletion src/other-scripts/recaptcha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,9 @@ function render( forms = [], onSuccess = null, onError = null ) {

const formsToHandle = forms.length
? forms
: [ ...document.querySelectorAll( 'form[data-newspack-recaptcha]' ) ];
: [ ...document.querySelectorAll(
'form[data-newspack-recaptcha],form#add_payment_method',
) ];

formsToHandle.forEach( form => {
const renderForm = () => {
Expand Down
Loading