PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.44.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.44.0
3.53.0 3.52.0 3.51.0 3.50.0 3.45.0 3.38.0 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.40.0 3.40.1 3.41.0 3.42.0 3.43.0 3.44.0 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 All 177 releases
simple-tags / inc / taxonomies-functions.php

taxonomies-functions.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.44.0, at inc/taxonomies-functions.php

2,600 lines 92.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Construct a dropdown of our taxonomies so users can select which to edit.
5 *
6 *
7 * @param array $taxonomies Array of taxonomies that are registered. Optional.
8 */
9 function taxopress_taxonomies_dropdown($taxonomies = [])
10 {
11
12 $ui = new taxopress_admin_ui();
13
14 if (!empty($taxonomies)) {
15 $select = [];
16 $select['options'] = [];
17
18 foreach ($taxonomies as $tax) {
19 $text = !empty($tax['label']) ? esc_html($tax['label']) : esc_html($tax['name']);
20 $select['options'][] = [
21 'attr' => $tax['name'],
22 'text' => $text,
23 ];
24 }
25
26 $current = taxopress_get_current_taxonomy();
27 $select['selected'] = $current;
28
29 /**
30 * Filters the taxonomy dropdown options before rendering.
31 *
32 * @param array $select Array of options for the dropdown.
33 * @param array $taxonomies Array of original passed in post types.
34 */
35 $select = apply_filters('taxopress_taxonomies_dropdown_options', $select, $taxonomies);
36
37 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
38 echo $ui->get_select_input([
39 'namearray' => 'taxopress_selected_taxonomy',
40 'name' => 'taxonomy',
41 'selections' => $select,// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
42 'wrap' => false,
43 ]);
44 }
45 }
46
47 /**
48 * Get the selected taxonomy from the $_POST global.
49 *
50 *
51 * @param bool $taxonomy_deleted Whether or not a taxonomy was recently deleted. Optional. Default false.
52 * @return bool|string False on no result, sanitized taxonomy if set.
53 * @internal
54 *
55 */
56 function taxopress_get_current_taxonomy($taxonomy_deleted = false)
57 {
58
59 $tax = false;
60
61 if (!empty($_POST)) {
62 if (!empty($_POST['taxopress_select_taxonomy_nonce_field'])) {
63 check_admin_referer('taxopress_select_taxonomy_nonce_action', 'taxopress_select_taxonomy_nonce_field');
64 }
65 if (isset($_POST['taxopress_selected_taxonomy']['taxonomy'])) {
66 $tax = sanitize_text_field($_POST['taxopress_selected_taxonomy']['taxonomy']);
67 } elseif ($taxonomy_deleted) {
68 $taxonomies = taxopress_get_taxonomy_data();
69 $tax = key($taxonomies);
70 } elseif (isset($_POST['cpt_custom_tax']['name'])) {
71 // Return the submitted value.
72 if (!in_array($_POST['cpt_custom_tax']['name'], taxopress_reserved_taxonomies(), true)) {
73 $tax = sanitize_text_field($_POST['cpt_custom_tax']['name']);
74 } else {
75 // Return the original value since user tried to submit a reserved term.
76 $tax = sanitize_text_field($_POST['tax_original']);
77 }
78 }
79 } elseif (!empty($_GET) && isset($_GET['taxopress_taxonomy'])) {
80 $tax = sanitize_text_field($_GET['taxopress_taxonomy']);
81 } else {
82 $taxonomies = taxopress_get_taxonomy_data();
83 if (!empty($taxonomies)) {
84 // Will return the first array key.
85 $tax = key($taxonomies);
86 }
87 }
88
89 /**
90 * Filters the current taxonomy to edit.
91 *
92 * @param string $tax Taxonomy slug.
93 */
94 return apply_filters('taxopress_current_taxonomy', $tax);
95 }
96
97 /**
98 * Delete our custom taxonomy from the array of taxonomies.
99 *
100 *
101 * @param array $data The $_POST values. Optional.
102 * @return bool|string False on failure, string on success.
103 * @internal
104 *
105 */
106 function taxopress_delete_taxonomy($data = [])
107 {
108
109 if (is_string($data) && taxonomy_exists($data)) {
110 $data = [
111 'cpt_custom_tax' => [
112 'name' => $data,
113 ],
114 ];
115 }
116
117 // Check if they selected one to delete.
118 if (empty($data['cpt_custom_tax']['name'])) {
119 return taxopress_admin_notices('error', '', false,
120 esc_html__('Please provide a taxonomy to delete', 'simple-tags'));
121 }
122
123 /**
124 * Fires before a taxonomy is deleted from our saved options.
125 *
126 *
127 * @param array $data Array of taxonomy data we are deleting.
128 */
129 do_action('taxopress_before_delete_taxonomy', $data);
130
131 $taxonomies = taxopress_get_taxonomy_data();
132 $external_taxonomies = taxopress_get_extername_taxonomy_data();
133
134 if (array_key_exists(strtolower($data['cpt_custom_tax']['name']), $taxonomies)) {
135
136 unset($taxonomies[$data['cpt_custom_tax']['name']]);
137
138 /**
139 * Filters whether or not 3rd party options were saved successfully within taxonomy deletion.
140 *
141 * @param bool $value Whether or not someone else saved successfully. Default false.
142 * @param array $taxonomies Array of our updated taxonomies data.
143 * @param array $data Array of submitted taxonomy to update.
144 */
145 if (false === ($success = apply_filters('taxopress_taxonomy_delete_tax', false, $taxonomies, $data))) {
146 $success = update_option('taxopress_taxonomies', $taxonomies);
147 }
148 }
149
150 if (array_key_exists(strtolower($data['cpt_custom_tax']['name']), $external_taxonomies)) {
151
152 unset($external_taxonomies[$data['cpt_custom_tax']['name']]);
153
154 /**
155 * Filters whether or not 3rd party options were saved successfully within taxonomy deletion.
156 *
157 * @param bool $value Whether or not someone else saved successfully. Default false.
158 * @param array $external_taxonomies Array of our updated taxonomies data.
159 * @param array $data Array of submitted taxonomy to update.
160 */
161 if (false === ($success = apply_filters('taxopress_taxonomy_delete_tax', false, $external_taxonomies, $data))) {
162 $success = update_option('taxopress_external_taxonomies', $external_taxonomies);
163 }
164 }
165
166 if ($data['cpt_custom_tax']['name'] === 'media_tag') {
167 $success = update_option('taxopress_media_tag_deleted', 1);
168 }
169
170 delete_option("default_term_{$data['cpt_custom_tax']['name']}");
171
172 /**
173 * Fires after a taxonomy is deleted from our saved options.
174 *
175 *
176 * @param array $data Array of taxonomy data that was deleted.
177 */
178 do_action('taxopress_after_delete_taxonomy', $data);
179
180 // Used to help flush rewrite rules on init.
181 set_transient('taxopress_flush_rewrite_rules', 'true', 5 * 60);
182
183 if (isset($success)) {
184 return 'delete_success';
185 }
186
187 return 'delete_fail';
188 }
189
190 /**
191 * Add to or update our TAXOPRESS option with new data.
192 *
193 *
194 * @param array $data Array of taxonomy data to update. Optional.
195 * @return bool|string False on failure, string on success.
196 * @internal
197 *
198 */
199 function taxopress_update_taxonomy($data = [])
200 {
201
202 //update our custom checkbox value if not checked
203
204 if (!isset($data['cpt_custom_tax']['hierarchical'])) {
205 $data['cpt_custom_tax']['hierarchical'] = 0;
206 }
207 if (!isset($data['cpt_custom_tax']['rewrite'])) {
208 $data['cpt_custom_tax']['rewrite'] = 0;
209 }
210 if (!isset($data['cpt_custom_tax']['rewrite_withfront'])) {
211 $data['cpt_custom_tax']['rewrite_withfront'] = 0;
212 }
213 if (!isset($data['cpt_custom_tax']['rewrite_hierarchical'])) {
214 $data['cpt_custom_tax']['rewrite_hierarchical'] = 0;
215 }
216 if (!isset($data['cpt_custom_tax']['show_ui'])) {
217 $data['cpt_custom_tax']['show_ui'] = 0;
218 }
219 if (!isset($data['cpt_custom_tax']['show_in_menu'])) {
220 $data['cpt_custom_tax']['show_in_menu'] = 0;
221 }
222 if (!isset($data['cpt_custom_tax']['show_in_nav_menus'])) {
223 $data['cpt_custom_tax']['show_in_nav_menus'] = 0;
224 }
225 if (!isset($data['cpt_custom_tax']['show_admin_column'])) {
226 $data['cpt_custom_tax']['show_admin_column'] = 0;
227 }
228 if (!isset($data['cpt_custom_tax']['show_in_rest'])) {
229 $data['cpt_custom_tax']['show_in_rest'] = 0;
230 }
231 if (!isset($data['cpt_custom_tax']['show_in_quick_edit'])) {
232 $data['cpt_custom_tax']['show_in_quick_edit'] = 0;
233 }
234 if (!isset($data['cpt_custom_tax']['public'])) {
235 $data['cpt_custom_tax']['public'] = 0;
236 }
237 if (!isset($data['cpt_custom_tax']['publicly_queryable'])) {
238 $data['cpt_custom_tax']['publicly_queryable'] = 0;
239 }
240 if (!isset($data['cpt_custom_tax']['query_var'])) {
241 $data['cpt_custom_tax']['query_var'] = 0;
242 }
243 if (!isset($data['cpt_custom_tax']['include_in_result'])) {
244 $data['cpt_custom_tax']['include_in_result'] = 0;
245 }
246 if ( ! isset( $data['cpt_custom_tax']['show_in_filter'] ) ) {
247 $data['cpt_custom_tax']['show_in_filter'] = 0;
248 }
249 if ( ! isset( $data['cpt_custom_tax']['order'] ) ) {
250 $data['cpt_custom_tax']['order'] = 'asc';
251 }
252 if ( ! isset( $data['cpt_custom_tax']['orderby'] ) ) {
253 $data['cpt_custom_tax']['orderby'] = 'term_id';
254 }
255 if ( ! isset( $data['cpt_custom_tax']['enable_taxopress_ordering'] ) ) {
256 $data['cpt_custom_tax']['enable_taxopress_ordering'] = 0;
257 }
258
259 /**
260 * Fires before a taxonomy is updated to our saved options.
261 *
262 *
263 * @param array $data Array of taxonomy data we are updating.
264 */
265 do_action('taxopress_before_update_taxonomy', $data);
266
267 // They need to provide a name.
268 if (empty($data['cpt_custom_tax']['name'])) {
269 return taxopress_admin_notices('error', '', false, esc_html__('Please provide a taxonomy name', 'simple-tags'));
270 }
271
272 if (!isset($data['taxonomy_external_edit'])) {
273 // Maybe a little harsh, but we shouldn't be saving THAT frequently.
274 delete_option("default_term_{$data['cpt_custom_tax']['name']}");
275 }
276
277 if (!isset($data['taxonomy_external_edit'])) {
278 if (!empty($data['tax_original']) && $data['tax_original'] !== $data['cpt_custom_tax']['name']) {
279 if (!empty($data['update_taxonomy'])) {
280 add_filter('taxopress_convert_taxonomy_terms', '__return_true');
281 }
282 }
283 }
284
285 $sanitized_data = [];
286 foreach ($data as $key => $value) {
287 if (!is_array($value)) {
288 $sanitized_data[$key] = taxopress_sanitize_text_field($value);
289 } else {
290 $new_value = [];
291 foreach ($data[$key] as $option_key => $option_value) {
292 $new_value[$option_key] = taxopress_sanitize_text_field($option_value);
293 }
294 $sanitized_data[$key] = $new_value;
295 }
296 }
297 $data = $sanitized_data;
298
299 if (false !== strpos($data['cpt_custom_tax']['name'], '\'') ||
300 false !== strpos($data['cpt_custom_tax']['name'], '\"') ||
301 false !== strpos($data['cpt_custom_tax']['rewrite_slug'], '\'') ||
302 false !== strpos($data['cpt_custom_tax']['rewrite_slug'], '\"')) {
303
304 add_filter('taxopress_custom_error_message', 'taxopress_slug_has_quotes');
305
306 return 'error';
307 }
308
309 $taxonomies = taxopress_get_taxonomy_data();
310 $external_taxonomies = taxopress_get_extername_taxonomy_data();
311
312
313 if (!isset($data['taxonomy_external_edit'])) {
314 /**
315 * Check if we already have a post type of that name.
316 *
317 * @param bool $value Assume we have no conflict by default.
318 * @param string $value Post type slug being saved.
319 * @param array $post_types Array of existing post types from TAXOPRESS.
320 */
321 $slug_exists = apply_filters('taxopress_taxonomy_slug_exists', false, $data['cpt_custom_tax']['name'],
322 $taxonomies);
323 if (true === $slug_exists) {
324 add_filter('taxopress_custom_error_message', 'taxopress_slug_matches_taxonomy');
325
326 return 'error';
327 }
328 }
329
330 foreach ($data['cpt_tax_labels'] as $key => $label) {
331 if (empty($label)) {
332 unset($data['cpt_tax_labels'][$key]);
333 }
334 $label = str_replace('"', '', htmlspecialchars_decode($label));
335 $label = htmlspecialchars($label, ENT_QUOTES);
336 $label = trim($label);
337 $data['cpt_tax_labels'][$key] = stripslashes_deep($label);
338 }
339
340 $label = ucwords(str_replace('_', ' ', $data['cpt_custom_tax']['name']));
341 if (!empty($data['cpt_custom_tax']['label'])) {
342 $label = str_replace('"', '', htmlspecialchars_decode($data['cpt_custom_tax']['label']));
343 $label = htmlspecialchars(stripslashes($label), ENT_QUOTES);
344 }
345
346 $name = trim($data['cpt_custom_tax']['name']);
347
348 $singular_label = ucwords(str_replace('_', ' ', $data['cpt_custom_tax']['name']));
349 if (!empty($data['cpt_custom_tax']['singular_label'])) {
350 $singular_label = str_replace('"', '', htmlspecialchars_decode($data['cpt_custom_tax']['singular_label']));
351 $singular_label = htmlspecialchars(stripslashes($singular_label));
352 }
353 $description = sanitize_textarea_field(stripslashes_deep($data['cpt_custom_tax']['description']));
354 $query_var_slug = trim($data['cpt_custom_tax']['query_var_slug']);
355 $rewrite_slug = trim($data['cpt_custom_tax']['rewrite_slug']);
356 $rest_base = trim($data['cpt_custom_tax']['rest_base']);
357 $rest_controller_class = trim($data['cpt_custom_tax']['rest_controller_class']);
358 $show_quickpanel_bulk = !empty($data['cpt_custom_tax']['show_in_quick_edit']) ? taxopress_disp_boolean($data['cpt_custom_tax']['show_in_quick_edit']) : '';
359 $show_in_filter = !empty($data['cpt_custom_tax']['show_in_filter']) ? taxopress_disp_boolean($data['cpt_custom_tax']['show_in_filter']) : '';
360 $order = !empty($data['cpt_custom_tax']['order']) ? sanitize_text_field($data['cpt_custom_tax']['order']) : 'asc';
361 $orderby = !empty($data['cpt_custom_tax']['orderby']) ? sanitize_text_field($data['cpt_custom_tax']['orderby']) : 'term_id';
362 $enable_taxopress_ordering = !empty($data['cpt_custom_tax']['enable_taxopress_ordering']) ? taxopress_disp_boolean($data['cpt_custom_tax']['enable_taxopress_ordering']) : 0;
363 $default_term = trim($data['cpt_custom_tax']['default_term']);
364
365 $meta_box_cb = trim($data['cpt_custom_tax']['meta_box_cb']);
366 // We may or may not need to force a boolean false keyword.
367 $maybe_false = strtolower(trim($data['cpt_custom_tax']['meta_box_cb']));
368 if ('false' === $maybe_false) {
369 $meta_box_cb = $maybe_false;
370 }
371
372 $internal_taxonomy_edit = true;
373
374 if ( isset($data['taxonomy_external_edit']) || $name === 'media_tag' ) {
375 $internal_taxonomy_edit = false;
376 }
377
378 if ($internal_taxonomy_edit) {
379
380 $taxonomies[$data['cpt_custom_tax']['name']] = [
381 'name' => $name,
382 'label' => $label,
383 'singular_label' => $singular_label,
384 'description' => $description,
385 'public' => taxopress_disp_boolean($data['cpt_custom_tax']['public']),
386 'publicly_queryable' => taxopress_disp_boolean($data['cpt_custom_tax']['publicly_queryable']),
387 'include_in_result' => taxopress_disp_boolean($data['cpt_custom_tax']['include_in_result']),
388 'hierarchical' => taxopress_disp_boolean($data['cpt_custom_tax']['hierarchical']),
389 'show_ui' => taxopress_disp_boolean($data['cpt_custom_tax']['show_ui']),
390 'show_in_menu' => taxopress_disp_boolean($data['cpt_custom_tax']['show_in_menu']),
391 'show_in_nav_menus' => taxopress_disp_boolean($data['cpt_custom_tax']['show_in_nav_menus']),
392 'query_var' => taxopress_disp_boolean($data['cpt_custom_tax']['query_var']),
393 'query_var_slug' => $query_var_slug,
394 'rewrite' => taxopress_disp_boolean($data['cpt_custom_tax']['rewrite']),
395 'rewrite_slug' => $rewrite_slug,
396 'rewrite_withfront' => $data['cpt_custom_tax']['rewrite_withfront'],
397 'rewrite_hierarchical' => $data['cpt_custom_tax']['rewrite_hierarchical'],
398 'show_admin_column' => taxopress_disp_boolean($data['cpt_custom_tax']['show_admin_column']),
399 'show_in_rest' => taxopress_disp_boolean($data['cpt_custom_tax']['show_in_rest']),
400 'show_in_quick_edit' => $show_quickpanel_bulk,
401 'rest_base' => $rest_base,
402 'rest_controller_class' => $rest_controller_class,
403 'labels' => $data['cpt_tax_labels'],
404 'meta_box_cb' => $meta_box_cb,
405 'default_term' => $default_term,
406 'show_in_filter' => $show_in_filter,
407 'order' => $order,
408 'orderby' => $orderby,
409 'enable_taxopress_ordering' => $enable_taxopress_ordering,
410 ];
411
412 $taxonomies[$data['cpt_custom_tax']['name']]['object_types'] = isset($data['cpt_post_types']) ? $data['cpt_post_types'] : '';
413
414
415 /**
416 * Filters final data to be saved right before saving taxoomy data.
417 *
418 * @param array $taxonomies Array of final taxonomy data to save.
419 * @param string $name Taxonomy slug for taxonomy being saved.
420 */
421 $taxonomies = apply_filters('taxopress_pre_save_taxonomy', $taxonomies, $name);
422
423 /**
424 * Filters whether or not 3rd party options were saved successfully within taxonomy add/update.
425 *
426 * @param bool $value Whether or not someone else saved successfully. Default false.
427 * @param array $taxonomies Array of our updated taxonomies data.
428 * @param array $data Array of submitted taxonomy to update.
429 */
430 if (false === ($success = apply_filters('taxopress_taxonomy_update_save', false, $taxonomies, $data))) {
431 $success = update_option('taxopress_taxonomies', $taxonomies);
432 }
433 } else {
434
435 $external_taxonomies[$data['cpt_custom_tax']['name']] = [
436 'name' => $name,
437 'label' => $label,
438 'singular_label' => $singular_label,
439 'description' => $description,
440 'public' => taxopress_disp_boolean($data['cpt_custom_tax']['public']),
441 'publicly_queryable' => taxopress_disp_boolean($data['cpt_custom_tax']['publicly_queryable']),
442 'include_in_result' => taxopress_disp_boolean($data['cpt_custom_tax']['include_in_result']),
443 'hierarchical' => taxopress_disp_boolean($data['cpt_custom_tax']['hierarchical']),
444 'show_ui' => taxopress_disp_boolean($data['cpt_custom_tax']['show_ui']),
445 'show_in_menu' => taxopress_disp_boolean($data['cpt_custom_tax']['show_in_menu']),
446 'show_in_nav_menus' => taxopress_disp_boolean($data['cpt_custom_tax']['show_in_nav_menus']),
447 'query_var' => taxopress_disp_boolean($data['cpt_custom_tax']['query_var']),
448 'query_var_slug' => $query_var_slug,
449 'rewrite' => taxopress_disp_boolean($data['cpt_custom_tax']['rewrite']),
450 'rewrite_slug' => $rewrite_slug,
451 'rewrite_withfront' => $data['cpt_custom_tax']['rewrite_withfront'],
452 'rewrite_hierarchical' => $data['cpt_custom_tax']['rewrite_hierarchical'],
453 'show_admin_column' => taxopress_disp_boolean($data['cpt_custom_tax']['show_admin_column']),
454 'show_in_rest' => taxopress_disp_boolean($data['cpt_custom_tax']['show_in_rest']),
455 'show_in_quick_edit' => $show_quickpanel_bulk,
456 'rest_base' => $rest_base,
457 'rest_controller_class' => $rest_controller_class,
458 'labels' => $data['cpt_tax_labels'],
459 'meta_box_cb' => $meta_box_cb,
460 'default_term' => $default_term,
461 'show_in_filter' => $show_in_filter,
462 'order' => $order,
463 'orderby' => $orderby,
464 'enable_taxopress_ordering' => $enable_taxopress_ordering,
465 ];
466
467 $external_taxonomies[$data['cpt_custom_tax']['name']]['object_types'] = isset($data['cpt_post_types']) ? $data['cpt_post_types'] : [];
468 $success = update_option('taxopress_external_taxonomies',
469 $external_taxonomies);
470 }
471
472 /**
473 * Fires after a taxonomy is updated to our saved options.
474 *
475 *
476 * @param array $data Array of taxonomy data that was updated.
477 */
478 do_action('taxopress_after_update_taxonomy', $data);
479
480 // Used to help flush rewrite rules on init.
481 set_transient('taxopress_flush_rewrite_rules', 'true', 5 * 60);
482
483 if (isset($success) && 'new' === $data['cpt_tax_status']) {
484 return 'add_success';
485 }
486
487 return 'update_success';
488 }
489
490 /**
491 * Return an array of names that users should not or can not use for taxonomy names.
492 *
493 * @return array $value Array of names that are recommended against.
494 */
495 function taxopress_reserved_taxonomies()
496 {
497
498 $reserved = [
499 'action',
500 'attachment',
501 'attachment_id',
502 'author',
503 'author_name',
504 'calendar',
505 'cat',
506 'category',
507 'category__and',
508 'category__in',
509 'category__not_in',
510 'category_name',
511 'comments_per_page',
512 'comments_popup',
513 'customize_messenger_channel',
514 'customized',
515 'cpage',
516 'day',
517 'debug',
518 'error',
519 'exact',
520 'feed',
521 'fields',
522 'hour',
523 'include',
524 'link_category',
525 'm',
526 'minute',
527 'monthnum',
528 'more',
529 'name',
530 'nav_menu',
531 'nonce',
532 'nopaging',
533 'offset',
534 'order',
535 'orderby',
536 'p',
537 'page',
538 'page_id',
539 'paged',
540 'pagename',
541 'pb',
542 'perm',
543 'post',
544 'post__in',
545 'post__not_in',
546 'post_format',
547 'post_mime_type',
548 'post_status',
549 'post_tag',
550 'post_type',
551 'posts',
552 'posts_per_archive_page',
553 'posts_per_page',
554 'preview',
555 'robots',
556 's',
557 'search',
558 'second',
559 'sentence',
560 'showposts',
561 'static',
562 'subpost',
563 'subpost_id',
564 'tag',
565 'tag__and',
566 'tag__in',
567 'tag__not_in',
568 'tag_id',
569 'tag_slug__and',
570 'tag_slug__in',
571 'taxonomy',
572 'tb',
573 'term',
574 'theme',
575 'type',
576 'types',
577 'w',
578 'withcomments',
579 'withoutcomments',
580 'year',
581 'output',
582 ];
583
584 /**
585 * Filters the list of reserved post types to check against.
586 * 3rd party plugin authors could use this to prevent duplicate post types.
587 *
588 *
589 * @param array $value Array of post type slugs to forbid.
590 */
591 $custom_reserved = apply_filters('taxopress_reserved_taxonomies', []);
592
593 if (is_string($custom_reserved) && !empty($custom_reserved)) {
594 $reserved[] = $custom_reserved;
595 } elseif (is_array($custom_reserved) && !empty($custom_reserved)) {
596 foreach ($custom_reserved as $slug) {
597 $reserved[] = $slug;
598 }
599 }
600
601 return $reserved;
602 }
603
604 /**
605 * Convert taxonomies.
606 *
607 * @param string $original_slug Original taxonomy slug. Optional. Default empty string.
608 * @param string $new_slug New taxonomy slug. Optional. Default empty string.
609 * @internal
610 *
611 */
612 function taxopress_convert_taxonomy_terms($original_slug = '', $new_slug = '')
613 {
614 global $wpdb;
615
616 $args = [
617 'taxonomy' => $original_slug,
618 'hide_empty' => false,
619 'fields' => 'ids',
620 ];
621
622 $term_ids = get_terms($args);
623
624 if (is_int($term_ids)) {
625 $term_ids = (array)$term_ids;
626 }
627
628 if (is_array($term_ids) && !empty($term_ids)) {
629 $term_ids = implode(',', $term_ids);
630
631 $query = "UPDATE `{$wpdb->term_taxonomy}` SET `taxonomy` = %s WHERE `taxonomy` = %s AND `term_id` IN ( {$term_ids} )";
632
633 $wpdb->query(
634 $wpdb->prepare($query, $new_slug, $original_slug)
635 );
636 }
637 taxopress_delete_taxonomy($original_slug);
638 }
639
640 /**
641 * Checks if we are trying to register an already registered taxonomy slug.
642 *
643 * @param bool $slug_exists Whether or not the post type slug exists. Optional. Default false.
644 * @param string $taxonomy_slug The post type slug being saved. Optional. Default empty string.
645 * @param array $taxonomies Array of TAXOPRESS-registered post types. Optional.
646 *
647 * @return bool
648 */
649 function taxopress_check_existing_taxonomy_slugs($slug_exists = false, $taxonomy_slug = '', $taxonomies = [])
650 {
651
652 // If true, then we'll already have a conflict, let's not re-process.
653 if (true === $slug_exists) {
654 return $slug_exists;
655 }
656
657 // Check if TAXOPRESS has already registered this slug.
658 if (array_key_exists(strtolower($taxonomy_slug), $taxonomies)) {
659 return true;
660 }
661
662 // Check if we're registering a reserved post type slug.
663 if (in_array($taxonomy_slug, taxopress_reserved_taxonomies())) {
664 return true;
665 }
666
667 // Check if other plugins have registered this same slug.
668 $public = get_taxonomies(['_builtin' => false, 'public' => true]);
669 $private = get_taxonomies(['_builtin' => false, 'public' => false]);
670 $registered_taxonomies = array_merge($public, $private);
671 if (in_array($taxonomy_slug, $registered_taxonomies)) {
672 return true;
673 }
674
675 // If we're this far, it's false.
676 return $slug_exists;
677 }
678
679 add_filter('taxopress_taxonomy_slug_exists', 'taxopress_check_existing_taxonomy_slugs', 10, 3);
680
681 /**
682 * Handle the save and deletion of taxonomy data.
683 */
684 function taxopress_process_taxonomy()
685 {
686
687 if (wp_doing_ajax()) {
688 return;
689 }
690
691 if (!is_admin()) {
692 return;
693 }
694
695 if (empty($_GET)) {
696 return;
697 }
698
699 if (!isset($_GET['page'])) {
700 return;
701 }
702 if ('st_taxonomies' !== $_GET['page']) {
703 return;
704 }
705
706 if(!current_user_can('simple_tags')){
707 return;
708 }
709
710 if (isset($_GET['new_taxonomy'])) {
711 if ((int)$_GET['new_taxonomy'] === 1) {
712 add_action('admin_notices', "taxopress_add_success_message_admin_notice");
713 add_filter('removable_query_args', 'taxopress_filter_removable_query_args_3');
714 }
715 }
716
717 if (!empty($_POST) && (isset($_POST['cpt_submit']) || isset($_POST['cpt_delete']))) {
718 $result = '';
719 if (isset($_POST['cpt_submit'])) {
720 check_admin_referer('taxopress_addedit_taxonomy_nonce_action', 'taxopress_addedit_taxonomy_nonce_field');
721 $result = taxopress_update_taxonomy($_POST);
722 } elseif (isset($_POST['cpt_delete'])) {
723 check_admin_referer('taxopress_addedit_taxonomy_nonce_action', 'taxopress_addedit_taxonomy_nonce_field');
724 $result = taxopress_delete_taxonomy($_POST);
725 add_filter('taxopress_taxonomy_deleted', '__return_true');
726 }
727
728 if ($result && is_callable("taxopress_{$result}_admin_notice")) {
729 if($result === 'add_success'){
730 taxopress_add_success_admin_notice();
731 }else{
732 add_action('admin_notices', "taxopress_{$result}_admin_notice");
733 }
734 }
735
736 if (isset($_POST['cpt_delete'])) {
737 wp_safe_redirect(
738 add_query_arg(
739 ['page' => 'st_taxonomies'],
740 taxopress_admin_url('admin.php?page=st_taxonomies')
741 )
742 );
743 exit();
744 }
745 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-deactivate-taxonomy') {
746 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
747 if (wp_verify_nonce($nonce, 'taxonomy-action-request-nonce')) {
748 taxopress_deactivate_taxonomy(sanitize_text_field($_REQUEST['taxonomy']));
749 add_action('admin_notices', "taxopress_deactivated_admin_notice");
750 }
751 add_filter('removable_query_args', 'taxopress_filter_removable_query_args');
752 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-reactivate-taxonomy') {
753 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
754 if (wp_verify_nonce($nonce, 'taxonomy-action-request-nonce')) {
755 taxopress_activate_taxonomy(sanitize_text_field($_REQUEST['taxonomy']));
756 add_action('admin_notices', "taxopress_activated_admin_notice");
757 }
758 add_filter('removable_query_args', 'taxopress_filter_removable_query_args');
759 } elseif (isset($_REQUEST['action']) && $_REQUEST['action'] === 'taxopress-delete-taxonomy') {
760 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
761 if (wp_verify_nonce($nonce, 'taxonomy-action-request-nonce')) {
762 taxopress_action_delete_taxonomy(sanitize_text_field($_REQUEST['taxonomy']));
763 }
764 add_filter('removable_query_args', 'taxopress_filter_removable_query_args');
765 } elseif (isset($_REQUEST['action2']) && $_REQUEST['action2'] === 'taxopress-reactivate-taxonomy') {
766 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
767 if (wp_verify_nonce($nonce, 'taxonomy-action-request-nonce')) {
768 taxopress_activate_taxonomy(sanitize_text_field($_REQUEST['taxonomy']));
769 add_action('admin_notices', "taxopress_activated_admin_notice");
770 }
771 add_filter('removable_query_args', 'taxopress_filter_removable_query_args_2');
772 } elseif (isset($_REQUEST['action2']) && $_REQUEST['action2'] === 'taxopress-deactivate-taxonomy') {
773 $nonce = sanitize_text_field($_REQUEST['_wpnonce']);
774 if (wp_verify_nonce($nonce, 'taxonomy-action-request-nonce')) {
775 taxopress_deactivate_taxonomy(sanitize_text_field($_REQUEST['taxonomy']));
776 add_action('admin_notices', "taxopress_deactivated_admin_notice");
777 }
778 add_filter('removable_query_args', 'taxopress_filter_removable_query_args_2');
779 }
780 }
781
782
783 /**
784 * Handle the conversion of taxonomy terms.
785 *
786 * This function came to be because we needed to convert AFTER registration.
787 */
788 function taxopress_do_convert_taxonomy_terms()
789 {
790
791 /**
792 * Whether or not to convert taxonomy terms.
793 *
794 * @param bool $value Whether or not to convert.
795 */
796 if (apply_filters('taxopress_convert_taxonomy_terms', false)) {
797 check_admin_referer('taxopress_addedit_taxonomy_nonce_action', 'taxopress_addedit_taxonomy_nonce_field');
798
799 taxopress_convert_taxonomy_terms(sanitize_text_field($_POST['tax_original']),
800 sanitize_text_field($_POST['cpt_custom_tax']['name']));
801 }
802 }
803
804 /**
805 * Handles slug_exist checks for cases of editing an existing taxonomy.
806 *
807 * @param bool $slug_exists Current status for exist checks.
808 * @param string $taxonomy_slug Taxonomy slug being processed.
809 * @param array $taxonomies TAXOPRESS taxonomies.
810 * @return bool
811 */
812 function taxopress_updated_taxonomy_slug_exists($slug_exists, $taxonomy_slug = '', $taxonomies = [])
813 {
814 if (
815 (!empty($_POST['cpt_tax_status']) && 'edit' === $_POST['cpt_tax_status']) &&
816 !in_array($taxonomy_slug, taxopress_reserved_taxonomies(), true) &&
817 (!empty($_POST['tax_original']) && $taxonomy_slug === $_POST['tax_original'])
818 ) {
819 $slug_exists = false;
820 }
821
822 return $slug_exists;
823 }
824
825 add_filter('taxopress_taxonomy_slug_exists', 'taxopress_updated_taxonomy_slug_exists', 11, 3);
826
827
828 /**
829 * Return boolean status depending on passed in value.
830 *
831 * @param mixed $bool_text text to compare to typical boolean values.
832 * @return bool Which bool value the passed in value was.
833 */
834 function get_taxopress_disp_boolean($bool_text)
835 {
836 $bool_text = (string)$bool_text;
837 if (empty($bool_text) || '0' === $bool_text || 'false' === $bool_text) {
838 return false;
839 }
840
841 return true;
842 }
843
844 /**
845 * Return string versions of boolean values.
846 *
847 * @param string $bool_text String boolean value.
848 * @return string standardized boolean text.
849 */
850 function taxopress_disp_boolean($bool_text)
851 {
852 $bool_text = (string)$bool_text;
853 if (empty($bool_text) || '0' === $bool_text || 'false' === $bool_text) {
854 return 'false';
855 }
856
857 return 'true';
858 }
859
860 /**
861 * Conditionally flushes rewrite rules if we have reason to.
862 */
863 function taxopress_flush_rewrite_rules()
864 {
865
866 if (wp_doing_ajax()) {
867 return;
868 }
869
870 /*
871 * Wise men say that you should not do flush_rewrite_rules on init or admin_init. Due to the nature of our plugin
872 * and how new post types or taxonomies can suddenly be introduced, we need to...potentially. For this,
873 * we rely on a short lived transient. Only 5 minutes life span. If it exists, we do a soft flush before
874 * deleting the transient to prevent subsequent flushes. The only times the transient gets created, is if
875 * post types or taxonomies are created, updated, deleted, or imported. Any other time and this condition
876 * should not be met.
877 */
878 if ('true' === ($flush_it = get_transient('taxopress_flush_rewrite_rules'))) {
879 flush_rewrite_rules(false);
880 // So we only run this once.
881 delete_transient('taxopress_flush_rewrite_rules');
882 }
883 }
884
885 /**
886 * Return the current action being done within TAXOPRESS context.
887 *
888 * @return string Current action being done by TAXOPRESS
889 */
890 function taxopress_get_current_action()
891 {
892 $current_action = '';
893 if (!empty($_GET) && isset($_GET['action'])) {
894 $current_action .= esc_textarea(sanitize_text_field($_GET['action']));
895 }
896
897 return $current_action;
898 }
899
900 /**
901 * Return an array of all taxonomy slugs from Custom Post Type UI.
902 *
903 * @return array TAXOPRESS taxonomy slugs.
904 */
905 function taxopress_get_taxonomy_slugs()
906 {
907 $taxonomies = get_option('taxopress_taxonomies');
908 if (!empty($taxonomies)) {
909 return array_keys($taxonomies);
910 }
911
912 return [];
913 }
914
915 /**
916 * Return the appropriate admin URL depending on our context.
917 *
918 * @param string $path URL path.
919 * @return string
920 */
921 function taxopress_admin_url($path)
922 {
923 if (is_multisite() && is_network_admin()) {
924 return network_admin_url($path);
925 }
926
927 return admin_url($path);
928 }
929
930 /**
931 * Construct action tag for `<form>` tag.
932 *
933 * @param object|string $ui TAXOPRESS Admin UI instance. Optional. Default empty string.
934 * @return string
935 */
936 function taxopress_get_post_form_action($ui = '')
937 {
938 /**
939 * Filters the string to be used in an `action=""` attribute.
940 */
941 return apply_filters('taxopress_post_form_action', '', $ui);
942 }
943
944 /**
945 * Display action tag for `<form>` tag.
946 *
947 * @param object $ui TAXOPRESS Admin UI instance.
948 */
949 function taxopress_post_form_action($ui)
950 {
951 echo esc_attr(taxopress_get_post_form_action($ui));
952 }
953
954 /**
955 * Fetch our TAXOPRESS taxonomies option.
956 *
957 * @return mixed
958 */
959 function taxopress_get_taxonomy_data()
960 {
961 $data = apply_filters('taxopress_get_taxonomy_data', get_option('taxopress_taxonomies', []), get_current_blog_id());
962 return is_array($data) ? $data : [];
963 }
964
965 /**
966 * Fetch both internal and external edited taxopress taxonomies
967 *
968 * @return mixed
969 */
970 function taxopress_get_all_edited_taxonomy_data()
971 {
972 $internal_taxonomies = (array) taxopress_get_taxonomy_data();
973 $external_taxonomies = (array) taxopress_get_extername_taxonomy_data();
974
975 $all_taxonomies = array_merge($internal_taxonomies, $external_taxonomies);
976
977 return array_filter($all_taxonomies);
978 }
979
980
981
982
983 /**
984 * Fetch our TAXOPRESS taxonomies option.
985 *
986 * @return mixed
987 */
988 function taxopress_get_extername_taxonomy_data()
989 {
990 return array_filter((array)apply_filters('taxopress_get_extername_taxonomy_data', get_option('taxopress_external_taxonomies', []),
991 get_current_blog_id()));
992 }
993
994
995 /**
996 * Checks if a taxonomy is already registered.
997 *
998 * @param string $slug Taxonomy slug to check. Optional. Default empty string.
999 * @param array|string $data Taxonomy data being utilized. Optional.
1000 *
1001 * @return mixed
1002 */
1003 function taxopress_get_taxonomy_exists($slug = '', $data = [])
1004 {
1005
1006 /**
1007 * Filters the boolean value for if a taxonomy exists for 3rd parties.
1008 *
1009 * @param string $slug Taxonomy slug to check.
1010 * @param array|string $data Taxonomy data being utilized.
1011 */
1012 return apply_filters('taxopress_get_taxonomy_exists', taxonomy_exists($slug), $data);
1013 }
1014
1015 /**
1016 * Secondary admin notices function for use with admin_notices hook.
1017 *
1018 * Constructs admin notice HTML.
1019 *
1020 * @param string $message Message to use in admin notice. Optional. Default empty string.
1021 * @param bool $success Whether or not a success. Optional. Default true.
1022 * @return mixed
1023 */
1024 function taxopress_admin_notices_helper($message = '', $success = true)
1025 {
1026
1027 $class = [];
1028 $class[] = $success ? 'updated' : 'error';
1029 $class[] = 'notice is-dismissible taxopress-notice';
1030
1031 $messagewrapstart = '<div id="message" class="' . esc_attr(implode(' ', $class)) . '"><p>';
1032
1033 $messagewrapend = '</p></div>';
1034
1035 $action = '';
1036
1037 /**
1038 * Filters the custom admin notice for TAXOPRESS.
1039 *
1040 *
1041 * @param string $value Complete HTML output for notice.
1042 * @param string $action Action whose message is being generated.
1043 * @param string $message The message to be displayed.
1044 * @param string $messagewrapstart Beginning wrap HTML.
1045 * @param string $messagewrapend Ending wrap HTML.
1046 */
1047 return apply_filters('taxopress_admin_notice', $messagewrapstart . $message . $messagewrapend, $action, $message,
1048 $messagewrapstart, $messagewrapend);
1049 }
1050
1051 /**
1052 * Grab post type or taxonomy slug from $_POST global, if available.
1053 *
1054 * @return string
1055 * @internal
1056 *
1057 */
1058 function taxopress_get_object_from_post_global()
1059 {
1060 if (isset($_POST['cpt_custom_post_type']['name'])) {
1061 return sanitize_text_field($_POST['cpt_custom_post_type']['name']);
1062 }
1063
1064 if (isset($_POST['cpt_custom_tax']['name'])) {
1065 return sanitize_text_field($_POST['cpt_custom_tax']['name']);
1066 }
1067
1068 return esc_html__('Object', 'simple-tags');
1069 }
1070
1071 /**
1072 * Successful add callback.
1073 */
1074 function taxopress_add_success_admin_notice()
1075 {
1076 //redirect to new taxonomy if success
1077 wp_safe_redirect(
1078 add_query_arg(
1079 [
1080 'page' => 'st_taxonomies',
1081 'add' => 'taxonomy',
1082 'action' => 'edit',
1083 'taxopress_taxonomy' => taxopress_get_object_from_post_global(),
1084 'new_taxonomy' => 1,
1085 ],
1086 taxopress_admin_url('admin.php')
1087 )
1088 );
1089 exit();
1090 }
1091
1092 /**
1093 * Successful add callback.
1094 */
1095 function taxopress_add_success_message_admin_notice()
1096 {
1097 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1098 echo taxopress_admin_notices_helper(
1099 sprintf(
1100 esc_html__('%s has been successfully added', 'simple-tags'),
1101 sanitize_text_field($_GET['taxopress_taxonomy'])
1102 )
1103 );
1104 }
1105
1106 /**
1107 * Fail to add callback.
1108 */
1109 function taxopress_add_fail_admin_notice()
1110 {
1111 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1112 echo taxopress_admin_notices_helper(
1113 sprintf(
1114 esc_html__('%s has failed to be added', 'simple-tags'),
1115 taxopress_get_object_from_post_global()
1116 ),
1117 false
1118 );
1119 }
1120
1121 /**
1122 * Successful update callback.
1123 */
1124 function taxopress_update_success_admin_notice()
1125 {
1126 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1127 echo taxopress_admin_notices_helper(
1128 sprintf(
1129 esc_html__('%s has been successfully updated', 'simple-tags'),
1130 taxopress_get_object_from_post_global()
1131 )
1132 );
1133 }
1134
1135 /**
1136 * Fail to update callback.
1137 */
1138 function taxopress_update_fail_admin_notice()
1139 {
1140 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1141 echo taxopress_admin_notices_helper(
1142 sprintf(
1143 esc_html__('%s has failed to be updated', 'simple-tags'),
1144 taxopress_get_object_from_post_global()
1145 ),
1146 false
1147 );
1148 }
1149
1150 /**
1151 * Successful delete callback.
1152 */
1153 function taxopress_delete_success_admin_notice()
1154 {
1155 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1156 echo taxopress_admin_notices_helper(
1157 sprintf(
1158 esc_html__('%s has been successfully deleted', 'simple-tags'),
1159 taxopress_get_object_from_post_global()
1160 )
1161 );
1162 }
1163
1164 /**
1165 * Fail to delete callback.
1166 */
1167 function taxopress_delete_fail_admin_notice()
1168 {
1169 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1170 echo taxopress_admin_notices_helper(
1171 sprintf(
1172 esc_html__('%s has failed to be deleted', 'simple-tags'),
1173 taxopress_get_object_from_post_global()
1174 ),
1175 false
1176 );
1177 }
1178
1179
1180 function taxopress_nonce_fail_admin_notice()
1181 {
1182 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1183 echo taxopress_admin_notices_helper(
1184 esc_html__('Nonce failed verification', 'simple-tags'),
1185 false
1186 );
1187 }
1188
1189 /**
1190 * Returns error message for if trying to register existing taxonomy.
1191 *
1192 * @return string
1193 */
1194 function taxopress_slug_matches_taxonomy()
1195 {
1196 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1197 return sprintf(
1198 esc_html__('Please choose a different taxonomy name. %s is already registered.', 'simple-tags'),
1199 taxopress_get_object_from_post_global()
1200 );
1201 }
1202
1203
1204 /**
1205 * Returns error message for if not providing a post type to associate taxonomy to.
1206 *
1207 * @return string
1208 */
1209 function taxopress_empty_cpt_on_taxonomy()
1210 {
1211 return esc_html__('Please provide a post type to attach to.', 'simple-tags');
1212 }
1213
1214 /**
1215 * Returns error message for if trying to register post type with matching page slug.
1216 *
1217 * @return string
1218 */
1219 function taxopress_slug_matches_page()
1220 {
1221 $slug = taxopress_get_object_from_post_global();
1222 $matched_slug = get_page_by_path(
1223 taxopress_get_object_from_post_global()
1224 );
1225 if ($matched_slug instanceof WP_Post) {
1226 $slug = sprintf(
1227 '<a href="%s">%s</a>',
1228 get_edit_post_link($matched_slug->ID),
1229 taxopress_get_object_from_post_global()
1230 );
1231 }
1232
1233 return sprintf(
1234 esc_html__('Please choose a different post type name. %s matches an existing page slug, which can cause conflicts.',
1235 'simple-tags'),
1236 $slug
1237 );
1238 }
1239
1240 /**
1241 * Returns error message for if trying to use quotes in slugs or rewrite slugs.
1242 *
1243 * @return string
1244 */
1245 function taxopress_slug_has_quotes()
1246 {
1247 return sprintf(
1248 esc_html__('Please do not use quotes in post type/taxonomy names or rewrite slugs', 'simple-tags'),
1249 taxopress_get_object_from_post_global()
1250 );
1251 }
1252
1253 /**
1254 * Error admin notice.
1255 */
1256 function taxopress_error_admin_notice()
1257 {
1258 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1259 echo taxopress_admin_notices_helper(
1260 apply_filters('taxopress_custom_error_message', ''),
1261 false
1262 );
1263 }
1264
1265 /**
1266 * Returns saved values for single taxonomy from TAXOPRESS settings.
1267 *
1268 * @param string $taxonomy Taxonomy to retrieve TAXOPRESS object for.
1269 * @return string
1270 */
1271 function taxopress_get_taxopress_taxonomy_object($taxonomy = '')
1272 {
1273 $taxonomies = get_option('taxopress_taxonomies');
1274
1275 if (array_key_exists($taxonomy, $taxonomies)) {
1276 return $taxonomies[$taxonomy];
1277 }
1278
1279 return '';
1280 }
1281
1282
1283 /**
1284 * Register our users' custom taxonomies.
1285 *
1286 * @internal
1287 */
1288 function taxopress_create_custom_taxonomies()
1289 {
1290 $taxes = get_option('taxopress_taxonomies');
1291
1292 if (empty($taxes)) {
1293 return;
1294 }
1295
1296 /**
1297 * Fires before the start of the taxonomy registrations.
1298 *
1299 * @param array $taxes Array of taxonomies to register.
1300 */
1301 do_action('taxopress_pre_register_taxonomies', $taxes);
1302
1303 if (is_array($taxes)) {
1304 foreach ($taxes as $tax) {
1305 /**
1306 * Filters whether or not to skip registration of the current iterated taxonomy.
1307 *
1308 * Dynamic part of the filter name is the chosen taxonomy slug.
1309 *
1310 * @param bool $value Whether or not to skip the taxonomy.
1311 * @param array $tax Current taxonomy being registered.
1312 */
1313 if ((bool)apply_filters("taxopress_disable_{$tax['name']}_tax", false, $tax)) {
1314 continue;
1315 }
1316
1317 /**
1318 * Filters whether or not to skip registration of the current iterated taxonomy.
1319 *
1320 * @param bool $value Whether or not to skip the taxonomy.
1321 * @param array $tax Current taxonomy being registered.
1322 */
1323 if ((bool)apply_filters('taxopress_disable_tax', false, $tax)) {
1324 continue;
1325 }
1326
1327 taxopress_register_single_taxonomy($tax);
1328 }
1329 }
1330
1331 /**
1332 * Fires after the completion of the taxonomy registrations.
1333 *
1334 * @param array $taxes Array of taxonomies registered.
1335 */
1336 do_action('taxopress_post_register_taxonomies', $taxes);
1337 }
1338
1339 /**
1340 * Helper function to register the actual taxonomy.
1341 *
1342 * @param array $taxonomy Taxonomy array to register. Optional.
1343 * @return null Result of register_taxonomy.
1344 * @internal
1345 *
1346 */
1347 function taxopress_register_single_taxonomy($taxonomy = [])
1348 {
1349 $labels = [
1350 'name' => $taxonomy['label'],
1351 'singular_name' => $taxonomy['singular_label'],
1352 ];
1353
1354 $description = '';
1355 if (!empty($taxonomy['description'])) {
1356 $description = $taxonomy['description'];
1357 }
1358
1359 $preserved = taxopress_get_preserved_keys('taxonomies');
1360 $preserved_labels = taxopress_get_preserved_labels();
1361 foreach ($taxonomy['labels'] as $key => $label) {
1362 if (!empty($label)) {
1363 $labels[$key] = $label;
1364 } elseif (empty($label) && in_array($key, $preserved, true)) {
1365 $singular_or_plural = (in_array($key,
1366 array_keys($preserved_labels['taxonomies']['plural']))) ? 'plural' : 'singular';
1367 $label_plurality = ('plural' === $singular_or_plural) ? $taxonomy['label'] : $taxonomy['singular_label'];
1368 $labels[$key] = sprintf($preserved_labels['taxonomies'][$singular_or_plural][$key], $label_plurality);
1369 }
1370 }
1371
1372 $rewrite = get_taxopress_disp_boolean($taxonomy['rewrite']);
1373 if (false !== get_taxopress_disp_boolean($taxonomy['rewrite'])) {
1374 $rewrite = [];
1375 $rewrite['slug'] = !empty($taxonomy['rewrite_slug']) ? $taxonomy['rewrite_slug'] : $taxonomy['name'];
1376 $rewrite['with_front'] = true;
1377 if (isset($taxonomy['rewrite_withfront'])) {
1378 $rewrite['with_front'] = ('false' === taxopress_disp_boolean($taxonomy['rewrite_withfront'])) ? false : true;
1379 }
1380 $rewrite['hierarchical'] = false;
1381 if (isset($taxonomy['rewrite_hierarchical'])) {
1382 $rewrite['hierarchical'] = ('true' === taxopress_disp_boolean($taxonomy['rewrite_hierarchical'])) ? true : false;
1383 }
1384 }
1385
1386 if (in_array($taxonomy['query_var'], ['true', 'false', '0', '1'], true)) {
1387 $taxonomy['query_var'] = get_taxopress_disp_boolean($taxonomy['query_var']);
1388 }
1389 if (true === $taxonomy['query_var'] && !empty($taxonomy['query_var_slug'])) {
1390 $taxonomy['query_var'] = $taxonomy['query_var_slug'];
1391 }
1392
1393 $public = (!empty($taxonomy['public']) && false === get_taxopress_disp_boolean($taxonomy['public'])) ? false : true;
1394 $publicly_queryable = (!empty($taxonomy['publicly_queryable']) && false === get_taxopress_disp_boolean($taxonomy['publicly_queryable'])) ? false : true;
1395 if (empty($taxonomy['publicly_queryable'])) {
1396 $publicly_queryable = $public;
1397 }
1398
1399 $show_admin_column = (!empty($taxonomy['show_admin_column']) && false !== get_taxopress_disp_boolean($taxonomy['show_admin_column'])) ? true : false;
1400
1401 $show_in_menu = (!empty($taxonomy['show_in_menu']) && false !== get_taxopress_disp_boolean($taxonomy['show_in_menu'])) ? true : false;
1402
1403 if (empty($taxonomy['show_in_menu'])) {
1404 $show_in_menu = get_taxopress_disp_boolean($taxonomy['show_ui']);
1405 }
1406
1407 $show_in_nav_menus = (!empty($taxonomy['show_in_nav_menus']) && false !== get_taxopress_disp_boolean($taxonomy['show_in_nav_menus'])) ? true : false;
1408 if (empty($taxonomy['show_in_nav_menus'])) {
1409 $show_in_nav_menus = $public;
1410 }
1411
1412 $show_in_rest = (!empty($taxonomy['show_in_rest']) && false !== get_taxopress_disp_boolean($taxonomy['show_in_rest'])) ? true : false;
1413
1414 $show_in_quick_edit = (!empty($taxonomy['show_in_quick_edit']) && false !== get_taxopress_disp_boolean($taxonomy['show_in_quick_edit'])) ? true : false;
1415
1416 $show_in_filter = (!empty($taxonomy['show_in_filter']) && false !== get_taxopress_disp_boolean($taxonomy['show_in_filter'])) ? true : false;
1417
1418 $rest_base = null;
1419 if (!empty($taxonomy['rest_base'])) {
1420 $rest_base = $taxonomy['rest_base'];
1421 }
1422
1423 $rest_controller_class = null;
1424 if (!empty($post_type['rest_controller_class'])) {
1425 $rest_controller_class = $post_type['rest_controller_class'];
1426 }
1427
1428 $meta_box_cb = null;
1429 if (!empty($taxonomy['meta_box_cb'])) {
1430 $meta_box_cb = (false !== get_taxopress_disp_boolean($taxonomy['meta_box_cb'])) ? $taxonomy['meta_box_cb'] : false;
1431 }
1432 $default_term = null;
1433 if (!empty($taxonomy['default_term'])) {
1434 $term_parts = explode(',', $taxonomy['default_term']);
1435 $term_parts = array_filter($term_parts);
1436 if (!empty($term_parts)) {
1437 $default_term = [];
1438 foreach ($term_parts as $term_part) {
1439 $default_term[] = ['name' => $term_part, 'slug' => $term_part];
1440 }
1441 }
1442 /*
1443 if (!empty($term_parts[0])) {
1444 $default_term['name'] = trim($term_parts[0]);
1445 }
1446 if (!empty($term_parts[1])) {
1447 $default_term['slug'] = trim($term_parts[1]);
1448 }
1449 if (!empty($term_parts[2])) {
1450 $default_term['description'] = trim($term_parts[2]);
1451 }*/
1452 }
1453
1454 $args = [
1455 'labels' => $labels,
1456 'label' => $taxonomy['label'],
1457 'description' => $description,
1458 'public' => $public,
1459 'publicly_queryable' => $publicly_queryable,
1460 'hierarchical' => get_taxopress_disp_boolean($taxonomy['hierarchical']),
1461 'show_ui' => get_taxopress_disp_boolean($taxonomy['show_ui']),
1462 'show_in_menu' => $show_in_menu,
1463 'show_in_nav_menus' => $show_in_nav_menus,
1464 'query_var' => $taxonomy['query_var'],
1465 'rewrite' => $rewrite,
1466 'show_admin_column' => $show_admin_column,
1467 'show_in_rest' => $show_in_rest,
1468 'rest_base' => $rest_base,
1469 'rest_controller_class' => $rest_controller_class,
1470 'show_in_quick_edit' => $show_in_quick_edit,
1471 'show_in_filter' => $show_in_filter,
1472 'meta_box_cb' => $meta_box_cb,
1473 'default_term' => $default_term,
1474 ];
1475
1476 $object_type = !empty($taxonomy['object_types']) ? $taxonomy['object_types'] : '';
1477
1478 /**
1479 * Filters the arguments used for a taxonomy right before registering.
1480 *
1481 * @param array $args Array of arguments to use for registering taxonomy.
1482 * @param string $value Taxonomy slug to be registered.
1483 * @param array $taxonomy Original passed in values for taxonomy.
1484 * @param array $object_type Array of chosen post types for the taxonomy.
1485 */
1486 $args = apply_filters('taxopress_pre_register_taxonomy', $args, $taxonomy['name'], $taxonomy, $object_type);
1487
1488 return register_taxonomy($taxonomy['name'], $object_type, $args);
1489 }
1490
1491
1492 /**
1493 * Return a notice based on conditions.
1494 *
1495 * @param string $action The type of action that occurred. Optional. Default empty string.
1496 * @param string $object_type Whether it's from a post type or taxonomy. Optional. Default empty string.
1497 * @param bool $success Whether the action succeeded or not. Optional. Default true.
1498 * @param string $custom Custom message if necessary. Optional. Default empty string.
1499 * @return bool|string false on no message, else HTML div with our notice message.
1500 */
1501 function taxopress_admin_notices($action = '', $object_type = '', $success = true, $custom = '')
1502 {
1503 $class = [];
1504 $class[] = $success ? 'updated' : 'error';
1505 $class[] = 'notice is-dismissible taxopress-notice';
1506 $object_type = esc_attr($object_type);
1507
1508 $messagewrapstart = '<div id="message" class="' . implode(' ', $class) . '"><p>';
1509 $message = '';
1510
1511 $messagewrapend = '</p></div>';
1512
1513 if ('add' === $action) {
1514 if ($success) {
1515 $message .= sprintf(__('%s has been successfully added', 'simple-tags'), $object_type);
1516 } else {
1517 $message .= sprintf(__('%s has failed to be added', 'simple-tags'), $object_type);
1518 }
1519 } elseif ('update' === $action) {
1520 if ($success) {
1521 $message .= sprintf(__('%s has been successfully updated', 'simple-tags'), $object_type);
1522 } else {
1523 $message .= sprintf(__('%s has failed to be updated', 'simple-tags'), $object_type);
1524 }
1525 } elseif ('delete' === $action) {
1526 if ($success) {
1527 $message .= sprintf(__('%s has been successfully deleted', 'simple-tags'), $object_type);
1528 } else {
1529 $message .= sprintf(__('%s has failed to be deleted', 'simple-tags'), $object_type);
1530 }
1531 } elseif ('import' === $action) {
1532 if ($success) {
1533 $message .= sprintf(__('%s has been successfully imported', 'simple-tags'), $object_type);
1534 } else {
1535 $message .= sprintf(__('%s has failed to be imported', 'simple-tags'), $object_type);
1536 }
1537 } elseif ('error' === $action) {
1538 if (!empty($custom)) {
1539 $message = $custom;
1540 }
1541 }
1542
1543 if ($message) {
1544
1545 /**
1546 * Filters the custom admin notice for TAXOPRESS.
1547 *
1548 * @param string $value Complete HTML output for notice.
1549 * @param string $action Action whose message is being generated.
1550 * @param string $message The message to be displayed.
1551 * @param string $messagewrapstart Beginning wrap HTML.
1552 * @param string $messagewrapend Ending wrap HTML.
1553 */
1554 return apply_filters('taxopress_admin_notice', $messagewrapstart . $message . $messagewrapend, $action,
1555 $message, $messagewrapstart, $messagewrapend);
1556 }
1557
1558 return false;
1559 }
1560
1561 /**
1562 * Return array of keys needing preserved.
1563 *
1564 * @param string $type Type to return. Either 'post_types' or 'taxonomies'. Optional. Default empty string.
1565 * @return array Array of keys needing preservered for the requested type.
1566 */
1567 function taxopress_get_preserved_keys($type = '')
1568 {
1569 $preserved_labels = [
1570 'post_types' => [
1571 'add_new_item',
1572 'edit_item',
1573 'new_item',
1574 'view_item',
1575 'view_items',
1576 'all_items',
1577 'search_items',
1578 'not_found',
1579 'not_found_in_trash',
1580 ],
1581 'taxonomies' => [
1582 'search_items',
1583 'popular_items',
1584 'all_items',
1585 'parent_item',
1586 'parent_item_colon',
1587 'edit_item',
1588 'update_item',
1589 'add_new_item',
1590 'new_item_name',
1591 'separate_items_with_commas',
1592 'add_or_remove_items',
1593 'choose_from_most_used',
1594 ],
1595 ];
1596
1597 return !empty($type) ? $preserved_labels[$type] : [];
1598 }
1599
1600 /**
1601 * Return label for the requested type and label key.
1602 *
1603 * @param string $type Type to return. Either 'post_types' or 'taxonomies'. Optional. Default empty string.
1604 * @param string $key Requested label key. Optional. Default empty string.
1605 * @param string $plural Plural verbiage for the requested label and type. Optional. Default empty string.
1606 * @param string $singular Singular verbiage for the requested label and type. Optional. Default empty string.
1607 * @return string Internationalized default label.
1608 * @deprecated
1609 *
1610 */
1611 function taxopress_get_preserved_label($type = '', $key = '', $plural = '', $singular = '')
1612 {
1613 $preserved_labels = [
1614 'post_types' => [
1615 'add_new_item' => sprintf(__('Add new %s', 'simple-tags'), $singular),
1616 'edit_item' => sprintf(__('Edit %s', 'simple-tags'), $singular),
1617 'new_item' => sprintf(__('New %s', 'simple-tags'), $singular),
1618 'view_item' => sprintf(__('View %s', 'simple-tags'), $singular),
1619 'view_items' => sprintf(__('View %s', 'simple-tags'), $plural),
1620 'all_items' => sprintf(__('All %s', 'simple-tags'), $plural),
1621 'search_items' => sprintf(__('Search %s', 'simple-tags'), $plural),
1622 'not_found' => sprintf(__('No %s found.', 'simple-tags'), $plural),
1623 'not_found_in_trash' => sprintf(__('No %s found in trash.', 'simple-tags'), $plural),
1624 ],
1625 'taxonomies' => [
1626 'search_items' => sprintf(__('Search %s', 'simple-tags'), $plural),
1627 'popular_items' => sprintf(__('Popular %s', 'simple-tags'), $plural),
1628 'all_items' => sprintf(__('All %s', 'simple-tags'), $plural),
1629 'parent_item' => sprintf(__('Parent %s', 'simple-tags'), $singular),
1630 'parent_item_colon' => sprintf(__('Parent %s:', 'simple-tags'), $singular),
1631 'edit_item' => sprintf(__('Edit %s', 'simple-tags'), $singular),
1632 'update_item' => sprintf(__('Update %s', 'simple-tags'), $singular),
1633 'add_new_item' => sprintf(__('Add new %s', 'simple-tags'), $singular),
1634 'new_item_name' => sprintf(__('New %s name', 'simple-tags'), $singular),
1635 'separate_items_with_commas' => sprintf(__('Separate %s with commas', 'simple-tags'), $plural),
1636 'add_or_remove_items' => sprintf(__('Add or remove %s', 'simple-tags'), $plural),
1637 'choose_from_most_used' => sprintf(__('Choose from the most used %s', 'simple-tags'), $plural),
1638 ],
1639 ];
1640
1641 return $preserved_labels[$type][$key];
1642 }
1643
1644 /**
1645 * Returns an array of translated labels, ready for use with sprintf().
1646 *
1647 * Replacement for taxopress_get_preserved_label for the sake of performance.
1648 *
1649 * @return array
1650 */
1651 function taxopress_get_preserved_labels()
1652 {
1653 return [
1654 'post_types' => [
1655 'singular' => [
1656 'add_new_item' => esc_html__('Add new %s', 'simple-tags'),
1657 'edit_item' => esc_html__('Edit %s', 'simple-tags'),
1658 'new_item' => esc_html__('New %s', 'simple-tags'),
1659 'view_item' => esc_html__('View %s', 'simple-tags'),
1660 ],
1661 'plural' => [
1662 'view_items' => esc_html__('View %s', 'simple-tags'),
1663 'all_items' => esc_html__('All %s', 'simple-tags'),
1664 'search_items' => esc_html__('Search %s', 'simple-tags'),
1665 'not_found' => esc_html__('No %s found.', 'simple-tags'),
1666 'not_found_in_trash' => esc_html__('No %s found in trash.', 'simple-tags'),
1667 ],
1668 ],
1669 'taxonomies' => [
1670 'singular' => [
1671 'parent_item' => esc_html__('Parent %s', 'simple-tags'),
1672 'parent_item_colon' => esc_html__('Parent %s:', 'simple-tags'),
1673 'edit_item' => esc_html__('Edit %s', 'simple-tags'),
1674 'update_item' => esc_html__('Update %s', 'simple-tags'),
1675 'add_new_item' => esc_html__('Add new %s', 'simple-tags'),
1676 'new_item_name' => esc_html__('New %s name', 'simple-tags'),
1677 ],
1678 'plural' => [
1679 'search_items' => esc_html__('Search %s', 'simple-tags'),
1680 'popular_items' => esc_html__('Popular %s', 'simple-tags'),
1681 'all_items' => esc_html__('All %s', 'simple-tags'),
1682 'separate_items_with_commas' => esc_html__('Separate %s with commas', 'simple-tags'),
1683 'add_or_remove_items' => esc_html__('Add or remove %s', 'simple-tags'),
1684 'choose_from_most_used' => esc_html__('Choose from the most used %s', 'simple-tags'),
1685 ],
1686 ],
1687 ];
1688 }
1689
1690
1691 function get_all_taxopress_taxonomies_request()
1692 {
1693
1694 $category = get_taxonomies(
1695 ['name' => 'category'],
1696 'objects');
1697 $post_tag = get_taxonomies(
1698 ['name' => 'post_tag'],
1699 'objects');
1700
1701 $public = get_taxonomies(['public' => true], 'objects');
1702 $private = get_taxonomies(['public' => false], 'objects');
1703
1704 if( !array_key_exists('category', $public) && !array_key_exists('category', $public) ){
1705 $public = array_merge($category, $public);
1706 }
1707 if( !array_key_exists('post_tag', $public) && !array_key_exists('post_tag', $public) ){
1708 $public = array_merge($post_tag, $public);
1709 }
1710
1711 if ( isset($_GET['taxonomy_type']) && $_GET['taxonomy_type'] === 'all' ) {
1712 $registered_taxonomies = array_merge($public, $private);
1713 }elseif ( isset($_GET['taxonomy_type']) && $_GET['taxonomy_type'] === 'private' ) {
1714 $registered_taxonomies = $private;
1715 }else{
1716 $registered_taxonomies = $public;
1717 }
1718
1719 return $registered_taxonomies;
1720 }
1721
1722
1723 function get_all_taxopress_taxonomies()
1724 {
1725
1726 $registered_taxonomies = get_taxonomies([], 'objects');
1727
1728 return $registered_taxonomies;
1729 }
1730
1731
1732 function get_all_taxopress_public_taxonomies()
1733 {
1734 return get_taxonomies(['public' => true], 'objects');
1735 }
1736
1737 /**
1738 * Return an array of all deactivated taxonomy.
1739 *
1740 * @return array TAXOPRESS taxonomy.
1741 */
1742 function taxopress_get_deactivated_taxonomy()
1743 {
1744 $taxonomies = get_option('taxopress_deactivated_taxonomies');
1745 if (!empty($taxonomies)) {
1746 return (array)$taxonomies;
1747 }
1748
1749 return [];
1750 }
1751
1752 /**
1753 * None callback.
1754 */
1755 function taxopress_noaction_admin_notice()
1756 {
1757 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1758 echo taxopress_admin_notices_helper(esc_html__('Kindly select an action in bulk action dropdown!', 'simple-tags'),
1759 false);
1760 }
1761
1762 /**
1763 * None callback.
1764 */
1765 function taxopress_none_admin_notice()
1766 {
1767 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1768 echo taxopress_admin_notices_helper(esc_html__('Kindly select atleast one taxonomy to proceed', 'simple-tags'),
1769 false);
1770 }
1771
1772 /**
1773 * Deactivated callback.
1774 */
1775 function taxopress_deactivated_admin_notice()
1776 {
1777 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1778 echo taxopress_admin_notices_helper(esc_html__('Taxonomy has been successfully deactivated', 'simple-tags'));
1779 }
1780
1781 /**
1782 * Activated callback.
1783 */
1784 function taxopress_activated_admin_notice()
1785 {
1786 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1787 echo taxopress_admin_notices_helper(esc_html__('Taxonomy has been successfully activated', 'simple-tags'));
1788 }
1789
1790 /**
1791 * Delete callback.
1792 */
1793 function taxopress_taxdeleted_admin_notice()
1794 {
1795 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
1796 echo taxopress_admin_notices_helper(esc_html__('Taxonomy has been successfully deleted', 'simple-tags'));
1797 }
1798
1799 /**
1800 * Deactivated taxonomy.
1801 *
1802 * @return array TAXOPRESS taxonomy.
1803 */
1804 function taxopress_deactivate_taxonomy($term_object)
1805 {
1806 $all_taxonomies = (array)get_option('taxopress_deactivated_taxonomies');
1807 $all_taxonomies[] = $term_object;
1808 $all_taxonomies = array_unique(array_filter($all_taxonomies));
1809 $success = update_option('taxopress_deactivated_taxonomies', $all_taxonomies);
1810 }
1811
1812 /**
1813 * Activate taxonomy.
1814 *
1815 * @return array TAXOPRESS taxonomy.
1816 */
1817 function taxopress_activate_taxonomy($term_object)
1818 {
1819 $all_taxonomies = (array)get_option('taxopress_deactivated_taxonomies');
1820 if (($key = array_search($term_object, $all_taxonomies)) !== false) {
1821 unset($all_taxonomies[$key]);
1822 $success = update_option('taxopress_deactivated_taxonomies', $all_taxonomies);
1823 }
1824 }
1825
1826
1827 /**
1828 * Fetch our TAXOPRESS disabled taxonomies option.
1829 *
1830 * @return mixed
1831 */
1832 function taxopress_get_deactivated_taxonomy_data()
1833 {
1834 return apply_filters('taxopress_get_deactivated_taxonomy_data', get_option('taxopress_deactivated_taxonomies', []),
1835 get_current_blog_id());
1836 }
1837
1838
1839 /**
1840 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
1841 *
1842 * @link https://core.trac.wordpress.org/ticket/23367
1843 *
1844 * @param string[] $args Array of removable query arguments.
1845 * @return string[] Updated array of removable query arguments.
1846 */
1847 function taxopress_filter_removable_query_args(array $args)
1848 {
1849 return array_merge($args, [
1850 'action',
1851 'taxonomy',
1852 '_wpnonce',
1853 ]);
1854 }
1855
1856 /**
1857 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
1858 *
1859 * @link https://core.trac.wordpress.org/ticket/23367
1860 *
1861 * @param string[] $args Array of removable query arguments.
1862 * @return string[] Updated array of removable query arguments.
1863 */
1864 function taxopress_filter_removable_query_args_2(array $args)
1865 {
1866 return array_merge($args, [
1867 'action2',
1868 'taxonomy',
1869 '_wpnonce',
1870 ]);
1871 }
1872
1873 /**
1874 * Filters the list of query arguments which get removed from admin area URLs in WordPress.
1875 *
1876 * @link https://core.trac.wordpress.org/ticket/23367
1877 *
1878 * @param string[] $args Array of removable query arguments.
1879 * @return string[] Updated array of removable query arguments.
1880 */
1881 function taxopress_filter_removable_query_args_3(array $args)
1882 {
1883 return array_merge($args, [
1884 'new_taxonomy',
1885 ]);
1886 }
1887
1888
1889 /**
1890 * Delete our custom taxonomy from the array of taxonomies.
1891 * @return bool|string False on failure, string on success.
1892 */
1893 function taxopress_action_delete_taxonomy($term_object)
1894 {
1895
1896 $data = [
1897 'cpt_custom_tax' => [
1898 'name' => $term_object,
1899 ],
1900 ];
1901 // Check if they selected one to delete.
1902 if (empty($data['cpt_custom_tax']['name'])) {
1903 return taxopress_admin_notices('error', '', false,
1904 esc_html__('Please provide a taxonomy to delete', 'simple-tags'));
1905 }
1906
1907 /**
1908 * Fires before a taxonomy is deleted from our saved options.
1909 *
1910 *
1911 * @param array $data Array of taxonomy data we are deleting.
1912 */
1913 do_action('taxopress_before_delete_taxonomy', $data);
1914
1915 $taxonomies = taxopress_get_taxonomy_data();
1916
1917 if (array_key_exists(strtolower($data['cpt_custom_tax']['name']), $taxonomies)) {
1918
1919 unset($taxonomies[$data['cpt_custom_tax']['name']]);
1920
1921 /**
1922 * Filters whether or not 3rd party options were saved successfully within taxonomy deletion.
1923 *
1924 * @param bool $value Whether or not someone else saved successfully. Default false.
1925 * @param array $taxonomies Array of our updated taxonomies data.
1926 * @param array $data Array of submitted taxonomy to update.
1927 */
1928 if (false === ($success = apply_filters('taxopress_taxonomy_delete_tax', false, $taxonomies, $data))) {
1929 $success = update_option('taxopress_taxonomies', $taxonomies);
1930 }
1931 }
1932 delete_option("default_term_{$data['cpt_custom_tax']['name']}");
1933
1934 /**
1935 * Fires after a taxonomy is deleted from our saved options.
1936 *
1937 *
1938 * @param array $data Array of taxonomy data that was deleted.
1939 */
1940 do_action('taxopress_after_delete_taxonomy', $data);
1941
1942 // Used to help flush rewrite rules on init.
1943 set_transient('taxopress_flush_rewrite_rules', 'true', 5 * 60);
1944
1945 if (isset($success)) {
1946 add_action('admin_notices', "taxopress_taxdeleted_admin_notice");
1947
1948 return 'delete_success';
1949 }
1950
1951 add_action('admin_notices', "taxopress_delete_fail_admin_notice");
1952
1953 return 'delete_fail';
1954 }
1955
1956 function unregister_tags()
1957 {
1958 global $remove_current_taxonomy;
1959
1960 $all_taxonomies = (array)get_option('taxopress_deactivated_taxonomies');
1961 $all_taxonomies = array_unique(array_filter($all_taxonomies));
1962
1963 if (count($all_taxonomies) > 0) {
1964 foreach ($all_taxonomies as $taxonomy) {
1965
1966 if (!empty($_GET) && isset($_GET['page']) && 'st_taxonomies' == $_GET['page']) {
1967 $remove_current_taxonomy = $taxonomy;
1968 add_action('admin_menu', 'taxopress_remove_taxonomy_from_menus');
1969 } else {
1970 taxopress_unregister_taxonomy($taxonomy);
1971 }
1972 }
1973 }
1974 }
1975
1976 // Remove menu
1977 function taxopress_remove_taxonomy_from_menus()
1978 {
1979 global $remove_current_taxonomy;
1980 remove_menu_page('edit-tags.php?taxonomy=' . $remove_current_taxonomy);
1981 }
1982
1983
1984 function taxopress_unregister_taxonomy($taxonomy)
1985 {
1986 if (!taxonomy_exists($taxonomy)) {
1987 return new WP_Error('invalid_taxonomy', esc_html__('Invalid taxonomy.'));
1988 }
1989
1990 $taxonomy_object = get_taxonomy($taxonomy);
1991
1992 // Do not allow unregistering internal taxonomies.
1993 /*if ( $taxonomy_object->_builtin ) {
1994 return new WP_Error( 'invalid_taxonomy', esc_html__( 'Unregistering a built-in taxonomy is not allowed.' ) );
1995 }*/
1996
1997 global $wp_taxonomies;
1998
1999 // Instead of removing totally, keep minimal structure but disable functionality
2000 if (isset($wp_taxonomies[$taxonomy])) {
2001 $wp_taxonomies[$taxonomy]->public = false;
2002 $wp_taxonomies[$taxonomy]->show_ui = false;
2003 $wp_taxonomies[$taxonomy]->show_in_menu = false;
2004 $wp_taxonomies[$taxonomy]->show_in_nav_menus = false;
2005 $wp_taxonomies[$taxonomy]->show_in_rest = false;
2006 $wp_taxonomies[$taxonomy]->show_tagcloud = false;
2007 $wp_taxonomies[$taxonomy]->show_in_quick_edit = false;
2008 $wp_taxonomies[$taxonomy]->show_admin_column = false;
2009 $wp_taxonomies[$taxonomy]->query_var = false;
2010
2011 // Remove rewrite rules
2012 if ($taxonomy_object) {
2013 $taxonomy_object->remove_rewrite_rules();
2014 $taxonomy_object->remove_hooks();
2015 }
2016
2017 // Remove custom taxonomy default term option.
2018 if (!empty($taxonomy_object->default_term)) {
2019 delete_option('default_term_' . $taxonomy_object->name);
2020 }
2021 }
2022
2023 /**
2024 * Fires after a taxonomy is unregistered.
2025 *
2026 * @param string $taxonomy Taxonomy name.
2027 */
2028 do_action('unregistered_taxonomy', $taxonomy);
2029
2030 return true;
2031 }
2032
2033
2034 function taxopress_convert_external_taxonomy($taxonomy_object, $request_tax)
2035 {
2036
2037 if (array_key_exists($request_tax, taxopress_get_extername_taxonomy_data())) {
2038 return taxopress_get_extername_taxonomy_data()[$request_tax];
2039 }
2040
2041 $taxonomy_data = (array)$taxonomy_object;
2042
2043 foreach ($taxonomy_data as $key => $value) {
2044 //change label to array
2045 if ($key === 'labels') {
2046 $taxonomy_data[$key] = (array)$value;
2047 }
2048 //change cap to array
2049 if ($key === 'cap') {
2050 $taxonomy_data[$key] = (array)$value;
2051 }
2052 //change default terms to strings
2053 if ($key === 'default_term') {
2054 if (is_array($value) && count($value) > 0) {
2055 $taxonomy_data[$key] = join(',', array_filter($value));
2056 }
2057 }
2058 //set query var value if not empty
2059 if ($key === 'query_var') {
2060 if (empty(trim($value))) {
2061 $taxonomy_data['query_var'] = 'false';
2062 $taxonomy_data['query_var_slug'] = '';
2063 } else {
2064 $taxonomy_data['query_var'] = 'true';
2065 $taxonomy_data['query_var_slug'] = $value;
2066 }
2067 }
2068 //set rewrite
2069 if ($key === 'rewrite') {
2070 if (!empty($value) && is_array($value)) {
2071 if (count($value) > 0) {
2072 foreach ($value as $holdkey => $holdvalue) {
2073 if ($holdkey === 'with_front') {
2074 $taxonomy_data['rewrite_withfront'] = is_bool($holdvalue) ? taxopress_disp_boolean($holdvalue) : $holdvalue;
2075 } else {
2076 $taxonomy_data[$key . '_' . $holdkey] = is_bool($holdvalue) ? taxopress_disp_boolean($holdvalue) : $holdvalue;
2077 }
2078 }
2079 }
2080 $taxonomy_data[$key] = (count($value) > 0) ? 'true' : 'false';
2081 } else {
2082 $taxonomy_data[$key] = 'false';
2083 $taxonomy_data['rewrite_hierarchical'] = '';
2084 $taxonomy_data['rewrite_withfront'] = '';
2085 }
2086 }
2087 //dispose bool value
2088 if (is_bool($value)) {
2089 $taxonomy_data[$key] = taxopress_disp_boolean($value);
2090 }
2091 }
2092 //add singular label
2093 $taxonomy_data['singular_label'] = $taxonomy_data['labels']['singular_name'];
2094 //add object terms
2095 $taxonomy_data['object_types'] = $taxonomy_data['object_type'];
2096
2097 return $taxonomy_data;
2098 }
2099
2100 /**
2101 * Register our users' custom taxonomies.
2102 *
2103 * @internal
2104 */
2105 function taxopress_recreate_custom_taxonomies()
2106 {
2107 $taxes = taxopress_get_extername_taxonomy_data();
2108
2109 if (empty($taxes)) {
2110 return;
2111 }
2112 /**
2113 * Fires before the start of the taxonomy registrations.
2114 *
2115 * @param array $taxes Array of taxonomies to register.
2116 */
2117 do_action('taxopress_pre_register_taxonomies', $taxes);
2118
2119 if (is_array($taxes)) {
2120 foreach ($taxes as $tax) {
2121 if ($tax['name'] === 'media_tag' && (int)get_option('taxopress_media_tag_deleted') > 0) {
2122 continue;
2123 }
2124
2125 taxopress_re_register_single_taxonomy($tax);
2126 }
2127 }
2128
2129 /**
2130 * Fires after the completion of the taxonomy registrations.
2131 *
2132 * @param array $taxes Array of taxonomies registered.
2133 */
2134 do_action('taxopress_post_register_taxonomies', $taxes);
2135 }
2136
2137 /**
2138 * Helper function to register the actual taxonomy.
2139 *
2140 * @param array $taxonomy Taxonomy array to register. Optional.
2141 * @return null Result of register_taxonomy.
2142 * @internal
2143 *
2144 */
2145 function taxopress_re_register_single_taxonomy($taxonomy)
2146 {
2147
2148 $labels = [
2149 'name' => $taxonomy['label'],
2150 'singular_name' => $taxonomy['singular_label'],
2151 ];
2152
2153 $description = '';
2154 if (!empty($taxonomy['description'])) {
2155 $description = $taxonomy['description'];
2156 }
2157
2158 $preserved = taxopress_get_preserved_keys('taxonomies');
2159 $preserved_labels = taxopress_get_preserved_labels();
2160 foreach ($taxonomy['labels'] as $key => $label) {
2161 if (!empty($label)) {
2162 $labels[$key] = $label;
2163 } elseif (empty($label) && in_array($key, $preserved, true)) {
2164 $singular_or_plural = (in_array($key,
2165 array_keys($preserved_labels['taxonomies']['plural']))) ? 'plural' : 'singular';
2166 $label_plurality = ('plural' === $singular_or_plural) ? $taxonomy['label'] : $taxonomy['singular_label'];
2167 $labels[$key] = sprintf($preserved_labels['taxonomies'][$singular_or_plural][$key], $label_plurality);
2168 }
2169 }
2170
2171 $rewrite = get_taxopress_disp_boolean($taxonomy['rewrite']);
2172 if (false !== get_taxopress_disp_boolean($taxonomy['rewrite'])) {
2173 $rewrite = [];
2174 $rewrite['slug'] = !empty($taxonomy['rewrite_slug']) ? $taxonomy['rewrite_slug'] : $taxonomy['name'];
2175 $rewrite['with_front'] = true;
2176 if (isset($taxonomy['rewrite_withfront'])) {
2177 $rewrite['with_front'] = ('false' === taxopress_disp_boolean($taxonomy['rewrite_withfront'])) ? false : true;
2178 }
2179 $rewrite['hierarchical'] = false;
2180 if (isset($taxonomy['rewrite_hierarchical'])) {
2181 $rewrite['hierarchical'] = ('true' === taxopress_disp_boolean($taxonomy['rewrite_hierarchical'])) ? true : false;
2182 }
2183 }
2184
2185 if (in_array($taxonomy['query_var'], ['true', 'false', '0', '1'], true)) {
2186 $taxonomy['query_var'] = get_taxopress_disp_boolean($taxonomy['query_var']);
2187 }
2188 if (true === $taxonomy['query_var'] && !empty($taxonomy['query_var_slug'])) {
2189 $taxonomy['query_var'] = $taxonomy['query_var_slug'];
2190 }
2191
2192 $public = (!empty($taxonomy['public']) && false === get_taxopress_disp_boolean($taxonomy['public'])) ? false : true;
2193 $publicly_queryable = (!empty($taxonomy['publicly_queryable']) && false === get_taxopress_disp_boolean($taxonomy['publicly_queryable'])) ? false : true;
2194 if (empty($taxonomy['publicly_queryable'])) {
2195 $publicly_queryable = $public;
2196 }
2197
2198 $show_admin_column = (!empty($taxonomy['show_admin_column']) && false !== get_taxopress_disp_boolean($taxonomy['show_admin_column'])) ? true : false;
2199
2200 $show_in_menu = (!empty($taxonomy['show_in_menu']) && false !== get_taxopress_disp_boolean($taxonomy['show_in_menu'])) ? true : false;
2201
2202 if (empty($taxonomy['show_in_menu'])) {
2203 $show_in_menu = get_taxopress_disp_boolean($taxonomy['show_ui']);
2204 }
2205
2206 $show_in_nav_menus = (!empty($taxonomy['show_in_nav_menus']) && false !== get_taxopress_disp_boolean($taxonomy['show_in_nav_menus'])) ? true : false;
2207 if (empty($taxonomy['show_in_nav_menus'])) {
2208 $show_in_nav_menus = $public;
2209 }
2210
2211 $show_in_rest = (!empty($taxonomy['show_in_rest']) && false !== get_taxopress_disp_boolean($taxonomy['show_in_rest'])) ? true : false;
2212
2213 $show_in_quick_edit = (!empty($taxonomy['show_in_quick_edit']) && false !== get_taxopress_disp_boolean($taxonomy['show_in_quick_edit'])) ? true : false;
2214
2215 $show_in_filter = (!empty($taxonomy['show_in_filter']) && false !== get_taxopress_disp_boolean($taxonomy['show_in_filter'])) ? true : false;
2216
2217 $rest_base = null;
2218 if (!empty($taxonomy['rest_base'])) {
2219 $rest_base = $taxonomy['rest_base'];
2220 }
2221
2222 $rest_controller_class = null;
2223 if (!empty($post_type['rest_controller_class'])) {
2224 $rest_controller_class = $post_type['rest_controller_class'];
2225 }
2226
2227 $meta_box_cb = null;
2228 if (!empty($taxonomy['meta_box_cb'])) {
2229 $meta_box_cb = (false !== get_taxopress_disp_boolean($taxonomy['meta_box_cb'])) ? $taxonomy['meta_box_cb'] : false;
2230 }
2231 $default_term = null;
2232
2233 if (!empty($taxonomy['default_term'])) {
2234 $term_parts = explode(',', $taxonomy['default_term']);
2235 $term_parts = array_filter($term_parts);
2236 if (!empty($term_parts)) {
2237 $default_term = [];
2238 foreach ($term_parts as $term_part) {
2239 $default_term[] = ['name' => $term_part, 'slug' => $term_part];
2240 }
2241 }
2242 /*
2243 if (!empty($term_parts[0])) {
2244 $default_term['name'] = trim($term_parts[0]);
2245 }
2246 if (!empty($term_parts[1])) {
2247 $default_term['slug'] = trim($term_parts[1]);
2248 }
2249 if (!empty($term_parts[2])) {
2250 $default_term['description'] = trim($term_parts[2]);
2251 }*/
2252 }
2253
2254 $args = [
2255 'labels' => $labels,
2256 'label' => $taxonomy['label'],
2257 'description' => $description,
2258 'public' => $public,
2259 'publicly_queryable' => $publicly_queryable,
2260 'hierarchical' => get_taxopress_disp_boolean($taxonomy['hierarchical']),
2261 'show_ui' => get_taxopress_disp_boolean($taxonomy['show_ui']),
2262 'show_in_menu' => $show_in_menu,
2263 'show_in_nav_menus' => $show_in_nav_menus,
2264 'query_var' => $taxonomy['query_var'],
2265 'rewrite' => $rewrite,
2266 'show_admin_column' => $show_admin_column,
2267 'show_in_rest' => $show_in_rest,
2268 'rest_base' => $rest_base,
2269 'rest_controller_class' => $rest_controller_class,
2270 'show_in_quick_edit' => $show_in_quick_edit,
2271 'show_in_filter' => $show_in_filter,
2272 'meta_box_cb' => $meta_box_cb,
2273 'default_term' => $default_term,
2274 ];
2275
2276 $object_type = !empty($taxonomy['object_types']) ? $taxonomy['object_types'] : '';
2277
2278 /**
2279 * Filters the arguments used for a taxonomy right before registering.
2280 *
2281 * @param array $args Array of arguments to use for registering taxonomy.
2282 * @param string $value Taxonomy slug to be registered.
2283 * @param array $taxonomy Original passed in values for taxonomy.
2284 * @param array $object_type Array of chosen post types for the taxonomy.
2285 */
2286 $args = apply_filters('taxopress_pre_register_taxonomy', $args, $taxonomy['name'], $taxonomy, $object_type);
2287
2288 return register_taxonomy($taxonomy['name'], $object_type, $args);
2289 }
2290
2291 /**
2292 * Set post taxonomy default term
2293 *
2294 * @param integer $post_id
2295 * @param object $post
2296 * @return void
2297 */
2298 function taxopress_set_default_taxonomy_terms($post_id, $post) {
2299 if ( 'auto-draft' === $post->post_status ) {
2300 $taxonomies = get_object_taxonomies($post->post_type, 'object');
2301 foreach ($taxonomies as $taxonomy => $tax_object ) {
2302 if (!empty($tax_object->default_term)) {
2303 if (is_array($tax_object->default_term)) {
2304 $new_terms = [];
2305 foreach ($tax_object->default_term as $term => $option) {
2306 if (is_array($option) && isset($option['name'])) {
2307 $new_terms[] = trim($option['name']);
2308 }
2309 }
2310 if (!empty($new_terms)) {
2311 wp_set_object_terms($post_id, $new_terms, $taxonomy, true);
2312 }
2313 }
2314 }
2315 }
2316 }
2317 }
2318
2319 function taxopress_show_all_cpt_in_archive_result($request_tax){
2320
2321 $taxonomies = taxopress_get_taxonomy_data();
2322
2323 $current = false;
2324 if ($request_tax && is_array($taxonomies) && array_key_exists($request_tax, $taxonomies)) {
2325 $current = $taxonomies[$request_tax];
2326 } elseif (taxonomy_exists($request_tax)) {
2327 //not out taxonomy
2328 $external_taxonomy = get_taxonomies(['name' => $request_tax], 'objects');
2329 if (isset($external_taxonomy) > 0) {
2330 $current = taxopress_convert_external_taxonomy($external_taxonomy[$request_tax],
2331 $request_tax);
2332 }
2333 }
2334
2335 $status = isset($current) && isset($current['include_in_result']) ? get_taxopress_disp_boolean($current['include_in_result']) : false;
2336
2337 return $status;
2338 }
2339
2340 /**
2341 * Filter the dropdown cats to remove the value="0" to solve issue with filter when
2342 * tag=0 https://github.com/TaxoPress/TaxoPress/issues/2372
2343 * @param string $output
2344 * @return string
2345 */
2346 function taxopress_filter_dropdown_cats($output) {
2347
2348 if (strpos($output, 'taxopress-select2-term-filter') !== false) {
2349 $output = str_replace(['value="0"', "value='0'"], ['value=""', "value=''"], $output);
2350 }
2351
2352 return $output;
2353 }
2354
2355 /* Show taxonomy filter on post list */
2356 function taxopress_filter_dropdown( $taxonomy, $show_filter ) {
2357
2358 $show_filter = get_taxopress_disp_boolean( $show_filter );
2359
2360 if ( $show_filter == true ) {
2361
2362 wp_dropdown_categories(
2363 array(
2364 'show_option_all' => sprintf( __( 'All %s', 'simple-tags' ), $taxonomy->label ),
2365 'orderby' => 'name',
2366 'order' => 'ASC',
2367 'hide_empty' => false,
2368 'hide_if_empty' => true,
2369 'selected' => sanitize_text_field(filter_input( INPUT_GET, $taxonomy->query_var, FILTER_UNSAFE_RAW )),
2370 'hierarchical' => true,
2371 'name' => $taxonomy->query_var,
2372 'taxonomy' => $taxonomy->name,
2373 'value_field' => 'slug',
2374 'id' => $taxonomy->name,
2375 'class' => 'taxopress-select2-term-filter'
2376 )
2377 );
2378
2379 }
2380
2381 }
2382
2383 function taxopress_get_all_taxonomies() {
2384 $custom_taxonomies = get_taxonomies(['_builtin' => false], 'objects');
2385 $builtin_taxonomies = get_taxonomies(['_builtin' => true], 'objects');
2386 return array_merge($custom_taxonomies, $builtin_taxonomies);
2387 }
2388
2389 function taxopress_get_dropdown(){
2390
2391 global $pagenow, $typenow;
2392
2393 if ( is_admin() ) {
2394
2395 $type = 'post';
2396
2397 if (isset($_GET['post_type'])) {
2398
2399 $type = sanitize_text_field($_GET['post_type']);
2400 }
2401
2402 $taxonomies = taxopress_get_all_edited_taxonomy_data();
2403
2404 if( !empty($taxonomies) ) {
2405
2406 $all_taxonomies = taxopress_get_all_taxonomies();
2407
2408 foreach ( $all_taxonomies as $taxonomy ) {
2409
2410 $taxonomy_name = $taxonomy->name;
2411
2412 if( array_key_exists( $taxonomy_name, $taxonomies ) ){
2413
2414 $current = isset($taxonomies[ $taxonomy_name ]) ? $taxonomies[ $taxonomy_name ] : '';
2415
2416 if( is_array($current) && array_key_exists( 'show_in_filter', $current ) ){
2417 if (isset($current['object_types']) && !empty($current['object_types'])) {
2418 foreach ($current['object_types'] as $object_type) {
2419
2420 //Media Page
2421 if($pagenow === 'upload.php') {
2422
2423 if($object_type == "attachment") {
2424
2425 taxopress_filter_dropdown($taxonomy, $current['show_in_filter']);
2426
2427 }
2428
2429 } else {
2430
2431 if($object_type == $type) {
2432
2433 taxopress_filter_dropdown($taxonomy, $current['show_in_filter']);
2434
2435 }
2436 }
2437 }
2438 }
2439 }
2440 }
2441 }
2442 }
2443
2444 }
2445
2446 }
2447
2448 /**
2449 * Helper to sort terms based on TaxoPress settings.
2450 */
2451 function taxopress_sort_terms_by_settings($terms, $taxonomy, $settings = [], $is_admin = false) {
2452 static $custom_orders_cache = [];
2453
2454 if (!is_array($terms) || empty($terms) || empty($taxonomy)) {
2455 return $terms;
2456 }
2457
2458 // Default fallback settings
2459 $valid_orderby = ['name', 'term_id', 'ID', 'count', 'random', 'taxopress_term_order'];
2460 $valid_order = ['asc', 'desc'];
2461 $default_orderby = 'name';
2462 $default_order = 'asc';
2463
2464 $orderby_setting = isset($settings['orderby']) && in_array($settings['orderby'], $valid_orderby, true)
2465 ? $settings['orderby']
2466 : $default_orderby;
2467
2468 $order_setting = isset($settings['order']) && in_array(strtolower($settings['order']), $valid_order, true)
2469 ? strtolower($settings['order'])
2470 : $default_order;
2471
2472 // Custom order logic
2473 if ($orderby_setting === 'taxopress_term_order') {
2474 if (!isset($custom_orders_cache[$taxonomy])) {
2475 $custom_orders_cache[$taxonomy] = get_option('taxopress_term_order_' . $taxonomy, []);
2476 }
2477 $custom_order = $custom_orders_cache[$taxonomy];
2478 if (!empty($custom_order)) {
2479 $terms_by_id = [];
2480 foreach ($terms as $term) {
2481 if (is_object($term) && isset($term->term_id)) {
2482 $terms_by_id[$term->term_id] = $term;
2483 }
2484 }
2485 $ordered_terms = [];
2486 foreach ($custom_order as $term_id) {
2487 if (isset($terms_by_id[$term_id])) {
2488 $ordered_terms[] = $terms_by_id[$term_id];
2489 unset($terms_by_id[$term_id]);
2490 }
2491 }
2492 // Append missing terms
2493 foreach ($terms_by_id as $term) {
2494 $ordered_terms[] = $term;
2495 }
2496
2497 if (count($ordered_terms) !== count($terms)) {
2498 foreach ($terms as $term) {
2499 if (!in_array($term, $ordered_terms, true)) {
2500 $ordered_terms[] = $term;
2501 }
2502 }
2503 }
2504
2505 return $order_setting === 'desc' ? array_reverse($ordered_terms) : $ordered_terms;
2506 }
2507 }
2508
2509 // Built-in sorting fallbacks
2510 usort($terms, function ($a, $b) use ($orderby_setting, $order_setting) {
2511 $get = fn($term, $key) => is_object($term) && isset($term->$key) ? $term->$key : null;
2512
2513 switch ($orderby_setting) {
2514 case 'term_id':
2515 case 'ID':
2516 return ($order_setting === 'desc' ? -1 : 1) * ((int) $get($a, 'term_id') - (int) $get($b, 'term_id'));
2517
2518 case 'count':
2519 return ($order_setting === 'desc' ? -1 : 1) * ((int) $get($a, 'count') - (int) $get($b, 'count'));
2520
2521 case 'name':
2522 $a_name = (string) ($get($a, 'name') ?? '');
2523 $b_name = (string) ($get($b, 'name') ?? '');
2524 return ($order_setting === 'desc')
2525 ? strcasecmp($b_name, $a_name)
2526 : strcasecmp($a_name, $b_name);
2527 }
2528
2529 return 0;
2530 });
2531
2532
2533 if ($orderby_setting === 'random') {
2534 shuffle($terms);
2535 }
2536
2537 return $terms;
2538 }
2539
2540 function taxopress_get_terms_args($args, $taxonomies) {
2541 if (!is_admin()) return $args;
2542
2543 $screen = function_exists('get_current_screen') ? get_current_screen() : null;
2544 if (!$screen || $screen->base !== 'edit-tags') return $args;
2545
2546 $tax = is_array($taxonomies) ? reset($taxonomies) : $taxonomies;
2547 if (!$tax) return $args;
2548
2549 $taxonomy_settings = taxopress_get_all_edited_taxonomy_data();
2550 $settings = $taxonomy_settings[$tax] ?? [];
2551
2552 // Only run if enabled
2553 if (!isset($settings['enable_taxopress_ordering']) || empty($settings['enable_taxopress_ordering'])) {
2554 return $args;
2555 }
2556
2557 // Validate/sanitize
2558 $valid_orderby = ['name', 'term_id', 'ID', 'count', 'random', 'taxopress_term_order'];
2559 $valid_order = ['asc', 'desc'];
2560
2561 $args['orderby'] = isset($settings['orderby']) && in_array($settings['orderby'], $valid_orderby, true)
2562 ? $settings['orderby']
2563 : 'name';
2564
2565 $args['order'] = isset($settings['order']) && in_array(strtolower($settings['order']), $valid_order, true)
2566 ? strtolower($settings['order'])
2567 : 'asc';
2568
2569 return $args;
2570 }
2571
2572 function taxopress_filter_terms($terms, $taxonomies, $args, $term_query) {
2573 $tax = is_array($taxonomies) ? reset($taxonomies) : $taxonomies;
2574 if (!$tax || empty($terms)) return $terms;
2575
2576 $taxonomy_settings = taxopress_get_all_edited_taxonomy_data();
2577 $settings = $taxonomy_settings[$tax] ?? [];
2578
2579 // Only run if enabled
2580 if (!isset($settings['enable_taxopress_ordering']) || empty($settings['enable_taxopress_ordering'])) {
2581 return $terms;
2582 }
2583
2584 return taxopress_sort_terms_by_settings($terms, $tax, $settings, true);
2585 }
2586
2587 function taxopress_terms_order_frontend($terms, $post_id, $taxonomy) {
2588 if (!is_array($terms) || empty($terms) || empty($taxonomy)) return $terms;
2589
2590 $taxonomy_settings = taxopress_get_all_edited_taxonomy_data();
2591 $settings = $taxonomy_settings[$taxonomy] ?? [];
2592
2593 //Only run if enabled
2594 if (!isset($settings['enable_taxopress_ordering']) || empty($settings['enable_taxopress_ordering'])) {
2595 return $terms;
2596 }
2597
2598 return taxopress_sort_terms_by_settings($terms, $taxonomy, $settings, false);
2599 }
2600