Jake Vanderwerf
2026-01-25 b38f03c0e7218762d90fa5092696b127f24f36db
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
<?php
namespace JVBase\managers;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Manages HTTP cache timestamps and relationship-based invalidation
 *
 * Data caching: Use wrapper methods or wp_cache_get/set directly
 * HTTP caching: This class manages timestamps for ETag/Last-Modified headers
 */
class CacheManager
{
    private const CONNECTIONS_OPTION = BASE.'cache_connections';
    private static ?array $connections_cache = null; // Cache in memory
    private string $prefix = BASE;
    private string $group;
    private int $cache_ttl;
    private static ?bool $use_object_cache = null;
    private static array $instances = []; // Cache instances per type
    private static array $http_timestamps = []; // Request-level memory cache
    private static ?CacheManager $singleton = null;
 
    /**
     * Private constructor - use for() factory method instead
     */
    private function __construct(string $group, ?int $ttl = null)
    {
        $this->group = jvbNoBase($group);
        $this->cache_ttl = $ttl ?: 3600;
 
        if (is_null(static::$use_object_cache)) {
            static::$use_object_cache = wp_using_ext_object_cache();
        }
 
        add_action('init', [$this, 'registerHooks']);
    }
 
    /**
     * Get singleton instance (for general cache operations)
     * For type-specific operations, use for() or forUser() instead
     */
    public static function getInstance(): self
    {
        if (self::$singleton === null) {
            self::$singleton = new self('global', HOUR_IN_SECONDS);
        }
        return self::$singleton;
    }
 
    /**
     * Get all cache connections (public accessor)
     *
     * @return array Array of cache group connections
     */
    public static function getAllConnections(): array
    {
        return self::getConnections();
    }
 
    /**
     * Get all registered cache groups
     *
     * @return array List of cache group names
     */
    public static function getAllGroups(): array
    {
        $connections = self::getConnections();
        return array_keys($connections);
    }
    /**
     * Register WordPress hooks for automatic cache invalidation
     * Call this once during plugin initialization
     */
    public static function registerHooks(): void
    {
        // Post updates (all post types including core)
        add_action('save_post', [self::class, 'onPostSave'], 10, 2);
        add_action('delete_post', [self::class, 'onPostDelete']);
        // Meta updates (will catch MetaManager updates)
        add_action('updated_post_meta', [self::class, 'onPostMetaUpdate'], 10, 4);
        add_action('added_post_meta', [self::class, 'onPostMetaUpdate'], 10, 4);
        add_action('deleted_post_meta', [self::class, 'onPostMetaDelete'], 10, 4);
        // transition_post_status?
 
        // Term updates (all taxonomies)
        add_action('edited_term', [self::class, 'onTermSave'], 10, 3);
        add_action('create_term', [self::class, 'onTermSave'], 10, 3);
        add_action('delete_term', [self::class, 'onTermDelete'], 10, 3);
 
        // Term meta updates
        add_action('updated_term_meta', [self::class, 'onTermMetaUpdate'], 10, 4);
        add_action('added_term_meta', [self::class, 'onTermMetaUpdate'], 10, 4);
        add_action('deleted_term_meta', [self::class, 'onTermMetaDelete'], 10, 4);
 
        // User updates
        add_action('profile_update', [self::class, 'onUserUpdate'], 10, 2);
        add_action('user_register', [self::class, 'onUserUpdate'], 10, 1);
        add_action('deleted_user', [self::class, 'onUserDelete']);
 
        // User meta updates
        add_action('updated_user_meta', [self::class, 'onUserMetaUpdate'], 10, 4);
        add_action('added_user_meta', [self::class, 'onUserMetaUpdate'], 10, 4);
        add_action('deleted_user_meta', [self::class, 'onUserMetaDelete'], 10, 4);
    }
 
    /**
     * Get or create a cache manager instance for a content type
     *
     * @param string $type Content type (tattoo, style, etc.)
     * @param int|null $ttl Optional TTL override
     * @return self Fluent interface
     */
    public static function for(string $type, ?int $ttl = null): self
    {
        $type = jvbNoBase($type);
        $key = $type . ($ttl ? "_{$ttl}" : '');
 
        if (!isset(self::$instances[$key])) {
            self::$instances[$key] = new self($type, $ttl);
        }
 
        return self::$instances[$key];
    }
 
    /**
     * Get cache manager for a specific user
     * Each user gets their own cache group for complete isolation
     *
     * @param int $user_id User ID
     * @param int|null $ttl Optional TTL
     * @return self
     */
    public static function forUser(int $user_id, ?int $ttl = null): self
    {
        return self::for("user_{$user_id}", $ttl);
    }
 
    /**
     * Get HTTP cache timestamp for content type(s)
     * Used for ETag and Last-Modified header generation
     *
     * @param string|array $types Single type or array of types
     * @return int Latest timestamp (Unix time)
     */
    public static function getTimestamp(string|array $types): int
    {
        // Multiple types - return latest
        if (is_array($types)) {
            $latest = 0;
            foreach ($types as $type) {
                $timestamp = self::getTimestamp($type);
                if ($timestamp > $latest) {
                    $latest = $timestamp;
                }
            }
            return $latest ?: time();
        }
 
        $type = jvbNoBase($types);
 
        // Check request-level cache
        if (isset(self::$http_timestamps[$type])) {
            return self::$http_timestamps[$type];
        }
 
        // Load from cache (Redis or transient - wp_cache handles it)
        $timestamp = (int)wp_cache_get("http_ts_{$type}", 'jvb_timestamps') ?: time();
 
        // Cache in memory for this request
        self::$http_timestamps[$type] = $timestamp;
 
        return $timestamp;
    }
 
    /**
     * Update HTTP cache timestamp (marks content as modified)
     *
     * @param string $type Content type
     * @return int The new timestamp
     */
    public static function updateTimestamp(string $type): int
    {
        $type = jvbNoBase($type);
        $timestamp = time();
 
        // Store (Redis or transient - wp_cache handles it)
        wp_cache_set("http_ts_{$type}", $timestamp, 'jvb_timestamps', WEEK_IN_SECONDS);
 
        // Update request cache
        self::$http_timestamps[$type] = $timestamp;
 
        do_action('jvb_http_timestamp_updated', $type, $timestamp);
 
        return $timestamp;
    }
 
    /**
     * Invalidate cache for a content type
     *
     * @param string $type Content type to invalidate
     * @param string|array|null $specific_keys Optional specific key(s) to delete without flushing group
     * @param bool $flush_connections Whether to flush connected caches
     * @return void
     */
    public static function invalidateAll(string $type, $specific_keys = null, bool $flush_connections = true): void
    {
        $type = jvbNoBase($type);
 
        // Update HTTP timestamp
        self::updateTimestamp($type);
 
        // If specific keys provided, only delete those
        if ($specific_keys !== null) {
            $instance = self::for($type);
            if (is_array($specific_keys)) {
                foreach ($specific_keys as $key) {
                    $instance->delete($key);
                }
            } else {
                $instance->delete($specific_keys);
            }
        } else {
            // Flush the entire group
            if (function_exists('wp_cache_flush_group')) {
                wp_cache_flush_group($type);
            } else {
                wp_cache_flush();
            }
        }
 
        // Flush connected caches
        if ($flush_connections) {
            self::for($type)->connections();
        }
 
        do_action('jvb_cache_invalidated', $type);
    }
 
    /**
     * Invalidate only specific keys for a type (doesn't flush group or update timestamp)
     * Use this when you want surgical cache invalidation
     *
     * @param string $type Content type
     * @param string|array $keys Key(s) to delete
     * @return void
     */
    public static function invalidateKeys(string $type, string|array $keys): void
    {
        $instance = self::for($type);
 
        if (is_array($keys)) {
            foreach ($keys as $key) {
                $instance->delete($key);
            }
        } else {
            $instance->delete($keys);
        }
    }
 
    /**
     * Fluent instance method to invalidate this cache type
     *
     * @param string|array|null $specific_keys Optional specific key(s)
     * @param bool $flush_connections Whether to flush connected caches
     * @return self For chaining
     */
    public function invalidate($specific_keys = null, bool $flush_connections = true): self
    {
        self::invalidateAll($this->group, $specific_keys, $flush_connections);
        return $this;
    }
 
    /**
     * Get the HTTP timestamp for this instance's type
     *
     * @return int
     */
    public function timestamp(): int
    {
        return self::getTimestamp($this->group);
    }
 
    /**
     * Update the HTTP timestamp for this instance's type
     *
     * @return self For chaining
     */
    public function touch(): self
    {
        self::updateTimestamp($this->group);
        return $this;
    }
 
    /**
     * Get a value from the cache
     * @param string|array $key The key to look up (auto-generates key from array of key=>values)
     * @param string|null $group The group to get from. Defaults to current group
     * @return mixed
     */
    public function get(string|array $key, ?string $group = null): mixed
    {
        $group = $group ?: $this->group;
        $key = $this->normalizeKey($key);
        $cache_key = $this->buildKey($key);
 
        $value = wp_cache_get($cache_key, $group);
 
        // Fallback to transient if no external object cache
        if ($value === false && !wp_using_ext_object_cache()) {
            $value = get_transient($group . '_' . $cache_key);
        }
 
        return $value;
    }
 
    /**
     * Store a value in cache
     * @param string|array $key The key to look up (auto-generates key from array of key=>values)
     * @param mixed $value The Value to set
     * @param int|null $ttl The ttl (defaults to current set ttl)
     * @param string|null $group The group to add cache to (defaults to current group))
     * @return bool
     */
    public function set(string|array $key, mixed $value, ?int $ttl = null, ?string $group = null): bool
    {
        $ttl = $ttl ?: $this->cache_ttl;
        $group = $group ?: $this->group;
        $key = $this->normalizeKey($key);
        $cache_key = $this->buildKey($key);
 
        self::updateTimestamp($this->group);
 
        // Try object cache first
        $result = wp_cache_set($cache_key, $value, $group, $ttl);
 
        // If no external object cache, also store in transient for persistence
        if (!wp_using_ext_object_cache()) {
            set_transient($group . '_' . $cache_key, $value, $ttl);
        }
 
        return $result;
    }
    /**
     * Delete a cached value
     * @param string|array $key The key to look up (auto-generates key from array of key=>values)
     * @param string|null $group The group to delete from (defaults to current group)
     * @return bool
     */
    public function delete(string|array $key, ?string $group = null): bool
    {
        $group = $group ?: $this->group;
        $key = $this->normalizeKey($key);
        $cache_key = $this->buildKey($key);
 
        $result = wp_cache_delete($cache_key, $group);
 
        // Also delete transient if no external object cache
        if (!wp_using_ext_object_cache()) {
            delete_transient($group . '_' . $cache_key);
        }
 
        return $result;
    }
 
 
    /**
     * Clear all cache for this group
     * @return bool
     */
    public function clear(): bool
    {
        try {
            if (function_exists('wp_cache_flush_group')) {
                wp_cache_flush_group($this->group);
            }
 
            // Clear transients for this group if no external object cache
            if (!wp_using_ext_object_cache()) {
                $this->clearGroupTransients();
            }
 
            self::updateTimestamp($this->group);
            return true;
        } catch (\Exception $e) {
            return false;
        }
    }
 
    /**
     * Clear all transients for this cache group
     */
    private function clearGroupTransients(): void
    {
        global $wpdb;
 
        $pattern = '_transient_' . $this->group . '_' . $this->prefix . '%';
        $timeout_pattern = '_transient_timeout_' . $this->group . '_' . $this->prefix . '%';
 
        $wpdb->query(
            $wpdb->prepare(
                "DELETE FROM $wpdb->options WHERE option_name LIKE %s OR option_name LIKE %s",
                $pattern,
                $timeout_pattern
            )
        );
    }
 
    /**
     * Helper to generateKey from array if applicable
     * @param string|array $key
     * @return string
     */
    private function normalizeKey(string|array $key): string
    {
        return is_array($key) ? $this->generateKey($key) : $key;
    }
 
    /**
     * Generate a cache key from parameters
     * @param array $params An array of key/values that differentiates this cache item from others
     * @return string
     */
    public function generateKey(array $params): string
    {
        // Sort params for consistent key generation
        ksort($params);
        return md5(serialize($params));
    }
 
    /**
     * The workhorse shorthand of CacheManager. Tests the cache, and calls the callback if nothing is found.
     * @param string|array $key The key to look up (auto-generates key from array of key=>values)
     * @param callable $callback The callback to generate the value for this key
     * @param int|null $ttl The time-to-live for the cache. Defaults to constructor
     * @param string|null $group The group to save cache to. Defaults to constructor
     * @return mixed
     */
    public function remember(string|array $key, callable $callback, ?int $ttl = null, ?string $group = null): mixed
    {
        $group = $group ?: $this->group;
        $ttl = $ttl ?: $this->cache_ttl;
        $key = $this->normalizeKey($key);
 
        $value = $this->get($key, $group);
 
        if ($value === false) {
            $value = $callback();
            if ($value !== false && $value !== null) {
                $this->set($key, $value, $ttl, $group);
            }
        }
 
        return $value;
    }
 
    /**
     * Build the cache key
     * @param string $key
     * @return string
     */
    private function buildKey(string $key): string
    {
        return $this->prefix . $key;
    }
 
    /**
     * Get instance group name (for debugging)
     */
    public function getGroup(): string
    {
        return $this->group;
    }
 
 
    /***************************************************************************
     * CONNECTIONS
     * Connect to other caches by instantiating and defining connection
     * Ex: CacheManager::for('usernames')->connectTo($type, $scope = 'all', $keyPattern)
     * Where:   $type = content / taxonomy / user
     *          $scope = either 'id' for specific item, or the entire group (registered post type, taxonomy, or user role)
     *          $keyPattern = ??
     ***************************************************************************/
    /**
     * Define a connection between cache groups
     * Connected caches will have their ID-based keys deleted when this cache invalidates
     *
     * @param string $type Grand overview ('post', 'taxonomy', 'user')
     * @param string $scope Type-specific constant, user role, or 'id'
     * @return self For chaining
     */
    public function connectTo(string $type, string $scope = 'id'): self
    {
        //TODO: Handle connect to where $type === 'all'
        $connections = self::getConnections();
 
        if (!isset($connections[$this->group])) {
            $connections[$this->group] = [];
        }
 
        $new_connection = [
            'parent' => $type,
            'scope' => $scope
        ];
 
        // Check if already exists
        foreach ($connections[$this->group] as $existing) {
            if ($existing === $new_connection) {
                return $this;
            }
        }
 
        $connections[$this->group][] = $new_connection;
        update_option(self::CONNECTIONS_OPTION, $connections, false);
        self::$connections_cache = $connections;
 
        return $this;
    }
 
    /**
     * Get all registered connections (cached for performance)
     *
     * @param bool $refresh Force refresh from database
     * @return array
     */
    private static function getConnections(bool $refresh = false): array
    {
        if (self::$connections_cache === null || $refresh) {
            self::$connections_cache = get_option(self::CONNECTIONS_OPTION, []);
        }
 
        return self::$connections_cache;
    }
 
    /**
     * Flush all caches connected to this one
     *
     * @return self For chaining
     */
    public function connections(): self
    {
        $all_connections = self::getConnections();
 
        foreach ($all_connections as $cache_group => $connections) {
            foreach ($connections as $conn) {
                if ($this->matchesConnection($conn)) {
                    $this->flushConnection($cache_group, $conn);
                }
            }
        }
 
        return $this;
    }
 
    /**
     * Check if this cache group matches a connection definition
     */
    private function matchesConnection(array $connection): bool
    {
        $parent = $connection['parent'] ?? '';
        $scope = $connection['scope'] ?? 'id';
 
        // Grand overview match
        if ($this->group === $parent) {
            return true;
        }
 
        // Type-specific match
        if ($scope !== 'id') {
            if ($this->group === jvbNoBase($scope)) {
                return true;
            }
 
            // Check constants
            if ($parent === 'post' && defined('JVB_CONTENT')) {
                return isset(JVB_CONTENT[$scope]) && jvbNoBase($scope) === $this->group;
            }
 
            if ($parent === 'taxonomy' && defined('JVB_TAXONOMY')) {
                return isset(JVB_TAXONOMY[$scope]) && jvbNoBase($scope) === $this->group;
            }
        }
 
        // ID-specific match: 'user_123' matches 'user' + 'id'
        if ($scope === 'id' && str_starts_with($this->group, $parent . '_')) {
            return true;
        }
 
        return false;
    }
 
    /**
     * Flush a connected cache group
     * For ID-specific connections, deletes the specific ID key
     * For type/overview connections, flushes entire group
     */
    private function flushConnection(string $cache_group, array $connection): void
    {
        $scope = $connection['scope'] ?? 'id';
 
        // ID-specific: delete specific key
        if ($scope === 'id') {
            $id = $this->extractIdFromGroup();
 
            if ($id !== null) {
                self::invalidateKeys($cache_group, $id);
                return;
            }
        }
 
        // Type/overview: flush entire group
        self::invalidateAll($cache_group, specific_keys: null, flush_connections: false);
    }
 
    /**
     * Extract ID from group name like 'user_123' -> '123'
     *
     * @return string|null
     */
    private function extractIdFromGroup(): ?string
    {
        if (preg_match('/^[a-z]+_(\d+)$/', $this->group, $matches)) {
            return $matches[1];
        }
 
        return null;
    }
 
    /**
     * Register multiple connections at once
     */
    public static function registerConnections(array $connections): void
    {
        $existing = self::getConnections();
        $changed = false;
 
        foreach ($connections as $cache_group => $configs) {
            if (!isset($existing[$cache_group])) {
                $existing[$cache_group] = [];
            }
 
            foreach ($configs as $config) {
                $duplicate = false;
                foreach ($existing[$cache_group] as $existing_config) {
                    if ($existing_config === $config) {
                        $duplicate = true;
                        break;
                    }
                }
 
                if (!$duplicate) {
                    $existing[$cache_group][] = $config;
                    $changed = true;
                }
            }
        }
 
        if ($changed) {
            update_option(self::CONNECTIONS_OPTION, $existing, false);
            self::$connections_cache = $existing;
        }
    }
 
    /**
     * Handle post save/update
     */
    public static function onPostSave(int $post_id, \WP_Post $post): void
    {
        // Skip revisions and autosaves
        if (wp_is_post_revision($post_id) || wp_is_post_autosave($post_id)) {
            return;
        }
 
        $post_type = jvbNoBase($post->post_type);
 
        // Invalidate post type cache
        self::invalidateAll($post_type);
 
        // Invalidate specific post cache
        self::invalidateAll($post_id);
        // Clear WordPress core post object cache
        clean_post_cache($post_id);
    }
 
    /**
     * Handle post deletion
     */
    public static function onPostDelete(int $post_id): void
    {
        $post = get_post($post_id);
        if (!$post) {
            return;
        }
 
        $post_type = jvbNoBase($post->post_type);
 
        self::invalidateAll($post_type);
        self::invalidateAll($post_id);
        // Clear WordPress core post object cache
        clean_post_cache($post_id);
    }
 
    /**
     * Handle term save/update
     */
    public static function onTermSave(int $term_id, int $tt_id, string $taxonomy): void
    {
        // Clear WordPress core term cache
        clean_term_cache($term_id, $taxonomy);
        $taxonomy = jvbNoBase($taxonomy);
 
        // Invalidate taxonomy cache
        self::invalidateAll($taxonomy);
 
        // Invalidate specific term cache
        self::invalidateAll($term_id);
    }
 
    /**
     * Handle term deletion
     */
    public static function onTermDelete(int $term_id, int $tt_id, string $taxonomy): void
    {
        // Clear WordPress core term cache
        clean_term_cache($term_id, $taxonomy);
        $taxonomy = jvbNoBase($taxonomy);
 
        self::invalidateAll($taxonomy);
        self::invalidateAll($term_id);
    }
 
    /**
     * Handle user update
     */
    public static function onUserUpdate(int $user_id, ?\WP_User $old_user_data = null): void
    {
        // Invalidate user-specific cache
        self::invalidateAll($user_id);
 
        // Invalidate user role caches if roles changed
        if ($old_user_data) {
            $user = get_userdata($user_id);
            if ($user && $user->roles !== $old_user_data->roles) {
                foreach (array_merge($user->roles, $old_user_data->roles) as $role) {
                    self::invalidateAll($role);
                }
            }
        }
        // Clear WordPress core user cache
        clean_user_cache($user_id);
    }
 
    /**
     * Handle user deletion
     */
    public static function onUserDelete(int $user_id): void
    {
        self::invalidateAll($user_id);
        // Clear WordPress core user cache
        clean_user_cache($user_id);
    }
 
    /**
     * Handle post meta updates
     */
    public static function onPostMetaUpdate(int $meta_id, int $post_id, string $meta_key, mixed $meta_value): void
    {
        if (!str_starts_with($meta_key, BASE)) {
            return;
        }
 
        $post = get_post($post_id);
        if (!$post) {
            return;
        }
 
        self::onPostSave($post_id, $post);
    }
    public static function onPostMetaDelete(array $meta_ids, int $post_id, string $meta_key, mixed $meta_value):void
    {
        if (!str_starts_with($meta_key, BASE)) {
            return;
        }
 
        $post = get_post($post_id);
        if (!$post) {
            return;
        }
 
        self::onPostSave($post_id, $post);
    }
 
    /**
     * Handle term meta updates
     */
    public static function onTermMetaUpdate(int $meta_id, int $term_id, string $meta_key, mixed $meta_value): void
    {
        if (!str_starts_with($meta_key, BASE)) {
            return;
        }
 
        $term = get_term($term_id);
        if (!$term || is_wp_error($term)) {
            return;
        }
 
        self::onTermSave($term_id, $term->term_taxonomy_id, $term->taxonomy);
    }
 
    public static function onTermMetaDelete(array $meta_ids, int $term_id, string $meta_key, mixed $meta_value):void
    {
        if (!str_starts_with($meta_key, BASE)) {
            return;
        }
 
        $term = get_term($term_id);
        if (!$term || is_wp_error($term)) {
            return;
        }
 
        self::onTermSave($term_id, $term->term_taxonomy_id, $term->taxonomy);
    }
 
    /**
     * Handle user meta updates
     */
    public static function onUserMetaUpdate(int $meta_id, int $user_id, string $meta_key, mixed $meta_value): void
    {
        if (!str_starts_with($meta_key, BASE)) {
            return;
        }
 
        $user = get_userdata($user_id);
        if (!$user) {
            return;
        }
 
        self::onUserUpdate($user_id, null);
    }
 
    public static function onUserMetaDelete(array $meta_ids, int $user_id, string $meta_key, mixed $meta_value):void
    {
        if (!str_starts_with($meta_key, BASE)) {
            return;
        }
 
        $user = get_userdata($user_id);
        if (!$user) {
            return;
        }
 
        self::onUserUpdate($user_id, null);
    }
}