Make WordPress Core

Ticket #36246: 36246-attachment-metadata.patch

File 36246-attachment-metadata.patch, 3.6 KB (added by JorritSchippers, 9 years ago)

patch with test

  • src/wp-includes/media.php

    diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php
    index 7b99f95..83b6979 100644
    a b function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa 
    822822
    823823                // Generate 'srcset' and 'sizes' if not already present.
    824824                if ( empty( $attr['srcset'] ) ) {
    825                         $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     825                        $image_meta = wp_get_attachment_metadata( $attachment_id );
    826826
    827827                        if ( is_array( $image_meta ) ) {
    828828                                $size_array = array( absint( $width ), absint( $height ) );
    function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag 
    951951        }
    952952
    953953        if ( ! is_array( $image_meta ) ) {
    954                 $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     954                $image_meta = wp_get_attachment_metadata( $attachment_id );
    955955        }
    956956
    957957        $image_src = $image[0];
    function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image 
    11661166        }
    11671167
    11681168        if ( ! is_array( $image_meta ) ) {
    1169                 $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     1169                $image_meta = wp_get_attachment_metadata( $attachment_id );
    11701170        }
    11711171
    11721172        $image_src = $image[0];
    function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, 
    11991199                $width = absint( $size[0] );
    12001200        } elseif ( is_string( $size ) ) {
    12011201                if ( ! $image_meta && $attachment_id ) {
    1202                         $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     1202                        $image_meta = wp_get_attachment_metadata( $attachment_id );
    12031203                }
    12041204
    12051205                if ( is_array( $image_meta ) ) {
    function wp_make_content_images_responsive( $content ) { 
    12741274        }
    12751275
    12761276        foreach ( $selected_images as $image => $attachment_id ) {
    1277                 $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     1277                $image_meta = wp_get_attachment_metadata( $attachment_id );
    12781278                $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content );
    12791279        }
    12801280
  • tests/phpunit/tests/media.php

    diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php
    index 5399f85..56cd19c 100644
    a b EOF; 
    14761476
    14771477                $this->assertSame( $expected, $actual );
    14781478        }
     1479
     1480        /**
     1481         * Tests if wp_get_attachment_image() uses wp_get_attachment_metadata().
     1482         *
     1483         * In this way, the meta data can be filtered using the filter
     1484         * `wp_get_attachment_metadata`.
     1485         *
     1486         * The test checks if the image size that is added in the filter is
     1487         * used in the output of `wp_get_attachment_image()`.
     1488         *
     1489         * @ticket 36246
     1490         */
     1491        function test_wp_get_attachment_image_should_use_wp_get_attachment_metadata() {
     1492          add_filter( 'wp_get_attachment_metadata', array($this, 'filter_test_wp_get_attachment_image_should_use_wp_get_attachment_metadata'), 10, 2 );
     1493
     1494                $actual = wp_get_attachment_image( self::$large_id, 'testsize' );
     1495                $expeced = '<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" />';
     1496
     1497                remove_filter( 'wp_get_attachment_metadata', array($this, 'filter_test_wp_get_attachment_image_should_use_wp_get_attachment_metadata') );
     1498        }
     1499
     1500        function filter_test_wp_get_attachment_image_should_use_wp_get_attachment_metadata($data, $attachment_id) {
     1501                $data['sizes']['testsize'] = array(
     1502      'file' => 'test-image-testsize-999x999.png',
     1503      'width' => 999,
     1504      'height' => 999,
     1505      'mime-type' => 'image/png',
     1506    );
     1507                return $data;
     1508        }
    14791509}