Update individual term caches in get_terms()
.
This was removed in [29915] as part of the attempt to add cache support to `get_term_by()`. When that support was removed in [30900], it was not properly restored. This changeset includes a unit test to verify that the cache is properly primed for terms found in `get_terms()`, as well as tests to verify the other cache setting that was touched by [30900]. Fixes #30749. See #21760. git-svn-id: https://develop.svn.wordpress.org/trunk@30954 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
c7d2f360cb
commit
b824d6f56c
@ -1912,6 +1912,9 @@ function get_terms( $taxonomies, $args = '' ) {
|
||||
}
|
||||
|
||||
$terms = $wpdb->get_results($query);
|
||||
if ( 'all' == $_fields ) {
|
||||
update_term_cache( $terms );
|
||||
}
|
||||
|
||||
if ( empty($terms) ) {
|
||||
wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );
|
||||
|
@ -93,4 +93,111 @@ class Tests_Term_Cache extends WP_UnitTestCase {
|
||||
|
||||
_unregister_taxonomy( $tax );
|
||||
}
|
||||
|
||||
public function test_get_term_should_update_term_cache_when_passed_an_object() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$term = $this->factory->term->create( array(
|
||||
'taxonomy' => 'wptests_tax',
|
||||
) );
|
||||
|
||||
$term_object = get_term( $term, 'wptests_tax' );
|
||||
wp_cache_delete( $term, 'wptests_tax' );
|
||||
|
||||
// Affirm that the cache is empty.
|
||||
$this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
// get_term() will only be update the cache if the 'filter' prop is unset.
|
||||
unset( $term_object->filter );
|
||||
|
||||
$term_object_2 = get_term( $term_object, 'wptests_tax' );
|
||||
|
||||
// No new queries should have fired.
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
$this->assertEquals( $term_object, $term_object_2 );
|
||||
}
|
||||
|
||||
public function test_get_term_should_update_term_cache_when_passed_a_valid_term_identifier() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$term = $this->factory->term->create( array(
|
||||
'taxonomy' => 'wptests_tax',
|
||||
) );
|
||||
|
||||
wp_cache_delete( $term, 'wptests_tax' );
|
||||
|
||||
// Affirm that the cache is empty.
|
||||
$this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
// Prime cache.
|
||||
$term_object = get_term( $term, 'wptests_tax' );
|
||||
$this->assertNotEmpty( wp_cache_get( $term, 'wptests_tax' ) );
|
||||
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
|
||||
|
||||
$term_object_2 = get_term( $term, 'wptests_tax' );
|
||||
|
||||
// No new queries should have fired.
|
||||
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
|
||||
$this->assertEquals( $term_object, $term_object_2 );
|
||||
}
|
||||
|
||||
public function test_get_term_by_should_update_term_cache_when_passed_a_valid_term_identifier() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
$term = $this->factory->term->create( array(
|
||||
'taxonomy' => 'wptests_tax',
|
||||
) );
|
||||
|
||||
wp_cache_delete( $term, 'wptests_tax' );
|
||||
|
||||
// Affirm that the cache is empty.
|
||||
$this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
// Prime cache.
|
||||
$term_object = get_term_by( 'id', $term, 'wptests_tax' );
|
||||
$this->assertNotEmpty( wp_cache_get( $term, 'wptests_tax' ) );
|
||||
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
|
||||
|
||||
$term_object_2 = get_term( $term, 'wptests_tax' );
|
||||
|
||||
// No new queries should have fired.
|
||||
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
|
||||
$this->assertEquals( $term_object, $term_object_2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 30749
|
||||
*/
|
||||
public function test_get_terms_should_update_cache_for_located_terms() {
|
||||
global $wpdb;
|
||||
|
||||
register_taxonomy( 'wptests_tax', 'post' );
|
||||
|
||||
$terms = $this->factory->term->create_many( 5, array(
|
||||
'taxonomy' => 'wptests_tax',
|
||||
) );
|
||||
|
||||
$term_objects = get_terms( 'wptests_tax', array(
|
||||
'hide_empty' => false,
|
||||
) );
|
||||
|
||||
$num_queries = $wpdb->num_queries;
|
||||
|
||||
foreach ( $terms as $term_id ) {
|
||||
get_term( $term_id, 'wptests_tax' );
|
||||
}
|
||||
|
||||
$this->assertSame( $num_queries, $wpdb->num_queries );
|
||||
|
||||
_unregister_taxonomy( 'wptests_tax' );
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user