WordPress.org

Make WordPress Core

Changeset 38292


Ignore:
Timestamp:
08/20/2016 05:34:13 PM (3 years ago)
Author:
boonebgorges
Message:

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.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r38086 r38292  
    26952695 *
    26962696 * @since 2.5.0
     2697 * @since 4.7.0 Introduced the `$output` parameter.
    26972698 *
    26982699 * @param int|array|object $attachment Attachment ID, data array, or data object.
     2700 * @param string           $output     Output type. 'names' to return an array of taxonomy names,
     2701 *                                     or 'objects' to return an array of taxonomy objects.
     2702 *                                     Default is 'names'.
    26992703 * @return array Empty array on failure. List of taxonomies on success.
    27002704 */
    2701 function get_attachment_taxonomies( $attachment ) {
     2705function get_attachment_taxonomies( $attachment, $output = 'names' ) {
    27022706    if ( is_int( $attachment ) ) {
    27032707        $attachment = get_post( $attachment );
     
    27242728
    27252729    $taxonomies = array();
    2726     foreach ( $objects as $object )
    2727         if ( $taxes = get_object_taxonomies($object) )
    2728             $taxonomies = array_merge($taxonomies, $taxes);
     2730    foreach ( $objects as $object ) {
     2731        if ( $taxes = get_object_taxonomies( $object, $output ) ) {
     2732            $taxonomies = array_merge( $taxonomies, $taxes );
     2733        }
     2734    }
    27292735
    27302736    return array_unique($taxonomies);
  • trunk/src/wp-includes/taxonomy.php

    r38277 r38292  
    182182    if ( is_object($object) ) {
    183183        if ( $object->post_type == 'attachment' )
    184             return get_attachment_taxonomies($object);
     184            return get_attachment_taxonomies( $object, $output );
    185185        $object = $object->post_type;
    186186    }
  • trunk/tests/phpunit/tests/media/getAttachmentTaxonomies.php

    r38291 r38292  
    8080        $this->assertSame( $expected, $found );
    8181    }
     82
     83    /**
     84     * @ticket 37368
     85     */
     86    public function test_should_respect_output_objects() {
     87        register_taxonomy( 'wptests_tax2', 'attachment:image' );
     88
     89        $a = self::factory()->attachment->create_object( 'image.jpg', 0, array(
     90            'post_mime_type' => 'image/jpeg',
     91            'post_type' => 'attachment'
     92        ) );
     93        $attachment = get_post( $a );
     94
     95        $found = get_attachment_taxonomies( $attachment, 'objects' );
     96
     97        $this->assertSame( array( 'wptests_tax2' ), array_keys( $found ) );
     98        $this->assertInternalType( 'object', $found['wptests_tax2'] );
     99        $this->assertSame( 'wptests_tax2', $found['wptests_tax2']->name );
     100    }
    82101}
  • trunk/tests/phpunit/tests/taxonomy/getObjectTaxonomies.php

    r38290 r38292  
    4646        $this->assertSame( $expected, $found );
    4747    }
     48
     49    /**
     50     * @ticket 37368
     51     */
     52    public function test_should_return_all_attachment_taxonomies_for_attachment_object_type() {
     53        register_taxonomy( 'wptests_tax2', 'attachment:image' );
     54
     55        $a = self::factory()->attachment->create_object( 'image.jpg', 0, array(
     56            'post_mime_type' => 'image/jpeg',
     57            'post_type' => 'attachment'
     58        ) );
     59        $attachment = get_post( $a );
     60
     61        $found = get_object_taxonomies( $attachment, 'names' );
     62
     63        $this->assertSame( array( 'wptests_tax2' ), $found );
     64    }
     65
     66    /**
     67     * @ticket 37368
     68     */
     69    public function test_should_respect_output_objects_when_object_is_attachment() {
     70        register_taxonomy( 'wptests_tax2', 'attachment:image' );
     71
     72        $a = self::factory()->attachment->create_object( 'image.jpg', 0, array(
     73            'post_mime_type' => 'image/jpeg',
     74            'post_type' => 'attachment'
     75        ) );
     76        $attachment = get_post( $a );
     77
     78        $found = get_object_taxonomies( $attachment, 'objects' );
     79
     80        $this->assertSame( array( 'wptests_tax2' ), array_keys( $found ) );
     81        $this->assertInternalType( 'object', $found['wptests_tax2'] );
     82        $this->assertSame( 'wptests_tax2', $found['wptests_tax2']->name );
     83    }
    4884}
Note: See TracChangeset for help on using the changeset viewer.