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

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