Jake Vanderwerf
2026-02-14 27fb820ae9081fb56957cf75e79eccd8a99edd52
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
<?php
namespace JVBase\meta;
 
use Exception;
use JVBase\managers\Cache;
 
if (!defined('ABSPATH')) {
    exit;
}
 
/**
 * Handles persistence of meta values to WordPress
 * Pure storage layer - no validation or sanitization
 */
class Storage
{
    protected \wpdb $wpdb;
 
    public function __construct()
    {
        global $wpdb;
        $this->wpdb = $wpdb;
    }
 
    // ─────────────────────────────────────────────────────────────
    // Single Item Operations
    // ─────────────────────────────────────────────────────────────
 
    /**
     * Load a single field value from database
     */
    public function get(Item $item, string $name): mixed
    {
        if ($item->isWpDefault($name)) {
            return $this->getWpDefault($item, $name);
        }
 
        // Taxonomy fields are stored in term_relationships, not meta
        $config = $item->getFieldConfig($name);
        if ($config
            && (
                ($config['type'] ?? '') === 'taxonomy'
                || (($config['type']??'') === 'selector' && ($config['subtype']??'') === 'taxonomy')
            ) && !isset($config['taxonomy_type'])) {
            return $this->getTaxonomyField($item, $config);
        }
 
        $metaKey = BASE . $name;
 
        return match ($item->objectType) {
            'post' => get_post_meta($item->id, $metaKey, true),
            'term' => get_term_meta($item->id, $metaKey, true),
            'user', 'integrations' => get_user_meta($item->id, $metaKey, true),
            'options' => $this->getOption($item, $name),
            default => ''
        };
    }
 
    /**
     * Load multiple field values for single item
     */
    public function getAll(Item $item, array $fieldNames): array
    {
        if (empty($fieldNames) || (!$item->id && $item->objectType !== 'options')) {
            return [];
        }
 
        $defaults = Item::WP_DEFAULTS[$item->objectType] ?? [];
        $wpFields = array_intersect($defaults, $fieldNames);
 
        // Separate taxonomy fields from regular meta fields
        $taxonomyFields = [];
        $metaFields = [];
        foreach (array_diff($fieldNames, $wpFields) as $name) {
            $config = $item->getFieldConfig($name);
            if ($config
                && (
                    ($config['type'] ?? '') === 'taxonomy'
                    || (($config['type']??'') === 'selector' && ($config['subtype']??'') === 'taxonomy')
                ) && !isset($config['taxonomy_type'])) {
                $taxonomyFields[$name] = $config;
            } else {
                $metaFields[] = $name;
            }
        }
 
        $values = [];
 
        if (!empty($metaFields)) {
            $values = $this->bulkGetMeta($item, $metaFields);
        }
 
        foreach ($wpFields as $name) {
            $values[$name] = $this->getWpDefault($item, $name);
        }
 
        foreach ($taxonomyFields as $name => $config) {
            $values[$name] = $this->getTaxonomyField($item, $config);
        }
 
        return $values;
    }
 
    protected function getTaxonomyField(Item $item, array $config): string
    {
        $taxonomy = jvbCheckBase($config['taxonomy']);
        $terms = wp_get_object_terms($item->id, $taxonomy, ['fields' => 'ids']);
 
        if (is_wp_error($terms) || empty($terms)) {
            return '';
        }
 
        return implode(',', $terms);
    }
 
    /**
     * Save a single field
     */
    public function saveField(Item $item, Field $field): bool
    {
        if ($field->isWpDefault()) {
            return $this->saveWpDefault($item, $field);
        }
 
        if ($field->isTaxonomy()) {
            return $this->saveTaxonomyField($item, $field);
        }
 
        $metaKey = BASE . $field->name;
 
        return match ($item->objectType) {
            'post' => update_post_meta($item->id, $metaKey, $field->value) !== false,
            'term' => update_term_meta($item->id, $metaKey, $field->value) !== false,
            'user', 'integrations' => update_user_meta($item->id, $metaKey, $field->value) !== false,
            'options' => $this->saveOption($item, $field),
            default => false
        };
    }
 
    /**
     * Save all dirty fields on a single item
     */
    public function save(Item $item, bool $updateTimestamp = true): bool
    {
        $dirty = $item->getDirtyFields();
 
        if (empty($dirty)) {
            return true;
        }
 
        $this->wpdb->query('START TRANSACTION');
 
        try {
            foreach ($dirty as $field) {
                if (!$this->saveField($item, $field)) {
                    throw new Exception("Failed to save field: {$field->name}");
                }
                $field->markClean();
            }
 
            $this->wpdb->query('COMMIT');
 
            // Update post modified timestamp
            Cache::invalidateItem($item->objectType, $item->id);
            $this->clearCache($item);
 
            return true;
 
        } catch (Exception $e) {
            $this->wpdb->query('ROLLBACK');
            JVB()->error()->log('meta_storage', $e->getMessage(), [
                'item_id' => $item->id,
                'object_type' => $item->objectType,
                'fields' => array_keys($dirty)
            ], 'error');
            return false;
        }
    }
 
    /**
     * Delete a field value
     */
    public function delete(Item $item, string $name): bool
    {
        // Handle taxonomy fields
        $config = $item->getFieldConfig($name);
        if ($config && ($config['type'] ?? '') === 'taxonomy' && !isset($config['taxonomy_type'])) {
            $taxonomy = jvbCheckBase($config['taxonomy']);
            wp_set_object_terms($item->id, [], $taxonomy, false);
            return true;
        }
 
        $metaKey = BASE . $name;
 
        return match ($item->objectType) {
            'post' => delete_post_meta($item->id, $metaKey),
            'term' => delete_term_meta($item->id, $metaKey),
            'user', 'integrations' => delete_user_meta($item->id, $metaKey),
            'options' => delete_option($this->optionKey($item, $name)),
            default => false
        };
    }
 
    // ─────────────────────────────────────────────────────────────
    // Bulk Operations
    // ─────────────────────────────────────────────────────────────
 
    /**
     * Save multiple Meta instances in optimized transaction
     * @param Meta[] $metas Array of Meta instances
     * @return array<int, bool> Results keyed by item ID
     */
    public static function saveBulk(array $metas, bool $updateTimestamp = true): array
    {
        global $wpdb;
 
        $results = [];
        $postIdsToUpdate = [];
 
        $wpdb->query('START TRANSACTION');
 
        try {
            // Group by object type for efficient processing
            $grouped = [];
            foreach ($metas as $meta) {
                $item = $meta->item();
                $type = $item->objectType;
                $grouped[$type][] = ['meta' => $meta, 'item' => $item];
            }
 
            foreach ($grouped as $objectType => $group) {
                $storage = new self();
                [$table, $idColumn] = $storage->getTableInfo($objectType);
 
                if (!$table && $objectType !== 'options') {
                    continue;
                }
 
                // Collect all operations
                $metaInserts = [];
                $wpDefaultUpdates = [];
                $taxonomyUpdates = [];
                $optionUpdates = [];
 
                foreach ($group as $entry) {
                    $item = $entry['item'];
                    $dirty = $item->getDirtyFields();
 
                    if (empty($dirty)) {
                        $results[$item->id] = true;
                        continue;
                    }
 
                    foreach ($dirty as $field) {
                        if ($objectType === 'options') {
                            $optionUpdates[] = [
                                'key' => $storage->optionKey($item, $field->name),
                                'value' => $field->value
                            ];
                        } elseif ($field->isWpDefault()) {
                            $wpDefaultUpdates[$item->id][$field->name] = $field->value;
                        } elseif ($field->isTaxonomy()) {
                            $taxonomyUpdates[] = [
                                'object_id' => $item->id,
                                'taxonomy' => jvbCheckBase($field->config['taxonomy']),
                                'value' => $field->value
                            ];
                        } else {
                            $metaInserts[] = [
                                'id' => $item->id,
                                'key' => BASE . $field->name,
                                'value' => maybe_serialize($field->value)
                            ];
                        }
                    }
 
                    if ($updateTimestamp && $objectType === 'post') {
                        $postIdsToUpdate[] = $item->id;
                    }
                }
 
                // Execute bulk operations
                if (!empty($metaInserts)) {
                    self::bulkUpsertMeta($table, $idColumn, $metaInserts);
                }
 
                if (!empty($wpDefaultUpdates)) {
                    self::batchUpdateWpDefaults($objectType, $wpDefaultUpdates);
                }
 
                if (!empty($taxonomyUpdates)) {
                    self::batchUpdateTaxonomies($taxonomyUpdates);
                }
 
                if (!empty($optionUpdates)) {
                    self::batchUpdateOptions($optionUpdates);
                }
 
                // Mark all fields clean
                foreach ($group as $entry) {
                    $entry['item']->markAllClean();
                    $results[$entry['item']->id ?? 'options'] = true;
                }
            }
 
            $wpdb->query('COMMIT');
 
            // Update post timestamps in single query
            if (!empty($postIdsToUpdate)) {
                self::batchTouchPosts(array_unique($postIdsToUpdate));
            }
 
            // Clear caches
            foreach ($metas as $meta) {
                (new self())->clearCache($meta->item());
            }
 
            return $results;
 
        } catch (Exception $e) {
            $wpdb->query('ROLLBACK');
 
            foreach ($metas as $meta) {
                $results[$meta->item()->id ?? 'options'] = false;
            }
 
            JVB()->error()->log('meta_storage', 'Bulk save failed: ' . $e->getMessage(), [], 'error');
 
            return $results;
        }
    }
 
    /**
     * Bulk load meta for multiple items
     * @param array $ids Object IDs
     * @param string $objectType post, term, user
     * @param array $fields Field names to load
     * @return array<int, array<string, mixed>> Values keyed by ID then field name
     */
    public static function getBulkValues(array $ids, string $objectType, array $fields): array
    {
        if (empty($ids) || empty($fields)) {
            return [];
        }
 
        global $wpdb;
        $storage = new self();
 
        [$table, $idColumn] = $storage->getTableInfo($objectType);
 
        if (!$table) {
            return [];
        }
 
        // Separate WP defaults from custom meta
        $defaults = Item::WP_DEFAULTS[$objectType] ?? [];
        $wpFields = array_intersect($defaults, $fields);
        $metaFields = array_diff($fields, $wpFields);
 
        // Initialize results
        $values = [];
        foreach ($ids as $id) {
            $values[$id] = array_fill_keys($fields, '');
        }
 
        // Bulk get custom meta
        if (!empty($metaFields)) {
            $metaKeys = array_map(fn($f) => BASE . $f, $metaFields);
 
            $idPlaceholders = implode(',', array_fill(0, count($ids), '%d'));
            $keyPlaceholders = implode(',', array_fill(0, count($metaKeys), '%s'));
 
            $query = $wpdb->prepare(
                "SELECT {$idColumn} as object_id, meta_key, meta_value
                 FROM {$table}
                 WHERE {$idColumn} IN ({$idPlaceholders})
                 AND meta_key IN ({$keyPlaceholders})",
                array_merge($ids, $metaKeys)
            );
 
            $results = $wpdb->get_results($query, ARRAY_A);
 
            foreach ($results as $row) {
                $objectId = (int)$row['object_id'];
                $fieldName = str_replace(BASE, '', $row['meta_key']);
                $values[$objectId][$fieldName] = maybe_unserialize($row['meta_value']);
            }
        }
 
        // Get WP default fields (requires individual lookups unfortunately)
        if (!empty($wpFields)) {
            foreach ($ids as $id) {
                $tempItem = new Item($id, $objectType);
 
                // Load WP object for defaults
                $tempItem->wpObject = match ($objectType) {
                    'post' => get_post($id),
                    'term' => get_term($id),
                    'user' => get_user_by('id', $id),
                    default => null
                };
 
                foreach ($wpFields as $field) {
                    $values[$id][$field] = $storage->getWpDefault($tempItem, $field);
                }
            }
        }
 
        return $values;
    }
 
    // ─────────────────────────────────────────────────────────────
    // Protected Helpers - Single Item
    // ─────────────────────────────────────────────────────────────
 
    protected function getWpDefault(Item $item, string $name): mixed
    {
        if (in_array($name, ['featured_image', 'post_thumbnail'])) {
            return get_post_thumbnail_id($item->id);
        }
 
        return match ($item->objectType) {
            'post' => $this->getPostField($item, $name),
            'term' => $this->getTermField($item, $name),
            'user' => $this->getUserField($item, $name),
            default => ''
        };
    }
 
    protected function getPostField(Item $item, string $name): mixed
    {
        return match ($name) {
            'post_title' => get_the_title($item->id),
            'post_excerpt' => get_the_excerpt($item->id),
            'post_content' => get_post_field('post_content', $item->id),
            default => $item->wpObject->$name ?? ''
        };
    }
 
    protected function getTermField(Item $item, string $name): mixed
    {
        return match ($name) {
            'term_name' => get_term_field('name', $item->id),
            'description' => get_term_field('description', $item->id),
            default => ''
        };
    }
 
    protected function getUserField(Item $item, string $name): mixed
    {
        return match ($name) {
            'display_name' => get_the_author_meta('display_name', $item->id),
            'user_email' => get_the_author_meta('user_email', $item->id),
            'first_name' => get_the_author_meta('first_name', $item->id),
            'last_name' => get_the_author_meta('last_name', $item->id),
            default => $item->wpObject->$name ?? ''
        };
    }
 
    protected function saveWpDefault(Item $item, Field $field): bool
    {
        $name = $field->name;
        $value = $field->value;
 
        if (in_array($name, ['featured_image', 'post_thumbnail'])) {
            if (empty($value)) {
                return delete_post_thumbnail($item->id);
            }
            return set_post_thumbnail($item->id, $value) !== false;
        }
 
        // Special handling for post_status (trash/delete require specific functions)
        if ($item->objectType === 'post' && $name === 'post_status') {
            return $this->updatePostStatus($item->id, $value);
        }
 
        return match ($item->objectType) {
            'post' => wp_update_post(['ID' => $item->id, $name => $value]) !== 0,
            'term' => !is_wp_error(wp_update_term($item->id, $item->wpObject->taxonomy, [
                $name => $value,
                'slug' => $name === 'term_name' ? sanitize_title($value) : null
            ])),
            'user' => wp_update_user(['ID' => $item->id, $name => $value]) !== 0,
            default => false
        };
    }
 
    /**
     * Update post status with proper WordPress functions
     *
     * WordPress doesn't handle trash/delete via wp_update_post():
     * - wp_trash_post() required for trashing
     * - wp_delete_post() required for deletion
     * - 'delete' is not even a valid post_status value
     *
     * @param int $postId Post ID
     * @param string $status New status (trash, delete, publish, draft, etc.)
     * @return bool Success
     */
    protected function updatePostStatus(int $postId, string $status): bool
    {
        // Handle trash status
        if ($status === 'trash') {
            $result = wp_trash_post($postId);
            if ($result === false || $result === null) {
                error_log("[Storage] Failed to trash post {$postId}");
                return false;
            }
            return true;
        }
 
        // Handle permanent deletion
        if ($status === 'delete') {
            $result = wp_delete_post($postId, true); // true = force delete, bypass trash
            if ($result === false || $result === null) {
                error_log("[Storage] Failed to delete post {$postId}");
                return false;
            }
            return true;
        }
 
        // Handle all other statuses (publish, draft, pending, private, future)
        $result = wp_update_post([
            'ID' => $postId,
            'post_status' => $status
        ]);
 
        if ($result === 0 || is_wp_error($result)) {
            $error = is_wp_error($result) ? $result->get_error_message() : 'Unknown error';
            error_log("[Storage] Failed to update post {$postId} status to {$status}: {$error}");
            return false;
        }
 
        return true;
    }
 
    protected function saveTaxonomyField(Item $item, Field $field): bool
    {
        $taxonomy = jvbCheckBase($field->config['taxonomy']);
        $value = $field->value;
 
        if (empty(trim((string)$value))) {
            wp_set_object_terms($item->id, [], $taxonomy, false);
            return true;
        }
 
        $termIds = array_map('intval', array_filter(explode(',', $value)));
        $result = wp_set_object_terms($item->id, $termIds, $taxonomy, false);
 
        return !is_wp_error($result);
    }
 
    protected function bulkGetMeta(Item $item, array $fields): array
    {
        [$table, $idColumn] = $this->getTableInfo($item->objectType);
 
        if (!$table) {
            return [];
        }
 
        $metaKeys = array_map(fn($f) => BASE . $f, $fields);
        $placeholders = implode(',', array_fill(0, count($metaKeys), '%s'));
 
        $query = $this->wpdb->prepare(
            "SELECT meta_key, meta_value FROM {$table}
             WHERE {$idColumn} = %d AND meta_key IN ({$placeholders})",
            array_merge([$item->id], $metaKeys)
        );
 
        $results = $this->wpdb->get_results($query, ARRAY_A);
 
        $values = array_fill_keys($fields, '');
 
        foreach ($results as $row) {
            $key = str_replace(BASE, '', $row['meta_key']);
            $values[$key] = maybe_unserialize($row['meta_value']);
        }
 
        return $values;
    }
 
    public function getTableInfo(string $objectType): array
    {
        return match ($objectType) {
            'post' => [$this->wpdb->postmeta, 'post_id'],
            'term' => [$this->wpdb->termmeta, 'term_id'],
            'user', 'integrations' => [$this->wpdb->usermeta, 'user_id'],
            default => [null, null]
        };
    }
 
    protected function getOption(Item $item, string $name): mixed
    {
        return get_option($this->optionKey($item, $name));
    }
 
    protected function saveOption(Item $item, Field $field): bool
    {
        return update_option($this->optionKey($item, $field->name), $field->value);
    }
 
    public function optionKey(Item $item, string $name): string
    {
        return $item->baseKey
            ? BASE . $item->baseKey . '_' . $name
            : BASE . $name;
    }
 
    public function clearCache(Item $item): void
    {
        match ($item->objectType) {
            'post' => clean_post_cache($item->id),
            'term' => clean_term_cache($item->id),
            'user', 'integrations' => clean_user_cache($item->id),
            default => null
        };
    }
 
    // ─────────────────────────────────────────────────────────────
    // Protected Helpers - Bulk Operations
    // ─────────────────────────────────────────────────────────────
 
    /**
     * Bulk upsert meta using INSERT ... ON DUPLICATE KEY UPDATE
     */
    protected static function bulkUpsertMeta(string $table, string $idColumn, array $inserts): void
    {
        global $wpdb;
 
        if (empty($inserts)) {
            return;
        }
 
        // MySQL's ON DUPLICATE KEY requires a unique index
        // For meta tables, we need to check existing and do update/insert
        $existing = [];
        $toInsert = [];
        $toUpdate = [];
 
        // Check which meta keys exist
        $checks = [];
        foreach ($inserts as $row) {
            $checks[] = $wpdb->prepare("(%d, %s)", $row['id'], $row['key']);
        }
 
        $existingQuery = "SELECT {$idColumn}, meta_key FROM {$table}
                          WHERE ({$idColumn}, meta_key) IN (" . implode(',', $checks) . ")";
        $existingRows = $wpdb->get_results($existingQuery, ARRAY_A);
 
        foreach ($existingRows as $row) {
            $existing[$row[$idColumn] . '_' . $row['meta_key']] = true;
        }
 
        // Separate inserts and updates
        foreach ($inserts as $row) {
            $key = $row['id'] . '_' . $row['key'];
            if (isset($existing[$key])) {
                $toUpdate[] = $row;
            } else {
                $toInsert[] = $row;
            }
        }
 
        // Batch insert new records
        if (!empty($toInsert)) {
            $values = [];
            $placeholders = [];
 
            foreach ($toInsert as $row) {
                $placeholders[] = "(%d, %s, %s)";
                $values[] = $row['id'];
                $values[] = $row['key'];
                $values[] = $row['value'];
            }
 
            $sql = "INSERT INTO {$table} ({$idColumn}, meta_key, meta_value) VALUES " . implode(', ', $placeholders);
            $wpdb->query($wpdb->prepare($sql, $values));
        }
 
        // Batch update existing records
        if (!empty($toUpdate)) {
            foreach ($toUpdate as $row) {
                $wpdb->update(
                    $table,
                    ['meta_value' => $row['value']],
                    [$idColumn => $row['id'], 'meta_key' => $row['key']],
                    ['%s'],
                    ['%d', '%s']
                );
            }
        }
    }
 
    /**
     * Batch update post timestamps
     */
    protected static function batchTouchPosts(array $postIds): void
    {
        global $wpdb;
 
        if (empty($postIds)) {
            return;
        }
 
        $now = current_time('mysql');
        $nowGmt = current_time('mysql', true);
        $ids = implode(',', array_map('intval', $postIds));
 
        $wpdb->query(
            "UPDATE {$wpdb->posts}
             SET post_modified = '{$now}', post_modified_gmt = '{$nowGmt}'
             WHERE ID IN ({$ids})"
        );
    }
 
    /**
     * Batch update taxonomy relationships
     */
    protected static function batchUpdateTaxonomies(array $updates): void
    {
        foreach ($updates as $update) {
            $termIds = empty(trim((string)$update['value']))
                ? []
                : array_map('intval', array_filter(explode(',', $update['value'])));
 
            wp_set_object_terms($update['object_id'], $termIds, $update['taxonomy'], false);
        }
    }
 
    /**
     * Batch update WordPress default fields
     */
    protected static function batchUpdateWpDefaults(string $objectType, array $updates): void
    {
        foreach ($updates as $id => $fields) {
            // Handle post_thumbnail separately
            if (isset($fields['post_thumbnail'])) {
                set_post_thumbnail($id, $fields['post_thumbnail']);
                unset($fields['post_thumbnail']);
            }
            if (isset($fields['post_thumbnail'])) {
                if (empty($fields['post_thumbnail'])) {
                    delete_post_thumbnail($id);
                } else {
                    set_post_thumbnail($id, $fields['post_thumbnail']);
                }
                unset($fields['post_thumbnail']);
            }
            if (isset($fields['featured_image'])) {
                if (empty($fields['featured_image'])) {
                    delete_post_thumbnail($id);
                } else {
                    set_post_thumbnail($id, $fields['featured_image']);
                }
                unset($fields['featured_image']);
            }
 
            if (empty($fields)) {
                continue;
            }
 
            // Handle post_date conversion
            if (isset($fields['post_date'])) {
                $datetime = strtotime($fields['post_date']);
                if ($datetime !== false) {
                    $fields['post_date'] = date('Y-m-d H:i:s', $datetime);
                    $fields['post_date_gmt'] = get_gmt_from_date($fields['post_date']);
                    $fields['edit_date'] = true;
                }
            }
 
            match ($objectType) {
                'post' => wp_update_post(array_merge(['ID' => $id], $fields)),
                'user' => wp_update_user(array_merge(['ID' => $id], $fields)),
                'term' => null, // Terms need taxonomy, handled separately
                default => null
            };
        }
    }
 
    /**
     * Batch update options
     */
    protected static function batchUpdateOptions(array $updates): void
    {
        foreach ($updates as $update) {
            update_option($update['key'], $update['value']);
        }
    }
}