Jake Vanderwerf
2026-05-12 c32ed859f4abd1591c882f4f2a6ee16b1ec275e2
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
// /**
//  * PostSelector Extension for TaxonomySelector
//  * Handles post selection using the same modal system as taxonomy selection
//  */
//
// // Extend the TaxonomySelector to handle post selection
// class PostSelectorExtension {
//  constructor(selector) {
//      this.selector = selector;
//      this.initPostHandling();
//  }
//
//  /**
//   * Initialize post-specific handling
//   */
//  initPostHandling() {
//      // Override specific methods for post handling
//      this.originalFetchTerms = this.selector.fetchTerms.bind(this.selector);
//      this.originalBuildRequest = this.selector.buildRequest.bind(this.selector);
//      this.originalCreateTermElement = this.selector.createTermElement.bind(this.selector);
//      this.originalUpdateModalForTaxonomy = this.selector.updateModalForTaxonomy.bind(this.selector);
//
//      // Replace methods with post-aware versions
//      this.selector.fetchTerms = this.fetchPosts.bind(this);
//      this.selector.buildRequest = this.buildPostRequest.bind(this);
//      this.selector.createTermElement = this.createPostElement.bind(this);
//      this.selector.updateModalForTaxonomy = this.updateModalForPosts.bind(this);
//  }
//
//  /**
//   * Check if current field is a post selector
//   */
//  isPostSelector() {
//      return this.selector.currentConfig?.taxonomy === 'post_selector';
//  }
//
//  /**
//   * Fetch posts instead of terms
//   */
//  async fetchPosts(fieldId, forceRefresh = false, isSearch = false) {
//      if (!this.isPostSelector()) {
//          return this.originalFetchTerms(fieldId, forceRefresh, isSearch);
//      }
//
//      if (this.selector.isLoading || !fieldId) return;
//
//      const field = this.selector.fields.get(fieldId);
//      if (!field) return;
//
//      try {
//          this.selector.showLoading();
//
//          const requestUrl = `${jvbSettings.api}posts/search?${this.buildPostRequest()}`;
//
//          const response = await this.selector.cache.fetchWithCache(requestUrl, {
//              method: 'GET',
//              headers: {
//                  'Content-Type': 'application/json',
//                  'X-WP-Nonce': window.auth.getNonce()
//              }
//          }, {
//              content: `posts_${this.selector.currentConfig.postType}`,
//              forceRefresh: forceRefresh
//          });
//
//          if (response && response.terms && response.terms.length !== 0) {
//              this.selector.hasMore = response.pagination?.has_more || false;
//              this.renderPosts(response.terms, this.selector.page > 1, isSearch);
//
//              if (this.selector.hasMore) {
//                  this.selector.nextPage();
//              }
//          } else {
//              if (this.selector.page === 1) {
//                  this.selector.showEmptyState('No posts found');
//              }
//              this.selector.hasMore = false;
//          }
//
//          return response.terms || [];
//      } catch (error) {
//          this.selector.handleError(error);
//          return [];
//      } finally {
//          this.selector.hideLoading();
//      }
//  }
//
//  /**
//   * Build request parameters for post search
//   */
//  buildPostRequest() {
//      if (!this.isPostSelector()) {
//          return this.originalBuildRequest();
//      }
//
//      let params = new URLSearchParams({
//          post_type: this.selector.currentConfig.postType || 'post',
//          search: this.selector.searchQuery,
//          page: this.selector.page
//      });
//
//      // Add shop_id if present (for excluding existing artists)
//      if (this.selector.currentConfig.shopId) {
//          params.append('shop_id', this.selector.currentConfig.shopId);
//      }
//
//      return params.toString();
//  }
//
//  /**
//   * Render posts list (similar to renderTerms but for posts)
//   */
//  renderPosts(posts, append = false, showPath = false) {
//      if (!append) {
//          window.removeChildren(this.selector.elements.termsList);
//      }
//
//      if (posts.length === 0) {
//          if (!append) {
//              this.selector.showEmptyState('No posts found');
//          }
//          this.selector.a11y?.announce(0, append);
//          return;
//      }
//
//      // Posts don't have breadcrumbs, but we might show search info
//      this.updatePostNavigation();
//
//      for (const [index, post] of Object.entries(posts)) {
//          if (!post) continue;
//
//          // Store post in global cache using post type as "taxonomy"
//          const postType = this.selector.currentConfig.postType;
//          if (!this.selector.terms.has(postType)) {
//              this.selector.terms.set(postType, new Map());
//          }
//          this.selector.terms.get(postType).set(post.id, post);
//
//          this.createPostElement({
//              id: parseInt(post.id),
//              name: post.name || post.title,
//              title: post.title || post.name,
//              thumbnail: post.thumbnail,
//              city: post.city,
//              url: post.url,
//              hasChildren: false, // Posts don't have children
//              path: post.path || post.title || post.name,
//              show: showPath
//          });
//      }
//
//      this.selector.a11y?.announce(posts.length, append);
//  }
//
//  /**
//   * Create individual post element (adapted from createTermElement)
//   */
//  createPostElement(postData) {
//      if (!this.isPostSelector()) {
//          return this.originalCreateTermElement(postData);
//      }
//
//      if (!postData || !postData.name) return;
//
//      const listItem = window.getTemplate('postListItem') || window.getTemplate('termListItem');
//      listItem.dataset.id = postData.id;
//
//      const isSelected = this.selector.selectedTerms.has(postData.id);
//      const checkbox = listItem.querySelector('input');
//      const label = listItem.querySelector('label');
//      const nameSpan = listItem.querySelector('span, .term-name, .post-name');
//
//      if (checkbox && label && nameSpan) {
//          checkbox.id = `${this.selector.currentConfig.container.id}${postData.id}`;
//          checkbox.name = `${this.selector.currentConfig.container.id}post-select`;
//          checkbox.value = postData.id;
//          checkbox.disabled = !isSelected && this.selector.disabled;
//          checkbox.checked = isSelected;
//
//          label.htmlFor = checkbox.id;
//          label.title = postData.path || postData.name;
//          label.dataset.path = postData.path;
//
//          nameSpan.textContent = postData.show ? postData.path : postData.name;
//      }
//
//      // Add post-specific elements (thumbnail, city, etc.)
//      this.enhancePostElement(listItem, postData);
//
//      this.selector.elements.termsList.appendChild(listItem);
//  }
//
//  /**
//   * Enhance post element with post-specific data
//   */
//  enhancePostElement(listItem, postData) {
//      // Add thumbnail if available
//      if (postData.thumbnail) {
//          const label = listItem.querySelector('label');
//          if (label) {
//              const thumbnail = document.createElement('img');
//              thumbnail.src = postData.thumbnail;
//              thumbnail.alt = postData.name;
//              thumbnail.className = 'post-thumbnail';
//              thumbnail.loading = 'lazy';
//              label.insertBefore(thumbnail, label.firstChild);
//          }
//      }
//
//      // Add city if available
//      if (postData.city) {
//          const nameSpan = listItem.querySelector('span, .term-name, .post-name');
//          if (nameSpan) {
//              const citySpan = document.createElement('span');
//              citySpan.className = 'post-city';
//              citySpan.textContent = ` (${postData.city})`;
//              nameSpan.appendChild(citySpan);
//          }
//      }
//
//      // Add post-specific CSS class
//      listItem.classList.add('post-item');
//  }
//
//  /**
//   * Update modal content for post selection
//   */
//  updateModalForPosts() {
//      if (!this.isPostSelector()) {
//          return this.originalUpdateModalForTaxonomy();
//      }
//
//      const config = this.selector.currentConfig;
//      this.selector.elements.modalTitle.textContent = `Select ${config.plural || 'Posts'}`;
//
//      // Always show search for posts (more important than for taxonomies)
//      const searchWrapper = this.selector.modal.querySelector('.search-wrapper');
//      if (searchWrapper) {
//          searchWrapper.style.display = 'block';
//      }
//
//      // Never show create new for posts
//      if (this.selector.elements.createNewSection) {
//          this.selector.elements.createNewSection.style.display = 'none';
//          this.selector.elements.createNewSection.hidden = true;
//      }
//
//      // Hide breadcrumbs for posts (not hierarchical)
//      if (this.selector.elements.breadcrumbs) {
//          this.selector.elements.breadcrumbs.style.display = 'none';
//      }
//
//      const openMessage = `Opened ${config.singular || 'post'} selection. Search and choose from available posts.`;
//      this.selector.a11y?.announce(openMessage);
//  }
//
//  /**
//   * Update navigation for posts (simpler than taxonomy breadcrumbs)
//   */
//  updatePostNavigation() {
//      // Hide back button and breadcrumbs for posts
//      if (this.selector.elements.backButton) {
//          this.selector.elements.backButton.hidden = true;
//      }
//
//      // Clear breadcrumbs container but keep the back button
//      const breadcrumbs = this.selector.elements.breadcrumbs;
//      if (breadcrumbs) {
//          // Remove all children except the back button
//          Array.from(breadcrumbs.children).forEach(child => {
//              if (!child.classList.contains('back-to-parent')) {
//                  child.remove();
//              }
//          });
//      }
//
//      // Show search info if searching
//      if (this.selector.searchQuery) {
//          const searchInfo = document.createElement('div');
//          searchInfo.className = 'search-info';
//          searchInfo.textContent = `Searching for: "${this.selector.searchQuery}"`;
//          this.selector.elements.termsList.insertBefore(
//              searchInfo,
//              this.selector.elements.termsList.firstChild
//          );
//      }
//  }
//
//  /**
//   * Clean up when destroyed
//   */
//  destroy() {
//      // Restore original methods
//      if (this.originalFetchTerms) {
//          this.selector.fetchTerms = this.originalFetchTerms;
//      }
//      if (this.originalBuildRequest) {
//          this.selector.buildRequest = this.originalBuildRequest;
//      }
//      if (this.originalCreateTermElement) {
//          this.selector.createTermElement = this.originalCreateTermElement;
//      }
//      if (this.originalUpdateModalForTaxonomy) {
//          this.selector.updateModalForTaxonomy = this.originalUpdateModalForTaxonomy;
//      }
//  }
// }
//
// // Extend TaxonomySelector to support post selection
// document.addEventListener('DOMContentLoaded', function() {
//  if (window.jvbSelector) {
//      // Add post selector extension to the existing selector
//      window.jvbSelector.postExtension = new PostSelectorExtension(window.jvbSelector);
//
//      console.log('PostSelector extension loaded');
//  }
// });
//
// // Export for manual use
// window.PostSelectorExtension = PostSelectorExtension;