PluginProbe
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms / 3.51.0
Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms v3.51.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 / hidden-terms.php

hidden-terms.php in Tag, Category, and Taxonomy Manager – Autotagger Automatically Add Terms 3.51.0, at inc/hidden-terms.php

160 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!class_exists('SimpleTags_Hidden_Terms')) {
4 class SimpleTags_Hidden_Terms
5 {
6 public const MENU_SLUG = 'st_options';
7
8 // class instance
9 public static $instance;
10
11 public static function get_instance()
12 {
13 if (! isset(self::$instance)) {
14 self::$instance = new self();
15 }
16
17 return self::$instance;
18 }
19
20 /**
21 * Class Constructor
22 */
23 public function __construct()
24 {
25
26 add_action('taxopress_settings_saved', [$this, 'taxopress_schedule_hidden_terms_cron']);
27 add_action('taxopress_update_hidden_terms_event', [$this, 'taxopress_set_hidden_terms']);
28 add_filter('term_link', [$this, 'taxopress_modify_hidden_term_links'], 10, 3);
29 add_filter('get_the_terms', [$this,'taxopress_remove_hidden_terms'], 10, 3);
30 add_action('taxopress_settings_saved', [$this, 'taxopress_update_hidden_terms_immediately']);
31
32 }
33
34 /**
35 * Schedule the cron job to update hidden terms once a day if enabled
36 */
37 public function taxopress_schedule_hidden_terms_cron()
38 {
39 if ((int) SimpleTags_Plugin::get_option_value('enable_hidden_terms')) {
40 if (!wp_next_scheduled('taxopress_update_hidden_terms_event')) {
41 wp_schedule_event(time(), 'daily', 'taxopress_update_hidden_terms_event');
42 }
43 } else {
44 wp_clear_scheduled_hook('taxopress_update_hidden_terms_event');
45 }
46 }
47
48 public function taxopress_set_hidden_terms($taxonomy = 'post_tag', $min_usage = 0)
49 {
50 if (!(int) SimpleTags_Plugin::get_option_value('enable_hidden_terms')) {
51 return;
52 }
53 global $wpdb;
54
55 $min_usage = (int) SimpleTags_Plugin::get_option_value('hide-rarely');
56 if ((int) $min_usage > 100) {
57 return;
58 }
59
60 $taxonomies = array_keys(get_taxonomies(['public' => true, 'show_ui' => true]));
61
62 foreach ($taxonomies as $taxonomy) {
63 $offset = 0;
64 $limit = 5000;
65 $all_hidden_terms = [];
66
67 do {
68 $terms_id = $wpdb->get_col($wpdb->prepare(
69 "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND count < %d LIMIT %d OFFSET %d",
70 $taxonomy,
71 $min_usage,
72 $limit,
73 $offset
74 ));
75
76 if (!empty($terms_id)) {
77 $all_hidden_terms = array_merge($all_hidden_terms, $terms_id);
78 clean_term_cache($terms_id, $taxonomy);
79 }
80
81 $offset += $limit;
82 } while (!empty($terms_id));
83
84 // Check existing transient and update only if changed
85 $existing_hidden_terms = get_transient('taxopress_hidden_terms_' . $taxonomy);
86 if ($existing_hidden_terms === false || array_diff($all_hidden_terms, $existing_hidden_terms) || array_diff($existing_hidden_terms, $all_hidden_terms)) {
87 set_transient('taxopress_hidden_terms_' . $taxonomy, $all_hidden_terms, DAY_IN_SECONDS);
88 }
89 }
90
91 return true;
92 }
93
94 public static function taxopress_modify_hidden_term_links($term_link, $term, $taxonomy)
95 {
96 if (!(int) SimpleTags_Plugin::get_option_value('enable_hidden_terms')) {
97 return $term_link;
98 }
99
100 static $hidden_terms_cache = [];
101
102 if (!isset($hidden_terms_cache[$taxonomy])) {
103 $hidden_terms_cache[$taxonomy] = get_transient('taxopress_hidden_terms_' . $taxonomy);
104 }
105
106 if (!empty($hidden_terms_cache[$taxonomy]) && in_array($term->term_id, $hidden_terms_cache[$taxonomy])) {
107 return home_url();
108 }
109
110 return $term_link;
111 }
112
113 /**
114 * Remove hidden terms from the list on the frontend.
115 *
116 * @param array $terms List of terms.
117 * @param int $post_id Post ID.
118 * @param string $taxonomy Taxonomy slug.
119 * @return array Modified list of terms.
120 */
121 public static function taxopress_remove_hidden_terms($terms, $post_id, $taxonomy)
122 {
123 if (!(int) SimpleTags_Plugin::get_option_value('enable_hidden_terms')) {
124 return $terms;
125 }
126
127 if (!is_admin() && !empty($terms)) {
128 $hidden_terms = get_transient('taxopress_hidden_terms_' . $taxonomy);
129
130 if (!empty($hidden_terms)) {
131 $hidden_terms_lookup = array_flip($hidden_terms);
132
133 $terms = array_filter($terms, function ($term) use ($hidden_terms_lookup) {
134 return !isset($hidden_terms_lookup[$term->term_id]);
135 });
136
137 // Re-index the array to prevent issues with numeric keys
138 $terms = array_values($terms);
139 }
140 }
141
142 return $terms;
143 }
144
145
146 /**
147 * Run the hidden terms update when settings are saved.
148 */
149 public function taxopress_update_hidden_terms_immediately()
150 {
151 if ((int) SimpleTags_Plugin::get_option_value('enable_hidden_terms')) {
152 $this->taxopress_set_hidden_terms();
153 }
154 }
155
156 }
157
158 SimpleTags_Hidden_Terms::get_instance();
159 }
160