*/ protected static $scores = array(); /** * The score Google returned for the given form during this request. * * @param string $captcha_action The per-form flag key (accuaform_{fid}). * @return float|null Null when no v3 verification ran, or when Google * answered without a score. */ public static function getScore( $captcha_action ) { return isset( self::$scores[ $captcha_action ] ) ? self::$scores[ $captcha_action ] : null; } public function isValid( $value ) { if ( isset( $_POST['accua-forms-recaptcha3-response'] ) ) { $response = stripslashes_deep( $_POST['accua-forms-recaptcha3-response'] ); } else { $response = (string) $value; } if ( '' === $response ) { return $this->failed(); } $verify_url = 'https://www.google.com/recaptcha/api/siteverify'; $verify_data = array( 'secret' => $this->privateKey, 'response' => $response, 'remoteip' => isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '', ); $response_obj = wp_remote_post( $verify_url, array( 'body' => $verify_data, 'timeout' => 20, ) ); if ( is_wp_error( $response_obj ) ) { return $this->failed(); } $body = wp_remote_retrieve_body( $response_obj ); $result = json_decode( $body, true ); if ( empty( $result['success'] ) ) { return $this->failed(); } // Reject tokens generated for a different action (token replay from another context). if ( ! isset( $result['action'] ) || $result['action'] !== $this->captchaAction ) { return $this->failed(); } if ( ! isset( $result['score'] ) ) { return $this->failed(); } $score = (float) $result['score']; // Keep the score for the submission handler, whatever the outcome. self::$scores[ $this->captchaAction ] = $score; /** * Filter the minimum reCAPTCHA v3 score required to accept a submission. * * The default is the value configured by the administrator: the field * instance override if it has one, otherwise the site-wide minimum * score from the plugin settings page (0.5 out of the box). * * @param float $threshold Minimum score (0.0 - 1.0). * @param string $action The reCAPTCHA action of the form being validated. */ $threshold = (float) apply_filters( 'accua_forms_recaptcha3_score_threshold', (float) $this->scoreThreshold, $this->captchaAction ); if ( $score < $threshold ) { return $this->failed(); } return true; } }