100) { return; } $taxonomies = array_keys(get_taxonomies(['public' => true, 'show_ui' => true])); foreach ($taxonomies as $taxonomy) { $offset = 0; $limit = 5000; $all_hidden_terms = []; do { $terms_id = $wpdb->get_col($wpdb->prepare( "SELECT term_id FROM $wpdb->term_taxonomy WHERE taxonomy = %s AND count < %d LIMIT %d OFFSET %d", $taxonomy, $min_usage, $limit, $offset )); if (!empty($terms_id)) { $all_hidden_terms = array_merge($all_hidden_terms, $terms_id); clean_term_cache($terms_id, $taxonomy); } $offset += $limit; } while (!empty($terms_id)); // Check existing transient and update only if changed $existing_hidden_terms = get_transient('taxopress_hidden_terms_' . $taxonomy); if ($existing_hidden_terms === false || array_diff($all_hidden_terms, $existing_hidden_terms) || array_diff($existing_hidden_terms, $all_hidden_terms)) { set_transient('taxopress_hidden_terms_' . $taxonomy, $all_hidden_terms, DAY_IN_SECONDS); } } return true; } public static function taxopress_modify_hidden_term_links($term_link, $term, $taxonomy) { if (!(int) SimpleTags_Plugin::get_option_value('enable_hidden_terms')) { return $term_link; } static $hidden_terms_cache = []; if (!isset($hidden_terms_cache[$taxonomy])) { $hidden_terms_cache[$taxonomy] = get_transient('taxopress_hidden_terms_' . $taxonomy); } if (!empty($hidden_terms_cache[$taxonomy]) && in_array($term->term_id, $hidden_terms_cache[$taxonomy])) { return home_url(); } return $term_link; } /** * Remove hidden terms from the list on the frontend. * * @param array $terms List of terms. * @param int $post_id Post ID. * @param string $taxonomy Taxonomy slug. * @return array Modified list of terms. */ public static function taxopress_remove_hidden_terms($terms, $post_id, $taxonomy) { if (!(int) SimpleTags_Plugin::get_option_value('enable_hidden_terms')) { return $terms; } if (!is_admin() && !empty($terms)) { $hidden_terms = get_transient('taxopress_hidden_terms_' . $taxonomy); if (!empty($hidden_terms)) { $hidden_terms_lookup = array_flip($hidden_terms); $terms = array_filter($terms, function ($term) use ($hidden_terms_lookup) { return !isset($hidden_terms_lookup[$term->term_id]); }); // Re-index the array to prevent issues with numeric keys $terms = array_values($terms); } } return $terms; } /** * Run the hidden terms update when settings are saved. */ public function taxopress_update_hidden_terms_immediately() { if ((int) SimpleTags_Plugin::get_option_value('enable_hidden_terms')) { $this->taxopress_set_hidden_terms(); } } } SimpleTags_Hidden_Terms::get_instance(); } ?>