options_name ); if ( empty( $options ) || ! $options ) { $this->settings = $this->get_default_reCAPTCHA_settings(); } else { $this->settings = get_option( $this->options_name ); } add_action( 'admin_init', array( $this, 'register_reCAPTCHA_settings' ), 6 ); add_action( 'password_protected_after_password_field', array( $this, 'add_recaptcha' ) ); add_filter( 'password_protected_verify_recaptcha', array( $this, 'verify_recaptcha' ) ); } /** * reCAPTCHA Default Settings * * @return array * @since 2.6 */ private function get_default_reCAPTCHA_settings(): array { return array( 'enable' => 0, 'version' => 'google_recaptcha_v2', 'v2_site_key' => null, 'v3_site_key' => null, 'v2_secret_key' => null, 'v3_secret_key' => null, 'v3_score' => 0.3, 'v3_badge' => 'bottomright', 'v2_theme' => 'light', ); } /** * reCAPTCHA Settings Info * * Displays information on the settings page for helping * to configure Password Protected to work with Google reCAPTCHA v2 and v3. * * @since 2.6 */ public function register_reCAPTCHA_settings() { // reCAPTCHA Section add_settings_section( $this->options_group, __( 'Google reCAPTCHA', 'password-protected' ), array( $this, 'reCAPTCHA_section' ), $this->tab ); // Enable reCAPTCHA add_settings_field( 'password_protected_enable_recaptcha', __( 'Enable reCAPTCHA ', 'password-protected' ), array( $this, 'reCAPTCHA_enable' ), $this->tab, $this->options_group ); // reCAPTCHA version v2/v3 add_settings_field( 'password_protected_recaptcha_settings', __( 'Captcha Settings', 'password-protected' ), array( $this, 'reCAPTCHA_setting' ), $this->tab, $this->options_group ); // reCAPTCHA v2/v3 sitekey add_settings_field( 'password_protected_recaptcha_v2_site_key', __( 'Site Key', 'password-protected' ), array( $this, 'reCAPTCHA_site_key' ), $this->tab, $this->options_group ); // reCAPTCHA v2/v3 secretkey add_settings_field( 'password_protected_recaptcha_v2_secret_key', __( 'Secret Key', 'password-protected' ), array( $this, 'reCAPTCHA_secret_key' ), $this->tab, $this->options_group ); // reCAPTCHA v3 score add_settings_field( 'password_protected_recaptcha_score', __( 'Score', 'password-protected' ), array( $this, 'reCAPTCHA_score' ), $this->tab, $this->options_group ); // reCAPTCHA v3 badgeposition add_settings_field( 'password_protected_recaptcha_badge_position', __( 'Badge Position', 'password-protected' ), array( $this, 'reCAPTCHA_badge_position' ), $this->tab, $this->options_group ); // reCAPTCHA v2 theme add_settings_field( 'password_protected_recaptcha_theme', __( 'Theme', 'password-protected' ), array( $this, 'reCAPTCHA_theme' ), $this->tab, $this->options_group ); // register settings in an array group. register_setting( 'password-protected-advanced', $this->options_name, array( 'type' => 'array' ) ); } /** * reCAPTCHA Screen * * @since 2.6 * * @return void password protected reCAPTCHA settings */ public static function recpatcha_screen() { do_settings_sections( 'password-protected&tab=advanced' ); submit_button(); } /** * reCAPTCHA Section * * @return void password protected reCAPTCHA section */ public function reCAPTCHA_section() { return 1; } /** * ENable reCAPTCHA * * @since 2.6 * * @return void password protected reCAPTCHA status field */ public function reCAPTCHA_enable() { echo ''; } /** * reCAPTCHA Version * * @since 2.6 * * @return void password protected reCAPTCHA version field */ public function reCAPTCHA_setting() { echo '
'; } /** * reCAPTCHA Site Key * * @since 2.6 * * @return void password protected v2/v3 sitekey field */ public function reCAPTCHA_site_key() { echo '

Enter Google reCAPTCHA v2 Site Key.  Click Here

'; echo '

Enter Google reCAPTCHA v3 Site Key.  Click Here

'; } /** * reCAPTCHA Secret Key * * @since 2.6 * * @return void password protected v2/v3 secretkey field */ public function reCAPTCHA_secret_key() { echo '

Enter Google reCAPTCHA v2 Secret Key.  Click Here

'; echo '

Enter Google reCAPTCHA v3 Secret Key.  Click Here

'; } /** * reCAPTCHA V3 Score * * @since 2.6 * * @return void password protected v3 score field */ public function reCAPTCHA_score() { echo '
         

Select Google Version 3 Score.

'; } /** * reCAPTCHA V3 Badge Position * * @since 2.6 * * @return void password protected v3 badgeposition field */ public function reCAPTCHA_badge_position() { echo '
     
'; } /** * reCAPTCHA V2 Theme * * @since 2.6 * * @return void password protected v2 theme field */ public function reCAPTCHA_theme() { echo '

Select Google reCAPTCHA Version 2 Theme.

'; } /** * Add reCAPTCHA on Password Protected Form * * @since 2.6 * * @return void password protected reCAPTCHA v2 OR v3 */ public function add_recaptcha() { if ( ! @$this->settings['enable'] ) { return; // recpatcha is disabled } if ( $this->settings['version'] === 'google_recaptcha_v2' ) { $this->display_recaptcha_v2(); } if ( $this->settings['version'] === 'google_recaptcha_v3' ) { $this->display_recaptcha_v3(); } } /** * Diaplay reCAPTCHA V2 * * @since 2.6 * * @return void password protected reCAPTCHA v2 field */ public function display_recaptcha_v2() { wp_enqueue_style( 'pp-recaptcha-style', plugin_dir_url( __DIR__ ) . 'assets/css/recaptcha.css', array(), '2.6.2' ); wp_enqueue_script( 'pp-recaptcha-api-v2', esc_url( 'https://www.google.com/recaptcha/api.js' ), array(), null ); echo '
'; } /** * Diaplay reCAPTCHA V3 * * @since 2.6 * * @return void password protected reCAPTCHA v3 field */ public function display_recaptcha_v3() { $grecaptcha_v3_site_key = isset( $this->settings['v3_site_key'] ) ? esc_attr( $this->settings['v3_site_key'] ) : ''; $grecaptcha_v3_badge = isset( $this->settings['v3_badge'] ) ? esc_attr( $this->settings['v3_badge'] ) : 'bottomright'; $script = << settings['enable'] ) { return $errors; // return errors } if ( $this->settings['version'] === 'google_recaptcha_v2' ) { $grecaptcha_v2_site_key = isset( $this->settings['v2_site_key'] ) ? esc_attr( $this->settings['v2_site_key'] ) : ''; $grecaptcha_v2_secret_key = isset( $this->settings['v2_secret_key'] ) ? esc_attr( $this->settings['v2_secret_key'] ) : ''; if ( empty( $grecaptcha_v2_site_key ) || empty( $grecaptcha_v2_secret_key ) ) { $errors->add( 001, 'Google reCaptcha keys not found.' ); } if ( isset( $_POST['g-recaptcha-response'] ) && ! empty( $_POST['g-recaptcha-response'] ) ) { $response = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', array( 'body' => array( 'secret' => $grecaptcha_v2_secret_key, 'response' => sanitize_text_field( $_POST['g-recaptcha-response'] ), ), ) ); $data = wp_remote_retrieve_body( $response ); $data = json_decode( $data ); if ( isset( $data->{'error-codes'} ) && is_array( $data->{'error-codes'} ) && count( $data->{'error-codes'} ) ) { foreach ( $data->{'error-codes'} as $index => $error_code ) { $errors->add( $index, $error_code ); } } if ( isset( $data->success ) && true === $data->success ) { return $errors; } } $error_message = wp_kses( __( 'ERROR: Please confirm you are not a robot.', 'password-protected' ), array( 'strong' => array() ) ); $errors->add( 'captcha_invalid', $error_message ); return $errors; } elseif ( $this->settings['version'] === 'google_recaptcha_v3' ) { $grecaptcha_v3_site_key = isset( $this->settings['v3_site_key'] ) ? esc_attr( $this->settings['v3_site_key'] ) : ''; $grecaptcha_v3_secret_key = isset( $this->settings['v3_secret_key'] ) ? esc_attr( $this->settings['v3_secret_key'] ) : ''; $grecaptcha_v3_score = isset( $this->settings['v3_score'] ) ? esc_attr( $this->settings['v3_score'] ) : '0.3'; if ( empty( $grecaptcha_v3_site_key ) || empty( $grecaptcha_v3_secret_key ) ) { $errors->add( 001, 'Google reCaptcha keys not found.' ); } if ( isset( $_POST['g-recaptcha-response'] ) && ! empty( $_POST['g-recaptcha-response'] ) ) { $response = wp_remote_post( 'https://www.google.com/recaptcha/api/siteverify', array( 'body' => array( 'secret' => $grecaptcha_v3_secret_key, 'response' => sanitize_text_field( $_POST['g-recaptcha-response'] ), 'remoteip' => self::get_ip_address(), ), ) ); $data = wp_remote_retrieve_body( $response ); $data = json_decode( $data ); if ( isset( $data->{'error-codes'} ) && is_array( $data->{'error-codes'} ) && count( $data->{'error-codes'} ) ) { foreach ( $data->{'error-codes'} as $index => $error_code ) { $errors->add( $index, $error_code ); } } if ( isset( $data->success ) && true === $data->success ) { $grecaptcha_v3_score = (float) $grecaptcha_v3_score; if ( isset( $data->action ) && ( 'password_protected' === $data->action ) && isset( $data->score ) && $data->score >= $grecaptcha_v3_score ) { return $errors; } else { $error_message = wp_kses( __( 'ERROR: Low Score ', 'password-protected' ) . ':' . esc_html( $data->score ), array( 'strong' => array() ) ); $errors->add( 002, $error_message ); } } } return $errors; } } /** * Get IP Address * * @since 2.6 * * @return string client IP address */ private static function get_ip_address() { $ipaddress = ''; if ( isset( $_SERVER['HTTP_CLIENT_IP'] ) ) { $ipaddress = sanitize_text_field( $_SERVER['HTTP_CLIENT_IP'] ); } elseif ( isset( $_SERVER['HTTP_X_FORWARDED_FOR'] ) ) { $ipaddress = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED_FOR'] ); } elseif ( isset( $_SERVER['HTTP_X_FORWARDED'] ) ) { $ipaddress = sanitize_text_field( $_SERVER['HTTP_X_FORWARDED'] ); } elseif ( isset( $_SERVER['HTTP_FORWARDED_FOR'] ) ) { $ipaddress = sanitize_text_field( $_SERVER['HTTP_FORWARDED_FOR'] ); } elseif ( isset( $_SERVER['HTTP_FORWARDED'] ) ) { $ipaddress = sanitize_text_field( $_SERVER['HTTP_FORWARDED'] ); } elseif ( isset( $_SERVER['REMOTE_ADDR'] ) ) { $ipaddress = sanitize_text_field( $_SERVER['REMOTE_ADDR'] ); } else { $ipaddress = 'UNKNOWN'; } return $ipaddress; } }