PluginProbe
PublishPress Taxonomies – Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 4.0.0
PublishPress Taxonomies – Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v4.0.0
4.0.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 All 179 releases
simple-tags / inc / functions.inc.php

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

1,022 lines 31.6 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 PublishPress Taxonomies 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 PublishPress Taxonomies 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 PublishPress Taxonomies
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 PublishPress Taxonomies 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 PublishPress Taxonomies is
619 * pro version
620 *
621 * @return void
622 */
623 function taxopress_is_pro_version()
624 {
625 // Keep the legacy constant as a fallback for existing TaxoPress Pro integrations.
626 return defined('PUBLISHPRESS_TAXONOMIES_PRO_VERSION') || defined('TAXOPRESS_PRO_VERSION');
627 }
628
629 /**
630 * Case insensitive in_array function
631 *
632 * @param string $needle
633 * @param array $haystack
634 * @return bool
635 */
636 function taxopress_in_array_i($needle, $haystack)
637 {
638 return in_array(strtolower($needle), array_map('strtolower', $haystack));
639 }
640
641 /**
642 * Check if synonyms is enabled
643 *
644 * @return bool
645 */
646 function taxopress_is_synonyms_enabled()
647 {
648 return ((int) SimpleTags_Plugin::get_option_value('active_features_synonyms') === 1);
649 }
650
651 /**
652 * Check if linked term is enabled
653 *
654 * @return bool
655 */
656 function taxopress_is_linked_terms_enabled()
657 {
658 return ((int) SimpleTags_Plugin::get_option_value('active_features_linked_terms') === 1);
659 }
660
661 /**
662 * Get term synonyms
663 *
664 * @param string|integer $term
665 * @param string $taxonomy
666 * @return array $term_synonyms
667 */
668 function taxopress_get_term_synonyms($term, $taxonomy = '')
669 {
670 $term_synonyms = [];
671
672 if (!taxopress_is_synonyms_enabled()) {
673 // simply return empty array if feature is disabled
674 return $term_synonyms;
675 }
676
677 if ((int)$term > 0) {
678 $term_synonyms = (array) get_term_meta($term, '_taxopress_term_synonyms', true);
679 } else {
680 $terms_object = get_term_by('name', esc_attr($term), $taxonomy);
681 if (is_object($terms_object) && isset($terms_object->term_id)) {
682 $term_synonyms = (array) get_term_meta($terms_object->term_id, '_taxopress_term_synonyms', true);
683 }
684 }
685 $term_synonyms = array_filter($term_synonyms);
686
687 return $term_synonyms;
688 }
689
690 /**
691 * Get linked terms
692 *
693 * @param int|string $term_id
694 * @param string $taxonomy
695 * @param bool $term_object
696 * @param string $linked_terms_type
697 * @return array $linked_terms
698 */
699 function taxopress_get_linked_terms($term_id, $taxonomy = '', $term_object = false, $linked_terms_type = 'main')
700 {
701 global $wpdb;
702
703 $linked_terms = [];
704
705 if (!taxopress_is_pro_version() || !taxopress_is_linked_terms_enabled()) {
706 // Simply return an empty array if the feature is disabled
707 return $linked_terms;
708 }
709
710 $table_name = $wpdb->prefix . 'taxopress_linked_terms';
711
712 if ($linked_terms_type == 'primary') {
713 $query = $wpdb->prepare(
714 "SELECT * FROM $table_name WHERE term_id = %d",
715 $term_id
716 );
717 } elseif ($linked_terms_type == 'secondary') {
718 $query = $wpdb->prepare(
719 "SELECT * FROM $table_name WHERE linked_term_id = %d",
720 $term_id
721 );
722 } else {
723 $query = $wpdb->prepare(
724 "SELECT * FROM $table_name WHERE term_id = %d OR linked_term_id = %d",
725 $term_id,
726 $term_id
727 );
728 }
729
730 $linked_terms = $wpdb->get_results($query);
731
732 return $linked_terms;
733 }
734
735
736 /**
737 * Find out which is a linked term in our data
738 */
739 function taxopress_get_linked_term_data($linked_term_option, $term_id)
740 {
741
742 if ((int)$linked_term_option->linked_term_id === (int)$term_id) {
743 $taxopress_linked_term_id = $linked_term_option->term_id;
744 $taxopress_linked_term_name = $linked_term_option->term_name;
745 $taxopress_linked_term_taxonomy = $linked_term_option->term_taxonomy;
746 } else {
747 $taxopress_linked_term_id = $linked_term_option->linked_term_id;
748 $taxopress_linked_term_name = $linked_term_option->linked_term_name;
749 $taxopress_linked_term_taxonomy = $linked_term_option->linked_term_taxonomy;
750 }
751
752 $linked_term = (object) [
753 'term_id' => $taxopress_linked_term_id,
754 'term_name' => $taxopress_linked_term_name,
755 'term_taxonomy' => $taxopress_linked_term_taxonomy
756 ];
757
758 return $linked_term;
759 }
760
761 /**
762 * Add linked terms and it synonyms to list
763 *
764 * @param array $lists
765 * @param string|integer $term
766 * @param string $lists
767 * @param bool $linked
768 * @param bool $named_term
769 *
770 * @return array $term_object
771 */
772 function taxopress_add_linked_term_options($lists, $term, $taxonomy, $linked = false, $named_term = false)
773 {
774
775 if (!taxopress_is_linked_terms_enabled()) {
776 // simply return $lists if feature is disabled
777 return $lists;
778 }
779
780 if ((int)$term > 0) {
781 $term_id = $term;
782 } else {
783 $terms_object = get_term_by('name', esc_attr($term), $taxonomy);
784 if (is_object($terms_object) && isset($terms_object->term_id)) {
785 $term_id = $terms_object->term_id;
786 } else {
787 $term_id = 0;
788 }
789 }
790
791 if ($term_id > 0) {
792 // get linked terms
793 $linked_terms_type = SimpleTags_Plugin::get_option_value('linked_terms_type');
794 $linked_terms = taxopress_get_linked_terms($term_id, $taxonomy, true, $linked_terms_type);
795 if (!empty($linked_terms)) {
796 if (!empty($linked_terms)) {
797 foreach ($linked_terms as $linked_term) {
798 // skip this if term and linked term is not in the same category as linked terms is now category wide.
799 if ($linked_term->term_taxonomy !== $linked_term->linked_term_taxonomy) {
800 continue;
801 }
802 $linked_term_data = taxopress_get_linked_term_data($linked_term, $term_id);
803 $linked_term_name = stripslashes($linked_term_data->term_name);
804 $linked_term_id = $linked_term_data->term_id;
805 if ($linked) {
806 $term_value = get_term_link($linked_term->term_id, $linked_term_data->term_taxonomy);
807 if (is_wp_error($term_value)) {
808 $term_value = '';
809 }
810 } elseif ($named_term) {
811 $term_value = $linked_term_name;
812 } else {
813 $term_value = $linked_term_id;
814 }
815
816 // add linked term
817 $lists[$linked_term_name] = $term_value;
818
819 // add linked term synonyms
820 $term_synonyms = taxopress_get_term_synonyms($linked_term_id);
821 if (!empty($term_synonyms)) {
822 foreach ($term_synonyms as $term_synonym) {
823 $lists[$term_synonym] = $term_value;
824 }
825 }
826 }
827 }
828 }
829 }
830
831 return $lists;
832 }
833
834 /**
835 * Fetch our PublishPress Taxonomies SuggestTerms option.
836 * SuggestTerms screen has been removed but we need this function
837 * to migrate the needed settings.
838 *
839 * @return mixed
840 */
841 function taxopress_get_suggestterm_data()
842 {
843 return array_filter((array)apply_filters(
844 'taxopress_get_suggestterm_data',
845 get_option('taxopress_suggestterms', []),
846 get_current_blog_id()
847 ));
848 }
849
850 function taxopress_get_all_wp_roles()
851 {
852 global $wp_roles;
853
854 if (!isset($wp_roles)) {
855 $wp_roles = new \WP_Roles();
856 }
857
858 return $wp_roles->roles;
859 }
860
861 /**
862 * Check if current user can manage PublishPress Taxonomies metabox
863 */
864 function can_manage_taxopress_metabox($user_id = false)
865 {
866 $can_manage = false;
867
868 if (!$user_id) {
869 $user_id = get_current_user_id();
870 }
871
872 $user = get_userdata($user_id);
873 if (is_object($user) && isset($user->roles)) {
874 foreach ($user->roles as $role_name) {
875 if (!empty(SimpleTags_Plugin::get_option_value('enable_' . $role_name . '_metabox'))) {
876 $can_manage = true;
877 break;
878 }
879 }
880 }
881
882 return $can_manage;
883 }
884
885 /**
886 * Check if current user can manage metabox taxonomy
887 */
888 function can_manage_taxopress_metabox_taxonomy($taxonomy, $user_id = false, $preview_role = '')
889 {
890
891 if (!empty($preview_role)) {
892 $role_options = (array) SimpleTags_Plugin::get_option_value('enable_metabox_' . $preview_role . '');
893 return in_array($taxonomy, $role_options, true);
894 }
895
896 $can_manage = false;
897
898 if (!$user_id) {
899 $user_id = get_current_user_id();
900 }
901
902 $user = get_userdata($user_id);
903 if (is_object($user) && isset($user->roles)) {
904 foreach ($user->roles as $role_name) {
905 $role_options = (array) SimpleTags_Plugin::get_option_value('enable_metabox_' . $role_name . '');
906 if (in_array($taxonomy, $role_options, true)) {
907 $can_manage = true;
908 break;
909 }
910 }
911 }
912
913 return $can_manage;
914 }
915
916 /**
917 * Check if current user can edit (rename) PublishPress Taxonomies metabox labels.
918 * Only administrators can edit labels.
919 *
920 * @param int|false $user_id
921 * @return bool
922 */
923 function can_edit_taxopress_metabox_labels($user_id = false)
924 {
925 if (!$user_id) {
926 $user_id = get_current_user_id();
927 }
928
929 // Check if user is an administrator
930 return user_can($user_id, 'administrator');
931 }
932
933
934 /**
935 * Get all the taxonomy removed for current user
936 */
937 function taxopress_user_role_removed_taxonomy($user_id = false)
938 {
939
940 $removed_taxonomies_tax = [];
941 $removed_taxonomies_css = [];
942 if (!$user_id) {
943 $user_id = get_current_user_id();
944 }
945
946 $user = get_userdata($user_id);
947 if (is_object($user) && isset($user->roles)) {
948 foreach ($user->roles as $role_name) {
949 $role_options = (array) SimpleTags_Plugin::get_option_value('remove_taxonomy_metabox_' . $role_name . '');
950 $role_options = array_filter($role_options);
951 if (!empty($role_options)) {
952 foreach ($role_options as $removed_tax) {
953 $removed_taxonomies_tax[] = $removed_tax;
954 if ($removed_tax == 'category') {
955 $removed_taxonomies_css[] = '#category-add-toggle, #categories, #categorydiv, #categorydivsb, th.column-categories, td.categories, #screen-options-wrap label[for=categorydiv-hide]';
956 } elseif ($removed_tax == 'post_tag') {
957 $removed_taxonomies_css[] = '#tags, #tagsdiv,#tagsdivsb,#tagsdiv-post_tag, th.column-tags, td.tags, #screen-options-wrap label[for=tagsdiv-post_tag-hide]';
958 } else {
959 $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]";
960 }
961 }
962 }
963 }
964 }
965
966 $removed_taxonomies = [
967 'taxonomies' => $removed_taxonomies_tax,
968 'custom_css' => $removed_taxonomies_css
969 ];
970
971 return $removed_taxonomies;
972 }
973
974 /**
975 * Get post statuses
976 */
977 function taxopress_get_post_statuses()
978 {
979
980 $post_statuses = get_post_stati(array(), 'objects');
981
982 return $post_statuses;
983 }
984
985 /**
986 * Check if the current user role matches the given role name
987 *
988 * @param string $role_name
989 * @return bool
990 */
991 function taxopress_is_current_user_role($role_name)
992 {
993 if (is_user_logged_in()) {
994 $user = wp_get_current_user();
995 return in_array($role_name, (array) $user->roles);
996 }
997 return false;
998 }
999
1000 /**
1001 * Check if the current user can manage specific metabox tabs
1002 *
1003 * @param array $tabs
1004 * @return array
1005 */
1006 function can_manage_taxopress_metabox_tabs($tabs)
1007 {
1008 $user = wp_get_current_user();
1009 $role_name = $user->roles[0] ?? false;
1010
1011 if ($role_name) {
1012 $restrict_metabox = SimpleTags_Plugin::get_option_value('enable_restrict' . $role_name . '_metabox');
1013 if ($restrict_metabox && taxopress_is_current_user_role($role_name)) {
1014 $tabs['post_terms']['enabled'] = true;
1015 $tabs['suggest_local_terms']['enabled'] = true;
1016 $tabs['create_term']['enabled'] = false;
1017 }
1018 }
1019
1020 return $tabs;
1021 }
1022