From 2e848072eb57eb8a0ce9ea8ffa38ccb22191924e Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Wed, 28 Jan 2015 20:42:25 +0000 Subject: [PATCH] Don't use term IDs for array indexes when caching object terms. Uncached results pulled from `wp_get_object_terms()` are zero-indexed (ie 0, 1, 2...). As a result, `get_the_terms()` was returning a strictly different array when pulling from the cache and when the cache was empty. Props joshlevinson. Fixes #31086. git-svn-id: https://develop.svn.wordpress.org/trunk@31287 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 2 +- tests/phpunit/tests/term.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 16bdf26ae8..ccaae9041b 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -3783,7 +3783,7 @@ function update_object_term_cache($object_ids, $object_type) { $object_terms = array(); foreach ( (array) $terms as $term ) - $object_terms[$term->object_id][$term->taxonomy][$term->term_id] = $term; + $object_terms[$term->object_id][$term->taxonomy][] = $term; foreach ( $ids as $id ) { foreach ( $taxonomies as $taxonomy ) { diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index 78f4d29dc0..588b1dc9c8 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -1584,6 +1584,35 @@ class Tests_Term extends WP_UnitTestCase { $this->assertEquals( 'This description is even more amazing!', $terms[0]->description ); } + /** + * @ticket 31086 + */ + public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_empty() { + register_taxonomy( 'wptests_tax', 'post' ); + $p = $this->factory->post->create(); + wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' ); + + $found = get_the_terms( $p, 'wptests_tax' ); + + $this->assertEqualSets( array( 0, 1 ), array_keys( $found ) ); + } + + /** + * @ticket 31086 + */ + public function test_get_the_terms_should_return_zero_indexed_array_when_cache_is_primed() { + register_taxonomy( 'wptests_tax', 'post' ); + $p = $this->factory->post->create(); + wp_set_object_terms( $p, array( 'foo', 'bar' ), 'wptests_tax' ); + + // Prime cache. + update_object_term_cache( array( $p ), array( 'post' ) ); + + $found = get_the_terms( $p, 'wptests_tax' ); + + $this->assertEqualSets( array( 0, 1 ), array_keys( $found ) ); + } + /** * @ticket 19205 */