diff --git src/wp-includes/media.php src/wp-includes/media.php
index fc69bdc..a88188f 100644
|
|
|
function _wp_get_image_size_from_meta( $size, $image_meta ) { |
| 906 | 906 | * @param int $attachment_id Optional. Image attachment ID. |
| 907 | 907 | * @param array|string $size Image size. Accepts any valid image size, or an array of width and height |
| 908 | 908 | * values in pixels (in that order). Default 'medium'. |
| 909 | | * @param array $image_meta Optional. The image meta data. |
| | 909 | * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
| 910 | 910 | * @return string|bool A 'srcset' value string or false. |
| 911 | 911 | */ |
| 912 | 912 | function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $image_meta = null ) { |
| … |
… |
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag |
| 924 | 924 | absint( $image[2] ) |
| 925 | 925 | ); |
| 926 | 926 | |
| 927 | | // Calculate the sources for the srcset. |
| | 927 | // Calculate the sources for the 'srcset'. |
| 928 | 928 | $sources = wp_calculate_image_srcset( $image_url, $size_array, $image_meta, $attachment_id ); |
| 929 | 929 | |
| 930 | | // Only return a srcset value if there is more than one source. |
| | 930 | // Only return a 'srcset' value if there is more than one source. |
| 931 | 931 | if ( count( $sources ) < 2 ) { |
| 932 | 932 | return false; |
| 933 | 933 | } |
| … |
… |
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag |
| 937 | 937 | |
| 938 | 938 | |
| 939 | 939 | /** |
| 940 | | * A helper function to concatenate and filter the srcset attribute value. |
| | 940 | * A helper function to concatenate and filter the 'srcset' attribute value. |
| 941 | 941 | * |
| 942 | 942 | * @since 4.4.0 |
| 943 | 943 | * |
| 944 | | * @param array $sources The array containing image sizes data as returned by wp_calculate_image_srcset(). |
| | 944 | * @param array $sources The array containing image sizes data as returned by 'wp_calculate_image_srcset()'. |
| 945 | 945 | * @param array $size_array Array of width and height values in pixels (in that order). |
| 946 | | * @param array $image_meta The image meta data. |
| 947 | | * @param int $attachment_id The image attachment post id to pass to the filter. |
| 948 | | * @return string The srcset attribute value. |
| | 946 | * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
| | 947 | * @param int $attachment_id The image attachment ID to pass to the filter. |
| | 948 | * @return string The 'srcset' attribute value. |
| 949 | 949 | */ |
| 950 | 950 | function wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_id ) { |
| 951 | 951 | $srcset = ''; |
| … |
… |
function wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_i |
| 955 | 955 | } |
| 956 | 956 | |
| 957 | 957 | /** |
| 958 | | * Filter the output of wp_get_attachment_image_srcset(). |
| | 958 | * Filter the output of 'wp_image_srcset_attr()'. |
| 959 | 959 | * |
| 960 | 960 | * @since 4.4.0 |
| 961 | 961 | * |
| 962 | | * @param string $srcset A source set formatted for a `srcset` attribute. |
| | 962 | * @param string $srcset A source set formatted for a 'srcset' attribute. |
| 963 | 963 | * @param int $attachment_id Image attachment ID. |
| 964 | | * @param array|string $size Image size. Accepts any valid image size, or an array of width and height |
| 965 | | * values in pixels (in that order). Default 'medium'. |
| | 964 | * @param array|string $size Image size. Image size name, or an array of width and height |
| | 965 | * values in pixels (in that order). |
| 966 | 966 | * @param array $image_meta The image meta data. |
| 967 | 967 | */ |
| 968 | | return apply_filters( 'wp_get_attachment_image_srcset', rtrim( $srcset, ', ' ), $attachment_id, $size_array, $image_meta ); |
| | 968 | return apply_filters( 'wp_image_srcset_attr', rtrim( $srcset, ', ' ), $attachment_id, $size_array, $image_meta ); |
| 969 | 969 | } |
| 970 | 970 | |
| 971 | 971 | /** |
| 972 | | * A helper function to caclulate the image sources to include in a srcset attribute. |
| | 972 | * A helper function to calculate the image sources to include in a 'srcset' attribute. |
| 973 | 973 | * |
| 974 | 974 | * @since 4.4.0 |
| 975 | 975 | * |
| 976 | | * @param string $image_name The file name, path, URL or partial path or URL of the image being matched. |
| | 976 | * @param string $image_name The file name, path, URL, or partial path or URL, of the image being matched. |
| 977 | 977 | * @param array $size_array Array of width and height values in pixels (in that order). |
| 978 | | * @param array $image_meta The image meta data. |
| 979 | | * @param int $attachment_id Optional. The image attachment post id to pass to the filter. |
| | 978 | * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
| | 979 | * @param int $attachment_id Optional. The image attachment ID to pass to the filter. |
| 980 | 980 | * @return array|bool $sources { |
| 981 | 981 | * Array image candidate values containing a URL, descriptor type, and |
| 982 | 982 | * descriptor value. False if none exist. |
| 983 | 983 | * |
| 984 | 984 | * @type array $values { |
| 985 | 985 | * @type string $url An image URL. |
| 986 | | * @type string $descriptor A width or density descriptor used in a srcset. |
| | 986 | * @type string $descriptor A width or density descriptor used in a 'srcset'. |
| 987 | 987 | * @type int $value The descriptor value representing a width or |
| 988 | 988 | * or pixel density. |
| 989 | 989 | * } |
| 990 | 990 | * } |
| 991 | | * |
| 992 | 991 | */ |
| 993 | 992 | function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $attachment_id = 0 ) { |
| 994 | 993 | if ( empty( $image_meta['sizes'] ) ) { |
| … |
… |
function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta |
| 997 | 996 | |
| 998 | 997 | $image_sizes = $image_meta['sizes']; |
| 999 | 998 | |
| 1000 | | // Get the height and width for the image. |
| | 999 | // Get the width and height of the image. |
| 1001 | 1000 | $image_width = (int) $size_array[0]; |
| 1002 | 1001 | $image_height = (int) $size_array[1]; |
| 1003 | 1002 | |
| … |
… |
function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta |
| 1006 | 1005 | return false; |
| 1007 | 1006 | } |
| 1008 | 1007 | |
| 1009 | | // Add full size to the img_sizes array. |
| | 1008 | // Add full size to the '$img_sizes' array. |
| 1010 | 1009 | $image_sizes['full'] = array( |
| 1011 | 1010 | 'width' => $image_meta['width'], |
| 1012 | 1011 | 'height' => $image_meta['height'], |
| … |
… |
function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta |
| 1031 | 1030 | $image_edited = preg_match( '/-e[0-9]{13}/', $image_name, $image_edit_hash ); |
| 1032 | 1031 | |
| 1033 | 1032 | /** |
| 1034 | | * Filter the maximum width included in a 'srcset' attribute. |
| | 1033 | * Filter the maximum image width to be included in a 'srcset' attribute. |
| 1035 | 1034 | * |
| 1036 | 1035 | * @since 4.4.0 |
| 1037 | 1036 | * |
| 1038 | | * @param int $max_width The maximum width to include in the 'srcset'. Default '1600'. |
| | 1037 | * @param int $max_width The maximum image width to be included in the 'srcset'. Default '1600'. |
| 1039 | 1038 | * @param array|string $size_array Array of width and height values in pixels (in that order). |
| 1040 | 1039 | */ |
| 1041 | | $max_srcset_width = apply_filters( 'max_srcset_image_width', 1600, $size_array ); |
| | 1040 | $max_srcset_image_width = apply_filters( 'max_srcset_image_width', 1600, $size_array ); |
| 1042 | 1041 | |
| 1043 | 1042 | // Array to hold URL candidates. |
| 1044 | 1043 | $sources = array(); |
| … |
… |
function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta |
| 1054 | 1053 | continue; |
| 1055 | 1054 | } |
| 1056 | 1055 | |
| 1057 | | // Filter out images that are wider than $max_srcset_width. |
| 1058 | | if ( $max_srcset_width && $image['width'] > $max_srcset_width ) { |
| | 1056 | // Filter out images that are wider than '$max_srcset_image_width'. |
| | 1057 | if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width ) { |
| 1059 | 1058 | continue; |
| 1060 | 1059 | } |
| 1061 | 1060 | |
| … |
… |
function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta |
| 1080 | 1079 | } |
| 1081 | 1080 | |
| 1082 | 1081 | /** |
| 1083 | | * Filter the output of wp_get_attachment_image_srcset_array(). |
| | 1082 | * Filter the output of 'wp_calculate_image_srcset()'. |
| 1084 | 1083 | * |
| 1085 | 1084 | * @since 4.4.0 |
| 1086 | 1085 | * |
| 1087 | 1086 | * @param array $sources An array of image URLs and widths. |
| 1088 | | * @param int $attachment_id Attachment ID for image. |
| 1089 | | * @param array|string $size Image size. Accepts any valid image size, or an array of width and height |
| 1090 | | * values in pixels (in that order). Default 'medium'. |
| 1091 | | * @param array $image_meta The image meta data. |
| | 1087 | * @param int $attachment_id Image attachment ID. |
| | 1088 | * @param array|string $size Image size. Image size name, or an array of width and height |
| | 1089 | * values in pixels (in that order). |
| | 1090 | * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
| 1092 | 1091 | |
| 1093 | 1092 | */ |
| 1094 | | return apply_filters( 'wp_get_attachment_image_srcset_array', array_values( $sources ), $attachment_id, $size_array, $image_meta ); |
| | 1093 | return apply_filters( 'wp_calculate_image_srcset', array_values( $sources ), $attachment_id, $size_array, $image_meta ); |
| 1095 | 1094 | } |
| 1096 | 1095 | |
| 1097 | 1096 | /** |
| 1098 | | * Create `sizes` attribute value for an image. |
| | 1097 | * Create 'sizes' attribute value for an image. |
| 1099 | 1098 | * |
| 1100 | 1099 | * @since 4.4.0 |
| 1101 | 1100 | * |
| 1102 | | * @param array|string $size Image size. Accepts any valid image size name (thumbnail, medium, etc.), |
| | 1101 | * @param array|string $size Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.), |
| 1103 | 1102 | * or an array of width and height values in pixels (in that order). |
| 1104 | | * @param array $image_meta Optional. The image meta data. |
| 1105 | | * @param int $attachment_id Optional. Image attachment ID. Either $image_meta or $attachment_id is needed |
| 1106 | | * when using image size name. |
| | 1103 | * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
| | 1104 | * @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` is needed |
| | 1105 | * when using the image size name as argument for `$size`. |
| 1107 | 1106 | * |
| 1108 | 1107 | * @return string|bool A valid source size value for use in a 'sizes' attribute or false. |
| 1109 | 1108 | */ |
| … |
… |
function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_i |
| 1122 | 1121 | if ( $image_meta ) { |
| 1123 | 1122 | $width = _wp_get_image_size_from_meta( $size, $image_meta ); |
| 1124 | 1123 | if ( $width ) { |
| 1125 | | $width = $width[0]; |
| | 1124 | $width = absint( $width[0] ); |
| 1126 | 1125 | } |
| 1127 | 1126 | } |
| 1128 | 1127 | } |
| … |
… |
function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_i |
| 1131 | 1130 | return false; |
| 1132 | 1131 | } |
| 1133 | 1132 | |
| 1134 | | // Setup the default sizes attribute. |
| 1135 | | $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', (int) $width ); |
| | 1133 | // Setup the default 'sizes' attribute. |
| | 1134 | $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $width ); |
| 1136 | 1135 | |
| 1137 | 1136 | /** |
| 1138 | | * Filter the output of wp_get_attachment_image_sizes(). |
| | 1137 | * Filter the output of 'wp_get_attachment_image_sizes()'. |
| 1139 | 1138 | * |
| 1140 | 1139 | * @since 4.4.0 |
| 1141 | 1140 | * |
| 1142 | 1141 | * @param string $sizes A source size value for use in a 'sizes' attribute. |
| 1143 | | * @param array|string $size Image size. Accepts any valid image size, or an array of width and height |
| 1144 | | * values in pixels (in that order). Default 'medium'. |
| 1145 | | * @param array $image_meta The image meta data. |
| | 1142 | * @param array|string $size Image size. Image size name, or an array of width and height |
| | 1143 | * values in pixels (in that order). |
| | 1144 | * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
| 1146 | 1145 | * @param int $attachment_id Post ID of the original image. |
| 1147 | 1146 | */ |
| 1148 | 1147 | return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id ); |
| … |
… |
function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_i |
| 1153 | 1152 | * |
| 1154 | 1153 | * @since 4.4.0 |
| 1155 | 1154 | * |
| 1156 | | * @see wp_image_add_srcset_and_sizes() |
| | 1155 | * @see 'wp_image_add_srcset_and_sizes()' |
| 1157 | 1156 | * |
| 1158 | 1157 | * @param string $content The raw post content to be filtered. |
| 1159 | 1158 | * @return string Converted content with 'srcset' and 'sizes' attributes added to images. |
| … |
… |
function wp_make_content_images_responsive( $content ) { |
| 1167 | 1166 | if ( false === strpos( $image, ' srcset="' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) && |
| 1168 | 1167 | ( $attachment_id = absint( $class_id[1] ) ) ) { |
| 1169 | 1168 | |
| 1170 | | // If exactly the same image tag is used more than once, overwrite it. |
| 1171 | | // All identical tags will be replaced later with str_replace(). |
| | 1169 | /* |
| | 1170 | * If exactly the same image tag is used more than once, overwrite it. |
| | 1171 | * All identical tags will be replaced later with 'str_replace()'. |
| | 1172 | */ |
| 1172 | 1173 | $selected_images[ $image ] = $attachment_id; |
| 1173 | 1174 | // Overwrite the ID when the same image is included more than once. |
| 1174 | 1175 | $attachment_ids[ $attachment_id ] = true; |
| … |
… |
function wp_make_content_images_responsive( $content ) { |
| 1177 | 1178 | |
| 1178 | 1179 | if ( count( $attachment_ids ) > 1 ) { |
| 1179 | 1180 | /* |
| 1180 | | * Warm object cache for use with get_post_meta(). |
| | 1181 | * Warm object cache for use with 'get_post_meta()'. |
| 1181 | 1182 | * |
| 1182 | 1183 | * To avoid making a database call for each image, a single query |
| 1183 | 1184 | * warms the object cache with the meta information for all images. |
| … |
… |
function wp_make_content_images_responsive( $content ) { |
| 1198 | 1199 | * |
| 1199 | 1200 | * @since 4.4.0 |
| 1200 | 1201 | * |
| 1201 | | * @see wp_get_attachment_image_srcset() |
| 1202 | | * @see wp_get_attachment_image_sizes() |
| | 1202 | * @see 'wp_get_attachment_image_srcset()' |
| | 1203 | * @see 'wp_get_attachment_image_sizes()' |
| 1203 | 1204 | * |
| 1204 | 1205 | * @param string $image An HTML 'img' element to be filtered. |
| 1205 | | * @param array $image_meta The image meta data. |
| | 1206 | * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
| 1206 | 1207 | * @param int $attachment_id Image attachment ID. |
| 1207 | | * @return string Converted 'img' element with `srcset` and `sizes` attributes added. |
| | 1208 | * @return string Converted 'img' element with 'srcset' and 'sizes' attributes added. |
| 1208 | 1209 | */ |
| 1209 | 1210 | function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { |
| 1210 | | // Ensure the image meta exists |
| | 1211 | // Ensure the image meta exists. |
| 1211 | 1212 | if ( empty( $image_meta['sizes'] ) ) { |
| 1212 | 1213 | return $image; |
| 1213 | 1214 | } |
| … |
… |
function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { |
| 1215 | 1216 | $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; |
| 1216 | 1217 | list( $src ) = explode( '?', $src ); |
| 1217 | 1218 | |
| 1218 | | // Return early if we coudn't get the image source. |
| | 1219 | // Return early if we couldn't get the image source. |
| 1219 | 1220 | if ( ! $src ) { |
| 1220 | 1221 | return $image; |
| 1221 | 1222 | } |
| 1222 | 1223 | |
| 1223 | | // Bail early when an image has been inserted and later edited. |
| | 1224 | // Bail early if an image has been inserted and later edited. |
| 1224 | 1225 | if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && |
| 1225 | 1226 | strpos( wp_basename( $src ), $img_edit_hash[0] ) === false ) { |
| 1226 | 1227 | |
| … |
… |
function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { |
| 1232 | 1233 | |
| 1233 | 1234 | if ( ! $width || ! $height ) { |
| 1234 | 1235 | /* |
| 1235 | | * If attempts to parse the size value failed, attempt to use the image |
| 1236 | | * metadata to match the image file name from 'src' against the available sizes for an attachment. |
| | 1236 | * If attempts to parse the size value failed, attempt to use the image meta data to match |
| | 1237 | * the image file name from 'src' against the available sizes for an attachment. |
| 1237 | 1238 | */ |
| 1238 | 1239 | $image_filename = wp_basename( $src ); |
| 1239 | 1240 | |
| … |
… |
function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { |
| 1259 | 1260 | $sources = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id ); |
| 1260 | 1261 | |
| 1261 | 1262 | $srcset = $sizes = ''; |
| 1262 | | // Only calculate srcset and sizes values if there is more than one source. |
| | 1263 | // Only calculate 'srcset' and 'sizes' values if there is more than one source. |
| 1263 | 1264 | if ( count( $sources ) > 1 ) { |
| 1264 | 1265 | $srcset = wp_image_srcset_attr( $sources, $size_array, $image_meta, $attachment_id ); |
| 1265 | 1266 | $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id ); |
| 1266 | 1267 | } |
| 1267 | 1268 | |
| 1268 | 1269 | if ( $srcset && $sizes ) { |
| 1269 | | // Format the srcset and sizes string and escape attributes. |
| | 1270 | // Format the 'srcset' and 'sizes' string and escape attributes. |
| 1270 | 1271 | $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes ) ); |
| 1271 | 1272 | |
| 1272 | | // Add srcset and sizes attributes to the image markup. |
| | 1273 | // Add 'srcset' and 'sizes' attributes to the image markup. |
| 1273 | 1274 | $image = preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $srcset_and_sizes . ' />', $image ); |
| 1274 | 1275 | } |
| 1275 | 1276 | |