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

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

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