Taxonomy: Fix caching issues in WP_Term_Query class.

Introduced [52836] when passing `child_of` or `pad_counts` parameters to `get_terms` or `WP_Term_Query` class, the array of terms received by the query, was not correctly cached. This 
change simplifies the logic in `WP_Term_Query` and ensures terms are correctly cached. This change also, improves performance, by only caching an array of term ids where possible.  

Props denishua, spacedmonkey, oztaser, peterwilsoncc, SergeyBiryukov, georgestephanis, jnz31, knutsp, mukesh27, costdev.
Fixes #55837.



git-svn-id: https://develop.svn.wordpress.org/trunk@53496 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2022-06-14 11:41:33 +00:00
parent 2aa8e490e0
commit 6d89ea8f09
3 changed files with 125 additions and 44 deletions
+91 -11
View File
@@ -746,6 +746,27 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
$this->assertCount( 1, $terms );
}
/**
* @ticket 55837
* @covers ::get_terms
*/
public function test_get_terms_child_of_cache() {
$parent = self::factory()->category->create();
self::factory()->category->create( array( 'parent' => $parent ) );
$args = array(
'fields' => 'ids',
'child_of' => $parent,
'hide_empty' => false,
);
$terms = get_terms( 'category', $args );
$this->assertCount( 1, $terms, 'The first call to get_terms() did not return 1 term' );
$terms2 = get_terms( 'category', $args );
$this->assertCount( 1, $terms2, 'The second call to get_terms() did not return 1 term' );
$this->assertSameSets( $terms, $terms2, 'Results are not the same after caching' );
}
/**
* @ticket 46768
*/
@@ -2589,6 +2610,76 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
}
}
/**
* @ticket 55837
* @covers ::get_terms
*/
public function test_pad_counts_cached() {
register_taxonomy( 'wptests_tax_1', 'post', array( 'hierarchical' => true ) );
$posts = self::factory()->post->create_many( 3 );
$t1 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax_1',
)
);
$t2 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax_1',
'parent' => $t1,
)
);
$t3 = self::factory()->term->create(
array(
'taxonomy' => 'wptests_tax_1',
'parent' => $t2,
)
);
wp_set_object_terms( $posts[0], array( $t1 ), 'wptests_tax_1' );
wp_set_object_terms( $posts[1], array( $t2 ), 'wptests_tax_1' );
wp_set_object_terms( $posts[2], array( $t3 ), 'wptests_tax_1' );
$found = get_terms(
'wptests_tax_1',
array(
'pad_counts' => true,
)
);
$this->assertSameSets( array( $t1, $t2, $t3 ), wp_list_pluck( $found, 'term_id' ), 'Check to see if results are as expected' );
foreach ( $found as $f ) {
if ( $t1 === $f->term_id ) {
$this->assertSame( 3, $f->count, 'Check to see if term 1, has the correct count' );
} elseif ( $t2 === $f->term_id ) {
$this->assertSame( 2, $f->count, 'Check to see if term 2, has the correct count' );
} else {
$this->assertSame( 1, $f->count, 'Check to see if term 3, has the correct count' );
}
}
$found = get_terms(
'wptests_tax_1',
array(
'pad_counts' => true,
)
);
$this->assertSameSets( array( $t1, $t2, $t3 ), wp_list_pluck( $found, 'term_id' ), 'Check to see if results are as expected on second run' );
foreach ( $found as $f ) {
if ( $t1 === $f->term_id ) {
$this->assertSame( 3, $f->count, 'Check to see if term 1, has the correct count on second run' );
} elseif ( $t2 === $f->term_id ) {
$this->assertSame( 2, $f->count, 'Check to see if term 2, has the correct count on second run' );
} else {
$this->assertSame( 1, $f->count, 'Check to see if term 3, has the correct count on second run' );
}
}
}
/**
* @ticket 20635
*/
@@ -3099,17 +3190,6 @@ class Tests_Term_getTerms extends WP_UnitTestCase {
'fields' => 'slugs',
),
),
'meta cache off, pad count on vs meta cache on, pad count off' => array(
array(
'taxonomy' => self::$taxonomy,
'pad_counts' => true,
'update_term_meta_cache' => false,
),
array(
'taxonomy' => self::$taxonomy,
'fields' => 'ids',
),
),
'array slug vs string slug' => array(
array(
'taxonomy' => self::$taxonomy,