PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / trunk
Kirki – Freeform Page Builder, Website Builder & Customizer vtrunk
6.3.0 6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 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 1 month ago Form 3 weeks ago ActionHooks.php 1 month ago Canvas.php 1 month ago CollectionItem.php 1 month ago ContentManager.php 1 month ago DateTime.php 1 month ago EditorPreview.php 1 month ago FileHandler.php 7 hours ago FilterHooks.php 1 month ago PageUrl.php 1 month ago Recaptcha.php 1 week ago Role.php 1 month ago Session.php 1 month ago Template.php 1 month ago
Recaptcha.php
110 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\Framework\Http\Response;
14 use Kirki\Framework\Supports\Facades\Http;
15 use Kirki\App\Supports\Session;
16 use RuntimeException;
17
18 if (!defined('ABSPATH')) {
19 exit; // Exit if accessed directly.
20 }
21
22 class Recaptcha
23 {
24 /**
25 * Google reCAPTCHA verification endpoint.
26 *
27 * @var string
28 */
29 public const VERIFY_URL = 'https://www.google.com/recaptcha/api/siteverify';
30
31 /**
32 * Verify a reCAPTCHA token.
33 *
34 * No-op when no token is supplied (matches the legacy behaviour where the
35 * check only runs if the front-end sent a token). Throws on misconfiguration
36 * or a failed verification so the caller can abort the request.
37 *
38 * @param array|null $params Get the reCAPTCHA token from the submission params.
39 * @param string|null $form_id The form ID.
40 * @return void
41 *
42 * @throws RuntimeException When configuration is missing or verification fails.
43 */
44 public static function verify(?array $params = [], ?string $form_id = null)
45 {
46 $session_data = Session::get($form_id);
47 $recaptcha = $session_data['recaptcha'] ?? [];
48
49 if (empty($recaptcha)) {
50 return;
51 }
52
53 if (!isset($recaptcha['GRC_version'])) {
54 throw new RuntimeException(
55 esc_html__('reCAPTCHA configuration not found', 'kirki'),
56 (int) Response::BAD_REQUEST
57 );
58 }
59
60 $token = $recaptcha['GRC_version'] === '2.0' ? ($params['g-recaptcha-response'] ?? '') : ($params['g-recaptcha-token'] ?? '');
61
62 if (empty($token)) {
63 throw new RuntimeException(
64 esc_html__('Recaptcha is required.', 'kirki'),
65 (int) Response::BAD_REQUEST
66 );
67 }
68
69 $secret_key = $recaptcha['GRC_secret_key'] ?? '';
70
71 if (empty($secret_key)) {
72 throw new RuntimeException(
73 esc_html__('reCAPTCHA secret key not configured', 'kirki'),
74 (int) Response::BAD_REQUEST
75 );
76 }
77
78 if (!static::is_token_valid($secret_key, $token)) {
79 throw new RuntimeException(
80 esc_html__('Google reCAPTCHA verification failed', 'kirki'),
81 (int) Response::BAD_REQUEST
82 );
83 }
84 }
85
86 /**
87 * Call Google's siteverify endpoint for the given token.
88 *
89 * @param string $secret_key The reCAPTCHA secret key.
90 * @param string $token The reCAPTCHA token.
91 * @return bool
92 */
93 protected static function is_token_valid(string $secret_key, string $token)
94 {
95 $response = Http::as_form()->post(
96 static::VERIFY_URL,
97 [
98 'secret' => $secret_key,
99 'response' => $token,
100 ]
101 );
102
103 if ($response->failed()) {
104 return false;
105 }
106
107 return (bool) $response->json('success');
108 }
109 }
110