From a378749df67b385f8f09752ea9f5fe3dd884a5f4 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 25 May 2016 18:44:00 +0000 Subject: [PATCH] Fix termmeta pre-fetching in `wp_get_object_terms()`. [34529] introduced logic intended to prime the termmeta cache for certain values of the `fields` parameter. There were a few bugs: * The `all_with_object_id` param was misspelled. * `term_id` was used instead of `ids`. * The values being passed to `update_termmeta_cache()` in the case where `fields=ids` was not correct. All of these would result in a failure to pre-fetch termmeta in some cases. Props dlh. Fixes #36932. git-svn-id: https://develop.svn.wordpress.org/trunk@37567 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 6 +- tests/phpunit/tests/term/wpGetObjectTerms.php | 58 +++++++++++++++++++ 2 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index a60052c2da..ad3fba6714 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2628,9 +2628,9 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { } // Update termmeta cache, if necessary. - if ( $args['update_term_meta_cache'] && ( 'all' === $fields || 'all_with_object_ids' === $fields || 'term_id' === $fields ) ) { - if ( 'term_id' === $fields ) { - $term_ids = $fields; + if ( $args['update_term_meta_cache'] && ( 'all' === $fields || 'all_with_object_id' === $fields || 'ids' === $fields ) ) { + if ( 'ids' === $fields ) { + $term_ids = $terms; } else { $term_ids = wp_list_pluck( $terms, 'term_id' ); } diff --git a/tests/phpunit/tests/term/wpGetObjectTerms.php b/tests/phpunit/tests/term/wpGetObjectTerms.php index ffa87817d6..892e7a1524 100644 --- a/tests/phpunit/tests/term/wpGetObjectTerms.php +++ b/tests/phpunit/tests/term/wpGetObjectTerms.php @@ -472,6 +472,64 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase { $this->assertSame( $num_queries + 3, $wpdb->num_queries ); } + /** + * @ticket 36932 + */ + public function test_termmeta_cache_should_be_primed_when_fields_is_all_with_object_id() { + global $wpdb; + + register_taxonomy( 'wptests_tax', 'post' ); + $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); + add_term_meta( $terms[0], 'foo', 'bar' ); + add_term_meta( $terms[1], 'foo', 'bar' ); + add_term_meta( $terms[2], 'foo', 'bar' ); + + $p = self::factory()->post->create(); + wp_set_object_terms( $p, $terms, 'wptests_tax' ); + + $found = wp_get_object_terms( $p, 'wptests_tax', array( + 'update_term_meta_cache' => true, + 'fields' => 'all_with_object_id', + ) ); + + $num_queries = $wpdb->num_queries; + + foreach ( $terms as $t ) { + $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); + } + + $this->assertSame( $num_queries, $wpdb->num_queries ); + } + + /** + * @ticket 36932 + */ + public function test_termmeta_cache_should_be_primed_when_fields_is_ids() { + global $wpdb; + + register_taxonomy( 'wptests_tax', 'post' ); + $terms = self::factory()->term->create_many( 3, array( 'taxonomy' => 'wptests_tax' ) ); + add_term_meta( $terms[0], 'foo', 'bar' ); + add_term_meta( $terms[1], 'foo', 'bar' ); + add_term_meta( $terms[2], 'foo', 'bar' ); + + $p = self::factory()->post->create(); + wp_set_object_terms( $p, $terms, 'wptests_tax' ); + + $found = wp_get_object_terms( $p, 'wptests_tax', array( + 'update_term_meta_cache' => true, + 'fields' => 'ids', + ) ); + + $num_queries = $wpdb->num_queries; + + foreach ( $terms as $t ) { + $this->assertSame( 'bar', get_term_meta( $t, 'foo', true ) ); + } + + $this->assertSame( $num_queries, $wpdb->num_queries ); + } + /** * @ticket 10142 */