add($check->get_error_code(), $check->get_error_message()); } } public static function field_login() { self::field('login'); } public static function field_register() { self::field('register'); } public static function field_lost_password() { self::field('lost_password'); } public static function field($surface) { if (!class_exists('AIEO_SSO_Buttons_Data')) { return; } if (!AIEO_SSO_Buttons_Data::captcha_is_active($surface)) { return; } if (!self::should_enforce($surface)) { return; } self::$needed = true; echo ''; $captcha = AIEO_SSO_Buttons_Data::captcha_settings(); if (!empty($captcha['hide_badge'])) { self::notice_style(); echo '
' . wp_kses_post(sprintf( __('This site is protected by reCAPTCHA and the Google %1$sPrivacy Policy%2$s and %3$sTerms of Service%4$s apply.', 'ai-eshop-optimizer'), '', '', '', '' )) . '
'; } } private static function notice_style() { if (!wp_style_is(self::NOTICE_STYLE, 'registered')) { wp_register_style(self::NOTICE_STYLE, false, array(), null); wp_add_inline_style(self::NOTICE_STYLE, 'p.aieo-captcha-disclosure{margin:8px 0 0;font-size:12px;line-height:1.5;color:var(--wp--preset--color--muted,#6b7280)}'); } wp_enqueue_style(self::NOTICE_STYLE); } public static function register_script() { if (!class_exists('AIEO_SSO_Buttons_Data')) { return; } if (!AIEO_SSO_Buttons_Data::captcha_has_active_surface()) { return; } $notice = AIEO_SSO_Buttons_Data::captcha_settings(); if (!empty($notice['hide_badge'])) { self::notice_style(); } $asset = self::asset(); if ($asset === null) { return; } wp_register_script(self::HANDLE, $asset['url'], array(), (string) $asset['ver'], true); $captcha = AIEO_SSO_Buttons_Data::captcha_settings(); wp_localize_script(self::HANDLE, 'aieoCaptchaCfg', array( 'siteKey' => (string) $captcha['site_key'], 'field' => self::FIELD, 'hideBadge' => !empty($captcha['hide_badge']), 'timeoutMs' => 8000, )); } public static function require_script() { if (!wp_script_is(self::HANDLE, 'registered')) { return false; } self::$needed = true; wp_enqueue_script(self::HANDLE); return true; } public static function require_for_magic_link() { if (!class_exists('AIEO_SSO_Buttons_Data')) { return; } if (!AIEO_SSO_Buttons_Data::captcha_is_active('magic_link')) { return; } if (!wp_script_is('aieo-magic-link-modal', 'enqueued')) { return; } if (!self::should_enforce('magic_link')) { return; } self::$needed = true; if (wp_script_is(self::HANDLE, 'registered')) { wp_enqueue_script(self::HANDLE); } } public static function maybe_print_script() { if (!self::$needed) { return; } if (!wp_script_is(self::HANDLE, 'registered')) { return; } wp_enqueue_script(self::HANDLE); } private static function asset() { $rel = 'assets/js/aieo-captcha.js'; foreach (array(dirname(__DIR__, 2), dirname(__DIR__)) as $root) { $abs = $root . '/' . $rel; if (file_exists($abs)) { return array( 'url' => plugins_url($rel, $root . '/plugin.php'), 'ver' => (string) filemtime($abs), ); } } return null; } private static function siteverify($token, $expected_action = '') { $memo_key = md5($token . '|' . $expected_action); if (isset(self::$verified[$memo_key])) { return self::$verified[$memo_key]; } $captcha = AIEO_SSO_Buttons_Data::captcha_settings(); $fail_open = !empty($captcha['fail_open']); $threshold = (float) $captcha['threshold']; $body = array( 'secret' => (string) $captcha['secret_key'], 'response' => $token, ); $ip = self::client_ip(); if ($ip !== '') { $body['remoteip'] = $ip; } $response = wp_remote_post(self::VERIFY_URL, array( 'timeout' => 5, 'redirection' => 0, 'body' => $body, )); if (is_wp_error($response)) { return self::$verified[$memo_key] = self::soft_failure( $fail_open, 'transport: ' . $response->get_error_message() ); } if ((int) wp_remote_retrieve_response_code($response) !== 200) { return self::$verified[$memo_key] = self::soft_failure( $fail_open, 'http_' . (int) wp_remote_retrieve_response_code($response) ); } $decoded = json_decode((string) wp_remote_retrieve_body($response), true); if (!is_array($decoded)) { return self::$verified[$memo_key] = self::soft_failure($fail_open, 'unparseable_response'); } if (empty($decoded['success'])) { $codes = isset($decoded['error-codes']) && is_array($decoded['error-codes']) ? array_map('strval', $decoded['error-codes']) : array(); $misconfigured = array_intersect($codes, array( 'invalid-input-secret', 'missing-input-secret', 'bad-request', )); if (!empty($misconfigured)) { return self::$verified[$memo_key] = self::soft_failure( $fail_open, 'misconfigured: ' . implode(',', $codes) ); } return self::$verified[$memo_key] = array( 'outcome' => 'block', 'score' => null, 'message' => in_array('timeout-or-duplicate', $codes, true) ? __('Your security check expired. Please reload the page and try again.', 'ai-eshop-optimizer') : __('We could not verify that you are human. Please reload the page and try again.', 'ai-eshop-optimizer'), ); } if ($expected_action !== '' && isset($decoded['action']) && (string) $decoded['action'] !== $expected_action) { return self::$verified[$memo_key] = array( 'outcome' => 'block', 'score' => null, 'message' => __('We could not verify that you are human. Please reload the page and try again.', 'ai-eshop-optimizer'), ); } $score = isset($decoded['score']) ? (float) $decoded['score'] : null; if ($score !== null && $score < $threshold) { return self::$verified[$memo_key] = array( 'outcome' => 'block', 'score' => $score, 'message' => __('Your request looked automated and was blocked. If this is a mistake, please contact us.', 'ai-eshop-optimizer'), ); } return self::$verified[$memo_key] = array('outcome' => 'pass', 'score' => $score, 'message' => ''); } private static function soft_failure($fail_open, $reason) { self::log('verification unavailable (' . $reason . ') — ' . ($fail_open ? 'allowing through' : 'blocking')); set_transient('aieo_captcha_last_error', $reason . ' @ ' . gmdate('Y-m-d H:i:s') . ' UTC', WEEK_IN_SECONDS); return array( 'outcome' => $fail_open ? 'fail_open' : 'block', 'score' => null, 'message' => __('We could not complete the security check. Please try again in a moment.', 'ai-eshop-optimizer'), ); } private static function client_ip() { $candidates = array(); if (!empty($_SERVER['HTTP_CF_CONNECTING_IP'])) { $candidates[] = wp_unslash($_SERVER['HTTP_CF_CONNECTING_IP']); } if (!empty($_SERVER['REMOTE_ADDR'])) { $candidates[] = wp_unslash($_SERVER['REMOTE_ADDR']); } foreach ($candidates as $ip) { $ip = trim((string) $ip); if (filter_var($ip, FILTER_VALIDATE_IP)) { return $ip; } } return ''; } private static function merge($errors, $check) { if (!is_wp_error($check)) { return $errors; } if (is_wp_error($errors)) { $errors->add($check->get_error_code(), $check->get_error_message()); return $errors; } return $check; } private static function message($text) { return '' . esc_html__('Error:', 'ai-eshop-optimizer') . ' ' . esc_html($text); } private static function bump_blocked() { $key = 'aieo_captcha_blocked_' . gmdate('Ymd'); set_transient($key, (int) get_transient($key) + 1, 8 * DAY_IN_SECONDS); } public static function blocked_count($days = 7) { $total = 0; for ($i = 0; $i < max(1, (int) $days); $i++) { $total += (int) get_transient('aieo_captcha_blocked_' . gmdate('Ymd', time() - ($i * DAY_IN_SECONDS))); } return $total; } private static function log($message) { if (function_exists('wc_get_logger')) { wc_get_logger()->warning($message, array('source' => 'aieo-captcha')); return; } if (defined('WP_DEBUG') && WP_DEBUG) { error_log('[aieo-captcha] ' . $message); } } }