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
<?php
namespace JVBase\registry;
 
use JVBase\meta\MetaManager;
use JVBase\meta\MetaRegistry;
if (!defined('ABSPATH')) {
    exit;
}
class TaxonomyRegistrar
{
    private string $slug;
    private array $config;
    private array $fields;
    private string $taxonomy;
    private FieldRegistry $fieldRegistry;
 
    public function __construct(string $slug, array $config)
    {
        $this->slug = $slug;
        $this->config = $config;
        $this->taxonomy = BASE . $slug;
 
        $this->fieldRegistry = FieldRegistry::getInstance();
        $this->fields = $this->fieldRegistry->getFields($slug, 'term');
 
        // Set up custom table hooks if this is a content taxonomy
        if ($this->config['is_content'] ?? false) {
            $this->setupContentTaxonomyHooks();
        }
    }
 
    public function register(): void
    {
        $singular = $this->config['singular'] ?? ucfirst($this->slug);
        $plural = $this->config['plural'] ?? $singular . 's';
 
        $args = [
            'labels' => $this->buildLabels($singular, $plural),
            'hierarchical' => $this->config['hierarchical'] ?? true,
            'public' => $this->config['public'] ?? true,
            'show_ui' => $this->config['show_ui'] ?? true,
            'show_admin_column' => $this->config['show_admin_column'] ?? true,
            'show_in_rest' => $this->config['show_in_rest'] ?? true,
            'rewrite' => $this->config['rewrite'] ?? [
                    'slug' => $this->slug,
                    'with_front' => false
                ]
        ];
 
        $post_types = array_map(fn($type) => BASE . $type, $this->config['for_content'] ?? []);
        register_taxonomy($this->taxonomy, $post_types, $args);
 
        $this->maybeAddRewriteRule($args['rewrite']);
        if (!empty($this->fields)) {
            $meta_registry = new MetaRegistry($this->fields, $this->slug, 'term');
            $meta_registry->registerMetaFields();
        }
    }
 
    /**
     * Add custom rewrite rule for hierarchical taxonomy slugs (e.g., post-type/by/taxonomy-slug)
     */
    protected function maybeAddRewriteRule(array $rewrite_config): void
    {
        $slug = $rewrite_config['slug'] ?? $this->slug;
 
        // Only add custom rule if slug contains slashes (hierarchical path)
        if (!str_contains($slug, '/')) {
            return;
        }
 
        add_rewrite_rule(
            "^{$slug}/([^/]+)/?$",
            'index.php?' . $this->taxonomy . '=$matches[1]',
            'top'
        );
    }
 
    private function buildLabels(string $singular, string $plural): array
    {
        return [
            'name' => $plural,
            'singular_name' => $singular,
            'search_items' => "Search {$plural}",
            'all_items' => "All {$plural}",
            'edit_item' => "Edit {$singular}",
            'update_item' => "Update {$singular}",
            'add_new_item' => "Add New {$singular}",
            'new_item_name' => "New {$singular} Name",
            'menu_name' => $singular
        ];
    }
 
    /**
     * Set up hooks for content taxonomies to maintain custom table data
     */
    protected function setupContentTaxonomyHooks(): void
    {
        // Hook into term creation/updates
        add_action('created_term', [$this, 'handleTermSave'], 10, 3);
        add_action('edited_term', [$this, 'handleTermSave'], 10, 3);
 
        // Hook into term meta changes
        add_action('added_term_meta', [$this, 'handleTermMetaChange'], 10, 4);
        add_action('updated_term_meta', [$this, 'handleTermMetaChange'], 10, 4);
        add_action('deleted_term_meta', [$this, 'handleTermMetaChange'], 10, 4);
        add_action('add_term_relationship', [$this, 'addUserToContentType'], 10, 3);
        add_action('delete_term_relationships', [$this, 'removeUserFromContentType'], 10, 3);
    }
 
    public function addUserToContentType($postID, $termIDs, $taxonomy):void
    {
        if ($taxonomy !== $this->taxonomy) {
            return;
        }
 
        if (empty(jvbGetUserContentTypes($postID))) {
            return;
        }
 
        $userID = get_post_meta($postID, BASE . 'link', true);
        if (!is_numeric($userID)) {
            return;
        }
 
        foreach ($termIDs as $termID) {
            $users = get_term_meta($termID, BASE.'users', true);
            $users = ($users === '') ? [] : explode(',', $users);
            $users[] = $userID;
            update_term_meta($termID, BASE.'users', implode(',', $users));
        }
    }
 
 
    public function removeUserFromContentType($postID, $termIDs, $taxonomy):void
    {
        if ($taxonomy !== $this->taxonomy) {
            return;
        }
 
        if (empty(jvbGetUserContentTypes($postID))) {
            return;
        }
 
        $userID = get_post_meta($postID, BASE . 'link', true);
        if (!is_numeric($userID)) {
            return;
        }
 
        foreach ($termIDs as $termID) {
            $users = get_term_meta($termID, BASE.'users', true);
            $users = ($users === '') ? [] : explode(',', $users);
            unset($users[array_search($userID, $users)]);
            update_term_meta($termID, BASE.'users', implode(',', $users));
        }
    }
 
    /**
     * Handle term save (creation and updates)
     * @param int $term_id The term ID
     * @param int $taxonomy_id The taxonomy term ID
     * @param string $taxonomy The taxonomy name
     */
    public function handleTermSave(int $term_id, int $taxonomy_id, string $taxonomy): void
    {
        if ($taxonomy !== $this->taxonomy) {
            return;
        }
 
        $this->syncTermToCustomTable($term_id);
    }
 
    /**
     * Handle term meta changes
     * @param int $meta_id The meta ID
     * @param int $term_id The term ID
     * @param string $meta_key The meta key
     * @param mixed $meta_value The meta value
     */
    public function handleTermMetaChange(int $meta_id, int $term_id, string $meta_key, $meta_value): void
    {
        // Only handle meta for our taxonomy
        $term = get_term($term_id);
        if (!$term || is_wp_error($term) || $term->taxonomy !== $this->taxonomy) {
            return;
        }
 
        $this->syncTermToCustomTable($term_id);
    }
 
    /**
     * Sync a single term to the custom table
     * @param int $term_id The term ID to sync
     */
    public function syncTermToCustomTable(int $term_id): void
    {
        global $wpdb;
 
        $table = $wpdb->prefix . BASE . 'content_' . $this->slug;
        $term = get_term($term_id, $this->taxonomy);
 
        if (!$term || is_wp_error($term)) {
            return;
        }
 
        // Get custom table fields
        $custom_fields = $this->getCustomTableFields();
        if (empty($custom_fields)) {
            return;
        }
 
        // Prepare data for insertion/update
        $data = [
            'term_id' => $term_id,
            'name' => html_entity_decode($term->name),
            'slug' => $term->slug,
            'updated_at' => current_time('mysql')
        ];
 
        // Get meta manager for this term
        $meta = new MetaManager($term_id, 'term');
        $values = $meta->getAll(array_keys($custom_fields));
 
        // Process each custom field
        foreach ($custom_fields as $field_name => $field_config) {
            $value = $values[$field_name];
            // Handle different field types
            $data = $this->processFieldForCustomTable($data, $field_name, $value, $field_config);
        }
 
        foreach ($data as $k => $v) {
            switch ($v) {
                case 0:
                case '':
                case false:
                    $data[$k] = null;
                    break;
            }
        }
 
        // Check if record exists
        $exists = $wpdb->get_var($wpdb->prepare(
            "SELECT COUNT(*) FROM {$table} WHERE term_id = %d",
            $term_id
        ));
 
        if ($exists) {
            // Update existing record
            $wpdb->update($table, $data, ['term_id' => $term_id]);
        } else {
            // Insert new record
            $wpdb->insert($table, $data);
        }
    }
 
    /**
     * Process a field value for storage in custom table
     * @param array $data Current data array
     * @param string $field_name Field name
     * @param mixed $value Field value
     * @param array $field_config Field configuration
     * @return array Updated data array
     */
    protected function processFieldForCustomTable(array $data, string $field_name, $value, array $field_config): array
    {
        $field_type = $field_config['type'] ?? 'text';
 
        switch ($field_type) {
            case 'location':
                // Handle location fields - they create multiple columns
                if (is_array($value)) {
                    $data["{$field_name}_address"] = $value['address'] ?? '';
                    $data["{$field_name}_lat"] = (float)($value['lat'] ?? 0);
                    $data["{$field_name}_lng"] = (float)($value['lng'] ?? 0);
                    $data["{$field_name}_street"] = $value['street'] ?? '';
                    $data["{$field_name}_city"] = $value['city'] ?? '';
                    $data["{$field_name}_province"] = $value['province'] ?? '';
                    $data["{$field_name}_postal_code"] = $value['postal_code'] ?? '';
                    $data["{$field_name}_country"] = $value['country'] ?? '';
                }
                break;
 
            case 'taxonomy':
                // Handle taxonomy references
                $limit = $field_config['limit'] ?? null;
                if ($limit === 1) {
                    // Single selection - store as integer
                    $data[$field_name] = is_array($value) ? (int)($value[0] ?? 0) : (int)$value;
                } else {
                    // Multiple selections - store as JSON
                    $data[$field_name] = is_array($value) ? json_encode(array_map('intval', $value)) : $value;
                }
                break;
 
            case 'user':
                // Handle user references
                $limit = $field_config['limit'] ?? null;
                if ($limit === 1) {
                    $data[$field_name] = is_array($value) ? (int)($value[0] ?? 0) : (int)$value;
                } else {
                    $data[$field_name] = is_array($value) ? json_encode(array_map('intval', $value)) : (int)$value;
                }
                break;
 
            case 'repeater':
            case 'gallery':
            case 'set':
            case 'checkbox':
                // Store complex data as JSON
                $data[$field_name] = is_array($value) ? json_encode($value) : $value;
                break;
 
            case 'image':
            case 'file':
                // Store attachment ID
                $data[$field_name] = (int)$value;
                break;
 
            case 'number':
                $data[$field_name] = (int)$value;
                break;
 
            case 'true_false':
                $data[$field_name] = (bool)$value;
                break;
 
            default:
                // Store as string for text, textarea, email, url, etc.
                $data[$field_name] = (string)$value;
                break;
        }
 
        return $data;
    }
 
    /**
     * Get custom table fields for this taxonomy
     * @return array Field definitions
     */
    protected function getCustomTableFields():array
    {
        return jvbContentTaxonomiesTableFields($this->slug)['fields'] ?? [];
    }
}