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