Ticket #35045: 35045.8.diff
File 35045.8.diff, 6.1 KB (added by , 9 years ago) |
---|
-
src/wp-includes/media.php
971 971 * @return string|bool The 'srcset' attribute value. False on error or when only one source exists. 972 972 */ 973 973 function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) { 974 /** 975 * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data. 976 * 977 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 978 * @param array $size_array Array of width and height values in pixels (in that order). 979 * @param string $image_src The 'src' of the image. 980 * @param int $attachment_id The image attachment ID or 0 if not supplied. 981 */ 982 $image_meta = apply_filters( 'wp_calculate_image_srcset_meta', $image_meta, $size_array, $image_src, $attachment_id ); 983 974 984 if ( empty( $image_meta['sizes'] ) ) { 975 985 return false; 976 986 } … … 987 997 } 988 998 989 999 $image_basename = wp_basename( $image_meta['file'] ); 990 $image_baseurl = _wp_upload_dir_baseurl();991 1000 992 1001 /* 993 1002 * WordPress flattens animated GIFs into one frame when generating intermediate sizes. … … 1004 1013 return false; 1005 1014 } 1006 1015 1007 // Uploads are (or have been) in year/month sub-directories.1008 if ( $image_basename !== $image_meta['file'] ) {1009 $dirname = dirname( $image_meta['file']);1016 // Retrieve the uploads sub-directory from the full size image. 1017 $dirname = dirname( $image_meta['file'] ); 1018 $dirname = $dirname === '.' ? '' : trailingslashit( $dirname ); 1010 1019 1011 if ( $dirname !== '.' ) { 1012 $image_baseurl = trailingslashit( $image_baseurl ) . $dirname; 1013 } 1014 } 1020 $image_baseurl = _wp_upload_dir_baseurl(); 1021 $image_baseurl = trailingslashit( $image_baseurl ) . $dirname; 1015 1022 1016 $image_baseurl = trailingslashit( $image_baseurl );1017 1018 1023 /* 1019 1024 * Images that have been edited in WordPress after being uploaded will 1020 1025 * contain a unique hash. Look for that hash and use it later to filter … … 1035 1040 // Array to hold URL candidates. 1036 1041 $sources = array(); 1037 1042 1043 /** 1044 * To make sure the ID matches our image src, we will check to see if any sizes in our attachment 1045 * meta match our $image_src. If no mathces are found we don't return a srcset to avoid serving 1046 * an incorrect image. See #35045. 1047 */ 1048 $src_matched = false; 1049 1038 1050 /* 1039 1051 * Loop through available images. Only use images that are resized 1040 1052 * versions of the same edit. … … 1041 1053 */ 1042 1054 foreach ( $image_sizes as $image ) { 1043 1055 1056 // If the file name is part of the `src`, we've confirmed a match. 1057 if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) { 1058 $src_matched = true; 1059 } 1060 1044 1061 // Filter out images that are from previous edits. 1045 1062 if ( $image_edited && ! strpos( $image['file'], $image_edit_hash[0] ) ) { 1046 1063 continue; … … 1099 1116 $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id ); 1100 1117 1101 1118 // Only return a 'srcset' value if there is more than one source. 1102 if ( count( $sources ) < 2 ) {1119 if ( ! $src_matched || count( $sources ) < 2 ) { 1103 1120 return false; 1104 1121 } 1105 1122 … … 1281 1298 return $image; 1282 1299 } 1283 1300 1284 $base_url = trailingslashit( _wp_upload_dir_baseurl() );1285 $image_base_url = $base_url;1286 1287 $dirname = dirname( $image_meta['file'] );1288 if ( $dirname !== '.' ) {1289 $image_base_url .= trailingslashit( $dirname );1290 }1291 1292 $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' );1293 1294 foreach ( $all_sizes as $key => $file ) {1295 $all_sizes[ $key ] = $image_base_url . $file;1296 }1297 1298 // Add the original image.1299 $all_sizes[] = $base_url . $image_meta['file'];1300 1301 // Bail early if the image src doesn't match any of the known image sizes.1302 if ( ! in_array( $image_src, $all_sizes ) ) {1303 return $image;1304 }1305 1306 1301 $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; 1307 1302 $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0; 1308 1303 -
tests/phpunit/tests/media.php
1177 1177 // Intermediate sized GIFs should not include the full size in the srcset. 1178 1178 $this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) ); 1179 1179 } 1180 1181 /** 1182 * @ticket 35045 1183 * @ticket 33641 1184 */ 1185 function test_wp_make_content_images_responsive_schemes() { 1186 $image_meta = wp_get_attachment_metadata( self::$large_id ); 1187 $size_array = $this->_get_image_size_array_from_name( 'medium' ); 1188 1189 $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) ); 1190 $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) ); 1191 1192 // Build HTML for the editor. 1193 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 1194 $img_https = str_replace( 'http://', 'https://', $img ); 1195 $img_relative = str_replace( 'http://', '//', $img ); 1196 1197 // Manually add srcset and sizes to the markup from get_image_tag(). 1198 $respimg = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img ); 1199 $respimg_https = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_https ); 1200 $respimg_relative = preg_replace( '|<img ([^>]+) />|', '<img $1 ' . $srcset . ' ' . $sizes . ' />', $img_relative ); 1201 1202 $content = ' 1203 <p>Image, http: protocol. Should have srcset and sizes.</p> 1204 %1$s 1205 1206 <p>Image, http: protocol. Should have srcset and sizes.</p> 1207 %2$s 1208 1209 <p>Image, protocol-relative. Should have srcset and sizes.</p> 1210 %3$s'; 1211 1212 $unfiltered = sprintf( $content, $img, $img_https, $img_relative ); 1213 $expected = sprintf( $content, $respimg, $respimg_https, $respimg_relative ); 1214 $actual = wp_make_content_images_responsive( $unfiltered ); 1215 1216 $this->assertSame( $expected, $actual ); 1217 } 1180 1218 }