From 834c29fbc68f85efdd34cbb253e429e86cc3bbf3 Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Thu, 13 Jul 2017 13:40:39 +0000 Subject: [PATCH] Taxonomy: Avoid duplicates when querying for terms in taxonomies registered with `$args` parameter. [40514] introduced a bug that caused term queries to return some duplicates when the `$taxonomies` array contained only taxonomies that were originally registered with an `$args` array. We fix this bug by ensuring that recursive `get_terms()` queries stop when all queried `$taxonomies` have already been referenced. Props bor0, atanasangelovdev. Fixes #41010. git-svn-id: https://develop.svn.wordpress.org/trunk@41037 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 5 ++- tests/phpunit/tests/term/wpGetObjectTerms.php | 36 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index 05c6a10e8e..79d18f5fcd 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -1917,7 +1917,10 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { $args['taxonomy'] = $taxonomies; $args['object_ids'] = $object_ids; - $terms = array_merge( $terms, get_terms( $args ) ); + // Taxonomies registered without an 'args' param are handled here. + if ( ! empty( $taxonomies ) ) { + $terms = array_merge( $terms, get_terms( $args ) ); + } /** * Filters the terms for a given object or objects. diff --git a/tests/phpunit/tests/term/wpGetObjectTerms.php b/tests/phpunit/tests/term/wpGetObjectTerms.php index e527a601bf..5e3b8bcb9b 100644 --- a/tests/phpunit/tests/term/wpGetObjectTerms.php +++ b/tests/phpunit/tests/term/wpGetObjectTerms.php @@ -760,4 +760,40 @@ class Tests_Term_WpGetObjectTerms extends WP_UnitTestCase { $args['orderby'] = 'term_order'; return $args; } + + /** + * @ticket 41010 + */ + public function test_duplicate_terms_should_not_be_returned_when_passed_multiple_taxonomies_registered_with_args_array() { + $taxonomy1 = 'wptests_tax'; + $taxonomy2 = 'wptests_tax_2'; + + // Any non-empty 'args' array triggers the bug. + $taxonomy_arguments = array( + 'args' => array( 0 ), + ); + + register_taxonomy( $taxonomy1, 'post', $taxonomy_arguments ); + register_taxonomy( $taxonomy2, 'post', $taxonomy_arguments ); + + $post_id = self::factory()->post->create(); + $term_1_id = self::factory()->term->create( array( + 'taxonomy' => $taxonomy1, + ) ); + $term_2_id = self::factory()->term->create( array( + 'taxonomy' => $taxonomy2, + ) ); + + wp_set_object_terms( $post_id, $term_1_id, $taxonomy1 ); + wp_set_object_terms( $post_id, $term_2_id, $taxonomy2 ); + + $expected = array( $term_1_id, $term_2_id ); + + $actual = wp_get_object_terms( $post_id, array( $taxonomy1, $taxonomy2 ), array( + 'orderby' => 'term_id', + 'fields' => 'ids', + ) ); + + $this->assertEqualSets( $expected, $actual ); + } }