Make WordPress Core


Ignore:
Timestamp:
10/30/2015 11:26:44 PM (9 years ago)
Author:
azaozz
Message:

Responsive images:

  • Merge wp_image_srcset_attr() into wp_calculate_image_srcset().
  • Remove the wp_image_srcset filter.
  • Fix the tests for the above changes.

See #34430.

File:
1 edited

Legend:

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

    r35426 r35464  
    818818            if ( is_array( $image_meta ) ) {
    819819                $size_array = array( absint( $width ), absint( $height ) );
    820                 $sources    = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );
    821                 $sizes      = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id );
    822 
    823                 if ( count( $sources ) > 1 && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
    824                     $attr['srcset'] = wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_id );
     820                $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );
     821                $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id );
     822
     823                if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) {
     824                    $attr['srcset'] = $srcset;
    825825
    826826                    if ( empty( $attr['sizes'] ) ) {
     
    944944    );
    945945
    946     // Calculate the sources for the 'srcset'.
    947     $sources = wp_calculate_image_srcset( $image_url, $size_array, $image_meta, $attachment_id );
    948 
    949     // Only return a 'srcset' value if there is more than one source.
    950     if ( count( $sources ) < 2 ) {
    951         return false;
    952     }
    953 
    954     return wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_id );
    955 }
    956 
    957 
    958 /**
    959  * A helper function to concatenate and filter the 'srcset' attribute value.
    960  *
    961  * @since 4.4.0
    962  *
    963  * @param array   $sources       The array containing image sizes data as returned by 'wp_calculate_image_srcset()'.
    964  * @param array   $size_array    Array of width and height values in pixels (in that order).
    965  * @param array   $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    966  * @param int     $attachment_id The image attachment ID to pass to the filter.
    967  * @return string The 'srcset' attribute value.
    968  */
    969 function wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_id ) {
    970     $srcset = '';
    971 
    972     foreach ( $sources as $source ) {
    973         $srcset .= $source['url'] . ' ' . $source['value'] . $source['descriptor'] . ', ';
    974     }
    975 
    976     /**
    977      * Filter the output of 'wp_image_srcset_attr()'.
    978      *
    979      * @since 4.4.0
    980      *
    981      * @param string       $srcset        A source set formatted for a 'srcset' attribute.
    982      * @param int          $attachment_id Image attachment ID.
    983      * @param array|string $size          Image size. Image size name, or an array of width and height
    984      *                                    values in pixels (in that order).
    985      * @param array        $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    986      */
    987     return apply_filters( 'wp_image_srcset', rtrim( $srcset, ', ' ), $attachment_id, $size_array, $image_meta );
     946    return wp_calculate_image_srcset( $image_url, $size_array, $image_meta, $attachment_id );
    988947}
    989948
     
    997956 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    998957 * @param int    $attachment_id Optional. The image attachment ID to pass to the filter.
    999  * @return array|bool $sources {
    1000  *     Array image candidate values containing a URL, descriptor type, and
    1001  *     descriptor value. False if none exist.
    1002  *
    1003  *     @type array $values {
    1004  *        @type string $url        An image URL.
    1005  *        @type string $descriptor A width or density descriptor used in a 'srcset'.
    1006  *        @type int    $value      The descriptor value representing a width or
    1007  *                                 or pixel density.
    1008  *     }
    1009  * }
     958 * @return string|bool          The 'srcset' attribute value. False on error or when only one source exists.
    1010959 */
    1011960function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $attachment_id = 0 ) {
     
    11031052     * @since 4.4.0
    11041053     *
    1105      * @param array        $sources       An array of image URLs and widths.
    1106      * @param int          $attachment_id Image attachment ID.
    1107      * @param array|string $size          Image size. Image size name, or an array of width and height
    1108      *                                    values in pixels (in that order).
    1109      * @param array        $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    1110 
    1111      */
    1112     return apply_filters( 'wp_calculate_image_srcset', array_values( $sources ), $attachment_id, $size_array, $image_meta );
     1054     * @param array $sources       An array of image URLs and widths.
     1055     * @param int   $attachment_id Image attachment ID.
     1056     * @param array $size_array    Array of width and height values in pixels (in that order).
     1057     * @param array $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
     1058
     1059     */
     1060    $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $attachment_id, $size_array, $image_meta );
     1061
     1062    // Only return a 'srcset' value if there is more than one source.
     1063    if ( count( $sources ) < 2 ) {
     1064        return false;
     1065    }
     1066
     1067    $srcset = '';
     1068
     1069    foreach ( $sources as $source ) {
     1070        $srcset .= $source['url'] . ' ' . $source['value'] . $source['descriptor'] . ', ';
     1071    }
     1072
     1073    return rtrim( $srcset, ', ' );
    11131074}
    11141075
     
    12771238
    12781239    $size_array = array( $width, $height );
    1279     $sources = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );
    1280 
    1281     $srcset = $sizes = '';
    1282     // Only calculate 'srcset' and 'sizes' values if there is more than one source.
    1283     if ( count( $sources ) > 1 ) {
    1284         $srcset = wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_id );
     1240    $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id );
     1241
     1242    if ( $srcset ) {
    12851243        $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id );
    12861244    }
Note: See TracChangeset for help on using the changeset viewer.