PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.2.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.2.0
6.2.0 6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / app / Supports / Recaptcha.php
kirki / app / Supports Last commit date
Facades 5 days ago Form 5 days ago ActionHooks.php 5 days ago Canvas.php 1 month ago CollectionItem.php 5 days ago ContentManager.php 1 month ago DateTime.php 1 month ago EditorPreview.php 2 weeks ago FileHandler.php 2 weeks ago FilterHooks.php 2 weeks ago PageUrl.php 5 days ago Recaptcha.php 5 days ago Role.php 1 month ago Session.php 5 days ago Template.php 5 days ago
Recaptcha.php
102 lines
1 <?php
2 /**
3 * Google reCAPTCHA support
4 *
5 * Verifies a reCAPTCHA token against Google's siteverify endpoint using the
6 * keys configured in the Kirki admin common data option.
7 *
8 * @package kirki
9 */
10
11 namespace Kirki\App\Supports;
12
13 use Kirki\App\Constants\OptionKeys;
14 use Kirki\Framework\Http\Response;
15 use Kirki\Framework\Supports\Facades\Http;
16 use Kirki\Framework\Supports\Facades\Option;
17 use RuntimeException;
18
19 if (!defined('ABSPATH')) {
20 exit; // Exit if accessed directly.
21 }
22
23 class Recaptcha
24 {
25 /**
26 * Google reCAPTCHA verification endpoint.
27 *
28 * @var string
29 */
30 public const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
31
32 /**
33 * Verify a reCAPTCHA token.
34 *
35 * No-op when no token is supplied (matches the legacy behaviour where the
36 * check only runs if the front-end sent a token). Throws on misconfiguration
37 * or a failed verification so the caller can abort the request.
38 *
39 * @param string|null $token The reCAPTCHA token from the submission.
40 * @return void
41 *
42 * @throws RuntimeException When configuration is missing or verification fails.
43 */
44 public static function verify(?string $token = null)
45 {
46 if (empty($token)) {
47 return;
48 }
49
50 $common_data = Option::get(OptionKeys::WP_ADMIN_COMMON_DATA, [], false);
51
52 if (!isset($common_data['recaptcha']['GRC_version'])) {
53 throw new RuntimeException(
54 esc_html__('reCAPTCHA configuration not found', 'kirki'),
55 (int) Response::BAD_REQUEST
56 );
57 }
58
59 $version = $common_data['recaptcha']['GRC_version'];
60 $recaptcha = $common_data['recaptcha'][$version] ?? [];
61 $secret_key = $recaptcha['GRC_secret_key'] ?? '';
62
63 if (empty($secret_key)) {
64 throw new RuntimeException(
65 esc_html__('reCAPTCHA secret key not configured', 'kirki'),
66 (int) Response::BAD_REQUEST
67 );
68 }
69
70 if (!static::is_token_valid($secret_key, $token)) {
71 throw new RuntimeException(
72 esc_html__('Google reCAPTCHA verification failed', 'kirki'),
73 (int) Response::BAD_REQUEST
74 );
75 }
76 }
77
78 /**
79 * Call Google's siteverify endpoint for the given token.
80 *
81 * @param string $secret_key The reCAPTCHA secret key.
82 * @param string $token The reCAPTCHA token.
83 * @return bool
84 */
85 protected static function is_token_valid(string $secret_key, string $token)
86 {
87 $response = Http::as_form()->post(
88 static::VERIFY_URL,
89 [
90 'secret' => $secret_key,
91 'response' => $token,
92 ]
93 );
94
95 if ($response->failed()) {
96 return false;
97 }
98
99 return (bool) $response->json('success');
100 }
101 }
102