diff --git src/wp-includes/media.php src/wp-includes/media.php
index 4f43e59..d8ba4fc 100644
|
|
function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon |
881 | 881 | * } |
882 | 882 | * |
883 | 883 | */ |
884 | | function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' ) { |
885 | | // Get the intermediate size. |
886 | | $image = image_get_intermediate_size( $attachment_id, $size ); |
887 | | |
| 884 | function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium', $attachment_meta = null, $img_url = null ) { |
888 | 885 | // Get the post meta. |
889 | | $img_meta = wp_get_attachment_metadata( $attachment_id ); |
890 | | if ( ! is_array( $img_meta ) ) { |
| 886 | $img_meta = ( is_array( $attachment_meta ) ) ? $attachment_meta : wp_get_attachment_metadata( $attachment_id ); |
| 887 | if ( empty( $img_meta['sizes'] ) ) { |
891 | 888 | return false; |
892 | 889 | } |
893 | 890 | |
894 | | // Extract the height and width from the intermediate or the full size. |
895 | | $img_width = ( $image ) ? $image['width'] : $img_meta['width']; |
896 | | $img_height = ( $image ) ? $image['height'] : $img_meta['height']; |
897 | | |
898 | | // Bail early if the width isn't greater than zero. |
899 | | if ( ! $img_width > 0 ) { |
900 | | return false; |
| 891 | // Try using passed in $img_url if we have it. |
| 892 | if ( ! $img_url && $image = wp_get_attachment_image_src( $attachment_id, $size ) ) { |
| 893 | $img_url = $image[0]; |
901 | 894 | } |
902 | 895 | |
903 | | // Use the URL from the intermediate size or build the url from the metadata. |
904 | | if ( ! empty( $image['url'] ) ) { |
905 | | $img_url = $image['url']; |
| 896 | $img_sizes = $img_meta['sizes']; |
| 897 | |
| 898 | // Get the intermediate size. |
| 899 | if ( is_array( $size ) ) { |
| 900 | list( $img_width, $img_height ) = $size; |
906 | 901 | } else { |
907 | | $uploads_dir = wp_upload_dir(); |
908 | | $img_file = ( $image ) ? path_join( dirname( $img_meta['file'] ) , $image['file'] ) : $img_meta['file']; |
909 | | $img_url = $uploads_dir['baseurl'] . '/' . $img_file; |
| 902 | $img_width = ( ! empty( $img_sizes[$size] ) ) ? $img_sizes[$size]['width'] : $img_meta['width']; |
| 903 | $img_height = ( ! empty( $img_sizes[$size] ) ) ? $img_sizes[$size]['height'] : $img_meta['height']; |
910 | 904 | } |
911 | 905 | |
912 | | $img_sizes = $img_meta['sizes']; |
| 906 | // Bail early if the width isn't greater than zero. |
| 907 | if ( ! $img_width > 0 ) { |
| 908 | return false; |
| 909 | } |
913 | 910 | |
914 | 911 | // Add full size to the img_sizes array. |
915 | 912 | $img_sizes['full'] = array( |
… |
… |
function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' |
949 | 946 | */ |
950 | 947 | foreach ( $img_sizes as $img ) { |
951 | 948 | |
952 | | // Filter out images that are leftovers from previous renditions. |
| 949 | // Filter out images that are leftovers from previous versions. |
953 | 950 | if ( $img_edited && ! strpos( $img['file'], $img_edit_hash[0] ) ) { |
954 | 951 | continue; |
955 | 952 | } |
… |
… |
function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' |
959 | 956 | continue; |
960 | 957 | } |
961 | 958 | |
962 | | $candidate_url = path_join( dirname( $img_url ), $img['file'] ); |
963 | | |
964 | 959 | // Calculate the new image ratio. |
965 | 960 | if ( $img['width'] ) { |
966 | 961 | $img_ratio_compare = $img['height'] / $img['width']; |
… |
… |
function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' |
969 | 964 | } |
970 | 965 | |
971 | 966 | // If the new ratio differs by less than 0.01, use it. |
972 | | if ( abs( $img_ratio - $img_ratio_compare ) < 0.01 && ! in_array( $candidate_url, $candidates ) ) { |
973 | | // Add the URL to our list of candidates. |
974 | | $candidates[] = $candidate_url; |
| 967 | if ( abs( $img_ratio - $img_ratio_compare ) < 0.01 && ! in_array( $img['file'], $candidates ) ) { |
| 968 | // Add the image file to our list of candidates. |
| 969 | $candidates[] = $img['file']; |
975 | 970 | |
976 | 971 | // Add the url, descriptor, and value to the sources array to be returned. |
977 | 972 | $sources[] = array( |
978 | | 'url' => $candidate_url, |
| 973 | 'url' => path_join( dirname( $img_url ), $img['file'] ), |
979 | 974 | 'descriptor' => 'w', |
980 | 975 | 'value' => $img['width'], |
981 | 976 | ); |
… |
… |
function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' |
1005 | 1000 | * values in pixels (in that order). Default 'medium'. |
1006 | 1001 | * @return string|bool A 'srcset' value string or false. |
1007 | 1002 | */ |
1008 | | function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium' ) { |
1009 | | $srcset_array = wp_get_attachment_image_srcset_array( $attachment_id, $size ); |
| 1003 | function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $attachment_meta = null, $img_url = null ) { |
| 1004 | $srcset_array = wp_get_attachment_image_srcset_array( $attachment_id, $size, $attachment_meta, $img_url ); |
1010 | 1005 | |
1011 | 1006 | // Only return a srcset value if there is more than one source. |
1012 | 1007 | if ( count( $srcset_array ) <= 1 ) { |
… |
… |
function wp_make_content_images_responsive( $content ) { |
1091 | 1086 | $images = get_media_embedded_in_content( $content, 'img' ); |
1092 | 1087 | |
1093 | 1088 | $attachment_ids = array(); |
| 1089 | $match_groups = array(); |
1094 | 1090 | |
1095 | 1091 | foreach( $images as $image ) { |
1096 | 1092 | if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) { |
1097 | | $attachment_id = (int) $class_id[1]; |
1098 | | if ( $attachment_id ) { |
1099 | | $attachment_ids[] = $attachment_id; |
1100 | | } |
| 1093 | $attachment_ids[] = (int) $class_id[1]; |
| 1094 | $match_groups[] = array( |
| 1095 | 'id' => (int) $class_id[1], |
| 1096 | 'html' => $image, |
| 1097 | ); |
1101 | 1098 | } |
1102 | 1099 | } |
1103 | 1100 | |
… |
… |
function wp_make_content_images_responsive( $content ) { |
1111 | 1108 | _prime_post_caches( $attachment_ids, false, true ); |
1112 | 1109 | } |
1113 | 1110 | |
1114 | | foreach( $images as $image ) { |
1115 | | $content = str_replace( $image, wp_img_add_srcset_and_sizes( $image ), $content ); |
| 1111 | foreach( $match_groups as $match ) { |
| 1112 | $post_meta = get_metadata( 'post', $match['id'], '_wp_attachment_metadata', true ); |
| 1113 | $content = str_replace( $match['html'], wp_img_add_srcset_and_sizes( $match['html'], $post_meta ), $content ); |
1116 | 1114 | } |
1117 | 1115 | |
1118 | 1116 | return $content; |
… |
… |
function wp_make_content_images_responsive( $content ) { |
1129 | 1127 | * @param string $image An HTML 'img' element to be filtered. |
1130 | 1128 | * @return string Converted 'img' element with `srcset` and `sizes` attributes added. |
1131 | 1129 | */ |
1132 | | function wp_img_add_srcset_and_sizes( $image ) { |
| 1130 | function wp_img_add_srcset_and_sizes( $image, $attachment_meta = null ) { |
1133 | 1131 | // Return early if a 'srcset' attribute already exists. |
1134 | 1132 | if ( false !== strpos( $image, ' srcset="' ) ) { |
1135 | 1133 | return $image; |
1136 | 1134 | } |
1137 | 1135 | |
1138 | | // Parse id, size, width, and height from the `img` element. |
1139 | | $id = preg_match( '/wp-image-([0-9]+)/i', $image, $match_id ) ? (int) $match_id[1] : false; |
1140 | | $size = preg_match( '/size-([^\s|"]+)/i', $image, $match_size ) ? $match_size[1] : false; |
1141 | | $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : false; |
| 1136 | |
| 1137 | // Parse src, id, size, width, and height from the `img` element. |
| 1138 | $src = preg_match( '/ src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : false; |
| 1139 | $id = preg_match( '/wp-image-([0-9]+)/i', $image, $match_id ) ? (int) $match_id[1] : false; |
| 1140 | $size = preg_match( '/size-([^\s|"]+)/i', $image, $match_size ) ? $match_size[1] : false; |
| 1141 | $width = preg_match( '/ width="([0-9]+)"/i', $image, $match_width ) ? (int) $match_width[1] : false; |
1142 | 1142 | |
1143 | 1143 | if ( $id && false === $size ) { |
1144 | 1144 | $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false; |
… |
… |
function wp_img_add_srcset_and_sizes( $image ) { |
1148 | 1148 | } |
1149 | 1149 | } |
1150 | 1150 | |
| 1151 | if ( ! is_array( $attachment_meta ) ) { |
| 1152 | $attachment_meta = wp_get_attachment_metadata( $id ); |
| 1153 | } |
1151 | 1154 | /* |
1152 | 1155 | * If attempts to parse the size value failed, attempt to use the image |
1153 | 1156 | * metadata to match the 'src' against the available sizes for an attachment. |
1154 | 1157 | */ |
1155 | | if ( ! $size && ! empty( $id ) && is_array( $meta = wp_get_attachment_metadata( $id ) ) ) { |
| 1158 | if ( ! $size && ! empty( $id ) && $attachment_meta ) { |
1156 | 1159 | // Parse the image src value from the img element. |
1157 | 1160 | $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : false; |
1158 | 1161 | |
… |
… |
function wp_img_add_srcset_and_sizes( $image ) { |
1167 | 1170 | */ |
1168 | 1171 | $image_filename = wp_basename( $src ); |
1169 | 1172 | |
1170 | | if ( $image_filename === basename( $meta['file'] ) ) { |
| 1173 | if ( $image_filename === basename( $attachment_meta['file'] ) ) { |
1171 | 1174 | $size = 'full'; |
1172 | 1175 | } else { |
1173 | | foreach( $meta['sizes'] as $image_size => $image_size_data ) { |
| 1176 | foreach( $attachment_meta['sizes'] as $image_size => $image_size_data ) { |
1174 | 1177 | if ( $image_filename === $image_size_data['file'] ) { |
1175 | 1178 | $size = $image_size; |
1176 | 1179 | break; |
… |
… |
function wp_img_add_srcset_and_sizes( $image ) { |
1181 | 1184 | } |
1182 | 1185 | |
1183 | 1186 | // If ID and size exist, try for 'srcset' and 'sizes' and update the markup. |
1184 | | if ( $id && $size && ( $srcset = wp_get_attachment_image_srcset( $id, $size ) ) && ( $sizes = wp_get_attachment_image_sizes( $id, $size, $width ) ) ) { |
| 1187 | if ( $id && $size && ( $srcset = wp_get_attachment_image_srcset( $id, $size, $attachment_meta, $src ) ) && ( $sizes = wp_get_attachment_image_sizes( $id, $size, $width ) ) ) { |
1185 | 1188 | // Format the srcset and sizes string and escape attributes. |
1186 | 1189 | $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes) ); |
1187 | 1190 | |