- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/4.4/src/wp-includes/media.php
r35821 r36152 879 879 880 880 /** 881 * Get the attachment path relative to the upload directory. 882 * 883 * @since 4.4.1 884 * @access private 885 * 886 * @param string $file Attachment file name. 887 * @return string Attachment path relative to the upload directory. 888 */ 889 function _wp_get_attachment_relative_path( $file ) { 890 $dirname = dirname( $file ); 891 892 if ( '.' === $dirname ) { 893 return ''; 894 } 895 896 if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) { 897 // Get the directory name relative to the upload directory (back compat for pre-2.7 uploads) 898 $dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 ); 899 $dirname = ltrim( $dirname, '/' ); 900 } 901 902 return $dirname; 903 } 904 905 /** 881 906 * Caches and returns the base URL of the uploads directory. 882 907 * … … 972 997 */ 973 998 function 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 974 1009 if ( empty( $image_meta['sizes'] ) ) { 975 1010 return false; … … 988 1023 989 1024 $image_basename = wp_basename( $image_meta['file'] ); 990 $image_baseurl = _wp_upload_dir_baseurl();991 1025 992 1026 /* … … 1005 1039 } 1006 1040 1007 // Uploads are (or have been) in year/month sub-directories. 1008 if ( $image_basename !== $image_meta['file'] ) { 1009 $dirname = dirname( $image_meta['file'] ); 1010 1011 if ( $dirname !== '.' ) { 1012 $image_baseurl = trailingslashit( $image_baseurl ) . $dirname; 1013 } 1014 } 1015 1016 $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; 1017 1050 1018 1051 // Calculate the image aspect ratio. … … 1039 1072 $sources = array(); 1040 1073 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 1041 1081 /* 1042 1082 * Loop through available images. Only use images that are resized … … 1045 1085 foreach ( $image_sizes as $image ) { 1046 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 } 1091 1047 1092 // Filter out images that are from previous edits. 1048 1093 if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) { … … 1050 1095 } 1051 1096 1052 // Filter out images that are wider than '$max_srcset_image_width'. 1053 if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width ) { 1097 /* 1098 * Filter out images that are wider than '$max_srcset_image_width' unless 1099 * that file is in the 'src' attribute. 1100 */ 1101 if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width && 1102 false === strpos( $image_src, $image['file'] ) ) { 1103 1054 1104 continue; 1055 1105 } … … 1097 1147 1098 1148 // Only return a 'srcset' value if there is more than one source. 1099 if ( count( $sources ) < 2 ) {1149 if ( ! $src_matched || count( $sources ) < 2 ) { 1100 1150 return false; 1101 1151 } … … 1276 1326 strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) { 1277 1327 1278 return $image;1279 }1280 1281 $base_url = trailingslashit( _wp_upload_dir_baseurl() );1282 $image_base_url = $base_url;1283 1284 $dirname = dirname( $image_meta['file'] );1285 if ( $dirname !== '.' ) {1286 $image_base_url .= trailingslashit( $dirname );1287 }1288 1289 $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );1290 1291 foreach ( $all_sizes as $key => $file ) {1292 $all_sizes[ $key ] = $image_base_url . $file;1293 }1294 1295 // Add the original image.1296 $all_sizes[] = $base_url . $image_meta['file'];1297 1298 // Bail early if the image src doesn't match any of the known image sizes.1299 if ( ! in_array( $image_src, $all_sizes ) ) {1300 1328 return $image; 1301 1329 }
Note: See TracChangeset
for help on using the changeset viewer.