From 4313210c8232cd7961f9da9660636623319070e8 Mon Sep 17 00:00:00 2001 From: Jonny Harris Date: Tue, 16 May 2023 12:29:34 +0000 Subject: [PATCH] Taxonomy: Do not prime term meta in `wp_get_object_terms`. Passing `update_term_meta_cache` argument value false by default resulting in `get_terms` to not prime the term meta cache in `wp_get_object_terms`. Priming of term meta is not needed in this context. Props spacedmonkey, rutviksavsani. Fixes #57701. git-svn-id: https://develop.svn.wordpress.org/trunk@55759 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 8 +++++++- tests/phpunit/tests/term/getTheTerms.php | 8 ++++---- tests/phpunit/tests/term/wpGetObjectTerms.php | 6 ++++-- 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 178aa08f7b..71280f0437 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2192,6 +2192,8 @@ function wp_delete_category( $cat_id ) { * @since 4.4.0 Introduced `$meta_query` and `$update_term_meta_cache` arguments. When `$fields` is 'all' or * 'all_with_object_id', an array of `WP_Term` objects will be returned. * @since 4.7.0 Refactored to use WP_Term_Query, and to support any WP_Term_Query arguments. + * @since 6.3.0 Passing `update_term_meta_cache` argument value false by default resulting in get_terms() to not + * prime the term meta cache. * * @param int|int[] $object_ids The ID(s) of the object(s) to retrieve. * @param string|string[] $taxonomies The taxonomy names to retrieve terms from. @@ -2220,7 +2222,11 @@ function wp_get_object_terms( $object_ids, $taxonomies, $args = array() ) { } $object_ids = array_map( 'intval', $object_ids ); - $args = wp_parse_args( $args ); + $defaults = array( + 'update_term_meta_cache' => false, + ); + + $args = wp_parse_args( $args, $defaults ); /** * Filters arguments for retrieving object terms. diff --git a/tests/phpunit/tests/term/getTheTerms.php b/tests/phpunit/tests/term/getTheTerms.php index c357fc80a2..67362327c8 100644 --- a/tests/phpunit/tests/term/getTheTerms.php +++ b/tests/phpunit/tests/term/getTheTerms.php @@ -195,8 +195,9 @@ class Tests_Term_GetTheTerms extends WP_UnitTestCase { /** * @ticket 36814 + * @ticket 57701 */ - public function test_uncached_terms_should_be_primed_with_a_single_query() { + public function test_uncached_terms_should_not_be_primed_with_a_single_query_by_default() { register_taxonomy( 'wptests_tax', 'post' ); $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); @@ -213,9 +214,8 @@ class Tests_Term_GetTheTerms extends WP_UnitTestCase { $this->assertSameSets( $terms, wp_list_pluck( $found, 'term_id' ) ); - $num_queries++; - $this->assertSame( $num_queries, get_num_queries() ); - + // Two extra queries are expected as the cache is not primed and hence terms need to be queried. + $this->assertSame( 1, get_num_queries() - $num_queries ); } /** diff --git a/tests/phpunit/tests/term/wpGetObjectTerms.php b/tests/phpunit/tests/term/wpGetObjectTerms.php index 21c6c4b360..5c0faec53b 100644 --- a/tests/phpunit/tests/term/wpGetObjectTerms.php +++ b/tests/phpunit/tests/term/wpGetObjectTerms.php @@ -614,8 +614,9 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase { /** * @ticket 10142 + * @ticket 57701 */ - public function test_termmeta_cache_should_be_lazy_loaded_by_default() { + public function test_termmeta_cache_should_not_be_lazy_loaded_by_default() { register_taxonomy( 'wptests_tax', 'post' ); $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); add_term_meta( $terms[0], 'foo', 'bar' ); @@ -633,7 +634,8 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase { $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); } - $this->assertSame( $num_queries + 1, get_num_queries() ); + // Here we had extra queries as the term meta cache was not primed by default. + $this->assertSame( 3, get_num_queries() - $num_queries ); } /**