PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.3.27
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.3.27
1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk All 48 releases
fluent-cart / app / Modules / Turnstile / TurnstileValidator.php

TurnstileValidator.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.3.27, at app/Modules/Turnstile/TurnstileValidator.php

94 lines 2.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\App\Modules\Turnstile;
4
5 use FluentCart\Api\ModuleSettings;
6 use FluentCart\Framework\Support\Arr;
7 use FluentCart\App\Helpers\AddressHelper;
8
9 class TurnstileValidator
10 {
11 public function register()
12 {
13 add_filter('fluent_cart/checkout/validate_before_process', [$this, 'validateCheckout'], 10, 2);
14 }
15
16 /**
17 * Validate Turnstile token during checkout
18 *
19 * @param bool $isValid
20 * @param array $data
21 * @return bool|WP_Error
22 */
23 public function validateCheckout($isValid, $data)
24 {
25 // If validation already failed, don't proceed
26 if (is_wp_error($isValid)) {
27 return $isValid;
28 }
29
30 $turnstileSettings = ModuleSettings::getSettings('turnstile');
31
32 // Only validate if Turnstile is active
33 if (Arr::get($turnstileSettings, 'active', 'no') !== 'yes') {
34 return $isValid;
35 }
36
37 $turnstileToken = Arr::get($data, 'cf_turnstile_token', '');
38
39 if (empty($turnstileToken)) {
40 return new \WP_Error(
41 'turnstile_missing',
42 __('Security check failed. Please refresh the page and try again.', 'fluent-cart')
43 );
44 }
45
46 $isValidToken = $this->validateToken($turnstileToken, $turnstileSettings);
47
48 if (!$isValidToken) {
49 return new \WP_Error(
50 'turnstile_invalid',
51 __('Security check failed. Please try again or refresh the page.', 'fluent-cart')
52 );
53 }
54
55 return $isValid;
56 }
57
58 /**
59 * Validate Cloudflare Turnstile token
60 *
61 * @param string $token
62 * @param array $turnstileSettings
63 * @return bool
64 */
65 public function validateToken($token, $turnstileSettings)
66 {
67 $secretKey = Arr::get($turnstileSettings, 'secret_key', '');
68 if (empty($secretKey)) {
69 return false;
70 }
71
72 $ipAddress = AddressHelper::getIpAddress();
73
74 $response = wp_remote_post('https://challenges.cloudflare.com/turnstile/v0/siteverify', [
75 'body' => [
76 'secret' => $secretKey,
77 'response' => $token,
78 'remoteip' => $ipAddress
79 ],
80 'timeout' => 10
81 ]);
82
83 if (is_wp_error($response)) {
84 return false;
85 }
86
87 $body = wp_remote_retrieve_body($response);
88 $result = json_decode($body, true);
89
90 return isset($result['success']) && $result['success'] === true;
91 }
92 }
93
94