Jake Vanderwerf
2025-09-30 2cb91676044ecd0abd9c45b4835abb8b0d042312
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
<?php
namespace JVBase\rest\routes;
 
use JVBase\rest\RestRouteManager;
use JVBase\managers\CacheManager;
use JVBase\meta\MetaManager;
use JVBase\managers\CloudflareTurnstile;
use JVBase\blocks\FormBlock;
use WP_REST_Request;
use WP_REST_Response;
use WP_Error;
use Exception;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
/**
 * Form Routes Class
 *
 * Handles REST API endpoints for form submissions
 */
class FormRoutes extends RestRouteManager
{
    protected CacheManager $cache;
    protected FormBlock $form_block;
    protected CloudflareTurnstile|null $turnstile;
 
    public function __construct()
    {
        parent::__construct();
        $this->action = 'form-';
        $this->cache = new CacheManager('form_submissions', HOUR_IN_SECONDS);
 
        // Initialize Cloudflare Turnstile if available
        $this->turnstile = class_exists('JVBase\managers\CloudflareTurnstile') && jvbSiteUsesCloudflare()
            ? new CloudflareTurnstile()
            : null;
 
        // Add query vars
        add_filter('query_vars', [$this, 'addQueryVars']);
    }
 
    /**
     * Register REST routes
     */
    public function registerRoutes(): void
    {
        // Form submission endpoint
        register_rest_route($this->namespace, '/forms', [
            [
                'methods' => 'POST',
                'callback' => [$this, 'submitForm'],
                'permission_callback' => '__return_true', // Public endpoint
                [
                    'methods' => 'GET',
                    'callback' => [$this, 'getForms'],
                    'permission_callback' => [$this, 'checkPermission']
                ]
            ]
        ]);
 
        // Get specific form configuration
        register_rest_route($this->namespace, '/forms/(?P<form_type>[a-zA-Z0-9_-]+)', [
            [
                'methods' => 'GET',
                'callback' => [$this, 'getForm'],
                'permission_callback' => [$this, 'checkPermission'],
                'args' => [
                    'form_type' => [
                        'required' => true,
                        'type' => 'string',
                        'sanitize_callback' => 'sanitize_text_field'
                    ]
                ]
            ]
        ]);
    }
 
    /**
     * Add query vars for form handling
     */
    public function addQueryVars(array $vars): array
    {
        $vars[] = 'jvb_submitted';
        $vars[] = 'jvb_form_error';
        return $vars;
    }
    /**
     * Handle form submission via REST API
     */
    public function submitForm(WP_REST_Request $request): WP_REST_Response
    {
        try {
            $form_type = $request->get_param('form_type');
            $form_id = $request->get_param('form_id');
            $form_data = $request->get_params();
 
            error_log('Form submission: '.print_r($request->get_params(), true));
            // Process the submission
            $result = $this->handleFormSubmission($form_type, $form_id, $form_data);
 
            if (is_wp_error($result)) {
                return new WP_REST_Response([
                    'success'   => false,
                    'message'   => $result->get_error_message()
                ]);
            }
            if (array_key_exists('success', $result)){
                return new WP_REST_Response($result);
            }
 
            return new WP_REST_Response([
                'success' => true,
                'data'  => $result
            ], 200);
 
        } catch (Exception $e) {
            return new WP_REST_Response([
                'success' => false,
                'message' => 'An error occurred while processing your submission.'
            ], 500);
        }
    }
 
    /**
     * Handle the actual form submission logic
     */
    protected function handleFormSubmission(string $form_type, string $form_id, array $form_data): array|WP_Error
    {
        // Get form configuration
        $form_config = FormBlock::getForm($form_type);
 
        error_log('Config: '.print_r($form_config, true));
 
 
        // Verify Turnstile if enabled
        //TODO: Reenable
//      if (jvbSiteUsesCloudflare() && $this->turnstile) {
//          error_log('Verifying turnstile...');
//          $turnstile_token = $form_data['cf-turnstile-response'] ?? '';
//          if (!$this->turnstile->verifyTurnstile($turnstile_token)) {
//              return new WP_Error('turnstile_failed', 'Security verification failed. Please try again.');
//          }
//      }
 
        // Validate and sanitize form data
        $processed_data = $this->validateAndSanitizeData($form_config, $form_data);
        error_log('Processed data: '.print_r($processed_data, true));
        if (array_key_exists('success', $processed_data) && $processed_data['success'] === false) {
            return $processed_data;
        }
 
        // Send email notification
        $email_sent = $this->sendEmailNotification($form_type, $form_config, $processed_data);
 
        if (!$email_sent) {
            return new WP_Error('email_failed', 'Failed to send your message. Please try again later.');
        }
 
        // Store submission data temporarily for success display
        $this->cache->set('submission_' . $form_id, $processed_data, HOUR_IN_SECONDS);
 
        // Log successful submission
        $this->recordSubmission($_SERVER['REMOTE_ADDR'], $processed_data['email'] ?? '');
 
        // Allow other plugins to hook into successful submission
        do_action('jvb_form_submitted', $form_type, $processed_data, $form_id);
 
        return $processed_data;
    }
 
    /**
     * Validate and sanitize form data
     */
    protected function validateAndSanitizeData(array $form_config, array $form_data): array|WP_REST_Response
    {
        $meta = new MetaManager(null, 'form');
        $processed_data = [];
        $errors = [];
 
        // Normalize form data to handle HTML checkbox arrays (field[] -> field)
        $normalized_form_data = [];
        foreach ($form_data as $key => $value) {
            // Remove [] suffix from checkbox field names
            $normalized_key = str_ends_with($key, '[]') ? substr($key, 0, -2) : $key;
            $normalized_form_data[$normalized_key] = $value;
        }
 
 
 
        foreach ($form_config['fields'] as $field_name => $field_config) {
            // Skip system fields
            if (in_array($field_name, ['action', 'form_id', 'form_type', 'timestamp', '_wpnonce', '_wp_http_referer', 'cf-turnstile-response'])) {
                continue;
            }
 
            $value = $normalized_form_data[$field_name] ?? '';
 
            if (in_array($field_config['type'], ['checkbox', 'set'])) {
                $value = jvbCommaList((is_array($value)) ? $value : explode(',',$value));
            }
 
            // Check required fields
            if (!empty($field_config['required']) && empty($value)) {
                $errors['required'][] = $field_name;
                continue;
            }
 
            // Skip empty optional fields
            if (empty($value) && empty($field_config['required'])) {
                continue;
            }
 
            // Add field name to config for error messages
            $field_config['name'] = $field_name;
 
            // Validate field
            if (!$meta->validator->validate($value, $field_config)) {
                $label = $field_config['label'] ?? ucfirst(str_replace('_', ' ', $field_name));
                $errors['errors'][$field_name] = [
                    'message' => sprintf('Field "%s" contains invalid data.', $label)
                ];
                continue;
            }
 
            // Sanitize field
            $processed_data[$field_name] = $meta->sanitizer->sanitize($value, $field_config);
        }
 
        if (!empty($errors)) {
            $errors['success'] = false;
            return $errors;
        }
 
        return $processed_data;
    }
 
    /**
     * Send email notification
     */
    protected function sendEmailNotification(string $form_type, array $form_config, array $form_data): bool
    {
        $admin_email = apply_filters('jvb_form_email_to', $form_config['email_to'], $form_type, $form_data);
        $subject = apply_filters('jvb_form_email_subject', $form_config['email_subject'], $form_type, $form_data);
 
        // Get submitter details
        $submitter_email = $form_data['email'] ?? null;
        $submitter_name = $form_data['name'] ?? '';
 
        // Generate unique message ID for threading
        $form_id = $form_data['form_id'] ?? uniqid();
        $timestamp = current_time('timestamp');
        $message_id = sprintf('<%s-%s-%s@%s>',
            $form_type,
            $form_id,
            $timestamp,
            parse_url(home_url(), PHP_URL_HOST)
        );
 
        // Build unified email body
        $body = '<div style="font-family: -apple-system, BlinkMacSystemFont, \'Segoe UI\', Roboto, sans-serif; line-height: 1.6; color: #333;">';
        $body .= '<h2 style="color: #2563eb; border-bottom: 2px solid #e5e7eb; padding-bottom: 0.5rem;">New Form Submission: ' . esc_html($form_config['title']) . '</h2>';
        $body .= '<p style="color: #6b7280;"><strong>Submitted:</strong> ' . current_time('F j, Y \a\t g:i A') . '</p>';
 
        // Add note about email thread if submitter email exists
        if ($submitter_email) {
            $body .= '<div style="background: #f0f9ff; border-left: 4px solid #3b82f6; padding: 1rem; margin: 1rem 0; border-radius: 0 8px 8px 0;">';
            $body .= '<p style="margin: 0; color: #1e40af;"><strong>💬 Email Thread Created</strong></p>';
            $body .= '<p style="margin: 0.5rem 0 0 0; color: #1e40af; font-size: 0.9em;">This email includes both the admin and the person who submitted the form. Any replies will be shared between all parties, creating a conversation thread.</p>';
            $body .= '</div>';
        }
 
        $body .= '<div style="margin: 2rem 0;">';
 
        // Add submission details in a nice format
        foreach ($form_config['fields'] as $field_name => $config) {
            $label = $config['summaryTitle'] ?? $config['label'];
            //Submitted Value
            $value = $form_data[$field_name];
            if (str_contains($label, '%s')) {
                switch ($config['type']) {
                    case 'true_false':
                        $replace = ($value === '1') ? $config['isTrue'] : $config['isFalse'];
                        break;
                }
                $label = sprintf(
                    $label,
                    $replace
                );
            }
 
            $body .= '<div style="margin-bottom: 1rem; padding: 0.75rem; background: #f9fafb; border-radius: 6px;">';
            $body .= '<strong style="color: #374151; display: block; margin-bottom: 0.25rem;">' . esc_html($label) . ':</strong>';
            $body .= '<span style="color: #6b7280;">' . nl2br(esc_html($value)) . '</span>';
            $body .= '</div>';
        }
 
        $body .= '</div>';
 
        // Add footer
        $body .= '<div style="border-top: 1px solid #e5e7eb; padding-top: 1rem; margin-top: 2rem;">';
        $body .= '<p style="color: #9ca3af; font-size: 0.8em; margin: 0;"><em>';
        $body .= 'This message was sent through the ' . esc_html($form_config['title']) . ' on ' . get_bloginfo('name');
 
        if ($submitter_email) {
            $body .= '<br>To continue this conversation, simply reply to this email.';
        }
 
        $body .= '</em></p>';
        $body .= '</div>';
        $body .= '</div>';
 
        // Prepare email headers for unified thread with enhanced threading
        $headers = [
            'Content-Type: text/html; charset=UTF-8',
        ];
 
        // Set up recipients and reply-to based on whether submitter email exists
        if ($submitter_email) {
            // Primary recipient: admin
            $to = $admin_email;
 
            // Add submitter as CC so they both see the email
            $cc_name = $submitter_name ? $submitter_name : 'Form Submitter';
            $headers[] = 'CC: ' . $cc_name . ' <' . $submitter_email . '>';
 
            // Set reply-to to include both emails for unified thread
            $site_name = get_bloginfo('name');
            $admin_name = get_bloginfo('name') . ' Team';
 
            $headers[] = 'Reply-To: ' . $admin_name . ' <' . $admin_email . '>, ' . $cc_name . ' <' . $submitter_email . '>';
 
            // Set from address to be more professional
            $headers[] = 'From: ' . $site_name . ' Forms <' . $admin_email . '>';
 
        } else {
            // No submitter email, just send to admin
            $to = $admin_email;
            $site_name = get_bloginfo('name');
            $headers[] = 'From: ' . $site_name . ' Forms <' . $admin_email . '>';
            $headers[] = 'Reply-To: ' . $admin_email;
        }
 
        // Allow filtering of email data
        $email_data = apply_filters('jvb_form_unified_email_data', [
            'to' => $to,
            'subject' => $subject,
            'body' => $body,
            'headers' => $headers,
            'submitter_included' => !empty($submitter_email),
            'submitter_email' => $submitter_email,
            'message_id' => $message_id
        ], $form_type, $form_data);
 
        // Send the unified email
        $email_sent = jvbMail($email_data['to'], $email_data['subject'], $email_data['body'], implode(';',$email_data['headers']));
 
        // Log the email sending for debugging
        if ($email_sent) {
            error_log("Form email sent successfully. Recipients: {$to}" .
                ($submitter_email ? " (CC: {$submitter_email})" : "") .
                " | Message-ID: {$message_id}");
        } else {
            error_log("Failed to send form email to: {$to}" .
                ($submitter_email ? " (CC: {$submitter_email})" : ""));
        }
 
        return $email_sent;
    }
 
 
    /**
     * Record submission for rate limiting
     */
    protected function recordSubmission(string $ip_address, string $email): void
    {
        // Record IP-based submissions
        $hourly_key = 'submissions_hour_' . md5($ip_address);
        $daily_key = 'submissions_day_' . md5($ip_address);
 
        $hourly_count = (int) get_transient($hourly_key) + 1;
        $daily_count = (int) get_transient($daily_key) + 1;
 
        set_transient($hourly_key, $hourly_count, HOUR_IN_SECONDS);
        set_transient($daily_key, $daily_count, DAY_IN_SECONDS);
 
        // Record email-based submissions if provided
        if (!empty($email)) {
            $email_key = 'submissions_email_' . md5($email);
            $email_count = (int) get_transient($email_key) + 1;
            set_transient($email_key, $email_count, DAY_IN_SECONDS);
        }
    }
 
    /**
     * Get all available forms
     */
    public function getForms(WP_REST_Request $request): WP_REST_Response
    {
        $forms = FormBlock::getForms();
 
        // Remove sensitive data
        $public_forms = [];
        foreach ($forms as $key => $config) {
            $public_forms[$key] = [
                'title' => $config['title'],
                'description' => $config['description'],
                'fields' => array_map(function($field) {
                    // Remove sensitive configuration
                    unset($field['email_to']);
                    return $field;
                }, $config['fields'])
            ];
        }
 
        return new WP_REST_Response($public_forms, 200);
    }
 
    /**
     * Get specific form configuration
     */
    public function getForm(WP_REST_Request $request): WP_REST_Response
    {
        $form_type = $request->get_param('form_type');
        $form_config = FormBlock::getForm($form_type);
 
        if (!$form_config) {
            return new WP_REST_Response([
                'error' => 'Form not found'
            ], 404);
        }
 
        // Remove sensitive data
        unset($form_config['email_to']);
 
        return new WP_REST_Response($form_config, 200);
    }
}