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 |