Skip to content

Commit b824d6f

Browse files
committed
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
1 parent c7d2f36 commit b824d6f

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

src/wp-includes/taxonomy.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1912,6 +1912,9 @@ function get_terms( $taxonomies, $args = '' ) {
19121912
}
19131913

19141914
$terms = $wpdb->get_results($query);
1915+
if ( 'all' == $_fields ) {
1916+
update_term_cache( $terms );
1917+
}
19151918

19161919
if ( empty($terms) ) {
19171920
wp_cache_add( $cache_key, array(), 'terms', DAY_IN_SECONDS );

tests/phpunit/tests/term/cache.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,111 @@ function test_hierachy_invalidation() {
9393

9494
_unregister_taxonomy( $tax );
9595
}
96+
97+
public function test_get_term_should_update_term_cache_when_passed_an_object() {
98+
global $wpdb;
99+
100+
register_taxonomy( 'wptests_tax', 'post' );
101+
$term = $this->factory->term->create( array(
102+
'taxonomy' => 'wptests_tax',
103+
) );
104+
105+
$term_object = get_term( $term, 'wptests_tax' );
106+
wp_cache_delete( $term, 'wptests_tax' );
107+
108+
// Affirm that the cache is empty.
109+
$this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
110+
111+
$num_queries = $wpdb->num_queries;
112+
113+
// get_term() will only be update the cache if the 'filter' prop is unset.
114+
unset( $term_object->filter );
115+
116+
$term_object_2 = get_term( $term_object, 'wptests_tax' );
117+
118+
// No new queries should have fired.
119+
$this->assertSame( $num_queries, $wpdb->num_queries );
120+
$this->assertEquals( $term_object, $term_object_2 );
121+
}
122+
123+
public function test_get_term_should_update_term_cache_when_passed_a_valid_term_identifier() {
124+
global $wpdb;
125+
126+
register_taxonomy( 'wptests_tax', 'post' );
127+
$term = $this->factory->term->create( array(
128+
'taxonomy' => 'wptests_tax',
129+
) );
130+
131+
wp_cache_delete( $term, 'wptests_tax' );
132+
133+
// Affirm that the cache is empty.
134+
$this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
135+
136+
$num_queries = $wpdb->num_queries;
137+
138+
// Prime cache.
139+
$term_object = get_term( $term, 'wptests_tax' );
140+
$this->assertNotEmpty( wp_cache_get( $term, 'wptests_tax' ) );
141+
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
142+
143+
$term_object_2 = get_term( $term, 'wptests_tax' );
144+
145+
// No new queries should have fired.
146+
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
147+
$this->assertEquals( $term_object, $term_object_2 );
148+
}
149+
150+
public function test_get_term_by_should_update_term_cache_when_passed_a_valid_term_identifier() {
151+
global $wpdb;
152+
153+
register_taxonomy( 'wptests_tax', 'post' );
154+
$term = $this->factory->term->create( array(
155+
'taxonomy' => 'wptests_tax',
156+
) );
157+
158+
wp_cache_delete( $term, 'wptests_tax' );
159+
160+
// Affirm that the cache is empty.
161+
$this->assertEmpty( wp_cache_get( $term, 'wptests_tax' ) );
162+
163+
$num_queries = $wpdb->num_queries;
164+
165+
// Prime cache.
166+
$term_object = get_term_by( 'id', $term, 'wptests_tax' );
167+
$this->assertNotEmpty( wp_cache_get( $term, 'wptests_tax' ) );
168+
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
169+
170+
$term_object_2 = get_term( $term, 'wptests_tax' );
171+
172+
// No new queries should have fired.
173+
$this->assertSame( $num_queries + 1, $wpdb->num_queries );
174+
$this->assertEquals( $term_object, $term_object_2 );
175+
}
176+
177+
/**
178+
* @ticket 30749
179+
*/
180+
public function test_get_terms_should_update_cache_for_located_terms() {
181+
global $wpdb;
182+
183+
register_taxonomy( 'wptests_tax', 'post' );
184+
185+
$terms = $this->factory->term->create_many( 5, array(
186+
'taxonomy' => 'wptests_tax',
187+
) );
188+
189+
$term_objects = get_terms( 'wptests_tax', array(
190+
'hide_empty' => false,
191+
) );
192+
193+
$num_queries = $wpdb->num_queries;
194+
195+
foreach ( $terms as $term_id ) {
196+
get_term( $term_id, 'wptests_tax' );
197+
}
198+
199+
$this->assertSame( $num_queries, $wpdb->num_queries );
200+
201+
_unregister_taxonomy( 'wptests_tax' );
202+
}
96203
}

0 commit comments

Comments
 (0)