Make WordPress Core

Ticket #34612: 34612.diff

File 34612.diff, 12.5 KB (added by joemcgill, 10 years ago)
  • src/wp-includes/media.php

    diff --git src/wp-includes/media.php src/wp-includes/media.php
    index 562f283..e686d6a 100644
    function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa 
    826826
    827827                        if ( is_array( $image_meta ) ) {
    828828                                $size_array = array( absint( $width ), absint( $height ) );
    829                                 $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );
    830                                 $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id, $src );
     829                                $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id );
     830                                $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id );
    831831
    832832                                if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
    833833                                        $attr['srcset'] = $srcset;
    function _wp_get_image_size_from_meta( $size_name, $image_meta ) { 
    933933 * @since 4.4.0
    934934 *
    935935 * @param int          $attachment_id Image attachment ID.
    936  * @param array|string $size          Image size. Accepts any valid image size, or an array of width and height
    937  *                                    values in pixels (in that order). Default 'medium'.
     936 * @param array|string $size          Optional. Image size. Accepts any valid image size, or an array of
     937 *                                    width and height values in pixels (in that order). Default 'medium'.
    938938 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
    939939 * @return string|bool A 'srcset' value string or false.
    940940 */
    function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag 
    953953                absint( $image[2] )
    954954        );
    955955
    956         return wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attachment_id );
     956        return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
    957957}
    958958
    959959/**
    function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag 
    961961 *
    962962 * @since 4.4.0
    963963 *
    964  * @param string $image_src     The 'src' of the image.
    965964 * @param array  $size_array    Array of width and height values in pixels (in that order).
     965 * @param string $image_src     The 'src' of the image.
    966966 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    967967 * @param int    $attachment_id Optional. The image attachment ID to pass to the filter.
    968968 * @return string|bool          The 'srcset' attribute value. False on error or when only one source exists.
    969969 */
    970 function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attachment_id = 0 ) {
     970function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
    971971        if ( empty( $image_meta['sizes'] ) ) {
    972972                return false;
    973973        }
    function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attac 
    11051105}
    11061106
    11071107/**
     1108 * Retrieves the value for an image attachment's 'sizes' attribute.
     1109 *
     1110 * @since 4.4.0
     1111 *
     1112 * @param int          $attachment_id Image attachment ID.
     1113 * @param array|string $size          Optional. Image size. Accepts any valid image size, or an array of width
     1114 *                                    and height values in pixels (in that order). Default 'medium'.
     1115 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
     1116 * @return string|bool A 'srcset' value string or false.
     1117 */
     1118function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) {
     1119        if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) {
     1120                return false;
     1121        }
     1122
     1123        if ( ! is_array( $image_meta ) ) {
     1124                $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
     1125        }
     1126
     1127        $image_src = $image[0];
     1128        $size_array = array(
     1129                absint( $image[1] ),
     1130                absint( $image[2] )
     1131        );
     1132
     1133        return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
     1134}
     1135
     1136/**
    11081137 * Create 'sizes' attribute value for an image.
    11091138 *
    11101139 * @since 4.4.0
    11111140 *
    1112  * @param array|string $size          Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.),
    1113  *                                    or an array of width and height values in pixels (in that order).
     1141 * @param array|string $size          Image size to retrieve. Accepts any valid image size, or an array
     1142 *                                    of width and height values in pixels (in that order). Default 'medium'.
    11141143 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
    11151144 * @param int          $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` is needed
    11161145 *                                    when using the image size name as argument for `$size`.
    function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attac 
    11181147 *
    11191148 * @return string|bool A valid source size value for use in a 'sizes' attribute or false.
    11201149 */
    1121 function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $image_src = null ) {
     1150function wp_calculate_image_sizes( $size, $image_src, $image_meta, $attachment_id = 0 ) {
    11221151        $width = 0;
    11231152
    11241153        if ( is_array( $size ) ) {
    function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_i 
    11441173        $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $width );
    11451174
    11461175        /**
    1147          * Filter the output of 'wp_get_attachment_image_sizes()'.
     1176         * Filter the output of 'wp_calculate_image_sizes()'.
    11481177         *
    11491178         * @since 4.4.0
    11501179         *
    11511180         * @param string       $sizes         A source size value for use in a 'sizes' attribute.
    1152          * @param array|string $size          Image size. Image size name, or an array of width and height
    1153          *                                    values in pixels (in that order).
     1181         * @param array|string $size          Requested size. Image size or array of width and height values
     1182         *                                    in pixels (in that order). Default 'medium'.
     1183         * @param string       $image_src     The URL to the image file.
    11541184         * @param array        $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    11551185         * @param int          $attachment_id Image attachment ID of the original image.
    1156          * @param string       $image_src     Optional. The URL to the image file.
    11571186         */
    1158         return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $image_src );
     1187        return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id );
    11591188}
    11601189
    11611190/**
    function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { 
    12241253                return $image;
    12251254        }
    12261255
    1227         $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
    1228         list( $src ) = explode( '?', $src );
     1256        $image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';
     1257        list( $image_src ) = explode( '?', $image_src );
    12291258
    12301259        // Return early if we couldn't get the image source.
    1231         if ( ! $src ) {
     1260        if ( ! $image_src ) {
    12321261                return $image;
    12331262        }
    12341263
    12351264        // Bail early if an image has been inserted and later edited.
    12361265        if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) &&
    1237                 strpos( wp_basename( $src ), $img_edit_hash[0] ) === false ) {
     1266                strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) {
    12381267
    12391268                return $image;
    12401269        }
    function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { 
    12471276                 * If attempts to parse the size value failed, attempt to use the image meta data to match
    12481277                 * the image file name from 'src' against the available sizes for an attachment.
    12491278                 */
    1250                 $image_filename = wp_basename( $src );
     1279                $image_filename = wp_basename( $image_src );
    12511280
    12521281                if ( $image_filename === wp_basename( $image_meta['file'] ) ) {
    12531282                        $width = (int) $image_meta['width'];
    function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { 
    12681297        }
    12691298
    12701299        $size_array = array( $width, $height );
    1271         $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );
     1300        $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id );
    12721301
    12731302        if ( $srcset ) {
    1274                 $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id, $src );
     1303                $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );
    12751304        }
    12761305
    12771306        if ( $srcset && $sizes ) {
  • tests/phpunit/tests/media.php

    diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
    index 5734f3e..c666fd8 100644
    EOF; 
    754754                foreach ( $sizes as $size ) {
    755755                        $image_url = wp_get_attachment_image_url( self::$large_id, $size );
    756756                        $size_array = $this->_get_image_size_array_from_name( $size );
    757                         $this->assertSame( $expected, wp_calculate_image_srcset( $image_url, $size_array, $image_meta ) );
     757                        $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    758758                }
    759759        }
    760760
    EOF; 
    786786                foreach ( $sizes as $size ) {
    787787                        $size_array = $this->_get_image_size_array_from_name( $size );
    788788                        $image_url = wp_get_attachment_image_url( $id, $size );
    789                         $this->assertSame( $expected, wp_calculate_image_srcset( $image_url, $size_array, $image_meta ) );
     789                        $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    790790                }
    791791
    792792                // Remove the attachment
    EOF; 
    821821                $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file'] );
    822822
    823823                // Calculate a srcset array.
    824                 $sizes = explode( ', ', wp_calculate_image_srcset( $image_url, $size_array, $image_meta ) );
     824                $sizes = explode( ', ', wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) );
    825825
    826826                // Test to confirm all sources in the array include the same edit hash.
    827827                foreach ( $sizes as $size ) {
    EOF; 
    833833         * @ticket 33641
    834834         */
    835835        function test_wp_calculate_image_srcset_false() {
    836                 $sizes = wp_calculate_image_srcset( 'file.png', array( 400, 300 ), array() );
     836                $sizes = wp_calculate_image_srcset( array( 400, 300 ), 'file.png', array() );
    837837
    838838                // For canola.jpg we should return
    839839                $this->assertFalse( $sizes );
    EOF; 
    849849
    850850                $size_array = array(0, 0);
    851851
    852                 $srcset = wp_calculate_image_srcset( $image_url, $size_array, $image_meta );
     852                $srcset = wp_calculate_image_srcset( $size_array, $image_url, $image_meta );
    853853
    854854                // The srcset should be false.
    855855                $this->assertFalse( $srcset );
    EOF; 
    898898        function test_wp_get_attachment_image_sizes() {
    899899                // Test sizes against the default WP sizes.
    900900                $intermediates = array('thumbnail', 'medium', 'medium_large', 'large');
     901
     902                foreach( $intermediates as $int_size ) {
     903                        $image = wp_get_attachment_image_src( self::$large_id, $int_size );
     904
     905                        $expected = '(max-width: ' . $image[1] . 'px) 100vw, ' . $image[1] . 'px';
     906                        $sizes = wp_get_attachment_image_sizes( self::$large_id, $int_size );
     907
     908                        $this->assertSame( $expected, $sizes );
     909                }
     910        }
     911
     912        /**
     913         * @ticket 33641
     914         */
     915        function test_wp_calculate_image_sizes() {
     916                // Test sizes against the default WP sizes.
     917                $intermediates = array('thumbnail', 'medium', 'medium_large', 'large');
    901918                $image_meta = wp_get_attachment_metadata( self::$large_id );
    902919
    903920                foreach( $intermediates as $int_size ) {
    904921                        $size_array = $this->_get_image_size_array_from_name( $int_size );
     922                        $image_src = $image_meta['sizes'][$int_size]['file'];
    905923                        list( $width, $height ) = $size_array;
    906924
    907925                        $expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px';
    908                         $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta );
     926                        $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta );
    909927
    910928                        $this->assertSame( $expected, $sizes );
    911929                }
    EOF; 
    919937                $size_array = $this->_get_image_size_array_from_name( 'medium' );
    920938
    921939                $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) );
    922                 $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( $size_array, $image_meta, self::$large_id ) );
     940                $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) );
    923941
    924942                // Function used to build HTML for the editor.
    925943                $img = get_image_tag( self::$large_id, '', '', '', 'medium' );
    EOF; 
    10001018                // Test with soft resized size array.
    10011019                $size_array = array(900, 450);
    10021020
    1003                 $this->assertFalse( wp_calculate_image_srcset( $image_src, $size_array, $image_meta ) );
     1021                $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) );
    10041022        }
    10051023}