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
<?php
namespace JVBase\forms;
 
use JVBase\forms\TaxonomySelector;
use JVBase\JVBIcons;
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
}
class PostSelectorOld extends TaxonomySelector
{
    protected string $post_type;
 
    public function __construct(int $id, string $post_type, array $config = [])
    {
        $this->post_type = $post_type;
        parent::__construct($id, '', $config); // Empty taxonomy as we're using post type
    }
 
    protected function getAvailableTerms():array
    {
        $args = [
            'post_type' => $this->post_type,
            'posts_per_page' => 20, // Initial load amount
            'orderby' => 'title',
            'order' => 'ASC',
            'fields' => 'id=>name'
        ];
 
        // Add shop exclusion if shop_id is set
        if (!empty($this->config['shop_id'])) {
            $args['tax_query'] = [[
                'taxonomy' => BASE.'shop',
                'terms' => $this->config['shop_id'],
                'operator' => 'NOT IN'
            ]];
        }
 
        $posts = get_posts($args);
        return is_wp_error($posts) ? [] : array_combine(
            array_map(function ($post) {
                return $post->ID;
            }, $posts),
            array_map(function ($post) {
                return $post->post_title;
            }, $posts)
        );
    }
 
    public function render(array $selected = []):string
    {
        $wrapper_classes = $this->getWrapperClasses();
 
        ob_start();
        ?>
        <div class="<?= esc_attr($wrapper_classes); ?>"
             id="<?= esc_attr($this->id); ?>"
             data-post-type="<?= esc_attr($this->post_type); ?>"
             data-config='<?= esc_attr(wp_json_encode($this->getFrontendConfig())); ?>'>
 
            <div class="selector-wrapper">
                <?php $this->renderSelectedItems($selected); ?>
 
                <dialog class="selector-modal">
                    <div class="wrap col">
                        <header class="modal-header">
                            <h3><?= esc_html($this->config['title'] ?? 'Search Artists'); ?></h3>
                            <button type="button" class="cancel" aria-label="Close">×</button>
                        </header>
 
 
                        <div class="items-container">
                            <?php $this->renderSelectableItems(); ?>
                            <div class="scroll-sentinel"></div>
                        </div>
 
                        <?php if ($this->config['modal']) : ?>
                            <div class="pending-section" hidden>
                                <h4>Pending Approvals</h4>
                                <div class="pending-list" role="list"></div>
                            </div>
                        <?php endif; ?>
                    </div>
                    <?= jvbSearch() ?>
                </dialog>
            </div>
        </div>
        <?php
        return ob_get_clean();
    }
 
    protected function renderSelectedItems(array $selected = []):void
    {
        if (empty($selected)) {
            echo '<div class="selected-items"></div>';
            return;
        }
 
        echo '<div class="selected-items">';
        foreach ($selected as $id => $title) {
            printf(
                '<div class="selected-item" data-id="%s">
                    <span class="item-name">%s</span>
                    <button type="button"
                            class="remove-item"
                            aria-label="Remove %s">×</button>
                </div>',
                esc_attr($id),
                esc_html($title),
                esc_attr($title)
            );
        }
        echo '</div>';
    }
 
    public function renderSelectableItems():void
    {
        $posts = $this->getAvailableTerms();
 
        echo '<div class="wrap"><ul class="flat items">';
        foreach ($posts as $ID => $name) {
            echo '<li>';
            ?>
            <input id="<?= $this->id.'-'.esc_attr($ID); ?>"
                   type="<?= $this->config['multiple'] ? 'checkbox' : 'radio'; ?>"
                   name="<?= esc_attr($this->id); ?>"
                   value="<?= esc_attr($ID); ?>">
            <label class="selectable-item" for="<?= $this->id.'-'.esc_attr($ID); ?>">
                <?= esc_html($name); ?>
            </label>
            <?php
            echo '</li>';
        }
 
        echo '</ul></div>';
    }
 
    public function handleArtistSearch(WP_REST_Request $request):WP_REST_Response
    {
        $query = sanitize_text_field($request->get_param('query'));
        $page = (int)$request->get_param('page') ?: 1;
        $per_page = 30;
 
        $args = [
            'post_type' => BASE.'artist',
            'posts_per_page' => $per_page,
            'paged' => $page,
            'orderby' => 'title',
            'order' => 'ASC',
            's' => $query
        ];
 
        // Add shop exclusion if shop_id is set
        if (!empty($this->config['shop_id'])) {
            $args['tax_query'] = [[
                'taxonomy' => BASE.'shop',
                'terms' => $this->config['shop_id'],
                'operator' => 'NOT IN'
            ]];
        }
 
        $key = $this->cache->generateKey($args);
        $cache = $this->cache->get($key);
        if ($cache) {
            return new WP_REST_Response($cache);
        }
 
        $posts = new WP_Query($args);
        $results = [];
 
        foreach ($posts->posts as $post) {
            $city_terms = wp_get_object_terms($post->ID, BASE.'city');
            $city = !empty($city_terms) ? $city_terms[0]->name : '';
 
            $results[] = [
                'id' => $post->ID,
                'title' => $post->post_title,
                'thumbnail' => get_the_post_thumbnail_url($post->ID, 'thumbnail'),
                'city' => $city,
                'url' => get_permalink($post->ID)
            ];
        }
 
        $return = [
            'results' => $results,
            'hasMore' => $posts->max_num_pages > $page,
            'total' => $posts->found_posts
        ];
        $this->cache->set($key, $return);
        return new WP_REST_Response($return);
    }
 
    protected function getWrapperClasses():string
    {
        $classes = [
            'jvb-selector',
            'selector-' . $this->post_type,
            'post-selector'
        ];
 
        if ($this->config['multiple'] ?? false) {
            $classes[] = 'multiple';
        }
        if ($this->config['modal'] ?? false) {
            $classes[] = 'has-modal';
        }
 
        return implode(' ', $classes);
    }
}