From 42b2dd611c2f57a6cfb4a0e470fef5f12ff614e8 Mon Sep 17 00:00:00 2001 From: sawfly Date: Thu, 26 Nov 2026 16:17:05 +0000 Subject: [PATCH] Filter checks if more than one term of taxonomy has associated posts --- src/wp-includes/category.php | 22 ++++ src/wp-includes/default-filters.php | 4 + src/wp-includes/taxonomy.php | 104 +++++++++++++++++ .../tests/taxonomy/wpHasMultipleTerms.php | 108 ++++++++++++++++++ 4 files changed, 238 insertions(+) create mode 100644 tests/phpunit/tests/taxonomy/wpHasMultipleTerms.php diff --git a/src/wp-includes/category.php b/src/wp-includes/category.php index 79f05c6cfca60..aa20bf8339295 100644 --- a/src/wp-includes/category.php +++ b/src/wp-includes/category.php @@ -410,6 +410,28 @@ function clean_category_cache( $id ) { clean_term_cache( $id, 'category' ); } +/** + * Checks to see if more than one category has associated posts. + * + * @since 7.3.0 + * + * @return bool Whether or not we have more than one categories. + */ +function wp_has_multiple_categories() { + return wp_has_multiple_terms( 'category' ); +} + +/** + * Checks to see if more than one tag has associated posts. + * + * @since 7.3.0 + * + * @return bool Whether or not we have more than one tags. + */ +function wp_has_multiple_tags() { + return wp_has_multiple_terms( 'post_tag' ); +} + /** * Updates category structure to old pre-2.3 from new taxonomy structure. * diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 3c1234f9cfa8e..30d9a66c51b3e 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -697,6 +697,10 @@ // Taxonomy. add_action( 'init', 'create_initial_taxonomies', 0 ); // Highest priority. +add_action( 'edit_term', '_clear_multi_taxonomy_cache_for_term', 10, 3 ); +add_action( 'deleted_term_relationships', '_clear_multi_taxonomy_cache_for_removed_terms', 10, 3 ); +add_action( 'set_object_terms', '_clear_multi_taxonomy_cache_for_object_terms', 10, 4 ); +add_action( 'save_post', '_clear_multi_taxonomy_cache_for_post', 10, 1 ); add_action( 'change_locale', 'create_initial_taxonomies' ); // Canonical. diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 06d1d3581dd00..944bdb441dfdc 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -5329,3 +5329,107 @@ function wp_check_term_meta_support_prefilter( $check ) { return $check; } + +/** + * Checks to see if more than one term of a given taxonomy has associated posts. + * + * @since 7.3.0 + * + * @param string $taxonomy Taxonomy name. + * @return bool Whether or not we have more than one term of the given taxonomy. + */ +function wp_has_multiple_terms( $taxonomy ) { + $wp_has_multiple_terms = get_transient( 'has_multiple_terms_' . $taxonomy ); + if ( false === $wp_has_multiple_terms ) { + $wp_has_multiple_terms = get_terms( + array( + 'taxonomy' => $taxonomy, + 'fields' => 'count', + 'number' => 2, + ) + ); + $wp_has_multiple_terms = $wp_has_multiple_terms > 1 ? 1 : 0; + set_transient( 'has_multiple_terms_' . $taxonomy, $wp_has_multiple_terms ); + } + + /** + * Filters whether the site has more than one term of the given taxonomy with associated posts. + * + * @since 7.3.0 + * + * @param bool $wp_has_multiple_terms Whether $wp_has_multiple_terms should evaluate as true. + * @param string $taxonomy Taxonomy name. + */ + return apply_filters( 'wp_has_multiple_terms', (bool) $wp_has_multiple_terms, $taxonomy ); +} + +/** + * Helper function to clear the cache for a number of taxonomy terms. + * + * @since 7.3.0 + * @access private + * + * @param string|array $taxonomies One or more taxonomy names. + */ +function _clear_multi_taxonomy_cache( $taxonomies ) { + $taxonomies = (array) $taxonomies; + + foreach ( $taxonomies as $taxonomy ) { + delete_transient( 'has_multiple_terms_' . $taxonomy ); + } +} + +/** + * Helper function to clear the $is_multi_taxonomy cache for a term. + * + * @since 7.3.0 + * @access private + * + * @param int $term_id Term ID. + * @param int $tt_id Term taxonomy ID. + * @param string $taxonomy Taxonomy name. + */ +function _clear_multi_taxonomy_cache_for_term( $term_id, $tt_id, $taxonomy ) { + _clear_multi_taxonomy_cache( $taxonomy ); +} + +/** + * Helper function to clear the $is_multi_taxonomy cache for a post. + * + * @since 7.3.0 + * @access private + * + * @param int $post_id Post ID. + */ +function _clear_multi_taxonomy_cache_for_post( $post_id ) { + _clear_multi_taxonomy_cache( get_object_taxonomies( get_post( $post_id ) ) ); +} + +/** + * Clears the multiple terms cache when terms are set on an object. + * + * @since 7.3.0 + * @access private + * + * @param int $object_id Object ID. + * @param array $terms An array of object term IDs or slugs. + * @param array $tt_ids An array of term taxonomy IDs. + * @param string $taxonomy Taxonomy slug. + */ +function _clear_multi_taxonomy_cache_for_object_terms( $object_id, $terms, $tt_ids, $taxonomy ) { + _clear_multi_taxonomy_cache( $taxonomy ); +} + +/** + * Clears the multiple terms cache when terms are removed from an object. + * + * @since 7.3.0 + * @access private + * + * @param int $object_id Object ID. + * @param array $tt_ids An array of term taxonomy IDs. + * @param string $taxonomy Taxonomy slug. + */ +function _clear_multi_taxonomy_cache_for_removed_terms( $object_id, $tt_ids, $taxonomy ) { + _clear_multi_taxonomy_cache( $taxonomy ); +} diff --git a/tests/phpunit/tests/taxonomy/wpHasMultipleTerms.php b/tests/phpunit/tests/taxonomy/wpHasMultipleTerms.php new file mode 100644 index 0000000000000..0c5b1e05cd68e --- /dev/null +++ b/tests/phpunit/tests/taxonomy/wpHasMultipleTerms.php @@ -0,0 +1,108 @@ +term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); + + $this->assertFalse( wp_has_multiple_terms( 'wptests_tax' ) ); + } + + public function test_returns_false_when_only_one_term_has_posts() { + $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); + $post_id = self::factory()->post->create(); + wp_set_object_terms( $post_id, $terms[0], 'wptests_tax' ); + + $this->assertFalse( wp_has_multiple_terms( 'wptests_tax' ) ); + } + + public function test_returns_true_when_two_terms_have_posts() { + $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); + $p1 = self::factory()->post->create(); + $p2 = self::factory()->post->create(); + wp_set_object_terms( $p1, $terms[0], 'wptests_tax' ); + wp_set_object_terms( $p2, $terms[1], 'wptests_tax' ); + + $this->assertTrue( wp_has_multiple_terms( 'wptests_tax' ) ); + } + + public function test_cache_is_cleared_when_post_is_saved() { + $cats = self::factory()->category->create_many( 2 ); + self::factory()->post->create( array( 'post_category' => array( $cats[0] ) ) ); + + $this->assertFalse( wp_has_multiple_categories(), 'Precondition failed.' ); + + self::factory()->post->create( array( 'post_category' => array( $cats[1] ) ) ); + + $this->assertTrue( wp_has_multiple_categories() ); + } + + public function test_cache_is_cleared_when_terms_set_directly() { + $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); + $p1 = self::factory()->post->create(); + $p2 = self::factory()->post->create(); + wp_set_object_terms( $p1, $terms[0], 'wptests_tax' ); + + $this->assertFalse( wp_has_multiple_terms( 'wptests_tax' ), 'Precondition failed.' ); + + wp_set_object_terms( $p2, $terms[1], 'wptests_tax' ); // No save_post here. + + $this->assertTrue( wp_has_multiple_terms( 'wptests_tax' ) ); + } + + public function test_cache_is_cleared_when_term_is_deleted() { + $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); + $p1 = self::factory()->post->create(); + $p2 = self::factory()->post->create(); + wp_set_object_terms( $p1, $terms[0], 'wptests_tax' ); + wp_set_object_terms( $p2, $terms[1], 'wptests_tax' ); + + $this->assertTrue( wp_has_multiple_terms( 'wptests_tax' ), 'Precondition failed.' ); + + wp_delete_term( $terms[1], 'wptests_tax' ); + + $this->assertFalse( wp_has_multiple_terms( 'wptests_tax' ) ); + } + + public function test_cache_is_cleared_when_object_terms_are_removed() { + $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); + $p1 = self::factory()->post->create(); + $p2 = self::factory()->post->create(); + wp_set_object_terms( $p1, $terms[0], 'wptests_tax' ); + wp_set_object_terms( $p2, $terms[1], 'wptests_tax' ); + + $this->assertTrue( wp_has_multiple_terms( 'wptests_tax' ), 'Precondition failed.' ); + + wp_remove_object_terms( $p2, $terms[1], 'wptests_tax' ); + + $this->assertFalse( wp_has_multiple_terms( 'wptests_tax' ) ); + } + + public function test_cache_is_cleared_when_post_is_deleted() { + $terms = self::factory()->term->create_many( 2, array( 'taxonomy' => 'wptests_tax' ) ); + $p1 = self::factory()->post->create(); + $p2 = self::factory()->post->create(); + wp_set_object_terms( $p1, $terms[0], 'wptests_tax' ); + wp_set_object_terms( $p2, $terms[1], 'wptests_tax' ); + + $this->assertTrue( wp_has_multiple_terms( 'wptests_tax' ), 'Precondition failed.' ); + + wp_delete_post( $p2, true ); // Permanent delete, bypasses the trash. + + $this->assertFalse( wp_has_multiple_terms( 'wptests_tax' ) ); + } + + public function test_filter_overrides_result() { + add_filter( 'wp_has_multiple_terms', '__return_true' ); + + $this->assertTrue( wp_has_multiple_terms( 'wptests_tax' ) ); + } +}