Make WordPress Core


Ignore:
Timestamp:
01/02/2016 03:55:47 AM (11 years ago)
Author:
dd32
Message:

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

Merges [36121] to the 4.4 branch.
Props webaware, joemcgill, azaozz.
Fixes #35045 and #35102.

Location:
branches/4.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.4

  • branches/4.4/src/wp-includes/media.php

    r36151 r36152  
    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        // Calculate the image aspect ratio.
     
    10641072        $sources = array();
    10651073
     1074        /**
     1075         * To make sure the ID matches our image src, we will check to see if any sizes in our attachment
     1076         * meta match our $image_src. If no mathces are found we don't return a srcset to avoid serving
     1077         * an incorrect image. See #35045.
     1078         */
     1079        $src_matched = false;
     1080
    10661081        /*
    10671082         * Loop through available images. Only use images that are resized
     
    10691084         */
    10701085        foreach ( $image_sizes as $image ) {
     1086
     1087                // If the file name is part of the `src`, we've confirmed a match.
     1088                if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) {
     1089                        $src_matched = true;
     1090                }
    10711091
    10721092                // Filter out images that are from previous edits.
     
    11271147
    11281148        // Only return a 'srcset' value if there is more than one source.
    1129         if ( count( $sources ) < 2 ) {
     1149        if ( ! $src_matched || count( $sources ) < 2 ) {
    11301150                return false;
    11311151        }
     
    13061326                strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) {
    13071327
    1308                 return $image;
    1309         }
    1310 
    1311         $base_url = trailingslashit( _wp_upload_dir_baseurl() );
    1312         $image_base_url = $base_url;
    1313 
    1314         $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
    1315         if ( $dirname ) {
    1316                 $image_base_url .= trailingslashit( $dirname );
    1317         }
    1318 
    1319         $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );
    1320 
    1321         foreach ( $all_sizes as $key => $file ) {
    1322                 $all_sizes[ $key ] = $image_base_url . $file;
    1323         }
    1324 
    1325         // Add the original image.
    1326         $all_sizes[] = $image_base_url . basename( $image_meta['file'] );
    1327 
    1328         // Bail early if the image src doesn't match any of the known image sizes.
    1329         if ( ! in_array( $image_src, $all_sizes ) ) {
    13301328                return $image;
    13311329        }
Note: See TracChangeset for help on using the changeset viewer.