Jake Vanderwerf
2026-05-01 48721c85ebcfa973ee81719d2467ca80e4253dc9
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
<?php
namespace JVBase\meta;
 
use DateTime;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
/**
 * Handles meta value validation
 */
class Validator
{
    protected array $errors = [];
 
    public static function validate(mixed $value, array $config): bool|array
    {
        $errors = [];
        $type = $config['type'];
        $name = $config['name'] ?? 'field';
 
        // Required field check
        if (!empty($config['required']) && static::isEmpty($value)) {
            return [$name => __('This field is required', 'jvb')];
        }
 
        // Skip validation for empty optional fields
        if (static::isEmpty($value)) {
            return true;
        }
 
        // Type-specific validation
        $method = 'validate' . str_replace('_', '', ucwords($type, '_'));
        if (method_exists(static::class, $method)) {
            $result = static::$method($value, $config);
            if ($result !== true) {
                return is_array($result) ? $result : [$name => $result];
            }
        }
 
        return true;
    }
 
    protected static function isEmpty(mixed $value): bool
    {
        if ($value === null || $value === '' || $value === []) {
            return true;
        }
        if (is_array($value) && empty(array_filter($value))) {
            return true;
        }
        return false;
    }
 
    protected static function validateText(string $value, array $config): bool|string
    {
        if (isset($config['limit']) && strlen($value) > $config['limit']) {
            return sprintf(__('Must not exceed %d characters', 'jvb'), $config['limit']);
        }
 
        if (isset($config['pattern']) && !preg_match($config['pattern'], $value)) {
            return __('Invalid format', 'jvb');
        }
 
        return true;
    }
 
    protected static function validateTextarea(string $value, array $config): bool|string
    {
        return static::validateText($value, $config);
    }
 
    protected static function validateNumber(mixed $value, array $config): bool|string
    {
        if (!is_numeric($value)) {
            return __('Must be a number', 'jvb');
        }
 
        if (isset($config['min']) && $value < $config['min']) {
            return sprintf(__('Must be at least %s', 'jvb'), $config['min']);
        }
 
        if (isset($config['max']) && $value > $config['max']) {
            return sprintf(__('Must not exceed %s', 'jvb'), $config['max']);
        }
 
        return true;
    }
 
    protected static function validateEmail(string $value, array $config): bool|string
    {
        if (!is_email($value)) {
            return __('Invalid email address', 'jvb');
        }
        return true;
    }
 
    protected static function validateUrl(string $value, array $config): bool|string
    {
        if (filter_var($value, FILTER_VALIDATE_URL) === false) {
            return __('Must be a valid URL', 'jvb');
        }
        return true;
    }
 
    protected static function validateDate(string $value, array $config): bool|string
    {
        $timestamp = strtotime($value);
        if ($timestamp === false) {
            return __('Invalid date format', 'jvb');
        }
 
        if (isset($config['min_date']) && $timestamp < strtotime($config['min_date'])) {
            return sprintf(__('Date must be after %s', 'jvb'), $config['min_date']);
        }
 
        if (isset($config['max_date']) && $timestamp > strtotime($config['max_date'])) {
            return sprintf(__('Date must be before %s', 'jvb'), $config['max_date']);
        }
 
        return true;
    }
 
 
    protected static function validateDatetime(string $value, array $config): bool|string
    {
        $date = DateTime::createFromFormat('Y-m-d H:i:s', $value);
        if (!$date) {
            $formats = ['Y-m-d\TH:i:s', 'Y-m-d\TH:i', 'Y-m-d H:i'];
            foreach ($formats as $format) {
                $date = DateTime::createFromFormat($format, $value);
                if ($date) break;
            }
        }
 
        if (!$date) {
            return __('Invalid datetime format', 'jvb');
        }
 
        $timestamp = $date->getTimestamp();
 
        if (isset($config['min_datetime']) && $timestamp < strtotime($config['min_datetime'])) {
            return sprintf(__('DateTime must be after %s', 'jvb'), $config['min_datetime']);
        }
 
        if (isset($config['max_datetime']) && $timestamp > strtotime($config['max_datetime'])) {
            return sprintf(__('DateTime must be before %s', 'jvb'), $config['max_datetime']);
        }
 
        return true;
    }
 
    protected static function validateTime(string $value, array $config): bool|string
    {
        if (!preg_match('/^([01]?[0-9]|2[0-3]):[0-5][0-9]$/', $value)) {
            return __('Time must be in HH:MM format', 'jvb');
        }
 
        if (isset($config['min_time']) && strtotime($value) < strtotime($config['min_time'])) {
            return sprintf(__('Time must be after %s', 'jvb'), $config['min_time']);
        }
 
        if (isset($config['max_time']) && strtotime($value) > strtotime($config['max_time'])) {
            return sprintf(__('Time must be before %s', 'jvb'), $config['max_time']);
        }
 
        return true;
    }
 
    protected static function validateSelect(string $value, array $config): bool|string
    {
        if (!isset($config['options']) || !array_key_exists($value, $config['options'])) {
            return __('Invalid selection', 'jvb');
        }
        return true;
    }
 
    protected static function validateRadio(string $value, array $config): bool|string
    {
        return static::validateSelect($value, $config);
    }
 
    protected static function validateSet(array|string $value, array $config): bool|string
    {
        if (!is_array($value)) {
            $value = explode(',', $value);
        }
 
        if (!isset($config['options'])) {
            return true;
        }
 
        $invalid = array_diff($value, array_keys($config['options']));
        if (!empty($invalid)) {
            return __('Invalid selections', 'jvb');
        }
 
        return true;
    }
 
    protected static function validateCheckbox(array|string $value, array $config): bool|string
    {
        return static::validateSet($value, $config);
    }
 
    protected static function validateImage(int $value, array $config): bool|string
    {
        if (!wp_attachment_is_image($value)) {
            return __('Invalid image', 'jvb');
        }
        return true;
    }
 
    protected static function validateGallery(array|string $value, array $config): bool|string
    {
        $ids = is_array($value) ? $value : explode(',', $value);
 
        if (!empty($config['max_images']) && count($ids) > $config['max_images']) {
            return sprintf(__('Maximum of %d images allowed', 'jvb'), $config['max_images']);
        }
 
        foreach ($ids as $id) {
            if (absint($id) <= 0 || !wp_attachment_is_image(absint($id))) {
                return __('One or more invalid images', 'jvb');
            }
        }
 
        return true;
    }
 
    protected static function validateTaxonomy(array|string $value, array $config): bool|string
    {
        if (!is_array($value)) {
            $value = explode(',', $value);
        }
 
        $taxonomy = (str_starts_with($config['taxonomy'], BASE))
            ? $config['taxonomy']
            : BASE . $config['taxonomy'];
 
        foreach ($value as $term_id) {
            if (!term_exists((int)$term_id, $taxonomy)) {
                return __('Invalid term selected', 'jvb');
            }
        }
 
        return true;
    }
 
    protected static function validateUser(array|string $value, array $config): bool|string
    {
        if (!is_array($value)) {
            $value = explode(',', $value);
        }
 
        foreach ($value as $user_id) {
            if (!get_userdata((int)$user_id)) {
                return __('Invalid user selected', 'jvb');
            }
        }
 
        return true;
    }
 
    protected static function validateLocation(array $value, array $config): bool|string
    {
        if (!is_array($value)) {
            return __('Location must be an array', 'jvb');
        }
 
        $required_fields = ['lat', 'lng'];
        foreach ($required_fields as $field) {
            if (!isset($value[$field]) || !is_numeric($value[$field])) {
                return __('Invalid location coordinates', 'jvb');
            }
        }
 
        return true;
    }
 
    protected static function validateRepeater(array $value, array $config): bool|string
    {
        if (!is_array($value)) {
            return __('Invalid repeater data', 'jvb');
        }
 
        if (isset($config['min_rows']) && count($value) < $config['min_rows']) {
            return sprintf(__('Minimum of %d rows required', 'jvb'), $config['min_rows']);
        }
 
        if (isset($config['max_rows']) && count($value) > $config['max_rows']) {
            return sprintf(__('Maximum of %d rows allowed', 'jvb'), $config['max_rows']);
        }
 
        foreach ($value as $row) {
            foreach ($config['fields'] as $field_name => $field_config) {
                if (!isset($row[$field_name])) {
                    continue;
                }
                $field_config['name'] = $field_name;
                $result = static::validate($row[$field_name], $field_config);
                if ($result !== true) {
                    return is_array($result) ? array_values($result)[0] : $result;
                }
            }
        }
 
        return true;
    }
 
    protected static function validateGroup(array $value, array $config): bool|string
    {
        if (!is_array($value)) {
            return __('Invalid group data', 'jvb');
        }
 
        if (!empty($config['fields']) && is_array($config['fields'])) {
            foreach ($config['fields'] as $field_name => $field_config) {
                if (isset($value[$field_name])) {
                    $field_config['name'] = $field_name;
                    $result = static::validate($value[$field_name], $field_config);
                    if ($result !== true) {
                        return is_array($result) ? array_values($result)[0] : $result;
                    }
                }
            }
        }
 
        return true;
    }
 
    protected static function validateTagList(array $value, array $config): bool|string
    {
        if (!is_array($value)) {
            return __('Invalid data format', 'jvb');
        }
 
        if (isset($config['min_items']) && count($value) < $config['min_items']) {
            return sprintf(__('Minimum of %d items required', 'jvb'), $config['min_items']);
        }
 
        if (isset($config['max_items']) && count($value) > $config['max_items']) {
            return sprintf(__('Maximum of %d items allowed', 'jvb'), $config['max_items']);
        }
 
        if (!isset($config['fields']) || !is_array($config['fields'])) {
            return true;
        }
 
        foreach ($value as $row) {
            if (!is_array($row)) {
                continue;
            }
 
            foreach ($config['fields'] as $field_name => $field_config) {
                if (!isset($row[$field_name])) {
                    continue;
                }
 
                $field_config['name'] = $field_name;
                $result = static::validate($row[$field_name], $field_config);
                if ($result !== true) {
                    return is_array($result) ? array_values($result)[0] : $result;
                }
            }
        }
 
        return true;
    }
 
    protected static function validateTrueFalse(mixed $value, array $config): bool
    {
        return true; // Boolean values are always valid after sanitization
    }
}