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
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
<?php
namespace JVBase\admin;
 
if (!defined('ABSPATH')) {
    exit;
}
 
use JVBase\registrar\Registrar;
use WP_Error;
use Exception;
 
/**
 * Admin interface for managing content taxonomy tables
 * Add this to your AdminPages.php or create as a separate manager
 */
class ContentTaxonomy
{
    public function __construct()
    {
        if (empty(Registrar::getFeatured('is_content', 'term'))){
            return;
        }
        add_filter(BASE.'handle_bulk_operation', [ $this, 'processOperation' ], 10, 3);
        add_filter('jvbAdminSubpages', [$this, 'addSubpage'], 10, 1);
        add_action('admin_init', [$this, 'handleActions']);
    }
 
    public function addSubpage(array $subpages):array
    {
        $subpages[] = [
            'page_title' => 'Content Taxonomy Tables',           // page_title
            'menu_title' => 'Taxonomy Tables',           // menu_title
            'capability' => 'manage_options',         // capability
            'menu_slug'  => 'jvb-taxonomy-table',           // menu_slug (will become BASE.'integrations')
            'callback'   => [$this, 'renderAdminPage'],    // callback
        ];
 
        return $subpages;
    }
 
    /**
     * Handle admin actions
     */
    public function handleActions(): void
    {
        if (!current_user_can('manage_options')) {
            return;
        }
 
        // Handle individual taxonomy rebuild
        if (isset($_POST['rebuild_taxonomy']) && wp_verify_nonce($_POST['_wpnonce'], 'rebuild_taxonomy')) {
            $taxonomy = sanitize_text_field($_POST['taxonomy']);
            $registrar = Registrar::getInstance($taxonomy);
            if ($registrar && $registrar->hasFeature('is_content')) {
                $results = $this->rebuildCustomTable($taxonomy);
                // Store results in transient to display after redirect
                set_transient('jvb_rebuild_results_' . $taxonomy, $results, 300); // 5 minutes
 
 
                wp_redirect(add_query_arg('rebuilt', $taxonomy, wp_get_referer()));
                exit;
            }
            // Display results if available
            $results = get_transient('jvb_rebuild_results_' . $this->slug);
            if ($results) {
                delete_transient('jvb_rebuild_results_' . $this->slug);
                add_action('admin_notices', function() use ($results) {
                    $class = $results['errors'] > 0 ? 'notice-warning' : 'notice-success';
                    echo '<div class="notice ' . $class . ' is-dismissible">';
                    echo '<p><strong>Taxonomy Rebuild Results:</strong></p>';
                    echo '<ul>';
                    foreach ($results['messages'] as $message) {
                        echo '<li>' . esc_html($message) . '</li>';
                    }
                    echo '</ul>';
                    echo '</div>';
                });
            }
        }
 
        // Handle rebuild all
        if (isset($_POST['rebuild_all']) && wp_verify_nonce($_POST['_wpnonce'], 'rebuild_all')) {
            $results = $this->rebuildAllContentTaxonomies();
 
            $this->storeResults('rebuild_all', $results);
            wp_redirect(add_query_arg('rebuilt', 'all', wp_get_referer()));
            exit;
        }
    }
 
    /**
     * Store results in transient for display
     */
    protected function storeResults(string $key, array $results): void
    {
        set_transient("jvb_admin_results_{$key}", $results, 300);
    }
 
    /**
     * Render admin page
     */
    public function renderAdminPage(): void
    {
        // Display any stored results
        $this->displayResults();
 
        ?>
        <div class="wrap">
            <h1>Content Tables Management</h1>
 
            <div class="card">
                <h2>Content Taxonomies</h2>
                <p>These taxonomies have custom content tables that can be rebuilt from existing term data.</p>
 
                <table class="widefat fixed striped">
                    <thead>
                    <tr>
                        <th>Taxonomy</th>
                        <th>Table Status</th>
                        <th>Term Count</th>
                        <th>Table Count</th>
                        <th>Actions</th>
                    </tr>
                    </thead>
                    <tbody>
                    <?php
                    $taxonomies = Registrar::getFeatured('is_content', 'term');
                    foreach ($taxonomies as $slug):
                        $registrar = Registrar::getInstance($slug);
                        $taxonomy = BASE . $slug;
                        $table_info = $this->getTableInfo($slug);
                        $term_count = wp_count_terms($taxonomy, ['hide_empty' => false]);
                        ?>
                        <tr>
                            <td>
                                <strong><?= esc_html($registrar->getPlural()) ?></strong><br>
                                <code><?= esc_html($taxonomy) ?></code>
                            </td>
                            <td>
                                <?php if ($table_info['exists']): ?>
                                    <span class="dashicons dashicons-yes-alt" style="color: green;"></span>
                                    Table exists
                                <?php else: ?>
                                    <span class="dashicons dashicons-warning" style="color: orange;"></span>
                                    Table missing
                                <?php endif; ?>
                            </td>
                            <td><?= esc_html($term_count) ?> terms</td>
                            <td>
                                <?php if ($table_info['exists']): ?>
                                    <?= esc_html($table_info['count']) ?> records
                                    <?php if ($term_count != $table_info['count']): ?>
                                        <span class="dashicons dashicons-warning" style="color: orange;" title="Counts don't match"></span>
                                    <?php endif; ?>
                                <?php else: ?>
                                    N/A
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ($table_info['exists']): ?>
                                    <form method="post" style="display: inline;">
                                        <?php wp_nonce_field('rebuild_taxonomy'); ?>
                                        <input type="hidden" name="taxonomy" value="<?= esc_attr($slug) ?>">
                                        <button type="submit" name="rebuild_taxonomy" class="button button-secondary"
                                                onclick="return confirm('Are you sure you want to rebuild this table? This will replace all data.')">
                                            Rebuild Table
                                        </button>
                                    </form>
                                <?php else: ?>
                                    <span class="description">Create table first via plugin activation</span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
 
                <p class="submit">
                <form method="post" style="display: inline;">
                    <?php wp_nonce_field('rebuild_all'); ?>
                    <button type="submit" name="rebuild_all" class="button button-primary"
                            onclick="return confirm('Are you sure you want to rebuild ALL content tables? This may take a while.')">
                        Rebuild All Tables
                    </button>
                </form>
                </p>
            </div>
 
            <div class="card">
                <h2>Table Information</h2>
                <p>Content tables store frequently accessed data from taxonomies for improved performance.</p>
 
                <h3>When to Rebuild:</h3>
                <ul>
                    <li>After importing terms via CSV or other bulk methods</li>
                    <li>If you notice discrepancies between term counts and table counts</li>
                    <li>After modifying custom table field definitions</li>
                    <li>If custom table data appears to be corrupted or missing</li>
                </ul>
 
                <h3>WP-CLI Commands:</h3>
                <code>wp jvb rebuild-content-tables</code> - Rebuild all content tables<br>
                <code>wp jvb rebuild-content-tables &lt;taxonomy&gt;</code> - Rebuild specific taxonomy table
            </div>
        </div>
        <?php
    }
 
    /**
     * Get table information for a taxonomy
     */
    protected function getTableInfo(string $slug): array
    {
        global $wpdb;
 
        $table = $wpdb->prefix . BASE . 'content_' . $slug;
 
        $exists = $wpdb->get_var("SHOW TABLES LIKE '{$table}'") === $table;
        $count = 0;
 
        if ($exists) {
            $count = (int) $wpdb->get_var("SELECT COUNT(*) FROM {$table}");
        }
 
        return [
            'exists' => $exists,
            'count' => $count,
            'table' => $table
        ];
    }
 
    /**
     * Display results from previous actions
     */
    protected function displayResults(): void
    {
        // Check for rebuild completion
        if (isset($_GET['rebuilt'])) {
            $rebuilt = sanitize_text_field($_GET['rebuilt']);
 
            if ($rebuilt === 'all') {
                $results = get_transient('jvb_admin_results_rebuild_all');
                if ($results) {
                    delete_transient('jvb_admin_results_rebuild_all');
                    $this->displayAllRebuildResults($results);
                }
            } else {
                $results = get_transient("jvb_admin_results_rebuild_{$rebuilt}");
                if ($results) {
                    delete_transient("jvb_admin_results_rebuild_{$rebuilt}");
                    $this->displaySingleRebuildResults($rebuilt, $results);
                }
            }
        }
    }
 
    /**
     * Display results for single taxonomy rebuild
     */
    protected function displaySingleRebuildResults(string $taxonomy, array $results): void
    {
        $class = $results['errors'] > 0 ? 'notice-warning' : 'notice-success';
        ?>
        <div class="notice <?= $class ?> is-dismissible">
            <h3>Rebuild Results: <?= esc_html(ucfirst($taxonomy)) ?></h3>
            <ul>
                <?php foreach ($results['messages'] as $message): ?>
                    <li><?= esc_html($message) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
        <?php
    }
 
    /**
     * Display results for all taxonomies rebuild
     */
    protected function displayAllRebuildResults(array $results): void
    {
        $class = $results['failed_rebuilds'] > 0 ? 'notice-warning' : 'notice-success';
        ?>
        <div class="notice <?= $class ?> is-dismissible">
            <h3>Rebuild All Results</h3>
            <p>
                <strong>Summary:</strong>
                <?= $results['total_taxonomies'] ?> taxonomies processed,
                <?= $results['successful_rebuilds'] ?> successful,
                <?= $results['failed_rebuilds'] ?> failed
            </p>
 
            <?php foreach ($results['details'] as $taxonomy => $detail): ?>
                <details>
                    <summary class="row btw">
                        <strong><?= esc_html(ucfirst($taxonomy)) ?></strong>
                        <?php if ($detail['errors'] === 0): ?>
                            <span style="color: green;">✓ Success</span>
                        <?php else: ?>
                            <span style="color: orange;">⚠ <?= $detail['errors'] ?> errors</span>
                        <?php endif; ?>
                    </summary>
                    <ul style="margin-left: 20px;">
                        <?php foreach ($detail['messages'] as $message): ?>
                            <li><?= esc_html($message) ?></li>
                        <?php endforeach; ?>
                    </ul>
                </details>
            <?php endforeach; ?>
        </div>
        <?php
    }
 
    /**
     * Rebuild the entire custom table from existing terms
     * @return array Results with success/error counts
     */
    public function rebuildCustomTable(string $taxonomy):array
    {
        $results = [
            'success' => 0,
            'errors' => 0,
            'total' => 0,
            'messages' => []
        ];
 
        // Check if this is a content taxonomy
        $registrar = Registrar::getInstance($taxonomy);
        if (!$registrar->hasFeature('is_content')) {
            $results['messages'][] = "Taxonomy {$taxonomy} is not a content taxonomy";
            return $results;
        }
 
        global $wpdb;
        $table = $wpdb->prefix . BASE . 'content_' . $taxonomy;
 
        // Check if table exists
        if ($wpdb->get_var("SHOW TABLES LIKE '{$table}'") !== $table) {
            $results['messages'][] = "Custom table {$table} does not exist";
            return $results;
        }
 
        // Clear existing data
        $wpdb->query("TRUNCATE TABLE {$table}");
        $results['messages'][] = "Cleared existing data from {$table}";
 
        // Get all terms for this taxonomy
        $terms = get_terms([
            'taxonomy'  => jvbCheckBase($taxonomy),
            'hide_empty'=> false,
            'number'    => 0, // Get all terms
            'fields'    => 'ids'
        ]);
 
        if (is_wp_error($terms)) {
            $results['messages'][] = "Error fetching terms: " . $terms->get_error_message();
            return $results;
        }
 
        $results['total'] = count($terms);
        $results['messages'][] = "Found {$results['total']} terms to process";
 
        JVB()->queue()->queueOperation(
            'sync_content_taxonomy_tables',
            0,
            [
                'terms' => $terms,
                'taxonomy' => $taxonomy
            ],
            [
                'chunk_key' => 'terms',
                'chunk_size'        => 10,
            ]
        );
        $results['messages'][] = 'Added to queue!';
 
        return $results;
    }
 
    public function processOperation(WP_Error $result, object $operation, array $data):WP_Error|array
    {
        switch ($operation->type) {
            case 'sync_content_taxonomy_tables':
                return $this->processContentTaxonomyTableSync($operation, $data);
            default:
                return $result;
        }
    }
 
    public function processContentTaxonomyTableSync(object $operation, array $data):array
    {
        $taxonomy = $data['taxonomy'];
        $registrar = new TaxonomyRegistrar($taxonomy, JVB_TAXONOMY[$taxonomy]);
        $results = ['errors' => 0, 'success' => 0];
        foreach ($data['terms'] as $term) {
            try {
                $registrar->syncTermToCustomTable($term);
                $results['success']++;
            } catch (Exception $e) {
                $results['errors']++;
                $results['messages'][] = "Error processing term {$term->term_id} ({$term->name}): " . $e->getMessage();
            }
        }
 
        $results['messages'][] = "Rebuild complete: {$results['success']} successful, {$results['errors']} errors";
 
        return [
            'success'   => true,
            'result'    => $results
        ];
    }
}
new ContentTaxonomy();