array( 'secret' => $settings['secret_key'], 'response' => $response_token, 'remoteip' => isset( $_SERVER['REMOTE_ADDR'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ) ) : '', ), 'timeout' => 10, ) ); // Check for connection errors. if ( is_wp_error( $response ) ) { return new WP_Error( 'captcha_connection_error', __( 'Unable to verify CAPTCHA. Please try again.', 'airy-frontend-forms' ) ); } // Parse response. $response_body = wp_remote_retrieve_body( $response ); $result = json_decode( $response_body, true ); if ( empty( $result ) ) { return new WP_Error( 'captcha_invalid_response', __( 'CAPTCHA verification failed. Please try again.', 'airy-frontend-forms' ) ); } // Check if verification was successful. if ( empty( $result['success'] ) ) { $error_codes = isset( $result['error-codes'] ) ? implode( ', ', $result['error-codes'] ) : 'unknown'; return new WP_Error( 'captcha_verification_failed', __( 'CAPTCHA verification failed. Please try again.', 'airy-frontend-forms' ) ); } // Check score threshold. $score_threshold = isset( $settings['score_threshold'] ) ? (float) $settings['score_threshold'] : 0.5; $actual_score = isset( $result['score'] ) ? (float) $result['score'] : 0.0; if ( $actual_score < $score_threshold ) { // Log the score for debugging (removed to comply with PHPCS). return new WP_Error( 'captcha_low_score', __( 'Suspicious activity detected. Please try again or contact support if this problem persists.', 'airy-frontend-forms' ) ); } // Verification successful! // Return score for tracking. return array( 'success' => true, 'score' => $actual_score, ); } /** * Check if reCAPTCHA is enabled * * @return bool. */ public static function is_enabled() { $settings = get_option( 'affcf_captcha_settings', array() ); return ! empty( $settings['enabled'] ) && ! empty( $settings['site_key'] ) && ! empty( $settings['secret_key'] ); } /** * Get site key for frontend * * @return string. */ public static function get_site_key() { $settings = get_option( 'affcf_captcha_settings', array() ); return $settings['site_key'] ?? ''; } /** * Get score threshold * * @return float. */ public static function get_score_threshold() { $settings = get_option( 'affcf_captcha_settings', array() ); return isset( $settings['score_threshold'] ) ? (float) $settings['score_threshold'] : 0.5; } /** * Render reCAPTCHA badge in frontend */ public static function render_badge() { if ( ! self::is_enabled() ) { return; } ?>