WordPress.org

Make WordPress Core

Changeset 36121


Ignore:
Timestamp:
12/30/2015 01:03:11 AM (5 years ago)
Author:
azaozz
Message:

Responsive images: fix the check whether the attachment meta matches the image src to work with http/https and CDNs.

Props webaware, joemcgill, azaozz.
Fixes #35045 and #35102 for trunk.

Location:
trunk
Files:
2 edited

Legend:

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

    r36120 r36121  
    997997 */
    998998function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
     999    /**
     1000     * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data.
     1001     *
     1002     * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
     1003     * @param array  $size_array    Array of width and height values in pixels (in that order).
     1004     * @param string $image_src     The 'src' of the image.
     1005     * @param int    $attachment_id The image attachment ID or 0 if not supplied.
     1006     */
     1007    $image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id );
     1008
    9991009    if ( empty( $image_meta['sizes'] ) ) {
    10001010        return false;
     
    10131023
    10141024    $image_basename = wp_basename( $image_meta['file'] );
    1015     $image_baseurl = _wp_upload_dir_baseurl();
    10161025
    10171026    /*
     
    10301039    }
    10311040
    1032     // Uploads are (or have been) in year/month sub-directories.
    1033     if ( $image_basename !== $image_meta['file'] ) {
    1034         $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
    1035 
    1036         if ( $dirname ) {
    1037             $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    1038         }
    1039     }
    1040 
    1041     $image_baseurl = trailingslashit( $image_baseurl );
     1041    // Retrieve the uploads sub-directory from the full size image.
     1042    $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
     1043
     1044    if ( $dirname ) {
     1045        $dirname = trailingslashit( $dirname );
     1046    }
     1047
     1048    $image_baseurl = _wp_upload_dir_baseurl();
     1049    $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    10421050
    10431051    /*
     
    10611069    $sources = array();
    10621070
     1071    /**
     1072     * To make sure the ID matches our image src, we will check to see if any sizes in our attachment
     1073     * meta match our $image_src. If no mathces are found we don't return a srcset to avoid serving
     1074     * an incorrect image. See #35045.
     1075     */
     1076    $src_matched = false;
     1077
    10631078    /*
    10641079     * Loop through available images. Only use images that are resized
     
    10661081     */
    10671082    foreach ( $image_sizes as $image ) {
     1083
     1084        // If the file name is part of the `src`, we've confirmed a match.
     1085        if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) {
     1086            $src_matched = true;
     1087        }
    10681088
    10691089        // Filter out images that are from previous edits.
     
    11301150
    11311151    // Only return a 'srcset' value if there is more than one source.
    1132     if ( count( $sources ) < 2 ) {
     1152    if ( ! $src_matched || count( $sources ) < 2 ) {
    11331153        return false;
    11341154    }
     
    13091329        strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) {
    13101330
    1311         return $image;
    1312     }
    1313 
    1314     $base_url = trailingslashit( _wp_upload_dir_baseurl() );
    1315     $image_base_url = $base_url;
    1316 
    1317     $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
    1318     if ( $dirname ) {
    1319         $image_base_url .= trailingslashit( $dirname );
    1320     }
    1321 
    1322     $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );
    1323 
    1324     foreach ( $all_sizes as $key => $file ) {
    1325         $all_sizes[ $key ] = $image_base_url . $file;
    1326     }
    1327 
    1328     // Add the original image.
    1329     $all_sizes[] = $image_base_url . basename( $image_meta['file'] );
    1330 
    1331     // Bail early if the image src doesn't match any of the known image sizes.
    1332     if ( ! in_array( $image_src, $all_sizes ) ) {
    13331331        return $image;
    13341332    }
  • trunk/tests/phpunit/tests/media.php

    r36120 r36121  
    12661266        $this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) );
    12671267    }
     1268
     1269    /**
     1270     * @ticket 35045
     1271     * @ticket 33641
     1272     */
     1273    function test_wp_make_content_images_responsive_schemes() {
     1274        $image_meta = wp_get_attachment_metadata( self::$large_id );
     1275        $size_array = $this->_get_image_size_array_from_name( 'medium' );
     1276
     1277        $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) );
     1278        $sizes  = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) );
     1279
     1280        // Build HTML for the editor.
     1281        $img          = get_image_tag( self::$large_id, '', '', '', 'medium' );
     1282        $img_https    = str_replace( 'http://', 'https://', $img );
     1283        $img_relative = str_replace( 'http://', '//', $img );
     1284
     1285        // Manually add srcset and sizes to the markup from get_image_tag().
     1286        $respimg          = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img );
     1287        $respimg_https    = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_https );
     1288        $respimg_relative = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_relative );
     1289
     1290        $content = '
     1291            <p>Image, http: protocol. Should have srcset and sizes.</p>
     1292            %1$s
     1293
     1294            <p>Image, https: protocol. Should have srcset and sizes.</p>
     1295            %2$s
     1296
     1297            <p>Image, protocol-relative. Should have srcset and sizes.</p>
     1298            %3$s';
     1299
     1300        $unfiltered = sprintf( $content, $img, $img_https, $img_relative );
     1301        $expected   = sprintf( $content, $respimg, $respimg_https, $respimg_relative );
     1302        $actual     = wp_make_content_images_responsive( $unfiltered );
     1303
     1304        $this->assertSame( $expected, $actual );
     1305    }
    12681306}
Note: See TracChangeset for help on using the changeset viewer.