Ticket #36982: media-srcset-sizes.patch
File media-srcset-sizes.patch, 6.7 KB (added by , 8 years ago) |
---|
-
src/wp-includes/media.php
826 826 827 827 if ( is_array( $image_meta ) ) { 828 828 $size_array = array( absint( $width ), absint( $height ) ); 829 $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id );830 $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id );829 $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id, $attr ); 830 $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id, $attr ); 831 831 832 832 if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) { 833 833 $attr['srcset'] = $srcset; … … 972 972 * @param string $image_src The 'src' of the image. 973 973 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 974 974 * @param int $attachment_id Optional. The image attachment ID to pass to the filter. Default 0. 975 * @param array $attr Optional. Image attributes that are passed to the wp_calculate_image_srcset filter. 976 * @param string $context Optional. Context for the image that is passed to the wp_calculate_image_srcset filter. 975 977 * @return string|bool The 'srcset' attribute value. False on error or when only one source exists. 976 978 */ 977 function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) {979 function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0, $attr = array(), $context = '' ) { 978 980 /** 979 981 * Let plugins pre-filter the image meta to be able to fix inconsistencies in the stored data. 980 982 * … … 1142 1144 * @param array $size_array Array of width and height values in pixels (in that order). 1143 1145 * @param string $image_src The 'src' of the image. 1144 1146 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 1147 * @param array $attr Image attributes. 1148 * @param string $context Context for the image. 1145 1149 * @param int $attachment_id Image attachment ID or 0. 1146 1150 */ 1147 $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id );1151 $sources = apply_filters( 'wp_calculate_image_srcset', $sources, $size_array, $image_src, $image_meta, $attachment_id, $attr, $context ); 1148 1152 1149 1153 // Only return a 'srcset' value if there is more than one source. 1150 1154 if ( ! $src_matched || count( $sources ) < 2 ) { … … 1204 1208 * Default null. 1205 1209 * @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` 1206 1210 * is needed when using the image size name as argument for `$size`. Default 0. 1211 * @param array $attr Optional. Image attributes that are passed to the wp_calculate_image_sizes filter. 1212 * @param string $context Optional. Context for the image that is passed to the wp_calculate_image_sizes filter. 1207 1213 * @return string|bool A valid source size value for use in a 'sizes' attribute or false. 1208 1214 */ 1209 function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0 ) {1215 function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, $attachment_id = 0, $attr = array(), $context = '' ) { 1210 1216 $width = 0; 1211 1217 1212 1218 if ( is_array( $size ) ) { … … 1241 1247 * in pixels (in that order). 1242 1248 * @param string|null $image_src The URL to the image file or null. 1243 1249 * @param array|null $image_meta The image meta data as returned by wp_get_attachment_metadata() or null. 1250 * @param array $attr Image attributes. 1251 * @param string $context Context for the image. 1244 1252 * @param int $attachment_id Image attachment ID of the original image or 0. 1245 1253 */ 1246 return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id );1254 return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id, $attr, $context ); 1247 1255 } 1248 1256 1249 1257 /** … … 1254 1262 * @see wp_image_add_srcset_and_sizes() 1255 1263 * 1256 1264 * @param string $content The raw post content to be filtered. 1265 * @param string $context Optional. Context for the image that is passed to wp_image_add_srcset_and_sizes. 1257 1266 * @return string Converted content with 'srcset' and 'sizes' attributes added to images. 1258 1267 */ 1259 function wp_make_content_images_responsive( $content ) {1268 function wp_make_content_images_responsive( $content, $context = 'the_content' ) { 1260 1269 if ( ! preg_match_all( '/<img [^>]+>/', $content, $matches ) ) { 1261 1270 return $content; 1262 1271 } … … 1289 1298 1290 1299 foreach ( $selected_images as $image => $attachment_id ) { 1291 1300 $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); 1292 $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content );1301 $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id, $context ), $content ); 1293 1302 } 1294 1303 1295 1304 return $content; … … 1306 1315 * @param string $image An HTML 'img' element to be filtered. 1307 1316 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 1308 1317 * @param int $attachment_id Image attachment ID. 1318 * @param string $context Optional. Context for the image that is passed to wp_calculate_image_srcset and wp_calculate_image_sizes. 1309 1319 * @return string Converted 'img' element with 'srcset' and 'sizes' attributes added. 1310 1320 */ 1311 function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) {1321 function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id, $context = '' ) { 1312 1322 // Ensure the image meta exists. 1313 1323 if ( empty( $image_meta['sizes'] ) ) { 1314 1324 return $image; … … 1358 1368 } 1359 1369 1360 1370 $size_array = array( $width, $height ); 1361 $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); 1371 $image_attr = preg_match_all( '/(\S+)=\"([^\"]+)\"/', $image, $match_attr ) ? array_combine( array_map( 'esc_attr', $match_attr[1] ), array_map( 'esc_attr', $match_attr[2] ) ) : array(); 1372 $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id, $image_attr, $context ); 1362 1373 1363 1374 if ( $srcset ) { 1364 1375 // Check if there is already a 'sizes' attribute. … … 1365 1376 $sizes = strpos( $image, ' sizes=' ); 1366 1377 1367 1378 if ( ! $sizes ) { 1368 $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id );1379 $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id, $image_attr, $context ); 1369 1380 } 1370 1381 } 1371 1382