=' ) ) { add_filter( 'pre_comment_approved', array( $this, 'comment_verify_490' ), 99 ); } else { add_filter( 'preprocess_comment', array( $this, 'comment_verify' ) ); } } } function add_error_to_mgs( $mgs = false ) { if ( false === $mgs ) { $mgs = c4wp_get_option( 'error_message', '' ); } if ( ! $mgs ) { $mgs = __( 'Please solve Captcha correctly', 'advanced-nocaptcha-recaptcha' ); } $message = '' . __( 'ERROR', 'advanced-nocaptcha-recaptcha' ) . ': ' . $mgs; return apply_filters( 'c4wp_error_message', $message, $mgs ); } function total_captcha() { return self::$captcha_count; } function captcha_form_field() { self::$captcha_count++; $site_key = trim( c4wp_get_option( 'site_key' ) ); $number = $this->total_captcha(); $version = c4wp_get_option( 'captcha_version', 'v2_checkbox' ); $field = '
' . $errmsg . '
'; } $this->form_field(); } function verify( $response = false ) { static $last_verify = null; static $last_response = null; static $duplicate_response = false; $secret_key = trim( c4wp_get_option( 'secret_key' ) ); $verify = false; $remoteip = $_SERVER['REMOTE_ADDR']; if ( false === $response ) { $response = isset( $_POST['g-recaptcha-response'] ) ? $_POST['g-recaptcha-response'] : ''; } // Store what we send to google in case we need to verify if its a duplicated request. $last_response = $response; if ( $response == $last_response ) { $duplicate_response = true; } $pre_check = apply_filters( 'c4wp_verify_captcha_pre', null, $response ); if ( null !== $pre_check ) { return $pre_check; } if ( ! $secret_key ) { // if $secre_key is not set return true; } if ( ! $response || ! $remoteip ) { return $verify; } if ( null !== $last_verify ) { return $last_verify; } $url = apply_filters( 'c4wp_google_verify_url', sprintf( 'https://www.%s/recaptcha/api/siteverify', c4wp_recaptcha_domain() ) ); // make a POST request to the Google reCAPTCHA Server $request = wp_remote_post( $url, array( 'timeout' => 10, 'body' => array( 'secret' => $secret_key, 'response' => $response, 'remoteip' => $remoteip, ), ) ); // get the request response body $request_body = wp_remote_retrieve_body( $request ); if ( ! $request_body ) { return $verify; } $result = json_decode( $request_body, true ); if ( isset( $result['success'] ) && true == $result['success'] ) { if ( 'v3' === c4wp_get_option( 'captcha_version' ) ) { $score = isset( $result['score'] ) ? $result['score'] : 0; $action = isset( $result['action'] ) ? $result['action'] : ''; $verify = c4wp_get_option( 'score', '0.5' ) <= $score && 'advanced_nocaptcha_recaptcha' == $action; } else { $verify = true; } } $verify = apply_filters( 'c4wp_verify_captcha', $verify, $result, $response ); $last_verify = $verify; // If we know this is a duplicated request, pass verification. if ( isset( $result['error-codes'] ) && 'timeout-or-duplicate' == $result['error-codes'][0] && $duplicate_response ) { $verify = true; } return $verify; } function login_verify( $user, $username = '', $password = '' ) { global $wpdb; if ( ! $username ) { return $user; } // Bail if a rest request. if ( $this->is_rest_request() ) { return $user; } // Bail if we have nothign to work with. if ( ! isset( $_POST['g-recaptcha-response'] ) ) { return $user; } $show_captcha = $this->show_login_captcha(); if ( $show_captcha && ! $this->verify() ) { return new WP_Error( 'c4wp_error', $this->add_error_to_mgs() ); } return $user; } /** * Checks if the current authentication request is RESTy or a custom URL where it should not load. */ function is_rest_request() { if ( defined( 'REST_REQUEST' ) && REST_REQUEST || isset( $_GET[ 'rest_route' ] ) && strpos( $_GET[ 'rest_route' ], '/', 0 ) === 0 ) { return true; } global $wp_rewrite; if ( $wp_rewrite === null ) $wp_rewrite = new WP_Rewrite(); $rest_url = wp_parse_url( trailingslashit( rest_url() ) ); $current_url = wp_parse_url( add_query_arg( [] ) ); $is_rest = strpos( $current_url[ 'path' ], $rest_url[ 'path' ], 0 ) === 0; return $is_rest; } function registration_verify( $errors, $sanitized_user_login, $user_email ) { if ( ! $this->verify() ) { $errors->add( 'c4wp_error', $this->add_error_to_mgs() ); } return $errors; } function ms_form_field_verify( $result ) { if ( isset( $_POST['stage'] ) && 'validate-user-signup' === $_POST['stage'] && ! $this->verify() ) { $result['errors']->add( 'c4wp_error', c4wp_get_option( 'error_message' ) ); } return $result; } function ms_blog_verify( $result ) { if ( ! $this->verify() ) { $result['errors']->add( 'c4wp_error', c4wp_get_option( 'error_message' ) ); } return $result; } function lostpassword_verify( $result, $user_id ) { // Allow admins to send reset links. if ( current_user_can( 'manage_options' ) && isset( $_REQUEST['action'] ) && in_array( wp_unslash( $_REQUEST['action'] ), array('resetpassword', 'send-password-reset') ) ) { return $errors; } if ( ! $this->verify() ) { return new WP_Error( 'c4wp_error', $this->add_error_to_mgs() ); } return $result; } function reset_password_verify( $errors, $user ) { // Allow admins to send reset links. if ( current_user_can( 'manage_options' ) && isset( $_REQUEST['action'] ) && in_array( wp_unslash( $_REQUEST['action'] ), array('resetpassword', 'send-password-reset') ) ) { return $errors; } if ( ! $this->verify() ) { $errors->add( 'c4wp_error', $this->add_error_to_mgs() ); } } function comment_verify( $commentdata ) { if ( ! $this->verify() ) { wp_die( '' . $this->add_error_to_mgs() . '
', __( 'Comment Submission Failure' ), array( 'response' => 403, 'back_link' => true, ) ); } return $commentdata; } function comment_verify_490( $approved ) { if ( ! $this->verify() ) { return new WP_Error( 'c4wp_error', $this->add_error_to_mgs(), 403 ); } return $approved; } /** * Checks if the current page load is actually an iframe found in the new customizer/widgets areas within WP 5.8+. * * @return bool */ function check_should_js_embed() { // Ensure we dont load inside an iframe/preview. if ( isset( $_GET['legacy-widget-preview'] ) || isset( $_GET['customize_messenger_channel'] ) ) { return false; } return true; } /** * Auto detects language if applicable, otherwise returns the users desired language from the settings. * * @return string */ function determine_captcha_language() { $language = trim( c4wp_get_option( 'language' ) ); $lang = '&hl=' . $language; return $lang; } } //END CLASS } //ENDIF add_action( 'init', array( c4wp_captcha_class::init(), 'actions_filters' ), -9 );