Allow attachment taxonomies to be fetched as objects.
By adding the `$output` parameter to `get_attachment_taxonomies()`, the function signature matches that of `get_object_taxonomies()`. The change also allows for more consistent behavior when passing `output=objects` to `get_object_taxonomies()` for the 'attachment' object type, since the `$output` parameter is now passed through the function stack. Props codemovement.pk. See #37368. git-svn-id: https://develop.svn.wordpress.org/trunk@38292 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
773eb46bf8
commit
29a5d46701
|
@ -2694,11 +2694,15 @@ function adjacent_image_link( $prev = true, $size = 'thumbnail', $text = false )
|
|||
* Retrieves taxonomies attached to given the attachment.
|
||||
*
|
||||
* @since 2.5.0
|
||||
* @since 4.7.0 Introduced the `$output` parameter.
|
||||
*
|
||||
* @param int|array|object $attachment Attachment ID, data array, or data object.
|
||||
* @param string $output Output type. 'names' to return an array of taxonomy names,
|
||||
* or 'objects' to return an array of taxonomy objects.
|
||||
* Default is 'names'.
|
||||
* @return array Empty array on failure. List of taxonomies on success.
|
||||
*/
|
||||
function get_attachment_taxonomies( $attachment ) {
|
||||
function get_attachment_taxonomies( $attachment, $output = 'names' ) {
|
||||
if ( is_int( $attachment ) ) {
|
||||
$attachment = get_post( $attachment );
|
||||
} elseif ( is_array( $attachment ) ) {
|
||||
|
@ -2723,9 +2727,11 @@ function get_attachment_taxonomies( $attachment ) {
|
|||
}
|
||||
|
||||
$taxonomies = array();
|
||||
foreach ( $objects as $object )
|
||||
if ( $taxes = get_object_taxonomies($object) )
|
||||
$taxonomies = array_merge($taxonomies, $taxes);
|
||||
foreach ( $objects as $object ) {
|
||||
if ( $taxes = get_object_taxonomies( $object, $output ) ) {
|
||||
$taxonomies = array_merge( $taxonomies, $taxes );
|
||||
}
|
||||
}
|
||||
|
||||
return array_unique($taxonomies);
|
||||
}
|
||||
|
|
|
@ -181,7 +181,7 @@ function get_object_taxonomies( $object, $output = 'names' ) {
|
|||
|
||||
if ( is_object($object) ) {
|
||||
if ( $object->post_type == 'attachment' )
|
||||
return get_attachment_taxonomies($object);
|
||||
return get_attachment_taxonomies( $object, $output );
|
||||
$object = $object->post_type;
|
||||
}
|
||||
|
||||
|
|
|
@ -79,4 +79,23 @@ class Tests_Media_GetAttachmentTaxonomies extends WP_UnitTestCase {
|
|||
|
||||
$this->assertSame( $expected, $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 37368
|
||||
*/
|
||||
public function test_should_respect_output_objects() {
|
||||
register_taxonomy( 'wptests_tax2', 'attachment:image' );
|
||||
|
||||
$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 );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -45,4 +45,40 @@ class Tests_Taxonomy_GetObjectTaxonomies extends WP_UnitTestCase {
|
|||
|
||||
$this->assertSame( $expected, $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 37368
|
||||
*/
|
||||
public function test_should_return_all_attachment_taxonomies_for_attachment_object_type() {
|
||||
register_taxonomy( 'wptests_tax2', 'attachment:image' );
|
||||
|
||||
$a = self::factory()->attachment->create_object( 'image.jpg', 0, array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
'post_type' => 'attachment'
|
||||
) );
|
||||
$attachment = get_post( $a );
|
||||
|
||||
$found = get_object_taxonomies( $attachment, 'names' );
|
||||
|
||||
$this->assertSame( array( 'wptests_tax2' ), $found );
|
||||
}
|
||||
|
||||
/**
|
||||
* @ticket 37368
|
||||
*/
|
||||
public function test_should_respect_output_objects_when_object_is_attachment() {
|
||||
register_taxonomy( 'wptests_tax2', 'attachment:image' );
|
||||
|
||||
$a = self::factory()->attachment->create_object( 'image.jpg', 0, array(
|
||||
'post_mime_type' => 'image/jpeg',
|
||||
'post_type' => 'attachment'
|
||||
) );
|
||||
$attachment = get_post( $a );
|
||||
|
||||
$found = get_object_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 );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue