PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.38.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.38.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.38.0, at inc/functions.inc.php

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