PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.51.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.51.0
3.54.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 All 178 releases
simple-tags / inc / functions.inc.php

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

1,022 lines 32.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * trim and remove empty element
5 *
6 * @param string $element
7 *
8 * @return string
9 */
10 function _delete_empty_element($element)
11 {
12 $element = stripslashes($element);
13 $element = trim($element);
14 if (!empty($element)) {
15 return $element;
16 }
17
18 return false;
19 }
20
21 /**
22 * Test if page have tags or not...
23 *
24 * @return boolean
25 * @author WebFactory Ltd
26 */
27 function is_page_have_tags()
28 {
29 $taxonomies = get_object_taxonomies('page');
30
31 return in_array('post_tag', $taxonomies, true);
32 }
33
34 /**
35 * Register widget on WP
36 */
37 function st_register_widget()
38 {
39 register_widget('SimpleTags_Widget');
40 if (1 === (int) SimpleTags_Plugin::get_option_value('active_terms_display')) {
41 register_widget('SimpleTags_Shortcode_Widget');
42 }
43 if (1 === (int) SimpleTags_Plugin::get_option_value('active_post_tags')) {
44 register_widget('SimpleTags_PostTags_Widget');
45 }
46 if (1 === (int) SimpleTags_Plugin::get_option_value('active_related_posts_new')) {
47 register_widget('SimpleTags_RelatedPosts_Widget');
48 }
49 }
50
51 function taxopress_menu_separator($identifier, $parent)
52 {
53 // create a separator menu
54 $separator_menu = [];
55 $separator_menu[] = '';
56 $separator_menu[] = $parent;
57 $separator_menu[] = $parent . $identifier;
58 $separator_menu[] = '';
59 $separator_menu[] = 'taxopress-menu-separator ' . $parent . $identifier;
60
61 return $separator_menu;
62 }
63
64 /**
65 * Method for adding default hidden columns to the TaxoPress Terms screen.
66 */
67 add_filter('default_hidden_columns', 'taxopress_terms_default_hidden_columns', 10, 2);
68 function taxopress_terms_default_hidden_columns($hidden, $screen)
69 {
70 if (isset($screen->id) && $screen->id === 'taxopress_page_st_terms') {
71 $hidden[] = 'taxopress_custom_url';
72 }
73 return $hidden;
74 }
75
76 /**
77 * Change menu item order
78 */
79 add_action('admin_menu', 'taxopress_re_order_menu', 15);
80 function taxopress_re_order_menu()
81 {
82 global $submenu;
83
84 foreach ($submenu as $menuName => $menuItems) {
85 if ('st_options' === $menuName) {
86 $taxopress_settings = $taxopress_dashboard = $taxopress_taxonomies = $taxopress_linked_terms = $taxopress_autoterms_schedule = $taxopress_upgrade = false;
87
88 $taxopress_submenus = $submenu['st_options'];
89 $dashboard_options = taxopress_dashboard_options();
90 $dashboard_option_pages = array_keys($dashboard_options);
91 foreach ($taxopress_submenus as $key => $taxopress_submenu) {
92 $slug_ = $taxopress_submenu[2];
93 $parent_ = $taxopress_submenu[1];
94 if (in_array($slug_, $dashboard_option_pages)) {
95 if (1 === (int) SimpleTags_Plugin::get_option_value($dashboard_options[$slug_]['option_key'])) {
96 $showHide = '';
97 } else {
98 $showHide = ' taxopress-hide-menu-item';
99 }
100 $taxopress_submenus[$key][4] = $slug_ . '-menu-item' . $showHide;
101 }
102
103 if ($slug_ === 'st_options') { //settings
104 $taxopress_settings = $taxopress_submenus[$key];
105 unset($taxopress_submenus[$key]);
106 }
107 if ($slug_ === 'st_taxonomies') { //taxonomies
108 $taxopress_taxonomies = $taxopress_submenus[$key];
109 unset($taxopress_submenus[$key]);
110 }
111 if ($slug_ === 'st_dashboard') { //dashboard
112 $taxopress_dashboard = $taxopress_submenus[$key];
113 unset($taxopress_submenus[$key]);
114 }
115 if ($slug_ === 'st_linked_terms') { //linked terms
116 $taxopress_linked_terms = $taxopress_submenus[$key];
117 unset($taxopress_submenus[$key]);
118 }
119 if ($slug_ === 'st_autoterms_schedule') { //autoterms schedule
120 $taxopress_autoterms_schedule = $taxopress_submenus[$key];
121 unset($taxopress_submenus[$key]);
122 }
123
124 if ($slug_ === 'st_options-menu-upgrade-link') { //upgrade to pro link
125 $taxopress_upgrade = $taxopress_submenus[$key];
126 unset($taxopress_submenus[$key]);
127 }
128 }
129 // add taxonomies first so it can fall as second item after dashboard
130 if ($taxopress_taxonomies) {
131 $taxopress_submenus = array_merge([$taxopress_taxonomies], $taxopress_submenus);
132 }
133 // add dashboard as first item
134 if ($taxopress_dashboard) {
135 $taxopress_submenus = array_merge([$taxopress_dashboard], $taxopress_submenus);
136 }
137 // add linked terms
138 if ($taxopress_linked_terms) {
139 $taxopress_submenus = taxopress_add_at_menu_index(['st_terms', 'st_taxonomies', 'st_dashboard'], $taxopress_linked_terms, $taxopress_submenus);
140 }
141 // add autoterms schedule
142 if ($taxopress_autoterms_schedule) {
143 $taxopress_submenus = taxopress_add_at_menu_index(['st_autoterms', 'st_autoterms_content', 'st_taxopress_ai'], $taxopress_autoterms_schedule, $taxopress_submenus);
144 }
145 // add settings as last item
146 if ($taxopress_settings) {
147 $taxopress_submenus = array_merge($taxopress_submenus, [$taxopress_settings]);
148 }
149 // upgrade to pro should be last item if exists
150 if ($taxopress_upgrade) {
151 $taxopress_submenus = array_merge($taxopress_submenus, [$taxopress_upgrade]);
152 }
153
154 //resort array
155 ksort($taxopress_submenus);
156
157 // add separator 1 to menus
158 $separator1_positions = ['st_posts', 'st_linked_terms', 'st_terms', 'st_taxonomies', 'st_dashboard'];
159 foreach ($separator1_positions as $pos) {
160 $index = array_search($pos, array_column($taxopress_submenus, 2));
161 if ($index !== false) {
162 $separator = taxopress_menu_separator('st_separator_end', 'simple_tags');
163 array_splice($taxopress_submenus, $index + 1, 0, [$separator]);
164 break;
165 }
166 }
167
168 // Add separator 2 to menus
169 $separator2_positions = ['st_autolinks', 'st_related_posts', 'st_post_tags', 'st_terms_display'];
170 foreach ($separator2_positions as $pos) {
171 $index = array_search($pos, array_column($taxopress_submenus, 2));
172 if ($index !== false) {
173 $separator = taxopress_menu_separator('st_separator_end', 'simple_tags');
174 array_splice($taxopress_submenus, $index + 1, 0, [$separator]);
175 break;
176 }
177 }
178
179 // Add separator 3 to menus
180 $separator2_positions = ['st_autoterms_content', 'st_autoterms_schedule', 'st_autoterms'];
181 foreach ($separator2_positions as $pos) {
182 $index = array_search($pos, array_column($taxopress_submenus, 2));
183 if ($index !== false) {
184 $separator = taxopress_menu_separator('st_separator_end', 'simple_tags');
185 array_splice($taxopress_submenus, $index + 1, 0, [$separator]);
186 break;
187 }
188 }
189
190 if (!taxopress_is_pro_version()) {
191 // Add separator 4 to menus
192 $index = array_search('st_options', array_column($taxopress_submenus, 3));
193 if ($index !== false) {
194 $separator = taxopress_menu_separator('st_separator_end', 'simple_tags');
195 array_splice($taxopress_submenus, $index + 1, 0, [$separator]);
196 }
197 }
198
199 //resort array
200 ksort($taxopress_submenus);
201 $submenu['st_options'] = $taxopress_submenus;
202
203 break;
204 }
205 }
206 }
207
208 add_action('admin_init', 'taxopress_suppress_tag_groups_notice_on_taxopress_screens', 1);
209 function taxopress_suppress_tag_groups_notice_on_taxopress_screens()
210 {
211 if (! is_admin()) {
212 return;
213 }
214
215 if (
216 ! defined('TAG_GROUPS_FILE')
217 && ! defined('TAG_GROUPS_PLUGIN_BASENAME')
218 && ! defined('TAG_GROUPS_PLUGIN_ABSOLUTE_PATH')
219 && ! function_exists('tag_groups_init')
220 ) {
221 return;
222 }
223
224 if (! class_exists('PublishPress\\WordpressVersionNotices\\Module\\TopNotice\\Module')) {
225 return;
226 }
227
228 add_filter(
229 \PublishPress\WordpressVersionNotices\Module\TopNotice\Module::SETTINGS_FILTER,
230 function ($settings) {
231 if (! is_admin()) {
232 return $settings;
233 }
234
235 $isTaxoPressScreen = false;
236
237 if (function_exists('get_current_screen')) {
238 $screen = get_current_screen();
239 if ($screen && isset($screen->base)) {
240 $base = (string) $screen->base;
241 $isTaxoPressScreen = (
242 strpos($base, 'taxopress_page_') === 0
243 || $base === 'toplevel_page_st_options'
244 || $base === 'toplevel_page_st_posts'
245 );
246 }
247 }
248
249 if (! $isTaxoPressScreen) {
250 $page = isset($_GET['page']) ? sanitize_key($_GET['page']) : '';
251
252 if (
253 $page !== ''
254 && (
255 strpos($page, 'st_') === 0
256 || $page === 'st_options'
257 || $page === 'st_posts'
258 )
259 ) {
260 $isTaxoPressScreen = true;
261 }
262 }
263
264 if ($isTaxoPressScreen && isset($settings['taxopress-tag_groups'])) {
265 unset($settings['taxopress-tag_groups']);
266 }
267
268 return $settings;
269 },
270 9999
271 );
272 }
273
274 function taxopress_add_at_menu_index($key_options, $new_menu, $existing_menus)
275 {
276
277 foreach ($key_options as $key_option) {
278 $index = array_search($key_option, array_column($existing_menus, 2));
279 if ($index !== false) {
280 $index++;//add it after
281 array_splice($existing_menus, $index, 0, [$new_menu]);
282 break;
283 }
284 }
285 // add as last if index is false;
286 if ($index === false) {
287 $existing_menus = array_merge($existing_menus, [$new_menu]);
288 }
289
290 return $existing_menus;
291 }
292
293 // Init TaxoPress
294 function init_simple_tags()
295 {
296 new SimpleTags_Client();
297 new SimpleTags_Client_TagCloud();
298
299 if (1 === (int) SimpleTags_Plugin::get_option_value('active_auto_terms')) {
300 new SimpleTags_Client_Schedule();
301 }
302
303 // Admin and XML-RPC
304 if (is_admin()) {
305 require STAGS_DIR . '/inc/class.admin.php';
306 new SimpleTags_Admin();
307 }
308
309 add_action('widgets_init', 'st_register_widget');
310 }
311
312 function taxopress_admin_pages()
313 {
314
315 $taxopress_pages = [
316 'st_dashboard',
317 'st_mass_terms',
318 'st_auto',
319 'st_options',
320 'st_manage',
321 'st_taxonomies',
322 'st_terms_display',
323 'st_post_tags',
324 'st_related_posts',
325 'st_autolinks',
326 'st_autoterms',
327 'st_autoterms_content',
328 'st_autoterms_schedule',
329 'st_terms',
330 'st_posts',
331 'st_taxopress_ai'
332 ];
333
334 return apply_filters('taxopress_admin_pages', $taxopress_pages);
335 }
336
337 function is_taxopress_admin_page()
338 {
339
340 $taxopress_pages = taxopress_admin_pages();
341
342 $is_taxopress_page = false;
343 if (isset($_GET['page']) && in_array($_GET['page'], $taxopress_pages)) {
344 $is_taxopress_page = true;
345 }
346
347 return apply_filters('is_taxopress_admin_page', $is_taxopress_page);
348 }
349
350 function taxopress_starts_with($haystack, $needle)
351 {
352 $length = strlen($needle);
353 return substr($haystack, 0, $length) === $needle;
354 }
355
356 function taxopress_is_html($string)
357 {
358 return preg_match("/<[^<]+>/", $string, $m) != 0;
359 }
360
361
362
363 function taxopress_is_screen_main_page()
364 {
365
366 if (isset($_GET['action']) && $_GET['action'] === 'edit') {
367 return false;
368 }
369
370 if (isset($_GET['add']) && $_GET['add'] === 'new_item') {
371 return false;
372 }
373
374 if (isset($_GET['add']) && $_GET['add'] === 'taxonomy') {
375 return false;
376 }
377
378 return true;
379 }
380
381
382
383 function taxopress_format_class($class)
384 {
385 return esc_attr(ltrim($class, '.'));
386 }
387
388 function taxopress_add_class_to_format($xformat, $class)
389 {
390 $classes = $class;
391 $html = $xformat;
392
393 $patterns = array();
394 $replacements = array();
395
396 // matches where anchor has existing classes contained in single quotes
397 $patterns[0] = '/<a([^>]*)class=\'([^\']*)\'([^>]*)>/';
398 $replacements[0] = '<a\1class="' . $classes . ' \2"\3>';
399
400 // matches where anchor has existing classes contained in double quotes
401 $patterns[1] = '/<a([^>]*)class="([^"]*)"([^>]*)>/';
402 $replacements[1] = '<a\1class="' . $classes . ' \2"\3>';
403
404 // matches where anchor tag has no existing classes
405 $patterns[2] = '/<a(?![^>]*class)([^>]*)>/';
406 $replacements[2] = '<a\1 class="' . $classes . '">';
407
408 $html = preg_replace($patterns, $replacements, $html);
409
410 return $html;
411 }
412
413 function taxopress_change_to_strings($join)
414 {
415 if (is_array($join)) {
416 $join = join(", ", $join);
417 }
418 return $join;
419 }
420
421 function taxopress_change_to_array($array)
422 {
423 if (!is_array($array)) {
424 $array = preg_split("/\s*,\s*/", $array);
425 }
426 return $array;
427 }
428
429 function taxopress_html_character_and_entity($enity_code_as_key = false)
430 {
431 #Source https://dev.w3.org/html5/html-author/charref
432 $character_set = [
433 //'&amp;' => '&#38;',
434 '&lt;' => '&#60;',
435 '&gt;' => '&#62;',
436 '&nbsp;' => '&#160;',
437 '&excl;' => '&#33;',
438 '&quot;' => '&#34;',
439 '&num;' => '&#35;',
440 '&dollar;' => '&#36;',
441 '&percnt;' => '&#37;',
442 '&apos;' => '&#39;',
443 '&lpar;' => '&#40;',
444 '&rpar;' => '&#41;',
445 '&ast; ' => '&#42;',
446 '&plus;' => '&#43;',
447 '&comma;' => '&#44;',
448 '&period;' => '&#46;',
449 '&sol;' => '&#47;',
450 '&colon;' => '&#58',
451 '&semi;' => '&#59;',
452 '&equals;' => '&#61;',
453 '&quest;' => '&#63;',
454 '&commat;' => '&#64;',
455 '&lsqb;' => '&#91;',
456 '&bsol;' => '&#92;',
457 '&rsqb;' => '&#93;',
458 '&Hat;' => '&#94;',
459 '&lowbar;' => '&#95;',
460 '&grave;' => '&#96;',
461 '&lcub;' => '&#123;',
462 '&verbar;' => '&#124;',
463 '&rcub;' => '&#125;',
464 '&iexcl;' => '&#161;',
465 '&cent;' => '&#162;',
466 '&pound;' => '&#163;',
467 '&copy;' => '&#169;',
468 '&ordf;' => '&#170;',
469 '&laquo;' => '&#171;',
470 '&reg;' => '&#174;',
471 '&deg;' => '&#176;',
472 '&plusmn;' => '&#177;',
473 '&sup2;' => '&#178;',
474 '&sup3;' => '&#179;',
475 '&acute;' => '&#180;',
476 '&sup1;' => '&#185;',
477 '&ordm;' => '&#186;',
478 '&raquo;' => '&#187;',
479 '&frac14;' => '&#188;',
480 '&frac12;' => '&#189;',
481 '&frac34;' => '&#190;',
482 '&rarr;' => '&#8594;',
483 '&larr;' => '&#8592;',
484 '&uarr;' => '&#8593;',
485 '&darr;' => '&#8595;',
486 '&trade;' => '&#8482;',
487 '&rsquo;' => '&#8217;',
488 '&rsquor;' => '&#8217;',
489 '&lsquo;' => '&#8216;',
490 '&lsquor;' => '&#8218;',
491 '&amp;' => 'taxopressamp',
492 ];
493
494 if ($enity_code_as_key) {
495 $character_set = array_flip($character_set);
496 }
497
498 return $character_set;
499 }
500
501 /**
502 * Sanitize taxopress text field
503 *
504 * @param string $content
505 * @return string
506 */
507 function taxopress_sanitize_text_field($content)
508 {
509
510 if (is_array($content)) {
511 return stripslashes_deep(array_map('taxopress_sanitize_text_field', $content));
512 }
513
514 $content = stripslashes_deep($content);
515
516 $content = wp_kses_post($content);
517
518 return $content;
519 }
520
521
522 /**
523 * Enhanced sanitization function specifically for post types and post status arrays
524 * to prevent SQL injection vulnerabilities
525 *
526 * @param array|string $data The data to sanitize
527 * @return array|string Sanitized data
528 */
529 function taxopress_sanitize_post_type_status($data)
530 {
531 if (is_array($data)) {
532 return array_map('sanitize_key', $data);
533 }
534
535 return sanitize_key($data);
536 }
537
538 /**
539 * Dashboard items
540 *
541 * @param mixed $current
542 *
543 * @return array
544 */
545 function taxopress_dashboard_options()
546 {
547
548 $features = [];
549
550 $features['st_taxonomies'] = [
551 'label' => esc_html__('Taxonomies', 'simple-tags'),
552 'description' => esc_html__('This feature allows you to create new taxonomies and edit all the settings for each taxonomy.', 'simple-tags'),
553 'option_key' => 'active_taxonomies',
554 ];
555
556 $features['st_terms'] = [
557 'label' => esc_html__('Terms', 'simple-tags'),
558 'description' => esc_html__('This feature allows you to search and edit all the terms on your site.', 'simple-tags'),
559 'option_key' => 'active_st_terms',
560 ];
561
562 $features['st_posts'] = [
563 'label' => esc_html__('Posts', 'simple-tags'),
564 'description' => esc_html__('This feature allows you to search for terms and see all the posts attached to that term.', 'simple-tags'),
565 'option_key' => 'active_st_posts',
566 ];
567
568 $features['st_terms_display'] = [
569 'label' => esc_html__('Terms Display', 'simple-tags'),
570 'description' => esc_html__('This feature allows you to create a customizable display of all the terms in one taxonomy.', 'simple-tags'),
571 'option_key' => 'active_terms_display',
572 ];
573
574 $features['st_post_tags'] = [
575 'label' => esc_html__('Terms for Current Post', 'simple-tags'),
576 'description' => esc_html__('This feature allows you create a customizable display of all the terms assigned to the current post.', 'simple-tags'),
577 'option_key' => 'active_post_tags',
578 ];
579
580 $features['st_related_posts'] = [
581 'label' => esc_html__('Related Posts', 'simple-tags'),
582 'description' => esc_html__('This feature creates a display of similar posts. If a post has the terms “WordPress” and “Website”, Related Posts will display other posts with those same terms.', 'simple-tags'),
583 'option_key' => 'active_related_posts_new',
584 ];
585
586 $features['st_autolinks'] = [
587 'label' => esc_html__('Auto Links', 'simple-tags'),
588 'description' => esc_html__('This feature automatically adds links to your chosen terms. If you have a term called “WordPress”, Auto Links finds the word “WordPress” in your content and add links to the archive page for that term.', 'simple-tags'),
589 'option_key' => 'active_auto_links',
590 ];
591
592 $features['st_autoterms'] = [
593 'label' => esc_html__('Auto Terms', 'simple-tags'),
594 'description' => esc_html__('Auto Terms can scan your content and automatically assign new and existing terms.', 'simple-tags'),
595 'option_key' => 'active_auto_terms',
596 ];
597
598 $features['st_mass_terms'] = [
599 'label' => esc_html__('Mass Edit Terms', 'simple-tags'),
600 'description' => esc_html__('This feature allows you to quickly edit the terms attached to multiple posts at the same time.', 'simple-tags'),
601 'option_key' => 'active_mass_edit',
602 ];
603
604 $features['st_manage'] = [
605 'label' => esc_html__('Manage Terms', 'simple-tags'),
606 'description' => esc_html__('This feature allows you to add, rename, merge, and delete terms for any taxonomy.', 'simple-tags'),
607 'option_key' => 'active_manage',
608 ];
609
610 $features = apply_filters('taxopress_dashboard_features', $features);
611
612 return $features;
613 }
614
615 /**
616 * Check if current version of taxopress is
617 * pro version
618 *
619 * @return void
620 */
621 function taxopress_is_pro_version()
622 {
623 return defined('TAXOPRESS_PRO_VERSION');
624 }
625
626 /**
627 * Case insensitive in_array function
628 *
629 * @param string $needle
630 * @param array $haystack
631 * @return bool
632 */
633 function taxopress_in_array_i($needle, $haystack)
634 {
635 return in_array(strtolower($needle), array_map('strtolower', $haystack));
636 }
637
638 /**
639 * Check if synonyms is enabled
640 *
641 * @return bool
642 */
643 function taxopress_is_synonyms_enabled()
644 {
645 return ((int) SimpleTags_Plugin::get_option_value('active_features_synonyms') === 1);
646 }
647
648 /**
649 * Check if linked term is enabled
650 *
651 * @return bool
652 */
653 function taxopress_is_linked_terms_enabled()
654 {
655 return ((int) SimpleTags_Plugin::get_option_value('active_features_linked_terms') === 1);
656 }
657
658 /**
659 * Get term synonyms
660 *
661 * @param string|integer $term
662 * @param string $taxonomy
663 * @return array $term_synonyms
664 */
665 function taxopress_get_term_synonyms($term, $taxonomy = '')
666 {
667 $term_synonyms = [];
668
669 if (!taxopress_is_synonyms_enabled()) {
670 // simply return empty array if feature is disabled
671 return $term_synonyms;
672 }
673
674 if ((int)$term > 0) {
675 $term_synonyms = (array) get_term_meta($term, '_taxopress_term_synonyms', true);
676 } else {
677 $terms_object = get_term_by('name', esc_attr($term), $taxonomy);
678 if (is_object($terms_object) && isset($terms_object->term_id)) {
679 $term_synonyms = (array) get_term_meta($terms_object->term_id, '_taxopress_term_synonyms', true);
680 }
681 }
682 $term_synonyms = array_filter($term_synonyms);
683
684 return $term_synonyms;
685 }
686
687 /**
688 * Get linked terms
689 *
690 * @param int|string $term_id
691 * @param string $taxonomy
692 * @param bool $term_object
693 * @param string $linked_terms_type
694 * @return array $linked_terms
695 */
696 function taxopress_get_linked_terms($term_id, $taxonomy = '', $term_object = false, $linked_terms_type = 'main')
697 {
698 global $wpdb;
699
700 $linked_terms = [];
701
702 if (!taxopress_is_pro_version() || !taxopress_is_linked_terms_enabled()) {
703 // Simply return an empty array if the feature is disabled
704 return $linked_terms;
705 }
706
707 $table_name = $wpdb->prefix . 'taxopress_linked_terms';
708
709 if ($linked_terms_type == 'primary') {
710 $query = $wpdb->prepare(
711 "SELECT * FROM $table_name WHERE term_id = %d",
712 $term_id
713 );
714 } elseif ($linked_terms_type == 'secondary') {
715 $query = $wpdb->prepare(
716 "SELECT * FROM $table_name WHERE linked_term_id = %d",
717 $term_id
718 );
719 } else {
720 $query = $wpdb->prepare(
721 "SELECT * FROM $table_name WHERE term_id = %d OR linked_term_id = %d",
722 $term_id,
723 $term_id
724 );
725 }
726
727 $linked_terms = $wpdb->get_results($query);
728
729 return $linked_terms;
730 }
731
732
733 /**
734 * Find out which is a linked term in our data
735 */
736 function taxopress_get_linked_term_data($linked_term_option, $term_id)
737 {
738
739 if ((int)$linked_term_option->linked_term_id === (int)$term_id) {
740 $taxopress_linked_term_id = $linked_term_option->term_id;
741 $taxopress_linked_term_name = $linked_term_option->term_name;
742 $taxopress_linked_term_taxonomy = $linked_term_option->term_taxonomy;
743 } else {
744 $taxopress_linked_term_id = $linked_term_option->linked_term_id;
745 $taxopress_linked_term_name = $linked_term_option->linked_term_name;
746 $taxopress_linked_term_taxonomy = $linked_term_option->linked_term_taxonomy;
747 }
748
749 $linked_term = (object) [
750 'term_id' => $taxopress_linked_term_id,
751 'term_name' => $taxopress_linked_term_name,
752 'term_taxonomy' => $taxopress_linked_term_taxonomy
753 ];
754
755 return $linked_term;
756 }
757
758 /**
759 * Add linked terms and it synonyms to list
760 *
761 * @param array $lists
762 * @param string|integer $term
763 * @param string $lists
764 * @param bool $linked
765 * @param bool $named_term
766 *
767 * @return array $term_object
768 */
769 function taxopress_add_linked_term_options($lists, $term, $taxonomy, $linked = false, $named_term = false)
770 {
771
772 if (!taxopress_is_linked_terms_enabled()) {
773 // simply return $lists if feature is disabled
774 return $lists;
775 }
776
777 if ((int)$term > 0) {
778 $term_id = $term;
779 } else {
780 $terms_object = get_term_by('name', esc_attr($term), $taxonomy);
781 if (is_object($terms_object) && isset($terms_object->term_id)) {
782 $term_id = $terms_object->term_id;
783 } else {
784 $term_id = 0;
785 }
786 }
787
788 if ($term_id > 0) {
789 // get linked terms
790 $linked_terms_type = SimpleTags_Plugin::get_option_value('linked_terms_type');
791 $linked_terms = taxopress_get_linked_terms($term_id, $taxonomy, true, $linked_terms_type);
792 if (!empty($linked_terms)) {
793 if (!empty($linked_terms)) {
794 foreach ($linked_terms as $linked_term) {
795 // skip this if term and linked term is not in the same category as linked terms is now category wide.
796 if ($linked_term->term_taxonomy !== $linked_term->linked_term_taxonomy) {
797 continue;
798 }
799 $linked_term_data = taxopress_get_linked_term_data($linked_term, $term_id);
800 $linked_term_name = stripslashes($linked_term_data->term_name);
801 $linked_term_id = $linked_term_data->term_id;
802 if ($linked) {
803 $term_value = get_term_link($linked_term->term_id, $linked_term_data->term_taxonomy);
804 if (is_wp_error($term_value)) {
805 $term_value = '';
806 }
807 } elseif ($named_term) {
808 $term_value = $linked_term_name;
809 } else {
810 $term_value = $linked_term_id;
811 }
812
813 // add linked term
814 $lists[$linked_term_name] = $term_value;
815
816 // add linked term synonyms
817 $term_synonyms = taxopress_get_term_synonyms($linked_term_id);
818 if (!empty($term_synonyms)) {
819 foreach ($term_synonyms as $term_synonym) {
820 $lists[$term_synonym] = $term_value;
821 }
822 }
823 }
824 }
825 }
826 }
827
828 return $lists;
829 }
830
831 /**
832 * Fetch our TAXOPRESS SuggestTerms option.
833 * SuggestTerms screen has been removed but we need this function
834 * to migrate the needed settings.
835 *
836 * @return mixed
837 */
838 function taxopress_get_suggestterm_data()
839 {
840 return array_filter((array)apply_filters(
841 'taxopress_get_suggestterm_data',
842 get_option('taxopress_suggestterms', []),
843 get_current_blog_id()
844 ));
845 }
846
847 function taxopress_get_all_wp_roles()
848 {
849 global $wp_roles;
850
851 if (!isset($wp_roles)) {
852 $wp_roles = new \WP_Roles();
853 }
854
855 return $wp_roles->roles;
856 }
857
858 /**
859 * Check if current user can manage taxopress metabox
860 */
861 function can_manage_taxopress_metabox($user_id = false)
862 {
863 $can_manage = false;
864
865 if (!$user_id) {
866 $user_id = get_current_user_id();
867 }
868
869 $user = get_userdata($user_id);
870 if (is_object($user) && isset($user->roles)) {
871 foreach ($user->roles as $role_name) {
872 if (!empty(SimpleTags_Plugin::get_option_value('enable_' . $role_name . '_metabox'))) {
873 $can_manage = true;
874 break;
875 }
876 }
877
878 }
879
880 return $can_manage;
881 }
882
883 /**
884 * Check if current user can manage metabox taxonomy
885 */
886 function can_manage_taxopress_metabox_taxonomy($taxonomy, $user_id = false, $preview_role = '')
887 {
888
889 if (!empty($preview_role)) {
890 $role_options = (array) SimpleTags_Plugin::get_option_value('enable_metabox_' . $preview_role . '');
891 return in_array($taxonomy, $role_options, true);
892 }
893
894 $can_manage = false;
895
896 if (!$user_id) {
897 $user_id = get_current_user_id();
898 }
899
900 $user = get_userdata($user_id);
901 if (is_object($user) && isset($user->roles)) {
902 foreach ($user->roles as $role_name) {
903 $role_options = (array) SimpleTags_Plugin::get_option_value('enable_metabox_' . $role_name . '');
904 if (in_array($taxonomy, $role_options, true)) {
905 $can_manage = true;
906 break;
907 }
908 }
909
910 }
911
912 return $can_manage;
913 }
914
915 /**
916 * Check if current user can edit (rename) TaxoPress metabox labels.
917 * Only administrators can edit labels.
918 *
919 * @param int|false $user_id
920 * @return bool
921 */
922 function can_edit_taxopress_metabox_labels($user_id = false)
923 {
924 if (!$user_id) {
925 $user_id = get_current_user_id();
926 }
927
928 // Check if user is an administrator
929 return user_can($user_id, 'administrator');
930 }
931
932
933 /**
934 * Get all the taxonomy removed for current user
935 */
936 function taxopress_user_role_removed_taxonomy($user_id = false)
937 {
938
939 $removed_taxonomies_tax = [];
940 $removed_taxonomies_css = [];
941 if (!$user_id) {
942 $user_id = get_current_user_id();
943 }
944
945 $user = get_userdata($user_id);
946 if (is_object($user) && isset($user->roles)) {
947 foreach ($user->roles as $role_name) {
948 $role_options = (array) SimpleTags_Plugin::get_option_value('remove_taxonomy_metabox_' . $role_name . '');
949 $role_options = array_filter($role_options);
950 if (!empty($role_options)) {
951 foreach ($role_options as $removed_tax) {
952 $removed_taxonomies_tax[] = $removed_tax;
953 if ($removed_tax == 'category') {
954 $removed_taxonomies_css[] = '#category-add-toggle, #categories, #categorydiv, #categorydivsb, th.column-categories, td.categories, #screen-options-wrap label[for=categorydiv-hide]';
955 } elseif ($removed_tax == 'post_tag') {
956 $removed_taxonomies_css[] = '#tags, #tagsdiv,#tagsdivsb,#tagsdiv-post_tag, th.column-tags, td.tags, #screen-options-wrap label[for=tagsdiv-post_tag-hide]';
957 } else {
958 $removed_taxonomies_css[] = "#{$removed_tax}, #{$removed_tax}div,#{$removed_tax}divsb,#tagsdiv-{$removed_tax}, th.column-{$removed_tax}, td.{$removed_tax}, #screen-options-wrap label[for=tagsdiv-{$removed_tax}-hide], #screen-options-wrap label[for={$removed_tax}div-hide]";
959 }
960 }
961 }
962 }
963
964 }
965
966 $removed_taxonomies = [
967 'taxonomies' => $removed_taxonomies_tax,
968 'custom_css' => $removed_taxonomies_css
969 ];
970
971 return $removed_taxonomies;
972 }
973
974 /**
975 * Get post statuses
976 */
977 function taxopress_get_post_statuses()
978 {
979
980 $post_statuses = get_post_stati(array(), 'objects');
981
982 return $post_statuses;
983 }
984
985 /**
986 * Check if the current user role matches the given role name
987 *
988 * @param string $role_name
989 * @return bool
990 */
991 function taxopress_is_current_user_role($role_name)
992 {
993 if (is_user_logged_in()) {
994 $user = wp_get_current_user();
995 return in_array($role_name, (array) $user->roles);
996 }
997 return false;
998 }
999
1000 /**
1001 * Check if the current user can manage specific metabox tabs
1002 *
1003 * @param array $tabs
1004 * @return array
1005 */
1006 function can_manage_taxopress_metabox_tabs($tabs)
1007 {
1008 $user = wp_get_current_user();
1009 $role_name = $user->roles[0] ?? false;
1010
1011 if ($role_name) {
1012 $restrict_metabox = SimpleTags_Plugin::get_option_value('enable_restrict' . $role_name . '_metabox');
1013 if ($restrict_metabox && taxopress_is_current_user_role($role_name)) {
1014 $tabs['post_terms']['enabled'] = true;
1015 $tabs['suggest_local_terms']['enabled'] = true;
1016 $tabs['create_term']['enabled'] = false;
1017 }
1018 }
1019
1020 return $tabs;
1021 }
1022