WordPress.org

Make WordPress Core

Changeset 35464


Ignore:
Timestamp:
10/30/2015 11:26:44 PM (4 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.

Location:
trunk
Files:
2 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    }
  • trunk/tests/phpunit/tests/media.php

    r35412 r35464  
    740740        $year_month = date('Y/m');
    741741        $image_meta = wp_get_attachment_metadata( self::$large_id );
    742 
    743         $expected = array(
    744             array(
    745                 'url'        => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/' . $image_meta['sizes']['medium']['file'],
    746                 'descriptor' => 'w',
    747                 'value'      => $image_meta['sizes']['medium']['width'],
    748             ),
    749             array(
    750                 'url'        => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/' . $image_meta['sizes']['large']['file'],
    751                 'descriptor' => 'w',
    752                 'value'      => $image_meta['sizes']['large']['width'],
    753             ),
    754             array(
    755                 'url'        => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'],
    756                 'descriptor' => 'w',
    757                 'value'      => $image_meta['width'],
    758             ),
    759         );
     742        $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
     743
     744        $expected = $uploads_dir_url . $year_month . '/' . $image_meta['sizes']['medium']['file'] . ' ' . $image_meta['sizes']['medium']['width'] . 'w, ' .
     745                $uploads_dir_url . $year_month . '/' . $image_meta['sizes']['large']['file'] . ' ' . $image_meta['sizes']['large']['width'] . 'w, ' .
     746                $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
    760747
    761748        // Set up test cases for all expected size names and a random one.
     
    784771
    785772        $image_meta = wp_get_attachment_metadata( $id );
    786 
    787         $expected = array(
    788             array(
    789                 'url'        => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['medium']['file'],
    790                 'descriptor' => 'w',
    791                 'value'      => $image_meta['sizes']['medium']['width'],
    792             ),
    793             array(
    794                 'url'        => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'],
    795                 'descriptor' => 'w',
    796                 'value'      => $image_meta['sizes']['large']['width'],
    797             ),
    798             array(
    799                 'url'        => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'],
    800                 'descriptor' => 'w',
    801                 'value'      => $image_meta['width'],
    802             ),
    803         );
     773        $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/';
     774
     775        $expected = $uploads_dir_url . $image_meta['sizes']['medium']['file'] . ' ' . $image_meta['sizes']['medium']['width'] . 'w, ' .
     776                $uploads_dir_url . $image_meta['sizes']['large']['file'] . ' ' . $image_meta['sizes']['large']['width'] . 'w, ' .
     777                $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] . 'w';
    804778
    805779        // Set up test cases for all expected size names and a random one.
     
    836810
    837811        // Calculate a srcset array.
    838         $sizes = wp_calculate_image_srcset( $image_url, $size_array, $image_meta );
     812        $sizes = explode( ', ', wp_calculate_image_srcset( $image_url, $size_array, $image_meta ) );
    839813
    840814        // Test to confirm all sources in the array include the same edit hash.
    841815        foreach ( $sizes as $size ) {
    842             $this->assertTrue( false !== strpos( $size['url'], $hash ) );
     816            $this->assertTrue( false !== strpos( $size, $hash ) );
    843817        }
    844818    }
Note: See TracChangeset for help on using the changeset viewer.