Jake Vanderwerf
2025-09-30 6a627f09d33967ca7fa5b6d939a9347d8223a1e6
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
<?php
namespace JVBase\managers;
 
use JVBase\meta\MetaManager;
use Exception;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
/**
 * Form Manager Class
 * Mainly used for front-end forms.
 * Handles form rendering and processing using MetaManager
 */
class FormManager
{
 
    protected array $forms;
    protected array $fields = [];
    protected string $turnstile_site_key = '';
    protected string $turnstile_secret_key = '';
    protected object $meta;
    protected object $cache;
 
 
    /**
     * Constructor
     */
    public function __construct()
    {
        // Add form processing actions
        add_action('admin_post_nopriv_jvb_forms', [$this, 'processForm']);
        add_action('admin_post_jvb_forms', [$this, 'processForm']);
 
        // Add query var for form submission
        add_filter('query_vars', [$this, 'addQueryVars']);
 
        $this->forms = apply_filters('jvbForms', []);
        if (empty($this->forms)) {
            return;
        }
 
        $this->formContact = apply_filters('jvbFormContact', '');
 
        // Setup Turnstile
        $this->turnstile_site_key = JVB_CLOUDFLARE_SITE_KEY;
        $this->turnstile_secret_key = JVB_CLOUDFLARE_SECRET_KEY;
        $this->meta = new MetaManager(null, 'form');
        $this->cache = new CacheManager('forms', WEEK_IN_SECONDS);
    }
 
    /**
     * Add query vars for form submission
     * @param array $vars
     *
     * @return array
     */
    public function addQueryVars(array $vars):array
    {
        $vars[] = 'jvb_submitted';
        $vars[] = 'jvb_form_error';
        return $vars;
    }
 
 
    /**
     * @param string $type
     *
     * @return false|string
     */
    public function renderForm(string $type):string|false
    {
        if (!array_key_exists($type, $this->forms)) {
            return false;
        }
        $submitted = get_query_var('jvb_submitted', false);
        $error = get_query_var('jvb_form_error', false);
 
        ob_start();
 
        // Handle success state - return only success message
        if ($submitted) {
            $submission_id = sanitize_text_field($submitted);
            $submission_data = $this->cache->get('submission_' . $submission_id);
 
            echo '<div class="form-success">';
            echo '<h2>'.$this->forms[$type]['success_title']??'We got it'.'!</h2>';
            if (!empty($this->forms[$type]['success_message'])) {
                foreach ($this->forms[$type]['success_message'] as $message) {
                    echo '<p>'.$message.'</p>';
                }
            }
 
            if ($submission_data) {
                echo '<div class="submission-summary">';
                echo '<h3>Your submission:</h3>';
                echo '<ul>';
                foreach ($submission_data as $key => $value) {
                    if (!in_array($key, ['action', 'form_id', 'form_type', 'timestamp', '_wpnonce'])) {
                        if (is_array($value)) {
                            $value = implode(', ', $value);
                        }
                        echo '<li><strong>' . esc_html(ucfirst($key)) . ':</strong> ' . esc_html($value) . '</li>';
                    }
                }
                echo '</ul>';
                echo '</div>';
            }
 
            echo ($this->formContact !== '') ? '<p>'.$this->formContact.'</p>' : '';
            echo '</div>';
 
            return ob_get_clean();
        }
 
        // Handle error state - show error message above form
        if ($error) {
            echo '<div class="form-error">';
            echo '<h2>Whoops!</h2>';
            echo '<p>Something went wrong there. Sorry about that.</p>';
            echo ($this->formContact !== '') ? '<p>'.$this->formContact.'</p>' : '';
            echo '</div>';
        }
 
        $id = uniqid($type);
 
        $this->renderFormStart($type, $id);
 
        $this->renderFields($type);
 
        $this->renderTurnstile();
        $this->renderFormEnd($type, $id);
 
        return ob_get_clean();
    }
 
    /**
     * @param string $type
     * @param string $id
     *
     * @return void
     */
    protected function renderFormStart(string $type, string $id):void
    {
        ?>
        <form id="<?= $id ?>" class="jvb-form" action="<?=esc_url(admin_url('admin-post.php'))?>" method="post">
        <?php
        wp_nonce_field('jvb_form_' . $id);
    }
 
 
    /**
     * @return void
     */
    protected function renderFields(string $type): void
    {
        if (!array_key_exists($type, $this->forms)) {
            return;
        }
 
 
        if (empty($this->forms[$type]['fields'])) {
            return;
        }
 
        if (array_key_exists('sections', $this->forms[$type])) {
            $this->renderSections($type);
            return;
        } else {
            foreach ($this->forms[$type]['fields'] as $field_name => $field_config) {
                $this->meta->render('form', $field_name, $field_config, false, false);
            }
        }
 
 
    }
 
    protected function renderSections(string $type):void
    {
        echo '<div class="container">';
        $nav = '<nav class="tabs row start" role="tablist">';
        $i = 1;
        foreach ($this->forms[$type]['sections'] as $slug => $section) {
            $nav .= '<button type="button" class="tab';
 
            $ariaActive = 'false';
            if ($i === 1) {
                $nav .= ' active';
                $ariaActive = 'true';
            }
            $nav .= '" data-tab="'.$slug.'" role="tab" aria-selected="'.$ariaActive.'">
                <h2>'.$section.'</h2></button>';
            $i++;
        }
        echo $nav.'</nav>';
 
        $fields = $this->forms[$type]['fields'];
 
        $i = 0;
        foreach ($this->forms[$type]['sections'] as $slug => $section) {
            $class = ($i == 0) ? ' active' : '';
            ?>
            <section id="<?= $slug ?>" class="tab-content<?=$class?>" data-tab="<?=$slug?>" role="tabpanel">
                <?php if (!empty($section['title'])) : ?>
                    <h2><?= esc_html($section['title']); ?></h2>
                <?php endif; ?>
 
                <?php if (!empty($section['description'])) : ?>
                    <p class="section-description">
                        <?= wp_kses_post($section['description']); ?>
                    </p>
                <?php endif; ?>
 
 
                <?php
                $sectionFields = array_filter($fields, function ($f) use ($slug) {
                    return array_key_exists('section', $f) && $f['section'] == $slug;
                });
                foreach ($sectionFields as $field => $config) : ?>
                    <?php
                        $this->meta->render('form', $field, $config, false, false);
                    ?>
                <?php endforeach; ?>
            </section>
            <?php
            $i++;
        }
    }
 
 
    /**
     * @param string $type
     * @param string $id
     *
     * @return void
     */
    protected function renderFormEnd(string $type, string $id):void
    {
        $submit = $this->forms[$type]['submit']??'Submit';
        // Add hidden fields
        ?>
        <input type="hidden" name="action" value="jvb_forms">
        <input type="hidden" name="form_id" value="<?= $id ?>">
        <input type="hidden" name="form_type" value="<?=$type?>">
        <input type="hidden" name="timestamp" value="<?= time() ?>">
 
        <div class="form-actions">
        <button type="submit"><?= $submit ?></button>
        </div>
        </form>
<?php
    }
 
    /**
     * Render Cloudflare Turnstile
     * @return void
     */
    protected function renderTurnstile(): void
    {
        if (!jvbSiteUsesCloudflare()) {
            return;
        }
 
        $cloudflare = JVB()->connect('cloudflare');
        if ($cloudflare->isSetUp()) {
            $cloudflare->renderTurnstile();
        }
    }
 
    /**
     * Process form submission
     * @return void
     * @throws Exception
     */
    public function processForm():void
    {
        // Verify nonce
        if (!isset($_POST['_wpnonce'])) {
            wp_redirect(home_url());
            exit;
        }
 
        $form_id = sanitize_text_field($_POST['form_id']);
 
        if (!wp_verify_nonce($_POST['_wpnonce'], 'jvb_form_' . $form_id)) {
            wp_redirect(home_url());
            exit;
        }
 
        $type = sanitize_text_field($_POST['form_type']);
        if (!array_key_exists($type, $this->forms)) {
            wp_redirect(home_url());
        }
        error_log('Form Post Data: '.print_r($_POST, true));
 
        // Verify Turnstile
        if (!$this->verifyTurnstile()) {
            $referer = wp_get_referer() ?: home_url($path);
            wp_redirect(add_query_arg('jvb_form_error', urlencode('Please complete the security check.'), $referer));
            exit;
        }
 
        // Check rate limits
        $ip_address = $_SERVER['REMOTE_ADDR'];
        $email = isset($_POST['email']) ? sanitize_email($_POST['email']) : '';
        $rate_check = $this->checkRateLimit($ip_address, $email);
 
        if ($rate_check !== true) {
            $error_message = $rate_check === 'hourly_limit' ?
                'Too many submissions in the last hour' :
                'Too many submissions in the last 24 hours';
            wp_redirect(add_query_arg('jvb_form_error', urlencode($error_message), wp_get_referer()));
            exit;
        }
 
        // Process form data
        $form_data = [];
 
        foreach ($this->forms[$type]??[] as $field_name => $field_config) {
            // Skip fields that weren't submitted (like hidden conditional fields)
            if (!isset($_POST[$field_name])) {
                continue;
            }
 
            $value = $_POST[$field_name];
            if (!$this->meta->validator->validate($value, $field_config)) {
                error_log('Validation unsuccessful');
                throw new Exception("Validation failed for {$field_name}");
            }
 
            $form_data[$field_name] = $this->meta->sanitizer->sanitize($value, $field_config);
        }
 
        // Send email
        $email_sent = $this->sendEmail($type, $form_data);
 
        if (!$email_sent) {
            $referer = wp_get_referer() ?: home_url();
            wp_redirect(add_query_arg('jvb_form_error', urlencode('Failed to send your message. Please try again later.'), $referer));
            exit;
        }
 
        $this->cache->set('submission_' . $form_id, $form_data, HOUR_IN_SECONDS);
 
        // Redirect back to form with success parameter
        $redirect = wp_get_referer() ?: home_url($path);
        wp_redirect(add_query_arg('jvb_submitted', $form_id, $redirect));
        exit;
    }
 
    /**
     * Send email with form data
     */
    /**
     * @param string $type
     * @param array $form_data
     *
     * @return bool
     */
    protected function sendEmail(string $type, array $form_data):bool
    {
        // Set up email data
        $to = get_bloginfo('admin_email');
        $subject = $this->forms[$type]['subject']??'New Form Entry';
 
        // Build email body
        $body = '<h2>Hey team!</h2>';
        $body .= '<p><strong>Date:</strong> ' . date_i18n(get_option('date_format') . ' ' . get_option('time_format')) . '</p>';
        $body .= '<div class="divider">';
 
        foreach ($form_data as $field_name => $value) {
            // Skip internal fields
            if (in_array($field_name, ['action', 'form_id', 'form_type', 'timestamp', '_wpnonce'])) {
                continue;
            }
 
            // Get field label from config
            $label = $this->forms[$type][$field_name]['label'] ?? $field_name;
 
            // Format value for display
            if (is_array($value)) {
                $value = implode(', ', $value);
            }
 
            $body .= '<p><strong>' . esc_html($label) . ':</strong> ' . nl2br(esc_html($value)) . '</p>';
        }
 
        // Add reply-to if email field exists
        if (isset($form_data['email'])) {
            $name = isset($form_data['name']) ? $form_data['name'] : '';
            $headers[] = 'Reply-To: ' . $name . ' <' . $form_data['email'] . '>';
        }
 
        // Send email
        return jvbMail($to, $subject, $body, $headers);
    }
 
    /**
     * Verify Cloudflare Turnstile token
     * @return bool
     */
    protected function verifyTurnstile():bool
    {
        if (empty($_POST['cf-turnstile-response'])) {
            return false;
        }
 
 
        $cloudflare = JVB()->connect('cloudflare');
        if (!$cloudflare->isSetUp()){
            return true;
        }
 
        $token = $_POST['cf-turnstile-response'];
        $ip = $_SERVER['REMOTE_ADDR'];
        return $cloudflare->verifyTurnstile($token, $ip);
    }
 
    /**
     * Check rate limits for form submissions
     * @param string $ip_address
     * @param string $email
     *
     * @return string|true
     */
    protected function checkRateLimit(string $ip_address, string $email):string|bool
    {
        // Check submissions in last hour
        $hour_limit = 3;
        $day_limit = 10;
 
        $submissions = get_transient('jvb_form_submissions_' . md5($ip_address));
        if (!$submissions) {
            $submissions = [
                'hour' => [],
                'day' => [],
                'email' => []
            ];
        }
 
        // Clean old submissions
        $now = time();
        $submissions['hour'] = array_filter($submissions['hour'], function ($time) use ($now) {
            return ($now - $time) < 3600; // Last hour
        });
        $submissions['day'] = array_filter($submissions['day'], function ($time) use ($now) {
            return ($now - $time) < 86400; // Last 24 hours
        });
        $submissions['email'] = array_filter($submissions['email'], function ($data) use ($now) {
            return ($now - $data['time']) < 86400;
        });
 
        // Check limits
        if (count($submissions['hour']) >= $hour_limit) {
            return 'hourly_limit';
        }
        if (count($submissions['day']) >= $day_limit) {
            return 'daily_limit';
        }
 
        // Add new submission
        $submissions['hour'][] = $now;
        $submissions['day'][] = $now;
 
        if (!empty($email)) {
            $submissions['email'][] = [
                'email' => $email,
                'time' => $now
            ];
        }
 
        // Store updated submissions
        set_transient('jvb_form_submissions_' . md5($ip_address), $submissions, DAY_IN_SECONDS);
 
        return true;
    }
}