Jake Vanderwerf
2025-09-30 2cb91676044ecd0abd9c45b4835abb8b0d042312
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
<?php
namespace JVBase\forms;
 
use JVBase\managers\CacheManager;
use WP_REST_Request;
use WP_REST_Response;
use WP_Term;
use WP_Query;
 
if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
 
/**
 * Single Modal Taxonomy Selector
 *
 * Complete replacement for individual taxonomy selector modals.
 * Uses one shared modal instance with intelligent prefetching.
 *
 * @package TaxonomySelector
 * @version 2.0.0
 */
 
class TaxonomySelector {
    /**
     * Track if any taxonomy selectors are present on the page
     */
    private static $hasSelectors = false;
 
    protected string $id;
    protected string $name;
    protected string $singular;
    protected string $plural;
    protected string $taxonomy;
    protected string $base;
    protected array $config;
 
    public function __construct(string $id, string $taxonomy, array $config = []) {
        $this->id = sanitize_key($id);
        $this->taxonomy = jvbCheckBase($taxonomy);
        $this->name = jvbNoBase($taxonomy);
        $this->base = $config['base']??'';
 
        $this->config = wp_parse_args($config, [
            'types'     => false, //for feed block implementation
            'max'       => 0,
            'search'    => true,
            'createNew' => false,
            'required'  => false,
            'hidden'    => false,
            'update'    => false,
        ]);
 
        $this->plural = JVB_TAXONOMY[$taxonomy]['plural'];
        $this->singular = JVB_TAXONOMY[$taxonomy]['singular'];
 
 
    }
 
    /**
     * Mark that selectors are present (called when rendering toggles)
     */
    public static function markSelectorsPresent(): void {
        self::$hasSelectors = true;
    }
 
    /**
     * Get the full path for a term (for hierarchical taxonomies)
     *
     * @param WP_Term $term The term object
     * @return string The full term path
     */
    public static function getTermPath($term): string {
        if (!$term || is_wp_error($term)) {
            return '';
        }
 
        if (!is_taxonomy_hierarchical($term->taxonomy)) {
            return $term->name;
        }
 
        $path = [];
        $currentTerm = $term;
 
        while ($currentTerm) {
            array_unshift($path, $currentTerm->name);
 
            if ($currentTerm->parent) {
                $currentTerm = get_term($currentTerm->parent);
                if (is_wp_error($currentTerm)) {
                    break;
                }
            } else {
                break;
            }
        }
 
        return implode(' → ', $path);
    }
 
    /**
     * Output the single modal dialog in footer
     */
    public static function outputSelector(): void {
        echo self::getSingleModalHTML();
        remove_action('wp_footer', [self::class, 'outputSelector']);
    }
 
    /**
     * Get the single modal HTML structure
     */
    public static function getSingleModalHTML(): string {
        ob_start();
        ?>
        <dialog id="jvb-selector" aria-labelledby="modal-title" aria-modal="true">
            <div class="wrap col">
                <header class="modal-header">
                    <h3 id="modal-title">Select Taxonomy</h3>
                </header>
 
                <div class="selected-items-section">
                    <div class="selected-items row" role="region" aria-label="Selected items">
                        <!-- Selected items will be populated here -->
                    </div>
                </div>
 
                <div class="items-wrap">
                    <!-- Common/Favorite terms section -->
                    <details class="favourite-terms" hidden>
                        <summary class="title row btw">Your Go Tos:</summary>
                        <ul class="favourite-list row btw"></ul>
                    </details>
 
                    <!-- Pagination info -->
                    <p class="pagination-info" hidden></p>
 
                    <!-- Navigation breadcrumbs -->
                    <nav class="term-navigation row" aria-label="Term navigation">
                        <button type="button" class="back-to-parent" hidden>
                            <span aria-hidden="true">←</span> Back
                        </button>
                    </nav>
 
                    <!-- Terms list -->
                    <ul class="items-container col start" role="listbox" aria-label="Available terms">
                        <!-- Terms will be populated here -->
                    </ul>
 
                    <!-- Loading indicator -->
                    <p class="loading" hidden aria-live="polite">
                        <span>loading items</span>
                    </p>
 
                    <!-- Infinite scroll sentinel -->
                    <div class="scroll-sentinel" aria-hidden="true"></div>
                </div>
 
                <!-- Search section -->
                <div class="search-wrapper">
                    <div class="search-bar">
                        <?= jvbSearch('Search terms') ?>
                    </div>
                </div>
 
                <!-- Create new term section -->
                <details class="create-new-term" hidden>
                    <summary class="row btw">Add New Term</summary>
                    <div class="create-new-term-section">
                        <form class="create-term-form" data-nocache data-form-id="create-term" data-save="terms">
                            <div class="form-group">
                                <label for="term_name">Term Name:</label>
                                <input type="text" name="term_name" id="term_name" required>
                            </div>
 
                            <div class="form-group">
                                <label for="select_parent">Nest it under:</label>
                                <select name="parent" id="select_parent">
                                    <option value="0">None (Top Level)</option>
                                </select>
                            </div>
 
                            <button type="button" class="submit-term">Add Term</button>
                        </form>
 
                        <div class="loading-message create-term" hidden>
                            <span id="typed-text"></span>
                            <span class="cursor">|</span>
                        </div>
                    </div>
                </details>
                <?= jvbModalActions(); ?>
            </div>
        </dialog>
        <template class="loadingItems">
            <p>{ <span>loading items</span> }</p>
        </template>
        <template class="noResults">
            <p>{ <span>nothing found</span> }</p>
        </template>
        <template class="termListItem">
            <li>
                <input type ="checkbox">
                <label>
                    <span class="term-name"></span>
                </label>
            </li>
        </template>
        <template class="termChildrenToggle">
            <button type="button" class="toggle-children" aria-expanded="false">
                <?=jvbIcon('add')?>
            </button>
        </template>
        <template class="selectedTerm">
            <div class="selected-item row">
                <span class="item-name"></span>
                <button type="button" class="remove-item row"><?=jvbIcon('close')?></button>
            </div>
        </template>
        <template class="termBreadcrumb">
            <button type="button" class="path-level"></button>
        </template>
        <?php
        return ob_get_clean();
    }
 
    public function render(array $selected =[], string $extra = ''):string
    {
        // Mark that selectors are present for footer output
        self::markSelectorsPresent();
 
        $update = ($this->config['update']) ? '' : ' data-update="'.$this->config['update'].'"';
        $max = ($this->config['max'] === 0) ? '' : ' data-max="'.$this->config['max'];
        $search = ($this->config['search']) ? ' data-search' : '';
        $creatable = ($this->config['createNew']) ? ' data-creatable' : '';
        $required = ($this->config['required']) ? ' data-required' : '';
        $hidden = ($this->config['hidden']) ? ' hidden' : '';
        $for = ($this->config['types']) ? ' data-for="'.implode(',',$this->config['types']) : '';
        $dataSelected = ' data-selected="'.implode(',',$selected).'"';
        ob_start();
        ?>
 
        <div class="jvb-selector <?= $this->name ?>"
             id="<?= $this->id ?>"<?=$hidden?>>
            <button type="button"
                    class="filter-toggle row taxonomy-toggle"
                    data-taxonomy="<?=$this->name?>"
                    data-single="<?=$this->singular?>"
                    data-plural="<?=$this->plural?>"
                    <?= $max.$search.$creatable.$required.$for.$dataSelected?>
                    aria-label="Select <?= esc_attr($this->plural) ?>">
                <span class="button-text">Select <?= esc_html($this->plural) ?></span>
                <?= jvbIcon('add') ?>
            </button>
            <div class="selected-items row" role="region" aria-label="Selected <?=$this->plural?>">
                <?php if (!empty($selected)): ?>
                    <?php foreach ($selected as $termId ):
                        $term  = get_term($termId, $this->taxonomy);
                        $termData = [
                            'name'  => $term->name,
                            'path'  => $this->getTermPath($term)
                        ]; ?>
                        <div class="selected-item row" data-id="<?= esc_attr($termId) ?>">
                            <span><?= esc_html(is_array($termData) ? ($termData['path'] ?? $termData['name']) : $termData) ?></span>
                            <button type="button" class="remove-item row" aria-label="Remove term">×</button>
                        </div>
                    <?php endforeach; ?>
                <?php endif; ?>
            </div>
            <?= $extra ?>
        </div>
 
        <?php
        return ob_get_clean();
    }
}