From b824d6f56ca09a43454169bfe7424353c4474b34 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 17 Dec 2014 16:38:44 +0000 Subject: [PATCH] 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 --- src/wp-includes/taxonomy.php | 3 + tests/phpunit/tests/term/cache.php | 107 +++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index f4076218a9..1d93f69047 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -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 ); diff --git a/tests/phpunit/tests/term/cache.php b/tests/phpunit/tests/term/cache.php index d42435398d..1a31df06dd 100644 --- a/tests/phpunit/tests/term/cache.php +++ b/tests/phpunit/tests/term/cache.php @@ -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' ); + } }