=' ) ) { add_filter( 'pre_comment_approved', array( $this, 'comment_verify_490' ), 99 ); } else { add_filter( 'preprocess_comment', array( $this, 'comment_verify' ) ); } } add_action( 'wp_ajax_c4wp_ajax_verify', array( $this, 'c4wp_ajax_verify' ), 10, 1 ); add_action( 'wp_ajax_nopriv_c4wp_ajax_verify', array( $this, 'c4wp_ajax_verify' ), 10, 1 ); add_action( 'wp_ajax_c4wp_nocaptcha_plugin_notice_ignore', array( 'C4WP_Settings', 'c4wp_nocaptcha_plugin_notice_ignore' ), 10, 1 ); add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts' ) ); } /** * Add settings page scripts. * * @return void */ public function admin_enqueue_scripts() { wp_enqueue_script( 'c4wp-notices', C4WP_PLUGIN_URL . 'assets/js/notices.js', array( 'jquery' ), C4WP_PLUGIN_VERSION, false ); } /** * A neat wrapper to provide a response verification via AJAX call. */ public function c4wp_ajax_verify() { $url = apply_filters( 'c4wp_google_verify_url', sprintf( 'https://www.%s/recaptcha/api/siteverify', c4wp_recaptcha_domain() ) ); $remoteip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; $secret_key = trim( c4wp_get_option( 'secret_key' ) ); $verify = false; // Grab POSTed data. $nonce = filter_input( INPUT_POST, 'nonce', FILTER_SANITIZE_STRING ); $response = filter_input( INPUT_POST, 'response', FILTER_SANITIZE_STRING ); // Check nonce. if ( empty( $nonce ) || ! wp_verify_nonce( $nonce, 'c4wp_verify_nonce' ) ) { wp_send_json_error( esc_html__( 'Nonce Verification Failed.', 'advanced-nocaptcha-recaptcha' ) ); } $verify = $this->verify(); if ( ! $verify ) { wp_send_json_error(); } else { wp_send_json_success(); } } /** * Additional JS for getting a response via AJAX for further work. * * @return $code - Markup */ public function c4wp_ajax_verification_scripts() { $ajax_url = admin_url( 'admin-ajax.php' ); $flag_markup = ''; $fail_action = c4wp_get_option( 'failure_action', 'nothing' ); $redirect = c4wp_get_option( 'failure_redirect' ); $failure_v2_site_key = c4wp_get_option( 'failure_v2_site_key' ); $code = " var parentElem = captcha_div.parentElement; if ( ! form.classList.contains( 'c4wp_verify_underway' ) && captcha_div.parentElement.getAttribute( 'data-c4wp-use-ajax' ) == 'true' ) { form.classList.add('c4wp_verify_underway' ); const flagMarkup = '" . $flag_markup . "'; var flagMarkupDiv = document.createElement('div'); flagMarkupDiv.innerHTML = flagMarkup.trim(); form.appendChild( flagMarkupDiv ); var nonce = captcha_div.parentElement.getAttribute( 'data-nonce' ); var post_data = { 'action' : 'c4wp_ajax_verify', 'nonce' : nonce, 'response' : data }; var formData = new FormData(); formData.append( 'action', 'c4wp_ajax_verify' ); formData.append( 'nonce', nonce ); formData.append( 'response', data ); fetch( '" . $ajax_url . "', { method: 'POST', body: formData, } ) // wrapped .then( res => res.json() ) .then( data => { if ( data['success'] ) { form.classList.add( 'c4wp_verified' ); if ( typeof jQuery !== 'undefined' && form_type == 'wc_checkout' ) { form.classList.add( 'c4wp_v2_fallback_active' ); jQuery( '.woocommerce-checkout' ).submit(); return true; } else if ( typeof jQuery !== 'undefined' && form_type == 'wc_login' ) { jQuery( '.woocommerce-form-login__submit' ).trigger('click'); return true; } else { if ( typeof form.submit === 'function' ) { form.submit(); } else { HTMLFormElement.prototype.submit.call(form); } } } else { "; if ( 'redirect' === $fail_action ) { $code .= " window.location.href = '" . $redirect . "'; "; } if ( 'v2_checkbox' === $fail_action ) { $code .= " captcha_div.innerHTML = ''; form.classList.add( 'c4wp_v2_fallback_active' ); flagMarkupDiv.firstChild.setAttribute( 'name', 'c4wp_v2_fallback' ); var c4wp_captcha = grecaptcha.render( captcha_div,{ 'sitekey' : '" . $failure_v2_site_key . "', 'expired-callback' : function(){ grecaptcha.reset( c4wp_captcha ); } }); "; } $code .= ' // Prevent further submission event.preventDefault(); return false; } } ) .catch( err => console.error( err ) ); // Prevent further submission event.preventDefault(); return false; } '; return $code; } /** * Return our error message, for use with WP_Error etc. * * @param boolean $mgs - Current message. * @return string $message - Our message. */ public 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 ); } /** * Return number of captchas found in a page. * * @return int $captcha_count - Captcha count. */ public function total_captcha() { return self::$captcha_count; } /** * Create and return captcha field markup. * * @return string $field - Field HTML Markup. */ public 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' ); $verify_nonce = wp_create_nonce( 'c4wp_verify_nonce' ); $fail_action = c4wp_get_option( 'failure_action', 'nothing' ); $use_ajax = ''; if ( 'v2_checkbox' === $fail_action || 'redirect' === $fail_action ) { $use_ajax = 'data-c4wp-use-ajax="true"'; if ( 'v2_checkbox' === $fail_action ) { $key = c4wp_get_option( 'failure_v2_site_key' ); $use_ajax .= ' data-c4wp-v2-site-key="' . $key . '"'; } if ( 'redirect' === $fail_action ) { $redirect = c4wp_get_option( 'failure_v2_site_key' ); $use_ajax .= ' data-c4wp-failure-redirect="' . $redirect . '"'; } } $field = '
' . esc_attr( $errmsg ) . '
'; } $this->form_field(); } /** * Main verification function. Return if a submission is allowed or not. * * @param boolean $response - Current response. * @return bool - Was request valid? */ public function verify( $response = false ) { static $last_verify = null; static $last_response = null; static $duplicate_response = false; $remoteip = isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : ''; $secret_key = isset( $_POST['c4wp_v2_fallback'] ) ? trim( c4wp_get_option( 'failure_v2_secret_key' ) ) : trim( c4wp_get_option( 'secret_key' ) ); $verify = false; if ( false === $response ) { $response = isset( $_POST['g-recaptcha-response'] ) ? sanitize_text_field( wp_unslash( $_POST['g-recaptcha-response'] ) ) : ''; if ( empty( $response ) ) { $response = empty( $response ) && isset( $_POST['response'] ) ? sanitize_text_field( wp_unslash( $_POST['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 is not set. if ( ! $secret_key ) { 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'] : true; $verify = c4wp_get_option( 'score', '0.5' ) <= $score; } 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; } $dev_mode = apply_filters( 'c4wp_enable_dev_mode', false ); if ( $dev_mode ) { $store = array(); $store['verify_post'] = $_POST; $store['verify_result'] = $result; $store['verify_return'] = $verify; c4wp_log_verify_result( $store ); if ( 'return_false' === c4wp_get_option( 'override_result' ) ) { return false; } elseif ( 'return_true' === c4wp_get_option( 'override_result' ) ) { return true; } } return $verify; } /** * Verify a login attempt. * * @param WP_User $user - User object. * @param string $username - Login username. * @param string $password - Login password. * @return WP_User|WP_Error - Always return the user, WP Error otherwise. */ public function login_verify( $user, $username = '', $password = '' ) { global $wpdb; if ( ! $username ) { return $user; } // Bail if a rest request. if ( $this->is_rest_request() ) { return $user; } $show_captcha = $this->show_login_captcha(); if ( ! isset( $_POST['c4wp_ajax_flag'] ) ) { if ( $show_captcha && ! $this->verify() ) { // Bail if we have nothign to work with. if ( ! isset( $_POST['g-recaptcha-response'] ) ) { return $user; } 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. * * @return boolean - Was a rest request? */ public function is_rest_request() { if ( defined( 'REST_REQUEST' ) && REST_REQUEST || isset( $_GET['rest_route'] ) && strpos( sanitize_text_field( wp_unslash( $_GET['rest_route'] ) ), '/', 0 ) === 0 ) { return true; } global $wp_rewrite; if ( null === $wp_rewrite ) { $wp_rewrite = new WP_Rewrite(); // phpcs:ignore } $rest_url = wp_parse_url( trailingslashit( rest_url() ) ); $current_url = wp_parse_url( add_query_arg( array() ) ); // If no path to check, return. if ( ! isset( $current_url['path'] ) || ! isset( $rest_url['path'] ) ) { return apply_filters( 'c4wp_is_rest_request_no_path_found', false ); } $is_rest = strpos( $current_url['path'], $rest_url['path'], 0 ) === 0; return $is_rest; } /** * Verify a registration attempt. * * @param WP_Error $errors - Current error array. * @param string $sanitized_user_login - Current user login. * @param string $user_email - User email address. * @return WP_Error - Error array with ours added, if applicable. */ public function registration_verify( $errors, $sanitized_user_login, $user_email ) { if ( ! $this->verify() ) { // Bail if we have nothign to work with. if ( ! isset( $_POST['g-recaptcha-response'] ) ) { return $errors; } $errors->add( 'c4wp_error', $this->add_error_to_mgs() ); } return $errors; } /** * Verify a new user signup on a multisite network. * * @param array $result - Error array. * @return array $result - Error array with ours added, if applicable. */ public 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; } /** * Verify a new user signup on a WPMU form. * * @param array $result - Error array. * @return array $result - Error array with ours added, if applicable. */ public function ms_blog_verify( $result ) { if ( ! $this->verify() ) { $result['errors']->add( 'c4wp_error', c4wp_get_option( 'error_message' ) ); } return $result; } /** * Verify lost password form submissin. * * @param WP_Error $result - Current errors. * @param int $user_id - User ID. * @return WP_Error - Error array with ours added, if applicable. */ public 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' ), true ) ) { return $result; } if ( ! $this->verify() ) { // Bail if we have nothign to work with. if ( ! isset( $_POST['g-recaptcha-response'] ) ) { return $result; } $result->add( 'c4wp_error', $this->add_error_to_mgs() ); } return $result; } /** * Verify password reset submissions. * * @param WP_Error $errors - Current errors. * @param WP_User $user - User object. * @return WP_Error|WP_User - User object or error. */ public 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' ), true ) ) { return $errors; } if ( ! isset( $_POST['c4wp_ajax_flag'] ) ) { if ( ! $this->verify() ) { // Bail if we have nothign to work with. if ( ! isset( $_POST['g-recaptcha-response'] ) ) { return $user; } $errors->add( 'c4wp_error', $this->add_error_to_mgs() ); } } return $errors; } /** * Verify comment submissions. * * @param array $commentdata - Submitted data. * @return array - New comment data. */ public function comment_verify( $commentdata ) { if ( ! isset( $_POST['c4wp_ajax_flag'] ) ) { if ( ! $this->verify() ) { wp_die( '' . wp_kses_post( $this->add_error_to_mgs() ) . '
', esc_html__( 'Comment Submission Failure' ), array( 'response' => 403, 'back_link' => true, ) ); } } return $commentdata; } /** * Very comment in WP 4.9 and older. * * @param bool $approved - Approval currently. * @returnb ool $approved - Our approval. */ public function comment_verify_490( $approved ) { if ( ! isset( $_POST['c4wp_ajax_flag'] ) ) { 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 */ public 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 */ public function determine_captcha_language() { $language = trim( c4wp_get_option( 'language' ) ); /* @free:start */ $lang = '&hl=' . $language; /* @free:end */ return $lang; } } //END CLASS } //ENDIF add_action( 'init', array( C4wp_Captcha_Class::init(), 'actions_filters' ), -9 );