model = wd_di()->get( Captcha_Model::class ); $this->service = new Captcha_Component( $this->model ); // Use default msg to avoid empty message error. $default_values = $this->model->get_default_values(); $this->default_msg = $default_values['message']; $this->register_routes(); $this->is_woo_activated = wd_di()->get( Woocommerce::class )->is_activated(); $this->is_buddypress_activated = wd_di()->get( Buddypress::class )->is_activated(); add_filter( 'wp_defender_advanced_tools_data', array( $this, 'script_data' ) ); if ( $this->model->is_active() // No need the check by Woo and Buddypress are activated because we use this below. && $this->service->enable_any_location( $this->is_woo_activated, $this->is_buddypress_activated ) && ! $this->service->exclude_captcha_for_requests() ) { $this->declare_variables(); $this->add_actions(); add_filter( 'script_loader_tag', array( $this, 'script_loader_tag' ), 10, 2 ); } } /** * Declares the necessary variables for the reCAPTCHA functionality. * * @return void */ protected function declare_variables(): void { $this->recaptcha_type = $this->model->active_type; $this->recaptcha_theme = 'light'; $this->recaptcha_size = 'invisible'; $this->language = ! empty( $this->model->language ) && 'automatic' !== $this->model->language ? $this->model->language : get_locale(); // Add the CAPTCHA keys depending on the CAPTCHA type. if ( 'v2_checkbox' === $this->recaptcha_type ) { $this->public_key = $this->model->data_v2_checkbox['key']; $this->private_key = $this->model->data_v2_checkbox['secret']; $this->recaptcha_theme = $this->model->data_v2_checkbox['style']; $this->recaptcha_size = $this->model->data_v2_checkbox['size']; } elseif ( 'v2_invisible' === $this->recaptcha_type ) { $this->public_key = $this->model->data_v2_invisible['key']; $this->private_key = $this->model->data_v2_invisible['secret']; } elseif ( 'v3_recaptcha' === $this->recaptcha_type ) { $this->public_key = $this->model->data_v3_recaptcha['key']; $this->private_key = $this->model->data_v3_recaptcha['secret']; } elseif ( 'turnstile' === $this->recaptcha_type ) { $this->public_key = $this->model->data_turnstile['key']; $this->private_key = $this->model->data_turnstile['secret']; $this->recaptcha_theme = $this->model->data_turnstile['style'] ?? 'auto'; $this->recaptcha_size = $this->model->data_turnstile['size'] ?? 'normal'; $this->language = $this->model->data_turnstile['language'] ?? 'auto'; } } /** * Is it Defender's CAPTCHA page? * * @return bool */ protected function is_captcha_settings(): bool { $view = defender_get_data_from_request( 'view', 'g' ); return 'wdf-advanced-tools' === defender_get_current_page() && 'captcha' === $view; } /** * Add actions for CAPTCHA. * * @return void */ protected function add_actions() { $extra_conditions = is_admin() && ! ( defined( 'DOING_AJAX' ) && $this->is_captcha_settings() ); // Since v5.7.0. do_action( 'wd_captcha_before_actions', $extra_conditions ); do_action_deprecated( 'wd_recaptcha_before_actions', array( $extra_conditions ), '5.7.0', 'wd_captcha_before_actions', __( 'This hook is deprecated and will be removed in future versions.', 'defender-security' ) ); if ( $extra_conditions ) { return; } $display_for_known_users = $this->model->display_for_known_users(); $locations = $this->model->locations; if ( in_array( Captcha_Component::DEFAULT_LOGIN_FORM, $locations, true ) || in_array( Captcha_Component::DEFAULT_REGISTER_FORM, $locations, true ) || in_array( Captcha_Component::DEFAULT_LOST_PASSWORD_FORM, $locations, true ) ) { add_filter( 'cfturnstile_widget_disable', '__return_true' ); add_filter( 'easy_cloudflare_turnstile_render_list', '__return_empty_array' ); add_filter( 'easy_cloudflare_turnstile_verify_list', '__return_empty_array' ); add_action( 'login_enqueue_scripts', array( $this->service, 'remove_duplicate_captcha_scripts' ), PHP_INT_MAX ); } // Default login form. if ( in_array( Captcha_Component::DEFAULT_LOGIN_FORM, $locations, true ) ) { add_filter( 'authenticate', array( $this, 'validate_login_captcha' ), 9999 ); add_action( 'login_form', array( $this, 'display_login_captcha' ) ); add_filter( 'wp_authenticate_user', array( $this, 'validate_captcha_field_on_login' ), 8 ); } // Default register form. if ( in_array( Captcha_Component::DEFAULT_REGISTER_FORM, $locations, true ) ) { if ( ! is_multisite() ) { add_action( 'register_form', array( $this, 'display_login_captcha' ) ); add_filter( 'registration_errors', array( $this, 'validate_captcha_field_on_registration' ), 10 ); } else { add_action( 'signup_extra_fields', array( $this, 'display_signup_captcha' ) ); add_action( 'signup_blogform', array( $this, 'display_signup_captcha' ) ); add_filter( 'wpmu_validate_user_signup', array( $this, 'validate_captcha_field_on_wpmu_registration', ), 10 ); } } // Default lost password form. if ( in_array( Captcha_Component::DEFAULT_LOST_PASSWORD_FORM, $locations, true ) ) { add_action( 'lostpassword_form', array( $this, 'display_login_captcha' ) ); if ( $this->maybe_validate_captcha_for_lostpassword() ) { add_action( 'lostpassword_post', array( $this, 'validate_captcha_field_on_lostpassword' ) ); } } // Default comment form. if ( $display_for_known_users && in_array( Captcha_Component::DEFAULT_COMMENT_FORM, $locations, true ) ) { // @since v3.4.0 Change from 'comment_form_after_fields' to 'comment_form_defaults'. add_filter( 'comment_form_defaults', array( $this, 'comment_form_defaults' ), 10 ); add_action( 'pre_comment_on_post', array( $this, 'validate_captcha_field_on_comment' ) ); // When comments are loaded via Hummingbird's lazy load feature. if ( $this->is_lazy_load_comments_enabled() ) { add_action( 'wp_footer', array( $this, 'add_scripts_for_lazy_load' ) ); } } // Todo: move code to related class. // Only Recaptcha type has Integration fields in v5.7.0. // For Woo forms. Mandatory check for the activated Woo before. if ( $this->service->is_recaptcha_active_provider() && $this->model->check_woo_locations( $this->is_woo_activated ) ) { $woo_locations = $this->model->woo_checked_locations; // Woo login form. if ( in_array( Woocommerce::WOO_LOGIN_FORM, $woo_locations, true ) ) { add_action( 'woocommerce_login_form', array( $this, 'display_login_captcha' ) ); add_filter( 'woocommerce_process_login_errors', array( $this, 'validate_captcha_field_on_woo_login', ), 10 ); } // Woo register form. if ( in_array( Woocommerce::WOO_REGISTER_FORM, $woo_locations, true ) ) { add_action( 'woocommerce_register_form', array( $this, 'display_login_captcha' ) ); add_filter( 'woocommerce_registration_errors', array( $this, 'validate_captcha_field_on_woo_registration', ), 10 ); } // Woo lost password form. if ( in_array( Woocommerce::WOO_LOST_PASSWORD_FORM, $woo_locations, true ) ) { add_action( 'woocommerce_lostpassword_form', array( $this, 'display_login_captcha' ) ); // Use default WP hook because Woo doesn't have own hook, so there's the extra check for Woo form. $post_data = defender_get_data_from_request( null, 'p' ); if ( isset( $post_data['wc_reset_password'], $post_data['user_login'] ) ) { add_action( 'lostpassword_post', array( $this, 'validate_captcha_field_on_lostpassword' ) ); } } // Woo checkout form. if ( $display_for_known_users && in_array( Woocommerce::WOO_CHECKOUT_FORM, $woo_locations, true ) ) { add_action( 'woocommerce_after_checkout_billing_form', array( $this, 'display_login_captcha' ) ); add_action( 'woocommerce_after_checkout_validation', array( $this, 'validate_captcha_field_on_woo_checkout', ), 10, 2 ); } } // Todo: move code to related class. // Only Recaptcha type has Integration fields in v5.7.0. // For BuddyPress forms. Mandatory check for the activated BuddyPress before. if ( $this->service->is_recaptcha_active_provider() && $this->model->check_buddypress_locations( $this->is_buddypress_activated ) ) { $buddypress_locations = $this->model->buddypress_checked_locations; // Register form. if ( in_array( Buddypress::REGISTER_FORM, $buddypress_locations, true ) ) { add_action( 'bp_before_registration_submit_buttons', array( $this, 'display_buddypress_recaptcha' ) ); add_filter( 'bp_signup_validate', array( $this, 'validate_captcha_field_on_buddypress_registration', ), 10 ); } // Group form. if ( $display_for_known_users && in_array( Buddypress::NEW_GROUP_FORM, $buddypress_locations, true ) ) { add_action( 'bp_after_group_details_creation_step', array( $this, 'display_login_captcha' ) ); add_action( 'groups_group_before_save', array( $this, 'validate_captcha_field_on_buddypress_group' ) ); } } // Since v5.7.0. do_action( 'wd_captcha_after_actions', $display_for_known_users ); do_action_deprecated( 'wd_recaptcha_after_actions', array( $display_for_known_users ), '5.7.0', 'wd_captcha_after_actions', __( 'This hook is deprecated and will be removed in a future version.', 'defender-security' ) ); } /** * Validates the CAPTCHA response for the login form. * * @param null|WP_Error $error WP_Error object if validation fails, else null. * * @return null|WP_Error WP_Error object if validation fails else null. */ public function validate_login_captcha( $error ) { $post_data = defender_get_data_from_request( null, 'p' ); if ( array() === $post_data ) { return $error; } // Skip turnstile verification for current sprint plan. if ( $this->service->should_skip_turnstile_check() ) { return $error; } // Check if the $_POST array is not empty and if 'g-recaptcha-response' key is also empty. $response_field = 'turnstile' === $this->recaptcha_type ? 'wpdef-turnstile-response' : 'g-recaptcha-response'; $captcha_response = stripslashes( defender_get_data_from_request( $response_field, 'p' ) ); if ( '' === $captcha_response ) { $code = 'captcha_error'; $message = __( 'Please verify that you are not a robot.', 'defender-security' ); if ( is_wp_error( $error ) ) { $error->add( $code, $message ); } else { // Replace $user with a new WP_Error object with an error message. $error = new WP_Error( $code, $message ); } } // Return the $error variable. return $error; } /** * Modifies the script loader tag for the 'wpdef_captcha_api' handle. * * @param string $tag The original script loader tag. * @param string $handle The handle being loaded. * * @return string The modified script loader tag. */ public function script_loader_tag( string $tag, string $handle ) { if ( 'wpdef_captcha_api' === $handle ) { $tag = str_replace( ' src', ' data-cfasync="false" async="async" defer="defer" src', $tag ); } return $tag; } /** * Returns the API URL for reCAPTCHA based on the recaptcha_type property. * * @return string The API URL for reCAPTCHA. Returns an empty string if the recaptcha_type is not set. */ protected function get_api_url(): string { if ( isset( $this->recaptcha_type ) ) { if ( 'v3_recaptcha' === $this->recaptcha_type ) { return sprintf( 'https://www.google.com/recaptcha/api.js?hl=%s&render=%s', $this->language, $this->public_key ); } elseif ( 'turnstile' === $this->recaptcha_type ) { return 'https://challenges.cloudflare.com/turnstile/v0/api.js?render=explicit&onload=wpdefTurnstileCallback'; } elseif ( in_array( $this->recaptcha_type, array( 'v2_checkbox', 'v2_invisible' ), true ) ) { return sprintf( 'https://www.google.com/recaptcha/api.js?hl=%s&render=explicit', $this->language ); } } return ''; } /** * Enqueues the necessary scripts for the reCAPTCHA frontend. * * @return void */ public function add_captcha_scripts(): void { if ( isset( $this->recaptcha_type ) ) { $this->service->remove_duplicate_captcha_scripts(); } if ( 'turnstile' === $this->recaptcha_type ) { $handle = 'wpdef_turnstile_script'; $src_url = 'assets/js/turnstile.js'; } else { $handle = 'wpdef_recaptcha_script'; $src_url = 'assets/js/recaptcha_frontend.js'; } if ( ! wp_script_is( $handle, 'enqueued' ) ) { wp_enqueue_script( $handle, plugins_url( $src_url, WP_DEFENDER_FILE ), array( 'jquery', 'wpdef_captcha_api', ), DEFENDER_VERSION, true ); } // @since 2.5.6 do_action( 'wd_recaptcha_extra_assets' ); $error_text = sprintf( /* translators: 1: Captcha Type, 2: Captcha Type */ esc_html__( 'More than one %1$s has been found in the current form. Please remove all unnecessary %2$s fields to make it work properly.', 'defender-security' ), 'turnstile' === $this->recaptcha_type ? 'turnstile' : 'reCAPTCHA', 'turnstile' === $this->recaptcha_type ? 'turnstile' : 'reCAPTCHA' ); $options = array( 'size' => $this->recaptcha_size, 'sitekey' => $this->public_key, 'error' => sprintf( '%s: %s', esc_html__( 'Warning', 'defender-security' ), $error_text ), // For default comment form. 'disable' => '', ); if ( 'turnstile' === $this->recaptcha_type ) { $options['theme'] = $this->recaptcha_theme; $options['lang'] = $this->language; } else { $options['version'] = $this->recaptcha_type; $options['hl'] = $this->language; if ( 'v2_checkbox' === $this->recaptcha_type ) { $options['theme'] = $this->recaptcha_theme; } } wp_localize_script( $handle, 'WPDEF', array( 'options' => $options, 'vars' => array( 'visibility' => ( 'login_footer' === current_filter() ), ), ) ); } /** * Add scripts when comments are lazy loaded. * * @return void * @since 2.6.1 */ public function add_scripts_for_lazy_load(): void { if ( in_array( $this->recaptcha_type, array( 'v2_checkbox', 'v2_invisible' ), true ) && ( is_single() || is_page() ) && comments_open() ) { if ( ! wp_script_is( 'wpdef_captcha_api', 'registered' ) ) { $api_url = $this->get_api_url(); $deps = array( 'jquery' ); wp_register_script( 'wpdef_captcha_api', $api_url, $deps, DEFENDER_VERSION, true ); } $this->add_captcha_scripts(); } } /** * Display the CAPTCHA field. * * @return void */ public function display_login_captcha(): void { if ( in_array( $this->recaptcha_type, array( 'v2_checkbox', 'turnstile' ), true ) ) { $from_width = 302; ?> recaptcha_type ) { ?> array( 'class' => array(), 'id' => array(), 'style' => array(), ), 'iframe' => array( 'src' => array(), 'frameborder' => array(), 'scrolling' => array(), 'style' => array(), ), 'noscript' => array(), 'textarea' => array( 'name' => array( 'g-recaptcha-response' ), 'class' => array( 'g-recaptcha-response' ), 'style' => array(), ), 'input' => array( 'type' => array( 'hidden' ), 'class' => array( 'g-recaptcha-response' ), 'name' => array( 'g-recaptcha-response' ), ), ); echo wp_kses( $this->display_recaptcha(), $allowed_html ); } /** * Display the output of the recaptcha. * * @return string */ protected function display_recaptcha(): string { $deps = null; $content = '
%s
', wp_kses_post( $error_message ) ); } echo wp_kses_post( $this->display_recaptcha() ); } /** * Verify the recaptcha code on the multisite signup page. * * @param array $result An array of errors. * * @return array */ public function validate_captcha_field_on_wpmu_registration( array $result ): array { global $current_user; if ( is_admin() && ! defined( 'DOING_AJAX' ) && ! empty( $current_user->data->ID ) ) { return $result; } if ( ! $this->captcha_response( 'wpmu_registration' ) ) { if ( isset( $result['errors'] ) && ! empty( $result['errors'] ) ) { $errors = $result['errors']; } else { $errors = new WP_Error(); } $errors->add( 'invalid_captcha', $this->service->error_message() ); $result['errors'] = $errors; return $result; } return $result; } /** * Verify the recaptcha code on Woo login page. * * @param WP_Error $errors A WP_Error object containing any errors encountered during login. * * @return WP_Error */ public function validate_captcha_field_on_woo_login( WP_Error $errors ) { // Skip check if connecting to XMLRPC. if ( defined( 'XMLRPC_REQUEST' ) ) { return $errors; } if ( ! $this->captcha_response( 'woo_login' ) ) { // Remove 'Error: ' because Woo has it by default. $message = str_replace( sprintf( '%s: ', esc_html__( 'Error', 'defender-security' ) ), '', $this->service->error_message() ); $errors->add( 'invalid_captcha', $message ); } return $errors; } /** * Check recaptcha on Woo registration form. * * @param WP_Error $errors A WP_Error object containing any errors encountered during registration. * * @return WP_Error */ public function validate_captcha_field_on_woo_registration( WP_Error $errors ) { if ( defined( 'WOOCOMMERCE_CHECKOUT' ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) { return $errors; } if ( ! $this->captcha_response( 'woo_registration' ) ) { // Remove 'Error: ' because Woo has it by default. $message = str_replace( sprintf( '%s: ', esc_html__( 'Error', 'defender-security' ) ), '', $this->service->error_message() ); $errors->add( 'invalid_captcha', $message ); } return $errors; } /** * Fires before errors are returned from a password reset request. * Without 2nd `$user_data` parameter because it's since WP 5.4.0. * * @param WP_Error $errors A WP_Error object containing any errors encountered during password reset. * * @return void */ public function validate_captcha_field_on_lostpassword( WP_Error $errors ): void { if ( ! $this->captcha_response( 'default_lost_password' ) ) { $errors->add( 'invalid_captcha', $this->service->error_message() ); } } /** * Validates the reCAPTCHA field on the WooCommerce checkout form. * * @param array $fields The fields of the checkout form. * @param WP_Error $errors The errors encountered during the checkout process. * * @return WP_Error The updated errors with the reCAPTCHA validation result. */ public function validate_captcha_field_on_woo_checkout( $fields, $errors ): WP_Error { if ( ! $this->captcha_response( 'woo_checkout' ) ) { // Remove 'Error: ' because Woo has it by default. $message = str_replace( sprintf( '%s: ', esc_html__( 'Error', 'defender-security' ) ), '', $this->service->error_message() ); $errors->add( 'invalid_captcha', $message ); } return $errors; } /** * Display google recaptcha on comments form. * * @param array $defaults The default comment form arguments. * * @return array */ public function comment_form_defaults( array $defaults ) { $defaults['comment_notes_after'] .= '' . $this->display_recaptcha() . '
'; return $defaults; } /** * Check JS enabled for comment form. * * @param int $comment_post_id Post ID. * * @return void */ public function validate_captcha_field_on_comment( $comment_post_id ) { if ( $this->service->exclude_captcha_for_requests() ) { return; } // Skip if it's from WooCommerce review form. if ( 'product' === get_post_type( $comment_post_id ) ) { return; } if ( ! $this->captcha_response( 'default_comments' ) ) { // @since v2.5.6 wp_die( wp_kses_post( apply_filters( 'wd_recaptcha_require_valid_comment', $this->service->error_message() ) ) ); } } /** * Validates the reCAPTCHA response for a given form. * * @param string $form The form being verified. * * @return bool The result of the reCAPTCHA verification. * @since 2.5.6 */ protected function captcha_response( string $form ): bool { $response_field = 'turnstile' === $this->recaptcha_type ? 'wpdef-turnstile-response' : 'g-recaptcha-response'; $response = stripslashes( defender_get_data_from_request( $response_field, 'p' ) ); if ( empty( $this->private_key ) || empty( $response ) ) { return false; } // reCAPTCHA response post data. $remote_ip = filter_var( defender_get_data_from_request( 'REMOTE_ADDR', 's' ), FILTER_VALIDATE_IP ); /** * Filters to Get the remote IP address. * * @param mixed $remote_ip The remote IP address. * * @since 2.5.6 */ $remote_ip = (string) apply_filters( 'wd_recaptcha_remote_ip', $remote_ip ); $post_body = array( 'secret' => $this->private_key, 'response' => $response, 'remoteip' => $remote_ip, ); $result = $this->service->captcha_post_request( $post_body ); /** * Filters to check the result of a reCAPTCHA verification. * * @param mixed $result The result of the reCAPTCHA verification. * @param mixed $form The form being verified. * * @since 2.5.6 */ return apply_filters( 'wd_recaptcha_check_result', $result, $form ); } /** * Display the BuddyPress reCAPTCHA. * * @return void */ public function display_buddypress_recaptcha(): void { if ( ! empty( buddypress()->signup->errors['failed_recaptcha_verification'] ) ) { $output = '