Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/wp-includes/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
4 changes: 4 additions & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
104 changes: 104 additions & 0 deletions src/wp-includes/taxonomy.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
108 changes: 108 additions & 0 deletions tests/phpunit/tests/taxonomy/wpHasMultipleTerms.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* @group taxonomy
* @covers ::wp_has_multiple_terms
*/
class Tests_Taxonomy_WpHasMultipleTerms extends WP_UnitTestCase {

public function set_up() {
parent::set_up();
register_taxonomy( 'wptests_tax', 'post' );
}

public function test_returns_false_when_no_terms_have_posts() {
self::factory()->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' ) );
}
}
Loading