mirror of
https://github.com/WordPress/WordPress.git
synced 2024-11-05 02:10:45 +01:00
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. Built from https://develop.svn.wordpress.org/branches/4.4@36152 git-svn-id: http://core.svn.wordpress.org/branches/4.4@36118 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
67c8061bb9
commit
2ef6daa428
@ -996,6 +996,16 @@ function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag
|
|||||||
* @return string|bool The 'srcset' attribute value. False on error or when only one source exists.
|
* @return string|bool The 'srcset' attribute value. False on error or when only one source exists.
|
||||||
*/
|
*/
|
||||||
function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
|
function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {
|
||||||
|
/**
|
||||||
|
* Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data.
|
||||||
|
*
|
||||||
|
* @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'.
|
||||||
|
* @param array $size_array Array of width and height values in pixels (in that order).
|
||||||
|
* @param string $image_src The 'src' of the image.
|
||||||
|
* @param int $attachment_id The image attachment ID or 0 if not supplied.
|
||||||
|
*/
|
||||||
|
$image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id );
|
||||||
|
|
||||||
if ( empty( $image_meta['sizes'] ) ) {
|
if ( empty( $image_meta['sizes'] ) ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -1012,7 +1022,6 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac
|
|||||||
}
|
}
|
||||||
|
|
||||||
$image_basename = wp_basename( $image_meta['file'] );
|
$image_basename = wp_basename( $image_meta['file'] );
|
||||||
$image_baseurl = _wp_upload_dir_baseurl();
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* WordPress flattens animated GIFs into one frame when generating intermediate sizes.
|
* WordPress flattens animated GIFs into one frame when generating intermediate sizes.
|
||||||
@ -1029,16 +1038,15 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Uploads are (or have been) in year/month sub-directories.
|
// Retrieve the uploads sub-directory from the full size image.
|
||||||
if ( $image_basename !== $image_meta['file'] ) {
|
$dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
|
||||||
$dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
|
|
||||||
|
|
||||||
if ( $dirname ) {
|
if ( $dirname ) {
|
||||||
$image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
|
$dirname = trailingslashit( $dirname );
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$image_baseurl = trailingslashit( $image_baseurl );
|
$image_baseurl = _wp_upload_dir_baseurl();
|
||||||
|
$image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
|
||||||
|
|
||||||
// Calculate the image aspect ratio.
|
// Calculate the image aspect ratio.
|
||||||
$image_ratio = $image_height / $image_width;
|
$image_ratio = $image_height / $image_width;
|
||||||
@ -1063,12 +1071,24 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac
|
|||||||
// Array to hold URL candidates.
|
// Array to hold URL candidates.
|
||||||
$sources = array();
|
$sources = array();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* To make sure the ID matches our image src, we will check to see if any sizes in our attachment
|
||||||
|
* meta match our $image_src. If no mathces are found we don't return a srcset to avoid serving
|
||||||
|
* an incorrect image. See #35045.
|
||||||
|
*/
|
||||||
|
$src_matched = false;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Loop through available images. Only use images that are resized
|
* Loop through available images. Only use images that are resized
|
||||||
* versions of the same edit.
|
* versions of the same edit.
|
||||||
*/
|
*/
|
||||||
foreach ( $image_sizes as $image ) {
|
foreach ( $image_sizes as $image ) {
|
||||||
|
|
||||||
|
// If the file name is part of the `src`, we've confirmed a match.
|
||||||
|
if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) {
|
||||||
|
$src_matched = true;
|
||||||
|
}
|
||||||
|
|
||||||
// Filter out images that are from previous edits.
|
// Filter out images that are from previous edits.
|
||||||
if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
|
if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) {
|
||||||
continue;
|
continue;
|
||||||
@ -1126,7 +1146,7 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac
|
|||||||
$sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
|
$sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );
|
||||||
|
|
||||||
// Only return a 'srcset' value if there is more than one source.
|
// Only return a 'srcset' value if there is more than one source.
|
||||||
if ( count( $sources ) < 2 ) {
|
if ( ! $src_matched || count( $sources ) < 2 ) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1308,28 +1328,6 @@ function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) {
|
|||||||
return $image;
|
return $image;
|
||||||
}
|
}
|
||||||
|
|
||||||
$base_url = trailingslashit( _wp_upload_dir_baseurl() );
|
|
||||||
$image_base_url = $base_url;
|
|
||||||
|
|
||||||
$dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
|
|
||||||
if ( $dirname ) {
|
|
||||||
$image_base_url .= trailingslashit( $dirname );
|
|
||||||
}
|
|
||||||
|
|
||||||
$all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );
|
|
||||||
|
|
||||||
foreach ( $all_sizes as $key => $file ) {
|
|
||||||
$all_sizes[ $key ] = $image_base_url . $file;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add the original image.
|
|
||||||
$all_sizes[] = $image_base_url . basename( $image_meta['file'] );
|
|
||||||
|
|
||||||
// Bail early if the image src doesn't match any of the known image sizes.
|
|
||||||
if ( ! in_array( $image_src, $all_sizes ) ) {
|
|
||||||
return $image;
|
|
||||||
}
|
|
||||||
|
|
||||||
$width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0;
|
$width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0;
|
||||||
$height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0;
|
$height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0;
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '4.4.1-alpha-36151';
|
$wp_version = '4.4.1-alpha-36152';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
Loading…
Reference in New Issue
Block a user