diff --git a/includes/class-recaptcha.php b/includes/class-recaptcha.php index ae14401dc4..51edbd6ac5 100644 --- a/includes/class-recaptcha.php +++ b/includes/class-recaptcha.php @@ -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' ] ); // 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' ] ); } /** @@ -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(); diff --git a/src/other-scripts/recaptcha/index.js b/src/other-scripts/recaptcha/index.js index 924fc0135c..83c1f678e4 100644 --- a/src/other-scripts/recaptcha/index.js +++ b/src/other-scripts/recaptcha/index.js @@ -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 = () => {