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 / hidden-terms.php

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