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 / functions.inc.php

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

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