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

929 lines 29.8 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_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, 3));
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 * Enhanced sanitization function specifically for post types and post status arrays
450 * to prevent SQL injection vulnerabilities
451 *
452 * @param array|string $data The data to sanitize
453 * @return array|string Sanitized data
454 */
455 function taxopress_sanitize_post_type_status($data)
456 {
457 if (is_array($data)) {
458 return array_map('sanitize_key', $data);
459 }
460
461 return sanitize_key($data);
462 }
463
464 /**
465 * Dashboard items
466 *
467 * @param mixed $current
468 *
469 * @return array
470 */
471 function taxopress_dashboard_options()
472 {
473
474 $features = [];
475
476 $features['st_taxonomies'] = [
477 'label' => esc_html__('Taxonomies', 'simple-tags'),
478 'description' => esc_html__('This feature allows you to create new taxonomies and edit all the settings for each taxonomy.', 'simple-tags'),
479 'option_key' => 'active_taxonomies',
480 ];
481
482 $features['st_terms'] = [
483 'label' => esc_html__('Terms', 'simple-tags'),
484 'description' => esc_html__('This feature allows you to search and edit all the terms on your site.', 'simple-tags'),
485 'option_key' => 'active_st_terms',
486 ];
487
488 $features['st_posts'] = [
489 'label' => esc_html__('Posts', 'simple-tags'),
490 'description' => esc_html__('This feature allows you to search for terms and see all the posts attached to that term.', 'simple-tags'),
491 'option_key' => 'active_st_posts',
492 ];
493
494 $features['st_terms_display'] = [
495 'label' => esc_html__('Terms Display', 'simple-tags'),
496 'description' => esc_html__('This feature allows you to create a customizable display of all the terms in one taxonomy.', 'simple-tags'),
497 'option_key' => 'active_terms_display',
498 ];
499
500 $features['st_post_tags'] = [
501 'label' => esc_html__('Terms for Current Post', 'simple-tags'),
502 'description' => esc_html__('This feature allows you create a customizable display of all the terms assigned to the current post.', 'simple-tags'),
503 'option_key' => 'active_post_tags',
504 ];
505
506 $features['st_related_posts'] = [
507 'label' => esc_html__('Related Posts', 'simple-tags'),
508 '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'),
509 'option_key' => 'active_related_posts_new',
510 ];
511
512 $features['st_autolinks'] = [
513 'label' => esc_html__('Auto Links', 'simple-tags'),
514 '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'),
515 'option_key' => 'active_auto_links',
516 ];
517
518 $features['st_autoterms'] = [
519 'label' => esc_html__('Auto Terms', 'simple-tags'),
520 'description' => esc_html__('Auto Terms can scan your content and automatically assign new and existing terms.', 'simple-tags'),
521 'option_key' => 'active_auto_terms',
522 ];
523
524 $features['st_mass_terms'] = [
525 'label' => esc_html__('Mass Edit Terms', 'simple-tags'),
526 'description' => esc_html__('This feature allows you to quickly edit the terms attached to multiple posts at the same time.', 'simple-tags'),
527 'option_key' => 'active_mass_edit',
528 ];
529
530 $features['st_manage'] = [
531 'label' => esc_html__('Manage Terms', 'simple-tags'),
532 'description' => esc_html__('This feature allows you to add, rename, merge, and delete terms for any taxonomy.', 'simple-tags'),
533 'option_key' => 'active_manage',
534 ];
535
536 $features = apply_filters('taxopress_dashboard_features', $features);
537
538 return $features;
539 }
540
541 /**
542 * Check if current version of taxopress is
543 * pro version
544 *
545 * @return void
546 */
547 function taxopress_is_pro_version()
548 {
549 return defined('TAXOPRESS_PRO_VERSION');
550 }
551
552 /**
553 * Case insensitive in_array function
554 *
555 * @param string $needle
556 * @param array $haystack
557 * @return bool
558 */
559 function taxopress_in_array_i($needle, $haystack) {
560 return in_array(strtolower($needle), array_map('strtolower', $haystack));
561 }
562
563 /**
564 * Check if synonyms is enabled
565 *
566 * @return bool
567 */
568 function taxopress_is_synonyms_enabled() {
569 return ((int) SimpleTags_Plugin::get_option_value('active_features_synonyms') === 1);
570 }
571
572 /**
573 * Check if linked term is enabled
574 *
575 * @return bool
576 */
577 function taxopress_is_linked_terms_enabled() {
578 return ((int) SimpleTags_Plugin::get_option_value('active_features_linked_terms') === 1);
579 }
580
581 /**
582 * Get term synonyms
583 *
584 * @param string|integer $term
585 * @param string $taxonomy
586 * @return array $term_synonyms
587 */
588 function taxopress_get_term_synonyms($term, $taxonomy = '') {
589 $term_synonyms = [];
590
591 if (!taxopress_is_synonyms_enabled()) {
592 // simply return empty array if feature is disabled
593 return $term_synonyms;
594 }
595
596 if ((int)$term > 0) {
597 $term_synonyms = (array) get_term_meta($term, '_taxopress_term_synonyms', true);
598 } else {
599 $terms_object = get_term_by('name', esc_attr($term), $taxonomy);
600 if (is_object($terms_object) && isset($terms_object->term_id)) {
601 $term_synonyms = (array) get_term_meta($terms_object->term_id, '_taxopress_term_synonyms', true);
602 }
603 }
604 $term_synonyms = array_filter($term_synonyms);
605
606 return $term_synonyms;
607 }
608
609 /**
610 * Get linked terms
611 *
612 * @param int|string $term_id
613 * @param string $taxonomy
614 * @param bool $term_object
615 * @param string $linked_terms_type
616 * @return array $linked_terms
617 */
618 function taxopress_get_linked_terms($term_id, $taxonomy = '', $term_object = false, $linked_terms_type = 'main') {
619 global $wpdb;
620
621 $linked_terms = [];
622
623 if (!taxopress_is_pro_version() || !taxopress_is_linked_terms_enabled()) {
624 // Simply return an empty array if the feature is disabled
625 return $linked_terms;
626 }
627
628 $table_name = $wpdb->prefix . 'taxopress_linked_terms';
629
630 if ($linked_terms_type == 'primary') {
631 $query = $wpdb->prepare(
632 "SELECT * FROM $table_name WHERE term_id = %d",
633 $term_id
634 );
635 } elseif ($linked_terms_type == 'secondary') {
636 $query = $wpdb->prepare(
637 "SELECT * FROM $table_name WHERE linked_term_id = %d",
638 $term_id
639 );
640 } else {
641 $query = $wpdb->prepare(
642 "SELECT * FROM $table_name WHERE term_id = %d OR linked_term_id = %d",
643 $term_id,
644 $term_id
645 );
646 }
647
648 $linked_terms = $wpdb->get_results($query);
649
650 return $linked_terms;
651 }
652
653
654 /**
655 * Find out which is a linked term in our data
656 */
657 function taxopress_get_linked_term_data($linked_term_option, $term_id) {
658
659 if ((int)$linked_term_option->linked_term_id === (int)$term_id) {
660 $taxopress_linked_term_id = $linked_term_option->term_id;
661 $taxopress_linked_term_name = $linked_term_option->term_name;
662 $taxopress_linked_term_taxonomy = $linked_term_option->term_taxonomy;
663 } else {
664 $taxopress_linked_term_id = $linked_term_option->linked_term_id;
665 $taxopress_linked_term_name = $linked_term_option->linked_term_name;
666 $taxopress_linked_term_taxonomy = $linked_term_option->linked_term_taxonomy;
667 }
668
669 $linked_term = (object) [
670 'term_id' => $taxopress_linked_term_id,
671 'term_name' => $taxopress_linked_term_name,
672 'term_taxonomy' => $taxopress_linked_term_taxonomy
673 ];
674
675 return $linked_term;
676 }
677
678 /**
679 * Add linked terms and it synonyms to list
680 *
681 * @param array $lists
682 * @param string|integer $term
683 * @param string $lists
684 * @param bool $linked
685 * @param bool $named_term
686 *
687 * @return array $term_object
688 */
689 function taxopress_add_linked_term_options($lists, $term, $taxonomy, $linked = false, $named_term = false) {
690
691 if (!taxopress_is_linked_terms_enabled()) {
692 // simply return $lists if feature is disabled
693 return $lists;
694 }
695
696 if ((int)$term > 0) {
697 $term_id = $term;
698 } else {
699 $terms_object = get_term_by('name', esc_attr($term), $taxonomy);
700 if (is_object($terms_object) && isset($terms_object->term_id)) {
701 $term_id = $terms_object->term_id;
702 } else {
703 $term_id = 0;
704 }
705 }
706
707 if ($term_id > 0) {
708 // get linked terms
709 $linked_terms_type = SimpleTags_Plugin::get_option_value('linked_terms_type');
710 $linked_terms = taxopress_get_linked_terms($term_id, $taxonomy, true, $linked_terms_type);
711 if (!empty($linked_terms)) {
712 if (!empty($linked_terms)) {
713 foreach ($linked_terms as $linked_term) {
714 // skip this if term and linked term is not in the same category as linked terms is now category wide.
715 if ($linked_term->term_taxonomy !== $linked_term->linked_term_taxonomy) {
716 continue;
717 }
718 $linked_term_data = taxopress_get_linked_term_data($linked_term, $term_id);
719 $linked_term_name = stripslashes($linked_term_data->term_name);
720 $linked_term_id = $linked_term_data->term_id;
721 if ($linked) {
722 $term_value = get_term_link($linked_term->term_id, $linked_term_data->term_taxonomy);
723 if (is_wp_error($term_value)) {
724 $term_value = '';
725 }
726 } elseif ($named_term) {
727 $term_value = $linked_term_name;
728 } else {
729 $term_value = $linked_term_id;
730 }
731
732 // add linked term
733 $lists[$linked_term_name] = $term_value;
734
735 // add linked term synonyms
736 $term_synonyms = taxopress_get_term_synonyms($linked_term_id);
737 if (!empty($term_synonyms)) {
738 foreach ($term_synonyms as $term_synonym) {
739 $lists[$term_synonym] = $term_value;
740 }
741 }
742 }
743 }
744 }
745 }
746
747 return $lists;
748 }
749
750 /**
751 * Fetch our TAXOPRESS SuggestTerms option.
752 * SuggestTerms screen has been removed but we need this function
753 * to migrate the needed settings.
754 *
755 * @return mixed
756 */
757 function taxopress_get_suggestterm_data()
758 {
759 return array_filter((array)apply_filters('taxopress_get_suggestterm_data', get_option('taxopress_suggestterms', []),
760 get_current_blog_id()));
761 }
762
763 function taxopress_get_all_wp_roles() {
764 global $wp_roles;
765
766 if (!isset($wp_roles)) {
767 $wp_roles = new \WP_Roles();
768 }
769
770 return $wp_roles->roles;
771 }
772
773 /**
774 * Check if current user can manage taxopress metabox
775 */
776 function can_manage_taxopress_metabox($user_id = false) {
777 $can_manage = false;
778
779 if (!$user_id) {
780 $user_id = get_current_user_id();
781 }
782
783 $user = get_userdata($user_id);
784 if (is_object($user) && isset($user->roles)) {
785 foreach ($user->roles as $role_name) {
786 if (!empty(SimpleTags_Plugin::get_option_value('enable_' . $role_name . '_metabox'))) {
787 $can_manage = true;
788 break;
789 }
790 }
791
792 }
793
794 return $can_manage;
795 }
796
797 /**
798 * Check if current user can manage metabox taxonomy
799 */
800 function can_manage_taxopress_metabox_taxonomy($taxonomy, $user_id = false, $preview_role = '') {
801
802 if (!empty($preview_role)) {
803 $role_options = (array) SimpleTags_Plugin::get_option_value('enable_metabox_' . $preview_role . '');
804 return in_array($taxonomy, $role_options, true);
805 }
806
807 $can_manage = false;
808
809 if (!$user_id) {
810 $user_id = get_current_user_id();
811 }
812
813 $user = get_userdata($user_id);
814 if (is_object($user) && isset($user->roles)) {
815 foreach ($user->roles as $role_name) {
816 $role_options = (array) SimpleTags_Plugin::get_option_value('enable_metabox_' . $role_name . '');
817 if (in_array($taxonomy, $role_options, true)) {
818 $can_manage = true;
819 break;
820 }
821 }
822
823 }
824
825 return $can_manage;
826 }
827
828 /**
829 * Check if current user can edit (rename) TaxoPress metabox labels.
830 * Only administrators can edit labels.
831 *
832 * @param int|false $user_id
833 * @return bool
834 */
835 function can_edit_taxopress_metabox_labels($user_id = false) {
836 if (!$user_id) {
837 $user_id = get_current_user_id();
838 }
839
840 // Check if user is an administrator
841 return user_can($user_id, 'administrator');
842 }
843
844
845 /**
846 * Get all the taxonomy removed for current user
847 */
848 function taxopress_user_role_removed_taxonomy($user_id = false) {
849
850 $removed_taxonomies_tax = [];
851 $removed_taxonomies_css = [];
852 if (!$user_id) {
853 $user_id = get_current_user_id();
854 }
855
856 $user = get_userdata($user_id);
857 if (is_object($user) && isset($user->roles)) {
858 foreach ($user->roles as $role_name) {
859 $role_options = (array) SimpleTags_Plugin::get_option_value('remove_taxonomy_metabox_' . $role_name . '');
860 $role_options = array_filter($role_options);
861 if (!empty($role_options)) {
862 foreach ($role_options as $removed_tax) {
863 $removed_taxonomies_tax[] = $removed_tax;
864 if ($removed_tax == 'category') {
865 $removed_taxonomies_css[] = '#category-add-toggle, #categories, #categorydiv, #categorydivsb, th.column-categories, td.categories, #screen-options-wrap label[for=categorydiv-hide]';
866 } elseif ($removed_tax == 'post_tag') {
867 $removed_taxonomies_css[] = '#tags, #tagsdiv,#tagsdivsb,#tagsdiv-post_tag, th.column-tags, td.tags, #screen-options-wrap label[for=tagsdiv-post_tag-hide]';
868 } else {
869 $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]";
870 }
871 }
872 }
873 }
874
875 }
876
877 $removed_taxonomies = [
878 'taxonomies' => $removed_taxonomies_tax,
879 'custom_css' => $removed_taxonomies_css
880 ];
881
882 return $removed_taxonomies;
883 }
884
885 /**
886 * Get post statuses
887 */
888 function taxopress_get_post_statuses() {
889
890 $post_statuses = get_post_stati( array(), 'objects' );
891
892 return $post_statuses;
893 }
894
895 /**
896 * Check if the current user role matches the given role name
897 *
898 * @param string $role_name
899 * @return bool
900 */
901 function taxopress_is_current_user_role($role_name) {
902 if (is_user_logged_in()) {
903 $user = wp_get_current_user();
904 return in_array($role_name, (array) $user->roles);
905 }
906 return false;
907 }
908
909 /**
910 * Check if the current user can manage specific metabox tabs
911 *
912 * @param array $tabs
913 * @return array
914 */
915 function can_manage_taxopress_metabox_tabs($tabs) {
916 $user = wp_get_current_user();
917 $role_name = $user->roles[0] ?? false;
918
919 if ($role_name) {
920 $restrict_metabox = SimpleTags_Plugin::get_option_value('enable_restrict' . $role_name . '_metabox');
921 if ($restrict_metabox && taxopress_is_current_user_role($role_name)) {
922 $tabs['post_terms']['enabled'] = true;
923 $tabs['suggest_local_terms']['enabled'] = true;
924 $tabs['create_term']['enabled'] = false;
925 }
926 }
927
928 return $tabs;
929 }