From 53016dd3aa4565f7bd0465f9a61b4f36e8a7678f Mon Sep 17 00:00:00 2001 From: Boone Gorges Date: Tue, 30 Aug 2016 03:06:06 +0000 Subject: [PATCH] Remove unnecessary uniqueness check in `get_attachment_taxonomies()`. Running the taxonomy array through `array_unique()` is unnecessary when the function returns objects, because the associative keys already ensure uniqueness. This also fixes a bug when running `get_attachment_taxonomies()` in HHVM, which doesn't like casting objects to strings for the purposes of `array_unique()`. Props swissspidy. See #37368. git-svn-id: https://develop.svn.wordpress.org/trunk@38437 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 6 +++++- .../tests/media/getAttachmentTaxonomies.php | 20 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 699d7c9216..fc5508de48 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -2749,7 +2749,11 @@ function get_attachment_taxonomies( $attachment, $output = 'names' ) { } } - return array_unique($taxonomies); + if ( 'names' === $output ) { + $taxonomies = array_unique( $taxonomies ); + } + + return $taxonomies; } /** diff --git a/tests/phpunit/tests/media/getAttachmentTaxonomies.php b/tests/phpunit/tests/media/getAttachmentTaxonomies.php index f94b2f1007..c78ce6aa81 100644 --- a/tests/phpunit/tests/media/getAttachmentTaxonomies.php +++ b/tests/phpunit/tests/media/getAttachmentTaxonomies.php @@ -98,4 +98,24 @@ class Tests_Media_GetAttachmentTaxonomies extends WP_UnitTestCase { $this->assertInternalType( 'object', $found['wptests_tax2'] ); $this->assertSame( 'wptests_tax2', $found['wptests_tax2']->name ); } + + + /** + * @ticket 37368 + */ + public function test_should_return_unique_taxonomies_for_output_objects() { + register_taxonomy( 'wptests_tax2', array( 'attachment:image', 'attachment:image/jpeg' ) ); + + $a = self::factory()->attachment->create_object( 'image.jpg', 0, array( + 'post_mime_type' => 'image/jpeg', + 'post_type' => 'attachment' + ) ); + $attachment = get_post( $a ); + + $found = get_attachment_taxonomies( $attachment, 'objects' ); + + $this->assertSame( array( 'wptests_tax2' ), array_keys( $found ) ); + $this->assertInternalType( 'object', $found['wptests_tax2'] ); + $this->assertSame( 'wptests_tax2', $found['wptests_tax2']->name ); + } }