From 39fb88f96143e5cee384bd84a132cc806c7eca46 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Fri, 25 May 2018 01:22:44 +0000 Subject: [PATCH] Taxonomy: Improve cache handling when querying for terms using `all_with_object_id`. When a term query using `fields=all_with_object_id` hits the cache, the cached `stdClass` objects must be converted to `WP_Term` objects. This was overlooked when `WP_Term_Query` was refactored to support object queries in [38667]. Props dlh. Fixes #44221. git-svn-id: https://develop.svn.wordpress.org/trunk@43313 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/class-wp-term-query.php | 2 +- tests/phpunit/tests/term/query.php | 35 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-term-query.php b/src/wp-includes/class-wp-term-query.php index 9da5a20f32..25f8ede047 100644 --- a/src/wp-includes/class-wp-term-query.php +++ b/src/wp-includes/class-wp-term-query.php @@ -677,7 +677,7 @@ class WP_Term_Query { $cache_key = "get_terms:$key:$last_changed"; $cache = wp_cache_get( $cache_key, 'terms' ); if ( false !== $cache ) { - if ( 'all' === $_fields ) { + if ( 'all' === $_fields || 'all_with_object_id' === $_fields ) { $cache = $this->populate_terms( $cache ); } diff --git a/tests/phpunit/tests/term/query.php b/tests/phpunit/tests/term/query.php index 39496b16cb..09925cdc7b 100644 --- a/tests/phpunit/tests/term/query.php +++ b/tests/phpunit/tests/term/query.php @@ -318,6 +318,41 @@ class Tests_Term_Query extends WP_UnitTestCase { } } + /** + * @ticket 44221 + */ + public function test_all_with_object_id_should_return_term_objects() { + register_taxonomy( 'wptests_tax_1', 'post' ); + $posts = self::factory()->post->create_many( 2 ); + $t = self::factory()->term->create( array( 'taxonomy' => 'wptests_tax_1' ) ); + + foreach ( $posts as $p ) { + wp_set_object_terms( $p, array( $t ), 'wptests_tax_1' ); + } + + $query = new WP_Term_Query(); + $args = array( + 'taxonomy' => 'wptests_tax_1', + 'object_ids' => $posts, + 'fields' => 'all_with_object_id', + ); + + $terms = $query->query( $args ); + $this->assertNotEmpty( $terms ); + foreach ( $terms as $term ) { + $this->assertInstanceOf( 'WP_Term', $term ); + $this->assertObjectHasAttribute( 'object_id', $term ); + } + + // Run again to check the cached response. + $terms = $query->query( $args ); + $this->assertNotEmpty( $terms ); + foreach ( $terms as $term ) { + $this->assertInstanceOf( 'WP_Term', $term ); + $this->assertObjectHasAttribute( 'object_id', $term ); + } + } + /** * @ticket 37198 * @group cache