Jake Vanderwerf
2026-09-16 a239ef33ec489ce6d0f25ac2f256df0ac54a7e50
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
<?php
 
namespace JVBase\integrations\services;
use JVBase\integrations\Admin;
use JVBase\Instance;
use JVBase\integrations\Integrations;
 
if (!defined('ABSPATH')) {
    exit;
}
 
class Cloudflare extends Integrations
{
    use Admin, Instance;
    private string $theme = 'light';
    private string $size = 'normal';
 
    protected function __construct()
    {
        $this->service_name = 'cloudflare';
        $this->title = 'Cloudflare';
        $this->icon = 'cloud';
 
        $this->defineFields();
        $this->defineInstructions();
        parent::__construct();
    }
 
    protected function defineFields():void
    {
        $site_key = $this->addField('site_key', 'Site Key', 'text', 'manage_options');
            $site_key->setRequired();
            $site_key->setHint('Public key for Turnstile Widget');
        $secret_key = $this->addField('secret_key', 'Secret Key', 'text', 'manage_options');
            $secret_key->setRequired();
            $secret_key->setSubType('password');
            $secret_key->setHint('Secret key for server-side verification of Turnstile Widget');
    }
 
    protected function defineInstructions():void
    {
        $this->instructions = [
            'Go to <a href="https://dash.cloudflare.com/?to=/:account/turnstile" target="_blank">Cloudflare Dashboard</a>',
            'Create a new Turnstile widget',
            'Add your domain to the widget settings',
            'Copy the Site Key and Secret Key'
        ];
    }
    protected function initialize(): void
    {
        // TODO: Implement initialize() method.
        add_action('woocommerce_review_order_before_submit', [$this, 'addToCheckout']);
        add_action('wp_enqueue_scripts', [$this, 'enqueueOnCheckout']);
        add_action('woocommerce_checkout_process', [$this, 'checkoutTurnstile']);
    }
 
    public function getServiceDescription():string
    {
        return "Add Turnstile captcha protection to your forms and login pages to prevent spam and bot submissions.";
    }
 
    public function performConnectionTest(): bool
    {
        $credentials = $this->loadCredentials();
        if (!array_key_exists('site_key', $credentials) || !array_key_exists('secret_key', $credentials)) {
            return false;
        }
 
        // Test with a dummy token to verify API connectivity
        $response = wp_remote_post('https://challenges.cloudflare.com/turnstile/v0/siteverify', [
            'body' => [
                'secret' => $credentials['secret_key'],
                'response' => 'test-token-connection-check'
            ],
            'timeout' => 10
        ]);
 
        // We expect this to fail with success:false, but not with a connection error
        if (is_wp_error($response)) {
            return false;
        }
 
        $result = json_decode(wp_remote_retrieve_body($response), true);
        // If we get a response with success field (even if false), connection works
        return array_key_exists('success', $result);
    }
 
    /**
     * Enqueue Turnstile script
     */
    public function enqueueTurnstileScripts(): void
    {
        wp_enqueue_script(
            'cloudflare-turnstile',
            'https://challenges.cloudflare.com/turnstile/v0/api.js',
            [],
            null,
            true
        );
        wp_script_add_data('cloudflare-turnstile', 'async', true);
        wp_script_add_data('cloudflare-turnstile', 'defer', true);
    }
 
    /**
     * Render Turnstile widget
     * Can be called directly: jvbConnect('cloudflare')->renderTurnstile()
     * Or Cloudflare::getInstance()->renderTurnstile()
     */
    public function renderTurnstile(array|string $options = []): void
    {
        if (!$this->isSetUp()) {
            return;
        }
 
        if (!is_array($options)) {
            $options = [];
        }
 
        $defaults = [
            'theme' => $this->theme,
            'size' => $this->size,
            'wrapper_class' => 'cf-turnstile-wrapper',
            'wrapper_style' => 'margin: 1em 0;'
        ];
 
        $cred = $this->loadCredentials();
        $options = wp_parse_args($options, $defaults);
 
        echo sprintf(
            '<div class="%s" style="%s">
                <div class="cf-turnstile" data-sitekey="%s" data-theme="%s" data-size="%s"></div>
            </div>',
            esc_attr($options['wrapper_class']),
            esc_attr($options['wrapper_style']),
            $cred['site_key'],
            $options['theme'],
            $options['size']
        );
    }
 
    /**
     * Verify the Turnstile response
     * Can be called directly via Cloudflare::getInstance()->verifyTurnstile($token)
     * @param string|null $token
     * @param string $remote_ip
     * @return bool
     */
    public function verifyTurnstile(?string $token = null, string $remote_ip = ''):bool
    {
        if (!$this->isSetUp()) {
            return true;
        }
 
        // If no token provided, try to get it from $_POST
        if (!$token && isset($_POST['cf-turnstile-response'])) {
            $token = sanitize_text_field($_POST['cf-turnstile-response']);
        }
 
        if (empty($token)) {
            return false;
        }
 
        $cred = $this->loadCredentials();
        $data = [
            'secret'    => $cred['secret_key'],
            'response'  => $token,
        ];
 
        if (empty($remote_ip) && isset($_SERVER['REMOTE_ADDR'])) {
            $remote_ip = sanitize_text_field($_SERVER['REMOTE_ADDR']);
        }
 
        if (!empty($remote_ip)) {
            $data['remoteip'] = $remote_ip;
        }
        $response = wp_remote_post('https://challenges.cloudflare.com/turnstile/v0/siteverify', [
            'body' => $data,
            'timeout' => 10
        ]);
 
        if (is_wp_error($response)) {
            $this->logError('verifyTurnstile', 'Turnstile verification failed: ' . $response->get_error_message());
            return false;
        }
 
        $result = json_decode(wp_remote_retrieve_body($response), true);
 
        if (!$result['success']??false) {
            $error_codes = implode(', ', $result['error-codes'] ?? ['Unknown error']);
            $this->logError('verifyTurnstile', 'Turnstile verification failed: ' . $error_codes);
        }
        return $result['success'] ?? false;
    }
 
    public function enqueueOnCheckout():void
    {
        if (function_exists('is_checkout') && is_checkout()) {
            self::enqueueTurnstileScripts();
        }
    }
    public function addToCheckout():void
    {
        self::renderTurnstile();
    }
    public function checkoutTurnstile():void
    {
        $verified = $this->verifyTurnstile();
        if (!$verified && function_exists('wc_add_notice')) {
            wc_add_notice('Security check failed. Please try again', 'error');
            return;
        }
    }
}