WordPress.org

Make WordPress Core

Changeset 38296


Ignore:
Timestamp:
08/21/2016 06:14:37 AM (3 years ago)
Author:
wonderboymusic
Message:

Media: use wp_get_attachment_metadata() instead of get_post_meta() where appropriate.

Adds unit test.

Props JorritSchippers.
Fixes #36246.

Location:
trunk
Files:
2 edited

Legend:

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

    r38292 r38296  
    862862        // Generate 'srcset' and 'sizes' if not already present.
    863863        if ( empty( $attr['srcset'] ) ) {
    864             $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     864            $image_meta = wp_get_attachment_metadata( $attachment_id );
    865865
    866866            if ( is_array( $image_meta ) ) {
     
    991991
    992992    if ( ! is_array( $image_meta ) ) {
    993         $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     993        $image_meta = wp_get_attachment_metadata( $attachment_id );
    994994    }
    995995
     
    12071207
    12081208    if ( ! is_array( $image_meta ) ) {
    1209         $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     1209        $image_meta = wp_get_attachment_metadata( $attachment_id );
    12101210    }
    12111211
     
    12401240    } elseif ( is_string( $size ) ) {
    12411241        if ( ! $image_meta && $attachment_id ) {
    1242             $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     1242            $image_meta = wp_get_attachment_metadata( $attachment_id );
    12431243        }
    12441244
     
    13151315
    13161316    foreach ( $selected_images as $image => $attachment_id ) {
    1317         $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     1317        $image_meta = wp_get_attachment_metadata( $attachment_id );
    13181318        $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content );
    13191319    }
  • trunk/tests/phpunit/tests/media.php

    r38052 r38296  
    17561756        $this->assertSame( $expected, get_image_send_to_editor( $id, $caption, $title, $align, $url, $rel, $size, $alt ) );
    17571757    }
     1758
     1759    /**
     1760     * Tests if wp_get_attachment_image() uses wp_get_attachment_metadata().
     1761     *
     1762     * In this way, the meta data can be filtered using the filter
     1763     * `wp_get_attachment_metadata`.
     1764     *
     1765     * The test checks if the image size that is added in the filter is
     1766     * used in the output of `wp_get_attachment_image()`.
     1767     *
     1768     * @ticket 36246
     1769     */
     1770    function test_wp_get_attachment_image_should_use_wp_get_attachment_metadata() {
     1771        add_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ), 10, 2 );
     1772
     1773        $actual = wp_get_attachment_image( self::$large_id, 'testsize' );
     1774        $expected = '<img width="999" height="999" src="http://example.org/wp-content/uploads/2016/03/test-image-testsize-999x999.png" class="attachment-testsize size-testsize" alt="test-image-large.png" srcset="http://example.org/wp-content/uploads/2016/03/test-image-large-150x150.png 150w, http://example.org/wp-content/uploads/2016/03/test-image-testsize-999x999.png 999w" sizes="(max-width: 999px) 100vw, 999px" />';
     1775
     1776        remove_filter( 'wp_get_attachment_metadata', array( $this, '_filter_36246' ) );
     1777
     1778        $this->assertSame( $expected, $actual );
     1779    }
     1780
     1781    function _filter_36246( $data, $attachment_id ) {
     1782        $data['sizes']['testsize'] = array(
     1783            'file' => 'test-image-testsize-999x999.png',
     1784            'width' => 999,
     1785            'height' => 999,
     1786            'mime-type' => 'image/png',
     1787        );
     1788        return $data;
     1789    }
    17581790}
    17591791
Note: See TracChangeset for help on using the changeset viewer.