From 4dd4a32cde8f16dea6fc92d51275a6c8385b6ec1 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Tue, 27 May 2014 03:28:05 +0000 Subject: [PATCH] In `wp_get_object_terms()`, before returning terms (and before running them through the 'wp_get_object_terms' filter) - run them through `$terms = array_values( array_unique( $terms, SORT_REGULAR ) )`. There will be "dupes" when the function is called with `'fields' => 'all_with_object_id'`, but the objects will actually be unique due to the `object_id` addition, so they shouldn't be filtered out. Adds unit tests. All other unit tests pass. Fixes #11003. git-svn-id: https://develop.svn.wordpress.org/trunk@28583 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/taxonomy.php | 6 ++++-- tests/phpunit/tests/term.php | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/taxonomy.php b/src/wp-includes/taxonomy.php index b0f70d0e8c..b11ab45acb 100644 --- a/src/wp-includes/taxonomy.php +++ b/src/wp-includes/taxonomy.php @@ -2337,9 +2337,11 @@ function wp_get_object_terms($object_ids, $taxonomies, $args = array()) { } } - if ( ! $terms ) + if ( ! $terms ) { $terms = array(); - + } else { + $terms = array_values( array_unique( $terms, SORT_REGULAR ) ); + } /** * Filter the terms for a given object or objects. * diff --git a/tests/phpunit/tests/term.php b/tests/phpunit/tests/term.php index f2d3ee419b..0768cbda52 100644 --- a/tests/phpunit/tests/term.php +++ b/tests/phpunit/tests/term.php @@ -570,4 +570,24 @@ class Tests_Term extends WP_UnitTestCase { // this previously returned 2 $this->assertEquals( 0, $count ); } + + function test_wp_get_object_terms_no_dupes() { + $post_id1 = $this->factory->post->create(); + $post_id2 = $this->factory->post->create(); + $cat_id = $this->factory->category->create(); + $cat_id2 = $this->factory->category->create(); + wp_set_post_categories( $post_id1, array( $cat_id, $cat_id2 ) ); + wp_set_post_categories( $post_id2, $cat_id ); + + $terms = wp_get_object_terms( array( $post_id1, $post_id2 ), 'category' ); + $this->assertCount( 2, $terms ); + $this->assertEquals( array( $cat_id, $cat_id2 ), wp_list_pluck( $terms, 'term_id' ) ); + + $terms2 = wp_get_object_terms( array( $post_id1, $post_id2 ), 'category', array( + 'fields' => 'all_with_object_id' + ) ); + + $this->assertCount( 3, $terms2 ); + $this->assertEquals( array( $cat_id, $cat_id, $cat_id2 ), wp_list_pluck( $terms2, 'term_id' ) ); + } }