options = get_option( ADMINTOSH_OPTION_NAME ); $get_options = $this->options; add_action('init', [ $this, 'init_session' ], 999 ); if( !empty( $get_options['active_login_captcha'] ) ) { add_action('login_form', [ $this, 'login_form_captcha' ], 999 ); add_filter('wp_authenticate_user',[ $this, 'validate_login_captcha' ],10,2); } } public function init_session() { if(!session_id()) { session_start(); } } public function login_form_captcha() { $admintoshrev_options = $this->options; ?> addition_captcha(); }elseif( !empty( $admintoshrev_options['captcha_type'] ) && $admintoshrev_options['captcha_type'] == 'google_captcha' ) { $this->google_recaptcha(); } else { $this->random_number_captcha(); } } public function validate_login_captcha( $user, $password ) { $return_value = $user; $admintoshrev_options = $this->options; // Check captcha type if( !empty( $admintoshrev_options['captcha_type'] ) && $admintoshrev_options['captcha_type'] == 'google_captcha' ) { // Storing google recaptcha response in $recaptcha variable $recaptcha = isset( $_POST['g-recaptcha-response'] ) ? esc_html( $_POST['g-recaptcha-response'] ) : ''; // Put secret key here, which we get from google console $secret_key = !empty( $admintoshrev_options['grc_secret_key'] ) ? $admintoshrev_options['grc_secret_key'] : ''; // Hitting request to the URL, Google will respond with success or error scenario $url = 'https://www.google.com/recaptcha/api/siteverify?secret='. esc_html( $secret_key ) . '&response=' .esc_html( $recaptcha ); // Making request to verify captcha $response = wp_remote_get( esc_url_raw( $url ) ); // Response return by google is in JSON format, so we have to parse $response = json_decode( wp_remote_retrieve_body( $response ) ); if ($response->success != true) { $return_value = new \WP_Error( 'loginCaptchaError', esc_html__( 'Captcha Error. Please try again.', 'admintosh' ) ); } } else { if( !$this->check( sanitize_text_field( $_POST['adtosh_captcha_answer'] ) ) ) { // if there is a mis-match $return_value = new \WP_Error( 'loginCaptchaError', esc_html__( 'Captcha Error. Please try again. ', 'admintosh' ) ); } } return $return_value; } public function check( $ans ) { $uid = $_POST['adtosh_captcha_id'] ?? ''; return isset( $_SESSION['admintosh_security_captcha_'.$uid] ) && $_SESSION['admintosh_security_captcha_'.$uid] == $ans ? true : false; } public function addition_captcha() { ?>