options = get_option( ADMINTOSH_OPTION_NAME ); $get_options = $this->options; add_action('init', [ $this, 'init_session' ], 1 ); add_action('init', [ $this, 'init_task' ] ); if( !empty( $get_options['active_login_captcha'] ) ) { // WP login if( !empty( $get_options['captcha_wp_login'] ) ) { // wp login form add_action('login_form', [ $this, 'login_form_captcha' ], 999 ); // WooCommerce My account login if( adtosh_is_woo_active() ) { add_action('woocommerce_login_form', [ $this, 'add_recaptcha' ]); } // Edd Login if( adtosh_is_edd_active() ) { add_action('edd_login_fields_after', [ $this, 'add_recaptcha' ]); add_action('edd_user_login', [ $this, 'edd_recaptcha_validate_login' ] ); } add_filter('wp_authenticate_user',[ $this, 'validate_login_captcha' ],10,2); } // WP registration if( !empty( $get_options['captcha_wp_registration'] ) ) { // wp reg add_action('register_form', [ $this, 'login_form_captcha' ]); add_filter('registration_errors', [ $this, 'validate_recaptcha_wp_registration' ], 10, 3); // Edd register form if( adtosh_is_edd_active() ) { add_action('edd_register_form_fields_before_submit', [ $this, 'add_recaptcha' ]); add_action('edd_process_register_form', [ $this, 'edd_recaptcha_validate_registration' ], 10 ); } // WooCommerce Registration form if( adtosh_is_woo_active() ) { add_action('woocommerce_register_form', [ $this, 'add_recaptcha' ] ); add_action('woocommerce_register_post', [ $this, 'validate_recaptcha_wc_registration' ], 10, 3); } } // blog comment if( !empty( $get_options['captcha_blog_comment'] ) ) { add_action('comment_form_submit_field', [ $this, 'add_comment_recaptcha' ] ); add_filter('preprocess_comment', [ $this, 'verify_recaptcha_on_comment' ]); } } } public function init_task() { $this->errorMsg = esc_html__( 'CAPTCHA verification failed. Please try again.', 'admintosh' ); } public function init_session() { //session_destroy(); 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 ) { if( !$this->is_validate_captch() ) { return new \WP_Error( 'loginCaptchaError', $this->errorMsg ); } return $user; } public function is_validate_captch () { $admintoshrev_options = $this->options; $is_true = true; // 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) { $is_true = false; } } else { if( !$this->check( sanitize_text_field( $_POST['adtosh_captcha_answer'] ) ) ) { // if there is a mis-match $is_true = false; } } return $is_true; } 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() { ?>