WordPress.org

Make WordPress Core

Changeset 35569


Ignore:
Timestamp:
11/07/15 21:35:34 (19 months ago)
Author:
azaozz
Message:

Responsive images: make the new functions and filters signatures more consistent.

Props joemcgill.
Fixes #34612.

Location:
trunk
Files:
2 edited

Legend:

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

    r35567 r35569  
    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'] ) ) ) { 
     
    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. 
     
    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 
     
    962962 * @since 4.4.0 
    963963 * 
     964 * @param array  $size_array    Array of width and height values in pixels (in that order). 
    964965 * @param string $image_src     The 'src' of the image. 
    965  * @param array  $size_array    Array of width and height values in pixels (in that order). 
    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; 
     
    11071107 
    11081108/** 
     1109 * Retrieves the value for an image attachment's 'sizes' attribute. 
     1110 * 
     1111 * @since 4.4.0 
     1112 * 
     1113 * @param int          $attachment_id Image attachment ID. 
     1114 * @param array|string $size          Optional. Image size. Accepts any valid image size, or an array of width 
     1115 *                                    and height values in pixels (in that order). Default 'medium'. 
     1116 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. 
     1117 * @return string|bool A 'srcset' value string or false. 
     1118 */ 
     1119function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) { 
     1120    if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) { 
     1121        return false; 
     1122    } 
     1123 
     1124    if ( ! is_array( $image_meta ) ) { 
     1125        $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); 
     1126    } 
     1127 
     1128    $image_src = $image[0]; 
     1129    $size_array = array( 
     1130        absint( $image[1] ), 
     1131        absint( $image[2] ) 
     1132    ); 
     1133 
     1134    return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); 
     1135} 
     1136 
     1137/** 
    11091138 * Create 'sizes' attribute value for an image. 
    11101139 * 
    11111140 * @since 4.4.0 
    11121141 * 
    1113  * @param array|string $size          Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.), 
    1114  *                                    or an array of width and height values in pixels (in that order). 
     1142 * @param array|string $size          Image size to retrieve. Accepts any valid image size, or an array 
     1143 *                                    of width and height values in pixels (in that order). Default 'medium'. 
    11151144 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. 
    11161145 * @param int          $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` is needed 
     
    11201149 * @return string|bool A valid source size value for use in a 'sizes' attribute or false. 
    11211150 */ 
    1122 function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $image_src = null ) { 
     1151function wp_calculate_image_sizes( $size, $image_src, $image_meta, $attachment_id = 0 ) { 
    11231152    $width = 0; 
    11241153 
     
    11461175 
    11471176    /** 
    1148      * Filter the output of 'wp_get_attachment_image_sizes()'. 
     1177     * Filter the output of 'wp_calculate_image_sizes()'. 
    11491178     * 
    11501179     * @since 4.4.0 
    11511180     * 
    11521181     * @param string       $sizes         A source size value for use in a 'sizes' attribute. 
    1153      * @param array|string $size          Image size. Image size name, or an array of width and height 
    1154      *                                    values in pixels (in that order). 
     1182     * @param array|string $size          Requested size. Image size or array of width and height values 
     1183     *                                    in pixels (in that order). Default 'medium'. 
     1184     * @param string       $image_src     The URL to the image file. 
    11551185     * @param array        $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'. 
    11561186     * @param int          $attachment_id Image attachment ID of the original image. 
    1157      * @param string       $image_src     Optional. The URL to the image file. 
    1158      */ 
    1159     return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $image_src ); 
     1187     */ 
     1188    return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id ); 
    11601189} 
    11611190 
     
    12261255    } 
    12271256 
    1228     $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; 
    1229     list( $src ) = explode( '?', $src ); 
     1257    $image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; 
     1258    list( $image_src ) = explode( '?', $image_src ); 
    12301259 
    12311260    // Return early if we couldn't get the image source. 
    1232     if ( ! $src ) { 
     1261    if ( ! $image_src ) { 
    12331262        return $image; 
    12341263    } 
     
    12361265    // Bail early if an image has been inserted and later edited. 
    12371266    if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && 
    1238         strpos( wp_basename( $src ), $img_edit_hash[0] ) === false ) { 
     1267        strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) { 
    12391268 
    12401269        return $image; 
     
    12491278         * the image file name from 'src' against the available sizes for an attachment. 
    12501279         */ 
    1251         $image_filename = wp_basename( $src ); 
     1280        $image_filename = wp_basename( $image_src ); 
    12521281 
    12531282        if ( $image_filename === wp_basename( $image_meta['file'] ) ) { 
     
    12701299 
    12711300    $size_array = array( $width, $height ); 
    1272     $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id ); 
     1301    $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); 
    12731302 
    12741303    if ( $srcset ) { 
    1275         $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id, $src ); 
     1304        $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); 
    12761305    } 
    12771306 
  • trunk/tests/phpunit/tests/media.php

    r35561 r35569  
    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    } 
     
    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 
     
    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. 
     
    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 
     
    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. 
     
    915915        // Test sizes against the default WP sizes. 
    916916        $intermediates = array('thumbnail', 'medium', 'medium_large', 'large'); 
     917 
     918        foreach( $intermediates as $int_size ) { 
     919            $image = wp_get_attachment_image_src( self::$large_id, $int_size ); 
     920 
     921            $expected = '(max-width: ' . $image[1] . 'px) 100vw, ' . $image[1] . 'px'; 
     922            $sizes = wp_get_attachment_image_sizes( self::$large_id, $int_size ); 
     923 
     924            $this->assertSame( $expected, $sizes ); 
     925        } 
     926    } 
     927 
     928    /** 
     929     * @ticket 33641 
     930     */ 
     931    function test_wp_calculate_image_sizes() { 
     932        // Test sizes against the default WP sizes. 
     933        $intermediates = array('thumbnail', 'medium', 'medium_large', 'large'); 
    917934        $image_meta = wp_get_attachment_metadata( self::$large_id ); 
    918935 
    919936        foreach( $intermediates as $int_size ) { 
    920937            $size_array = $this->_get_image_size_array_from_name( $int_size ); 
     938            $image_src = $image_meta['sizes'][$int_size]['file']; 
    921939            list( $width, $height ) = $size_array; 
    922940 
    923941            $expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px'; 
    924             $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta ); 
     942            $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta ); 
    925943 
    926944            $this->assertSame( $expected, $sizes ); 
     
    936954 
    937955        $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) ); 
    938         $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( $size_array, $image_meta, self::$large_id ) ); 
     956        $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) ); 
    939957 
    940958        // Function used to build HTML for the editor. 
     
    10201038 
    10211039        // Full size GIFs should not return a srcset. 
    1022         $this->assertFalse( wp_calculate_image_srcset( $full_src, $size_array, $image_meta ) ); 
     1040        $this->assertFalse( wp_calculate_image_srcset( $size_array, $full_src, $image_meta ) ); 
    10231041        // Intermediate sized GIFs should not include the full size in the srcset. 
    1024         $this->assertFalse( strpos( wp_calculate_image_srcset( $large_src, $size_array, $image_meta ), $full_src ) ); 
     1042        $this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) ); 
    10251043    } 
    10261044} 
Note: See TracChangeset for help on using the changeset viewer.