Jake Vanderwerf
2025-11-04 42fa8304ddb811b0f725f245130f70c0f5e86a6c
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
<?php
namespace JVBase\rest;
 
use JVBase\JVB;
use JVBase\rest\RateLimiter;
use JVBase\managers\OperationQueue;
use JVBase\managers\CacheManager;
use JVBase\managers\NotificationManager;
use WP_REST_Request;
use WP_Error;
use Exception;
use WP_REST_Response;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
/**
 * Handles route registration and high-level coordination
 */
abstract class RestRouteManager
{
    protected string $namespace = 'jvb/v1';
 
    protected RateLimiter|null $rate_limiter;
    protected array $types;
    protected string $route;
    protected string $base;
    protected string $content_type; //the registered post type
    protected string $type; //post, user, term, for MetaManager
    protected string $action = ''; //optional additional nonce to check
    protected array $callback; //route->callback array
    protected string $operation_type; // from QueueManager.js and OperationQueue.php
    protected OperationQueue $queue;
    protected CacheManager $cache;
    protected NotificationManager $notifications;
    protected string $cache_name ='';
    protected int $cache_ttl = 3600; //1 hour default
 
 
    // Error code constants for consistency
    const ERROR_MISSING_PARAMS = 'missing_parameters';
    const ERROR_ACCESS_DENIED = 'access_denied';
    const ERROR_NOT_FOUND = 'not_found';
    const ERROR_INVALID_OPERATION = 'invalid_operation';
    const ERROR_PROCESSING = 'processing_error';
 
    public function __construct()
    {
        $this->base = BASE;
        $this->rate_limiter = new RateLimiter();
        if ($this->cache_name !== '') {
            $this->cache = CacheManager::for($this->cache_name, $this->cache_ttl);
        }
        add_action('rest_api_init', [$this, 'registerRoutes']);
    }
 
    public function init()
    {
        //Replace in child classes, if necessary
    }
 
 
    /**
     * @param WP_REST_Request $request Rest Request object
     * @param string $nonceName nonce name to check
     * @param string|null $nonce optional additional nonce to check
     *
     * @return void|WP_Error
     */
    public function verifyNonce(WP_REST_Request $request, string $nonceName = 'wp_rest', string|null $nonce = null):mixed
    {
        $nonce = (!is_null($nonce)) ? $nonce :  $request->get_header('X-WP-Nonce');
 
        if (!wp_verify_nonce($nonce, $nonceName)) {
            return new WP_Error(
                'invalid_nonce',
                'Invalid nonce',
                ['status' => 403]
            );
        }
        return true;
    }
 
    abstract public function registerRoutes();
 
 
    /**
     * @param WP_REST_Request $request the Request Object
     *
     * @return bool|WP_Error Whether or not we can proceed
     */
    public function checkPermission(WP_REST_Request $request):bool
    {
        // Check rate limits first
        if (!$this->rate_limiter->checkLimit($request)) {
            error_log('Rate Limit Reached');
            return new WP_Error(
                'rate_limit_reached',
                'Rate limit reached',
                ['status' => 403]
            );
        }
        $user_id = $request->get_param('user');
        if (!empty($user_id) && !$this->userCheck($user_id)) {
            error_log('Usercheck failed');
            return false;
        }
        // Verify nonces
        $this->verifyNonce($request, 'wp_rest');
        if ($this->action!=='') {
            $this->verifyNonce($request, $this->action . $user_id, $request->get_header('action_nonce'));
        }
        return true;
    }
 
    protected function checkContent(string $content, bool $bool = false):string|bool
    {
        $result = JVB_CONTENT[$content]??JVB_TAXONOMY[$content]??JVB_USER[$content]??'';
        if ($bool) {
            return $result !== '';
        }
        return $result;
    }
 
 
    public function formatError($code, $message, $status = 400)
    {
        return new WP_Error($code, $message, ['status' => $status]);
    }
 
    /**
     * @param int $userID the userID to check
     *
     * @return bool whether or not the $userID matches the currently logged in user
     */
    protected function userCheck(int $userID)
    {
        return $userID === get_current_user_id();
    }
 
    /**
     * Makes sure the value is in the format it needs to be
     * @param mixed $value
     *
     * @return mixed
     */
    protected function getMetaValues(mixed $value):mixed
    {
        //get the repeater out of there
        $temp = json_decode($value);
        if (is_array($temp)) {
            $value = [];
            foreach ($temp as $t) {
                if (is_object($t)) {
                    $t = (array)$t;
                }
                $value[] = $t;
            }
        }
        return $value;
    }
    /**
     * Log errors with proper context
     * @param string $message The error message
     * @param array $context Additional context
     * @param string $severity
     * @return void
     */
    protected function logError(string $message, array $context = [], string $severity = 'error')
    {
        try {
            JVB()->error()->log(
                'artist_invitations', // component
                $message,
                $context,
                $severity
            );
        } catch (Exception $e) {
            // Fallback if error handler fails
            error_log("Invitations Error: {$message} - " . json_encode($context));
        }
    }
 
    /**
     * Check if user exists (cached)
     */
    protected function checkUser(int $userID): bool
    {
        $cache = CacheManager::for('users');
 
        return $cache->remember("user_exists_{$userID}", function() use ($userID) {
            return (bool)get_userdata($userID);
        }, DAY_IN_SECONDS);
    }
 
    /**
     * Check if shop exists (cached)
     */
    protected function checkShop(int $shopID): bool
    {
        $cache = CacheManager::for('shop');
 
        return $cache->remember("shop_exists_{$shopID}", function() use ($shopID) {
            return (bool)term_exists($shopID, BASE . 'shop');
        }, DAY_IN_SECONDS);
    }
 
    /**
     * Check if term exists (cached)
     */
    protected function checkTerm(array $args): bool
    {
        $termID = $args['to_term'] ?? $args['term_id'] ?? false;
        if (!$termID) {
            return false;
        }
 
        $taxonomy = $args['taxonomy'] ?? false;
        if (!$taxonomy) {
            return false;
        }
 
        $taxonomy = jvbCheckBase($taxonomy);
        $cache = CacheManager::for($taxonomy);
 
        return $cache->remember("term_exists_{$termID}", function() use ($termID, $taxonomy) {
            return (bool)term_exists($termID, $taxonomy);
        }, DAY_IN_SECONDS);
    }
 
    /**
     * Check if an artist is verified
     */
    public function isVerifiedUser(int $user_id): bool
    {
        $cache = CacheManager::forUser($user_id);
 
        return $cache->remember('is_verified', function() use ($user_id) {
            return user_can($user_id, 'skip_moderation');
        }, DAY_IN_SECONDS);
    }
 
    protected function applyTaxonomyFilters(array $args, array $data):array
    {
        $taxQuery = [];
        foreach($data['taxonomies']??[] as $taxonomy => $terms) {
            if (array_key_exists(jvbNoBase($taxonomy), JVB_TAXONOMY)) {
                $taxQuery[] = [
                    'taxonomy'  => jvbCheckBase($taxonomy),
                    'terms'     => array_map(
                        'absint',
                        is_array($terms) ? $terms : explode(',', $terms)
                    )
                ];
            }
        }
 
        if (!empty($taxQuery)) {
            $args['tax_query'] = array_merge([
                'relation'  => (array_key_exists('match', $data)) ? 'AND' : 'OR',
            ], $taxQuery);
        }
 
        $authorQuery = [];
        foreach (jvbAuthorUsers() as $type) {
            if (array_key_exists($type, $data)) {
                $artist_ids = array_map(
                    'absint',
                    is_array($data[$type]) ?
                        $data[$type] :
                        explode(',', $data[$type])
                );
                $authorQuery = array_merge($authorQuery, $artist_ids);
            }
        }
        if (!empty($authorQuery)) {
            $args['author__in'] = array_unique($authorQuery);
        }
 
        return $args;
    }
 
    protected function applyOrderFilters(array $args, array $data):array
    {
        if (array_key_exists('orderby', $data) && $data['orderby'] === 'random') {
            $current_seed = jvbGetRandomSeed();
            $args['orderby'] = 'RAND(' . $current_seed . ')';
            unset($args['order']);
            return $args;
        }
        if (in_array($data['orderby'], ['date', 'title', 'alphabetical'])) {
            $args['orderby'] = ($data['orderby'] === 'alphabetical') ? 'title' : $data['orderby'];
        } else {
            switch ($data['orderby']) {
                case 'popularity':
                    $args['meta_key'] = BASE.'upvotes';
                    $args['orderby'] = 'meta_value_num';
                    break;
                case 'karma':
                    $args['meta_key'] = BASE.'karma';
                    $args['orderby'] = 'meta_value_num';
                    break;
                default:
                    $args['orderby'] = 'date';
            }
        }
        $order = (array_key_exists('order', $data)) ? strtoupper($data['order']) : 'DESC';
        $args['order'] = (in_array($order, ['ASC', 'DESC'])) ? $order : 'DESC';
 
        return $args;
    }
 
    protected function applyDateFilters(array $args, array $data):array
    {
        if (!array_key_exists('date-filter', $data) && !array_key_exists('dateFrom', $data)) {
            return $args;
        }
        if (array_key_exists('dateFrom', $data)) {
            $dateFrom = strtotime(sanitize_text_field($data['dateFrom']));
            $dateTo = strtotime(sanitize_text_field($data['dateTo']));
            if ($dateFrom && $dateTo) {
                $args['date_query'] = [
                    [
                        'after'     => date('c', $dateFrom),
                        'before'    => date('c', $dateTo),
                        'inclusive' => true,
                    ]
                ];
            }
        } else {
            switch ($data['date-filter']) {
                case 'today':
                    $args['date_query'] = [['after' => '1 day ago']];
                    break;
                case 'week':
                    $args['date_query'] = [['after' => '1 week ago']];
                    break;
                case 'month':
                    $args['date_query'] = [['after' => '1 month ago']];
                    break;
                case 'year':
                    $args['date_query'] = [['after' => '1 year ago']];
                    break;
            }
        }
        return $args;
    }
 
    protected function applyCalendarFilters(array $args, array $data):array
    {
        $meta_query = [];
        $today = date('Y-m-d');
        if (in_array('future', $args['post_status'])) {
            $meta_query[] = [
                'key'   => 'jvb_start_date',
                'value' => $today,
                'compare'   => '>=',
                'type'  => 'DATE'
            ];
        }
        if (in_array('past', $args['post_status'])) {
            $meta_query[] = [
                'key' => 'jvb_end_date',
                'value' => $today,
                'compare' => '<',
                'type' => 'DATE'
            ];
        }
        if (in_array('recurring', $args['post_status'])) {
            $meta_query[] = [
                'key' => 'jvb_is_recurring',
                'value' => true,
                'compare' => '='
            ];
        }
        if (!empty($meta_query)) {
            $args['meta_query'] = (array_key_exists('meta_query', $args)) ? array_merge($args['meta_query'], $meta_query) : $meta_query;
        }
        return $args;
 
    }
 
    protected function table_exists($tableName, $wpdb = null)
    {
        if (!$wpdb) {
            global $wpdb;
        }
        return $wpdb->get_var("SHOW TABLES LIKE '{$tableName}'") !== $tableName;
    }
 
    // ========== HTTP CACHING METHODS ==========
 
    /**
     * Check HTTP caching headers (ETag and If-Modified-Since)
     * Returns 304 Not Modified if content hasn't changed
     *
     * @param WP_REST_Request $request The REST request
     * @param string|array $content_types Content type(s) to check timestamps for
     * @param array $additional_params Additional params for ETag uniqueness (e.g., user_id, filters)
     * @return WP_REST_Response|null Returns 304 response if not modified, null to continue processing
     */
    protected function checkHeaders(
        WP_REST_Request $request,
        string|array $content_types,
        array $additional_params = []
    ): WP_REST_Response|null {
 
        // Get latest timestamp for the content type(s)
        $last_modified = CacheManager::getTimestamp($content_types);
 
        // Generate ETag from request params + timestamp
        $etag = $this->generateETag($request->get_params(), $additional_params, $last_modified);
 
        // Check If-None-Match (ETag) header
        $if_none_match = $request->get_header('If-None-Match');
        if ($if_none_match === $etag) {
            return $this->createNotModifiedResponse($etag, $last_modified);
        }
 
        // Check If-Modified-Since header
        $if_modified_since = $request->get_header('If-Modified-Since');
        if ($if_modified_since) {
            $if_modified_timestamp = strtotime($if_modified_since);
            if ($last_modified <= $if_modified_timestamp) {
                return $this->createNotModifiedResponse($etag, $last_modified);
            }
        }
 
        // Content has changed - store headers to add to successful response
        $this->response_headers = $this->buildCacheHeaders($etag, $last_modified);
 
        return null; // Continue processing
    }
 
    /**
     * Generate ETag from request parameters and timestamp
     *
     * @param array $params Request parameters
     * @param array $additional Additional parameters for uniqueness
     * @param int $timestamp Last modified timestamp
     * @return string ETag value with quotes
     */
    private function generateETag(array $params, array $additional, int $timestamp): string
    {
        // Combine all data that makes this response unique
        $etag_data = array_merge(
            $params,
            $additional,
            ['t' => $timestamp]
        );
 
        return '"' . md5(serialize($etag_data)) . '"';
    }
 
    /**
     * Create 304 Not Modified response with proper headers
     *
     * @param string $etag ETag value
     * @param int $last_modified Last modified timestamp
     * @return WP_REST_Response 304 response
     */
    private function createNotModifiedResponse(string $etag, int $last_modified): WP_REST_Response
    {
        $response = new WP_REST_Response(null, 304);
        $response->set_headers($this->buildCacheHeaders($etag, $last_modified));
        return $response;
    }
 
    /**
     * Build cache headers array
     *
     * @param string $etag ETag value
     * @param int $last_modified Last modified timestamp
     * @return array Headers array
     */
    private function buildCacheHeaders(string $etag, int $last_modified): array
    {
        return [
            'ETag' => $etag,
            'Last-Modified' => gmdate('D, d M Y H:i:s', $last_modified) . ' GMT',
            'Cache-Control' => 'private, max-age=60, must-revalidate'
        ];
    }
 
    /**
     * Add stored cache headers to a response
     * Call this on your final WP_REST_Response before returning
     *
     * @param WP_REST_Response $response The response to add headers to
     * @return WP_REST_Response The response with headers added
     */
    protected function addCacheHeaders(WP_REST_Response $response): WP_REST_Response
    {
        if (!empty($this->response_headers)) {
            $response->set_headers($this->response_headers);
            $this->response_headers = []; // Clear after use
        }
        return $response;
    }
 
    /**
     * Helper: Check headers for user-specific endpoints
     * Automatically includes user_id in ETag
     *
     * @param WP_REST_Request $request The REST request
     * @param int $user_id User ID
     * @param string|array $content_types Content type(s)
     * @return WP_REST_Response|null
     */
    protected function checkUserHeaders(
        WP_REST_Request $request,
        int $user_id,
        string|array $content_types = 'user'
    ): WP_REST_Response|null {
 
        // Include user-specific timestamp
        $types = is_array($content_types) ? $content_types : [$content_types];
        $types[] = "user_{$user_id}";
 
        return $this->checkHeaders($request, $types, ['user_id' => $user_id]);
    }
}
//
//Simple example:
//public function getTattoos(WP_REST_Request $request): WP_REST_Response
//{
//  // Check HTTP cache headers first
//  $cache_check = $this->checkHeaders($request, 'tattoo');
//  if ($cache_check) {
//      return $cache_check; // Returns 304 Not Modified
//  }
//
//  // Get data (use CacheManager for data caching too!)
//  $filters = $request->get_params();
//  $cache = CacheManager::for('tattoo');
//
//  $tattoos = $cache->remember($filters, function() use ($filters) {
//      return $this->queryTattoos($filters);
//  }, 300);
//
//  $response = new WP_REST_Response(['items' => $tattoos]);
//  return $this->addCacheHeaders($response); // Add ETag and Last-Modified
//}
//
//Multiple Content Types:
//public function getTermsWithContent(WP_REST_Request $request): WP_REST_Response
//{
//  $taxonomy = $request->get_param('taxonomy');
//
//  // Check both taxonomy and its content types
//  $cache_check = $this->checkHeaders($request, [$taxonomy, 'tattoo', 'artwork']);
//  if ($cache_check) {
//      return $cache_check;
//  }
//
//  // ... fetch data ...
//
//  $response = new WP_REST_Response($data);
//  return $this->addCacheHeaders($response);
//}
//
//User-specific:
//public function getUserFavorites(WP_REST_Request $request): WP_REST_Response
//{
//  $user_id = $request->get_param('user');
//
//  // Automatically checks user_{$user_id} timestamp + includes user_id in ETag
//  $cache_check = $this->checkUserHeaders($request, $user_id);
//  if ($cache_check) {
//      return $cache_check;
//  }
//
//  // Get user's favorites (cached per user)
//  $favorites = CacheManager::forUser($user_id)->remember('favorites', function() use ($user_id) {
//      return $this->getUserFavorites($user_id);
//  }, 1800);
//
//  $response = new WP_REST_Response(['items' => $favorites]);
//  return $this->addCacheHeaders($response);
//}
//
//Complex with additional params:
//public function getFilteredContent(WP_REST_Request $request): WP_REST_Response
//{
//  $user_id = get_current_user_id();
//  $filters = $request->get_params();
//
//  // Include custom params in ETag for uniqueness
//  $cache_check = $this->checkHeaders(
//      $request,
//      'tattoo',
//      [
//          'user_id' => $user_id,
//          'is_verified' => $this->isVerifiedUser($user_id)
//      ]
//  );
//
//  if ($cache_check) {
//      return $cache_check;
//  }
//
//  // ... fetch filtered data ...
//
//  $response = new WP_REST_Response($data);
//  return $this->addCacheHeaders($response);
//}