diff --git src/wp-includes/media.php src/wp-includes/media.php
index 30df322..739408d 100644
|
|
function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa |
813 | 813 | |
814 | 814 | // Generate srcset and sizes if not already present. |
815 | 815 | if ( empty( $attr['srcset'] ) ) { |
816 | | $srcset = wp_get_attachment_image_srcset( $attachment_id, $size ); |
817 | | $sizes = wp_get_attachment_image_sizes( $attachment_id, $size, $width ); |
| 816 | $image_meta = wp_get_attachment_metadata( $attachment_id ); |
| 817 | $size_array = array( absint( $width ), absint( $height ) ); |
| 818 | $srcset = wp_get_attachment_image_srcset( $size_array, $image_meta, $attachment_id ); |
818 | 819 | |
819 | | if ( $srcset && $sizes ) { |
| 820 | if ( $srcset ) { |
820 | 821 | $attr['srcset'] = $srcset; |
821 | 822 | |
822 | | if ( empty( $attr['sizes'] ) ) { |
| 823 | if ( empty( $attr['sizes'] ) && ( $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id ) ) ) { |
823 | 824 | $attr['sizes'] = $sizes; |
824 | 825 | } |
825 | 826 | } |
… |
… |
function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon |
864 | 865 | } |
865 | 866 | |
866 | 867 | /** |
| 868 | * Private, do not use |
| 869 | */ |
| 870 | function _wp_upload_dir_baseurl() { |
| 871 | static $baseurl = null; |
| 872 | |
| 873 | if ( ! $baseurl ) { |
| 874 | $uploads_dir = wp_upload_dir(); |
| 875 | $baseurl = $uploads_dir['baseurl']; |
| 876 | } |
| 877 | |
| 878 | return $baseurl; |
| 879 | } |
| 880 | |
| 881 | /** |
| 882 | * Private, do not use |
| 883 | */ |
| 884 | function _wp_get_image_size_from_meta( $size, $image_meta ) { |
| 885 | if ( $size === 'full' ) { |
| 886 | return array( |
| 887 | absint( $image_meta['width'] ), |
| 888 | absint( $image_meta['height'] ), |
| 889 | ); |
| 890 | } elseif ( ! empty( $image_meta['sizes'][$size] ) ) { |
| 891 | return array( |
| 892 | absint( $image_meta['sizes'][$size]['width'] ), |
| 893 | absint( $image_meta['sizes'][$size]['height'] ), |
| 894 | ); |
| 895 | } |
| 896 | |
| 897 | return false; |
| 898 | } |
| 899 | |
| 900 | /** |
867 | 901 | * Retrieves an array of URLs and pixel widths representing sizes of an image. |
868 | 902 | * |
869 | 903 | * The purpose is to populate a source set when creating responsive image markup. |
870 | 904 | * |
871 | 905 | * @since 4.4.0 |
872 | 906 | * |
873 | | * @param int $attachment_id Image attachment ID. |
| 907 | * @param int $attachment_id Optional. Image attachment ID. |
874 | 908 | * @param array|string $size Image size. Accepts any valid image size, or an array of width and height |
875 | 909 | * values in pixels (in that order). Default 'medium'. |
| 910 | * @param array $image_meta Optional. The image meta data. |
| 911 | * |
876 | 912 | * @return array|bool $sources { |
877 | 913 | * Array image candidate values containing a URL, descriptor type, and |
878 | 914 | * descriptor value. False if none exist. |
… |
… |
function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon |
886 | 922 | * } |
887 | 923 | * |
888 | 924 | */ |
889 | | function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' ) { |
890 | | // Get the intermediate size. |
891 | | $image = image_get_intermediate_size( $attachment_id, $size ); |
| 925 | function wp_get_attachment_image_srcset_array( $attachment_id = 0, $size = 'medium', $image_meta = null ) { |
| 926 | if ( ! is_array($image_meta ) ) { |
| 927 | $image_meta = wp_get_attachment_metadata( $attachment_id ); |
| 928 | } |
892 | 929 | |
893 | | // Get the post meta. |
894 | | $img_meta = wp_get_attachment_metadata( $attachment_id ); |
895 | | if ( ! is_array( $img_meta ) ) { |
| 930 | if ( empty( $image_meta['sizes'] ) ) { |
896 | 931 | return false; |
897 | 932 | } |
898 | 933 | |
899 | | // Extract the height and width from the intermediate or the full size. |
900 | | $img_width = ( $image ) ? $image['width'] : $img_meta['width']; |
901 | | $img_height = ( $image ) ? $image['height'] : $img_meta['height']; |
| 934 | $img_sizes = $image_meta['sizes']; |
902 | 935 | |
903 | | // Bail early if the width isn't greater than zero. |
904 | | if ( ! $img_width > 0 ) { |
905 | | return false; |
906 | | } |
| 936 | $size_array = is_array( $size ) ? $size : _wp_get_image_size_from_meta( $size, $image_meta ); |
907 | 937 | |
908 | | // Use the URL from the intermediate size or build the url from the metadata. |
909 | | if ( ! empty( $image['url'] ) ) { |
910 | | $img_url = $image['url']; |
911 | | } else { |
912 | | $uploads_dir = wp_upload_dir(); |
913 | | $img_file = ( $image ) ? path_join( dirname( $img_meta['file'] ) , $image['file'] ) : $img_meta['file']; |
914 | | $img_url = $uploads_dir['baseurl'] . '/' . $img_file; |
915 | | } |
| 938 | // Get the height and width for the image. |
| 939 | $img_width = (int) $size_array[0]; |
| 940 | $img_height = (int) $size_array[1]; |
916 | 941 | |
917 | | $img_sizes = $img_meta['sizes']; |
| 942 | // Bail early if error/no width. |
| 943 | if ( $img_width < 1 ) { |
| 944 | return false; |
| 945 | } |
918 | 946 | |
919 | 947 | // Add full size to the img_sizes array. |
920 | 948 | $img_sizes['full'] = array( |
921 | | 'width' => $img_meta['width'], |
922 | | 'height' => $img_meta['height'], |
923 | | 'file' => wp_basename( $img_meta['file'] ) |
| 949 | 'width' => $image_meta['width'], |
| 950 | 'height' => $image_meta['height'], |
| 951 | 'file' => wp_basename( $image_meta['file'] ), |
924 | 952 | ); |
925 | 953 | |
| 954 | $img_baseurl = _wp_upload_dir_baseurl(); |
| 955 | $dirname = dirname( $image_meta['file'] ); |
| 956 | |
| 957 | if ( $dirname !== '.' ) { |
| 958 | $img_baseurl = path_join( $img_baseurl, $dirname ); |
| 959 | } |
| 960 | |
926 | 961 | // Calculate the image aspect ratio. |
927 | 962 | $img_ratio = $img_height / $img_width; |
928 | 963 | |
… |
… |
function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' |
931 | 966 | * contain a unique hash. Look for that hash and use it later to filter |
932 | 967 | * out images that are leftovers from previous versions. |
933 | 968 | */ |
934 | | $img_edited = preg_match( '/-e[0-9]{13}/', $img_url, $img_edit_hash ); |
| 969 | $img_edited = preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ); |
935 | 970 | |
936 | 971 | /** |
937 | 972 | * Filter the maximum width included in a srcset attribute. |
938 | 973 | * |
939 | 974 | * @since 4.4.0 |
940 | 975 | * |
941 | | * @param array|string $size Size of image, either array or string. |
| 976 | * @param array|string $size_array Size of image, either array or string. |
942 | 977 | */ |
943 | | $max_srcset_width = apply_filters( 'max_srcset_image_width', 1600, $size ); |
| 978 | $max_srcset_width = apply_filters( 'max_srcset_image_width', 1600, $size_array ); |
944 | 979 | |
945 | | /* |
946 | | * Set up arrays to hold url candidates and matched image sources so |
947 | | * we can avoid duplicates without looping through the full sources array |
948 | | */ |
949 | | $candidates = $sources = array(); |
| 980 | // Array to hold url candidates. |
| 981 | $sources = array(); |
950 | 982 | |
951 | 983 | /* |
952 | | * Loop through available images and only use images that are resized |
953 | | * versions of the same rendition. |
| 984 | * Loop through available images. Only use images that are resized |
| 985 | * versions of the same edit. |
954 | 986 | */ |
955 | 987 | foreach ( $img_sizes as $img ) { |
956 | 988 | |
957 | | // Filter out images that are leftovers from previous renditions. |
| 989 | // Filter out images that are from previous edits. |
958 | 990 | if ( $img_edited && ! strpos( $img['file'], $img_edit_hash[0] ) ) { |
959 | 991 | continue; |
960 | 992 | } |
… |
… |
function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' |
964 | 996 | continue; |
965 | 997 | } |
966 | 998 | |
967 | | $candidate_url = path_join( dirname( $img_url ), $img['file'] ); |
| 999 | $candidate_url = $img['file']; |
968 | 1000 | |
969 | 1001 | // Calculate the new image ratio. |
970 | 1002 | if ( $img['width'] ) { |
… |
… |
function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' |
974 | 1006 | } |
975 | 1007 | |
976 | 1008 | // If the new ratio differs by less than 0.01, use it. |
977 | | if ( abs( $img_ratio - $img_ratio_compare ) < 0.01 && ! in_array( $candidate_url, $candidates ) ) { |
978 | | // Add the URL to our list of candidates. |
979 | | $candidates[] = $candidate_url; |
980 | | |
| 1009 | if ( abs( $img_ratio - $img_ratio_compare ) < 0.01 && ! array_key_exists( $candidate_url, $sources ) ) { |
981 | 1010 | // Add the url, descriptor, and value to the sources array to be returned. |
982 | | $sources[] = array( |
983 | | 'url' => $candidate_url, |
| 1011 | $sources[ $candidate_url ] = array( |
| 1012 | 'url' => path_join( $img_baseurl, $candidate_url ), |
984 | 1013 | 'descriptor' => 'w', |
985 | 1014 | 'value' => $img['width'], |
986 | 1015 | ); |
… |
… |
function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' |
996 | 1025 | * @param int $attachment_id Attachment ID for image. |
997 | 1026 | * @param array|string $size Image size. Accepts any valid image size, or an array of width and height |
998 | 1027 | * values in pixels (in that order). Default 'medium'. |
| 1028 | * @param array $image_meta The image meta data. |
| 1029 | |
999 | 1030 | */ |
1000 | | return apply_filters( 'wp_get_attachment_image_srcset_array', $sources, $attachment_id, $size ); |
| 1031 | return apply_filters( 'wp_get_attachment_image_srcset_array', array_values( $sources ), $attachment_id, $size, $image_meta ); |
1001 | 1032 | } |
1002 | 1033 | |
1003 | 1034 | /** |
… |
… |
function wp_get_attachment_image_srcset_array( $attachment_id, $size = 'medium' |
1005 | 1036 | * |
1006 | 1037 | * @since 4.4.0 |
1007 | 1038 | * |
1008 | | * @param int $attachment_id Image attachment ID. |
| 1039 | |
| 1040 | * @param int $attachment_id Optional. Image attachment ID. |
1009 | 1041 | * @param array|string $size Image size. Accepts any valid image size, or an array of width and height |
1010 | 1042 | * values in pixels (in that order). Default 'medium'. |
| 1043 | * @param array $image_meta Optional. The image meta data. |
1011 | 1044 | * @return string|bool A 'srcset' value string or false. |
1012 | 1045 | */ |
1013 | | function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium' ) { |
1014 | | $srcset_array = wp_get_attachment_image_srcset_array( $attachment_id, $size ); |
| 1046 | function wp_get_attachment_image_srcset( $attachment_id = 0, $size = 'medium', $image_meta = null ) { |
| 1047 | $srcset_array = wp_get_attachment_image_srcset_array( $attachment_id, $size, $image_meta ); |
1015 | 1048 | |
1016 | 1049 | // Only return a srcset value if there is more than one source. |
1017 | | if ( count( $srcset_array ) <= 1 ) { |
| 1050 | if ( count( $srcset_array ) < 2 ) { |
1018 | 1051 | return false; |
1019 | 1052 | } |
1020 | 1053 | |
… |
… |
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium' ) { |
1029 | 1062 | * @since 4.4.0 |
1030 | 1063 | * |
1031 | 1064 | * @param string $srcset A source set formated for a `srcset` attribute. |
1032 | | * @param int $attachment_id Attachment ID for image. |
| 1065 | * @param int $attachment_id Image attachment ID. |
1033 | 1066 | * @param array|string $size Image size. Accepts any valid image size, or an array of width and height |
1034 | 1067 | * values in pixels (in that order). Default 'medium'. |
| 1068 | * @param array $image_meta The image meta data. |
1035 | 1069 | */ |
1036 | | return apply_filters( 'wp_get_attachment_image_srcset', rtrim( $srcset, ', ' ), $attachment_id, $size ); |
| 1070 | return apply_filters( 'wp_get_attachment_image_srcset', rtrim( $srcset, ', ' ), $attachment_id, $size, $image_meta ); |
1037 | 1071 | } |
1038 | 1072 | |
1039 | 1073 | /** |
1040 | | * Retrieves a source size attribute for an image from an array of values. |
| 1074 | * Create `sizes` attribute value for an image. |
1041 | 1075 | * |
1042 | 1076 | * @since 4.4.0 |
1043 | 1077 | * |
1044 | | * @param int $attachment_id Image attachment ID. |
| 1078 | * @param int $attachment_id Optional. Image attachment ID. |
1045 | 1079 | * @param array|string $size Image size. Accepts any valid image size, or an array of width and height |
1046 | 1080 | * values in pixels (in that order). Default 'medium'. |
1047 | | * @param int $width Optional. Display width of the image. |
| 1081 | * @param array $image_meta Optional. The image meta data. |
| 1082 | * |
1048 | 1083 | * @return string|bool A valid source size value for use in a 'sizes' attribute or false. |
1049 | 1084 | */ |
1050 | | function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $width = null ) { |
1051 | | // Try to get the image width from the $width parameter. |
1052 | | if ( is_numeric( $width ) ) { |
1053 | | $img_width = (int) $width; |
1054 | | // Next, see if a width value was passed in the $size parameter. |
| 1085 | function wp_get_attachment_image_sizes( $attachment_id = 0, $size, $image_meta = null ) { |
| 1086 | $width = 0; |
| 1087 | |
| 1088 | if ( is_numeric( $size ) ) { |
| 1089 | $width = absint( $size ); |
1055 | 1090 | } elseif ( is_array( $size ) ) { |
1056 | | $img_width = $size[0]; |
1057 | | // Finally, use the $size name to return the width of the image. |
1058 | | } else { |
1059 | | $image = image_get_intermediate_size( $attachment_id, $size ); |
1060 | | $img_width = $image ? $image['width'] : false; |
| 1091 | $width = absint( $size[0] ); |
| 1092 | } |
| 1093 | |
| 1094 | if ( ! $width && ( $image_meta || $attachment_id ) ) { |
| 1095 | $image_meta = ( $image_meta ) ? $image_meta : wp_get_attachment_metadata( $attachment_id ); |
| 1096 | $width = _wp_get_image_size_from_meta( $size, $image_meta )[0]; |
1061 | 1097 | } |
1062 | 1098 | |
1063 | | // Bail early if $img_width isn't set. |
1064 | | if ( ! $img_width ) { |
| 1099 | if ( ! $width ) { |
1065 | 1100 | return false; |
1066 | 1101 | } |
1067 | 1102 | |
1068 | 1103 | // Setup the default sizes attribute. |
1069 | | $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $img_width ); |
| 1104 | $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', (int) $width ); |
1070 | 1105 | |
1071 | 1106 | /** |
1072 | 1107 | * Filter the output of wp_get_attachment_image_sizes(). |
… |
… |
function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $width |
1078 | 1113 | * @param array|string $size Image size. Accepts any valid image size, or an array of width and height |
1079 | 1114 | * values in pixels (in that order). Default 'medium'. |
1080 | 1115 | * @param int $width Display width of the image. |
| 1116 | * @param array $image_meta The image meta data. |
1081 | 1117 | */ |
1082 | | return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $attachment_id, $size, $width ); |
| 1118 | return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $attachment_id, $size, $width, $image_meta ); |
1083 | 1119 | } |
1084 | 1120 | |
1085 | 1121 | /** |
… |
… |
function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $width |
1087 | 1123 | * |
1088 | 1124 | * @since 4.4.0 |
1089 | 1125 | * |
1090 | | * @see wp_img_add_srcset_and_sizes() |
| 1126 | * @see wp_image_add_srcset_and_sizes() |
1091 | 1127 | * |
1092 | 1128 | * @param string $content The raw post content to be filtered. |
1093 | 1129 | * @return string Converted content with 'srcset' and 'sizes' attributes added to images. |
… |
… |
function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $width |
1095 | 1131 | function wp_make_content_images_responsive( $content ) { |
1096 | 1132 | $images = get_media_embedded_in_content( $content, 'img' ); |
1097 | 1133 | |
1098 | | $attachment_ids = array(); |
| 1134 | $selected_images = array(); |
1099 | 1135 | |
1100 | 1136 | foreach( $images as $image ) { |
1101 | | if ( preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) ) { |
1102 | | $attachment_id = (int) $class_id[1]; |
1103 | | if ( $attachment_id ) { |
1104 | | $attachment_ids[] = $attachment_id; |
1105 | | } |
| 1137 | if ( false === strpos( $image, ' srcset="' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) && |
| 1138 | ( $attachment_id = absint( $class_id[1] ) ) ) { |
| 1139 | |
| 1140 | $selected_images[$image] = $attachment_id; |
1106 | 1141 | } |
1107 | 1142 | } |
1108 | 1143 | |
1109 | | if ( 0 < count( $attachment_ids ) ) { |
| 1144 | if ( count( $selected_images ) > 1 ) { |
1110 | 1145 | /* |
1111 | | * Warm object caches for use with wp_get_attachment_metadata. |
| 1146 | * Warm object cache for use with get_post_meta(). |
1112 | 1147 | * |
1113 | 1148 | * To avoid making a database call for each image, a single query |
1114 | 1149 | * warms the object cache with the meta information for all images. |
1115 | 1150 | */ |
1116 | | _prime_post_caches( $attachment_ids, false, true ); |
| 1151 | update_meta_cache( 'post', array_unique( array_values( ( $selected_images ) ) ) ); |
1117 | 1152 | } |
1118 | 1153 | |
1119 | | foreach( $images as $image ) { |
1120 | | $content = str_replace( $image, wp_img_add_srcset_and_sizes( $image ), $content ); |
| 1154 | foreach ( $selected_images as $image => $attachment_id ) { |
| 1155 | $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); |
| 1156 | $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content ); |
1121 | 1157 | } |
1122 | 1158 | |
1123 | 1159 | return $content; |
… |
… |
function wp_make_content_images_responsive( $content ) { |
1131 | 1167 | * @see wp_get_attachment_image_srcset() |
1132 | 1168 | * @see wp_get_attachment_image_sizes() |
1133 | 1169 | * |
1134 | | * @param string $image An HTML 'img' element to be filtered. |
| 1170 | * @param string $image An HTML 'img' element to be filtered. |
| 1171 | * @param array $image_meta The image meta data. |
| 1172 | * @param int $attachment_id Image attachment ID. |
1135 | 1173 | * @return string Converted 'img' element with `srcset` and `sizes` attributes added. |
1136 | 1174 | */ |
1137 | | function wp_img_add_srcset_and_sizes( $image ) { |
1138 | | // Return early if a 'srcset' attribute already exists. |
1139 | | if ( false !== strpos( $image, ' srcset="' ) ) { |
| 1175 | function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { |
| 1176 | // Ensure the image meta exists |
| 1177 | if ( empty( $image_meta['sizes'] ) ) { |
1140 | 1178 | return $image; |
1141 | 1179 | } |
1142 | 1180 | |
1143 | | // Parse id, size, width, and height from the `img` element. |
1144 | | $id = preg_match( '/wp-image-([0-9]+)/i', $image, $match_id ) ? (int) $match_id[1] : false; |
1145 | | $size = preg_match( '/size-([^\s|"]+)/i', $image, $match_size ) ? $match_size[1] : false; |
1146 | | $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : false; |
1147 | | |
1148 | | if ( $id && ! $size ) { |
1149 | | $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false; |
| 1181 | $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; |
| 1182 | list( $src ) = explode( '?', $src ); |
1150 | 1183 | |
1151 | | if ( $width && $height ) { |
1152 | | $size = array( $width, $height ); |
1153 | | } |
| 1184 | // Return early if we coudn't get the image source. |
| 1185 | if ( ! $src ) { |
| 1186 | return $image; |
1154 | 1187 | } |
1155 | 1188 | |
1156 | | /* |
1157 | | * If attempts to parse the size value failed, attempt to use the image |
1158 | | * metadata to match the 'src' against the available sizes for an attachment. |
1159 | | */ |
1160 | | if ( $id && ! $size ) { |
1161 | | $meta = wp_get_attachment_metadata( $id ); |
| 1189 | // Bail early when an image has been inserted and later edited. |
| 1190 | // We don't have the previous image meta to generate srcset. |
| 1191 | if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && |
| 1192 | strpos( wp_basename( $src ), $img_edit_hash[0] ) === false ) { |
1162 | 1193 | |
1163 | | // Parse the image src value from the img element. |
1164 | | $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : false; |
| 1194 | return $image; |
| 1195 | } |
1165 | 1196 | |
1166 | | // Return early if the metadata does not exist or the src value is empty. |
1167 | | if ( ! $meta || ! $src ) { |
1168 | | return $image; |
1169 | | } |
| 1197 | $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; |
| 1198 | $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0; |
1170 | 1199 | |
| 1200 | if ( ! $width || ! $height ) { |
1171 | 1201 | /* |
1172 | | * First, see if the file is the full size image. If not, loop through |
1173 | | * the intermediate sizes until we find a file that matches. |
| 1202 | * If attempts to parse the size value failed, attempt to use the image |
| 1203 | * metadata to match the 'src' against the available sizes for an attachment. |
1174 | 1204 | */ |
1175 | 1205 | $image_filename = wp_basename( $src ); |
1176 | 1206 | |
1177 | | if ( $image_filename === basename( $meta['file'] ) ) { |
1178 | | $size = 'full'; |
| 1207 | if ( $image_filename === basename( $image_meta['file'] ) ) { |
| 1208 | $width = (int) $image_meta['width']; |
| 1209 | $height = (int) $image_meta['height']; |
1179 | 1210 | } else { |
1180 | | foreach( $meta['sizes'] as $image_size => $image_size_data ) { |
| 1211 | foreach( $image_meta['sizes'] as $image_size_data ) { |
1181 | 1212 | if ( $image_filename === $image_size_data['file'] ) { |
1182 | | $size = $image_size; |
| 1213 | $width = (int) $image_size_data['width']; |
| 1214 | $height = (int) $image_size_data['height']; |
1183 | 1215 | break; |
1184 | 1216 | } |
1185 | 1217 | } |
1186 | 1218 | } |
| 1219 | } |
1187 | 1220 | |
| 1221 | if ( ! $width || ! $height ) { |
| 1222 | return $image; |
1188 | 1223 | } |
1189 | 1224 | |
1190 | | // If ID and size exist, try for 'srcset' and 'sizes' and update the markup. |
1191 | | if ( $id && $size ) { |
1192 | | $srcset = wp_get_attachment_image_srcset( $id, $size ); |
1193 | | $sizes = wp_get_attachment_image_sizes( $id, $size, $width ); |
| 1225 | $size_array = array( $width, $height ); |
| 1226 | $srcset = wp_get_attachment_image_srcset( $attachment_id, $size_array, $image_meta ); |
| 1227 | $sizes = wp_get_attachment_image_sizes( $attachment_id, $size_array, $image_meta ); |
1194 | 1228 | |
1195 | | if ( $srcset && $sizes ) { |
1196 | | // Format the srcset and sizes string and escape attributes. |
1197 | | $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes) ); |
| 1229 | if ( $srcset && $sizes ) { |
| 1230 | // Format the srcset and sizes string and escape attributes. |
| 1231 | $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes ) ); |
1198 | 1232 | |
1199 | | // Add srcset and sizes attributes to the image markup. |
1200 | | $image = preg_replace( '/<img ([^>]+)[\s?][\/?]>/', '<img $1' . $srcset_and_sizes . ' />', $image ); |
1201 | | } |
| 1233 | // Add srcset and sizes attributes to the image markup. |
| 1234 | $image = preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $srcset_and_sizes . ' />', $image ); |
1202 | 1235 | } |
1203 | 1236 | |
1204 | 1237 | return $image; |
diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
index b682b09..0ea12a7 100644
|
|
EOF; |
716 | 716 | } |
717 | 717 | |
718 | 718 | /** |
| 719 | * Helper function to get image size array from size "name" |
| 720 | */ |
| 721 | function _get_image_size_array_from_name( $size_name ) { |
| 722 | switch ( $size_name ) { |
| 723 | case 'thumbnail': |
| 724 | return array( 150, 150 ); |
| 725 | case 'medium': |
| 726 | return array( 300, 225 ); |
| 727 | case 'large': |
| 728 | return array( 1024, 768 ); |
| 729 | case 'full': |
| 730 | return array( 1600, 1200 ); // actual size of ../data/images/test-image-large.png |
| 731 | default: |
| 732 | return array( 800, 600 ); // soft-resized image |
| 733 | } |
| 734 | } |
| 735 | |
| 736 | /** |
719 | 737 | * @ticket 33641 |
720 | 738 | */ |
721 | 739 | function test_wp_get_attachment_image_srcset_array() { |
722 | 740 | $year_month = date('Y/m'); |
723 | | $image = wp_get_attachment_metadata( self::$large_id ); |
| 741 | $image_meta = wp_get_attachment_metadata( self::$large_id ); |
724 | 742 | |
725 | 743 | $expected = array( |
726 | 744 | array( |
727 | | 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/' . $image['sizes']['medium']['file'], |
| 745 | 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/' . $image_meta['sizes']['medium']['file'], |
728 | 746 | 'descriptor' => 'w', |
729 | | 'value' => $image['sizes']['medium']['width'], |
| 747 | 'value' => $image_meta['sizes']['medium']['width'], |
730 | 748 | ), |
731 | 749 | array( |
732 | | 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/' . $image['sizes']['large']['file'], |
| 750 | 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month . '/' . $image_meta['sizes']['large']['file'], |
733 | 751 | 'descriptor' => 'w', |
734 | | 'value' => $image['sizes']['large']['width'], |
| 752 | 'value' => $image_meta['sizes']['large']['width'], |
735 | 753 | ), |
736 | 754 | array( |
737 | | 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image['file'], |
| 755 | 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'], |
738 | 756 | 'descriptor' => 'w', |
739 | | 'value' => $image['width'], |
| 757 | 'value' => $image_meta['width'], |
740 | 758 | ), |
741 | 759 | ); |
742 | 760 | |
… |
… |
EOF; |
744 | 762 | $sizes = array( 'medium', 'large', 'full', 'yoav' ); |
745 | 763 | |
746 | 764 | foreach ( $sizes as $size ) { |
747 | | $this->assertSame( $expected, wp_get_attachment_image_srcset_array( self::$large_id, $size ) ); |
| 765 | $size_array = $this->_get_image_size_array_from_name( $size ); |
| 766 | $this->assertSame( $expected, wp_get_attachment_image_srcset_array( self::$large_id, $size_array, $image_meta ) ); |
748 | 767 | } |
749 | 768 | } |
750 | 769 | |
… |
… |
EOF; |
762 | 781 | $filename = DIR_TESTDATA . '/images/test-image-large.png'; |
763 | 782 | $id = self::factory()->attachment->create_upload_object( $filename ); |
764 | 783 | |
765 | | $image = wp_get_attachment_metadata( $id ); |
| 784 | $image_meta = wp_get_attachment_metadata( $id ); |
766 | 785 | |
767 | 786 | $expected = array( |
768 | 787 | array( |
769 | | 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image['sizes']['medium']['file'], |
| 788 | 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['medium']['file'], |
770 | 789 | 'descriptor' => 'w', |
771 | | 'value' => $image['sizes']['medium']['width'], |
| 790 | 'value' => $image_meta['sizes']['medium']['width'], |
772 | 791 | ), |
773 | 792 | array( |
774 | | 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image['sizes']['large']['file'], |
| 793 | 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['sizes']['large']['file'], |
775 | 794 | 'descriptor' => 'w', |
776 | | 'value' => $image['sizes']['large']['width'], |
| 795 | 'value' => $image_meta['sizes']['large']['width'], |
777 | 796 | ), |
778 | 797 | array( |
779 | | 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image['file'], |
| 798 | 'url' => 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'], |
780 | 799 | 'descriptor' => 'w', |
781 | | 'value' => $image['width'], |
| 800 | 'value' => $image_meta['width'], |
782 | 801 | ), |
783 | 802 | ); |
784 | 803 | |
… |
… |
EOF; |
786 | 805 | $sizes = array( 'medium', 'large', 'full', 'yoav' ); |
787 | 806 | |
788 | 807 | foreach ( $sizes as $size ) { |
789 | | $this->assertSame( $expected, wp_get_attachment_image_srcset_array( $id, $size ) ); |
| 808 | $size_array = $this->_get_image_size_array_from_name( $size ); |
| 809 | $this->assertSame( $expected, wp_get_attachment_image_srcset_array( $id, $size_array, $image_meta ) ); |
790 | 810 | } |
791 | 811 | |
792 | 812 | // Leave the uploads option the way you found it. |
… |
… |
EOF; |
799 | 819 | function test_wp_get_attachment_image_srcset_array_with_edits() { |
800 | 820 | // For this test we're going to mock metadata changes from an edit. |
801 | 821 | // Start by getting the attachment metadata. |
802 | | $meta = wp_get_attachment_metadata( self::$large_id ); |
| 822 | $image_meta = wp_get_attachment_metadata( self::$large_id ); |
| 823 | $size_array = $this->_get_image_size_array_from_name( 'medium' ); |
803 | 824 | |
804 | 825 | // Copy hash generation method used in wp_save_image(). |
805 | 826 | $hash = 'e' . time() . rand(100, 999); |
806 | 827 | |
807 | 828 | // Replace file paths for full and medium sizes with hashed versions. |
808 | | $filename_base = basename( $meta['file'], '.png' ); |
809 | | $meta['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $meta['file'] ); |
810 | | $meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $meta['sizes']['medium']['file'] ); |
811 | | |
812 | | // Save edited metadata. |
813 | | wp_update_attachment_metadata( self::$large_id, $meta ); |
| 829 | $filename_base = basename( $image_meta['file'], '.png' ); |
| 830 | $image_meta['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['file'] ); |
| 831 | $image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium']['file'] ); |
| 832 | $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file'] ); |
814 | 833 | |
815 | 834 | // Calculate a srcset array. |
816 | | $sizes = wp_get_attachment_image_srcset_array( self::$large_id, 'medium' ); |
| 835 | $sizes = wp_get_attachment_image_srcset_array( self::$large_id, $size_array, $image_meta ); |
817 | 836 | |
818 | 837 | // Test to confirm all sources in the array include the same edit hash. |
819 | 838 | foreach ( $sizes as $size ) { |
… |
… |
EOF; |
825 | 844 | * @ticket 33641 |
826 | 845 | */ |
827 | 846 | function test_wp_get_attachment_image_srcset_array_false() { |
828 | | $sizes = wp_get_attachment_image_srcset_array( 99999, 'foo' ); |
| 847 | $sizes = wp_get_attachment_image_srcset_array( 99999, array( 400, 300 ), array() ); |
829 | 848 | |
830 | 849 | // For canola.jpg we should return |
831 | 850 | $this->assertFalse( $sizes ); |
… |
… |
EOF; |
835 | 854 | * @ticket 33641 |
836 | 855 | */ |
837 | 856 | function test_wp_get_attachment_image_srcset_array_no_width() { |
838 | | // Filter image_downsize() output. |
839 | | add_filter( 'wp_generate_attachment_metadata', array( $this, '_test_wp_get_attachment_image_srcset_array_no_width_filter' ) ); |
840 | | |
841 | | $old_meta = get_post_meta( self::$large_id, '_wp_attachment_metadata', true ); |
842 | 857 | $file = get_attached_file( self::$large_id ); |
| 858 | $image_meta = wp_generate_attachment_metadata( self::$large_id, $file ); |
843 | 859 | |
844 | | $data = wp_generate_attachment_metadata( self::$large_id, $file ); |
845 | | wp_update_attachment_metadata( self::$large_id, $data ); |
| 860 | // Remove the sizes for 'large' and 'full' |
| 861 | $image_meta['width'] = 0; |
| 862 | $image_meta['height'] = 0; |
| 863 | $image_meta['sizes']['large']['width'] = 0; |
| 864 | $image_meta['sizes']['large']['height'] = 0; |
846 | 865 | |
847 | | $srcset = wp_get_attachment_image_srcset_array( self::$large_id, 'medium' ); |
| 866 | $size_array = $this->_get_image_size_array_from_name( 'medium' ); |
848 | 867 | |
849 | | update_post_meta( self::$large_id, '_wp_attachment_metadata', $old_meta ); |
| 868 | $srcset = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ); |
850 | 869 | |
851 | 870 | // The srcset should be false. |
852 | 871 | $this->assertFalse( $srcset ); |
853 | 872 | } |
854 | 873 | |
855 | 874 | /** |
856 | | * Helper function to filter image_downsize and return zero values for width and height. |
857 | | */ |
858 | | public function _test_wp_get_attachment_image_srcset_array_no_width_filter( $meta ) { |
859 | | remove_filter( 'wp_generate_attachment_metadata', array( $this, __FUNCTION__ ) ); |
860 | | |
861 | | $meta['sizes']['medium']['width'] = 0; |
862 | | $meta['sizes']['medium']['height'] = 0; |
863 | | return $meta; |
864 | | } |
865 | | |
866 | | /** |
867 | 875 | * @ticket 33641 |
868 | 876 | */ |
869 | 877 | function test_wp_get_attachment_image_srcset() { |
870 | | $sizes = wp_get_attachment_image_srcset( self::$large_id, 'full-size' ); |
| 878 | $image_meta = wp_get_attachment_metadata( self::$large_id ); |
| 879 | $size_array = array( 1600, 1200 ); // full size |
| 880 | |
| 881 | $sizes = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ); |
871 | 882 | |
872 | | $image = wp_get_attachment_metadata( self::$large_id ); |
873 | 883 | $year_month = date('Y/m'); |
874 | 884 | |
875 | 885 | $expected = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month = date('Y/m') . '/' |
876 | | . $image['sizes']['medium']['file'] . ' ' . $image['sizes']['medium']['width'] . 'w, '; |
| 886 | . $image_meta['sizes']['medium']['file'] . ' ' . $image_meta['sizes']['medium']['width'] . 'w, '; |
877 | 887 | $expected .= 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $year_month = date('Y/m') . '/' |
878 | | . $image['sizes']['large']['file'] . ' ' . $image['sizes']['large']['width'] . 'w, '; |
879 | | $expected .= 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image['file'] . ' ' . $image['width'] .'w'; |
| 888 | . $image_meta['sizes']['large']['file'] . ' ' . $image_meta['sizes']['large']['width'] . 'w, '; |
| 889 | $expected .= 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/' . $image_meta['file'] . ' ' . $image_meta['width'] .'w'; |
880 | 890 | |
881 | 891 | $this->assertSame( $expected, $sizes ); |
882 | 892 | } |
… |
… |
EOF; |
885 | 895 | * @ticket 33641 |
886 | 896 | */ |
887 | 897 | function test_wp_get_attachment_image_srcset_single_srcset() { |
| 898 | $image_meta = wp_get_attachment_metadata( self::$large_id ); |
| 899 | $size_array = array( 150, 150 ); |
888 | 900 | /* |
889 | 901 | * In our tests, thumbnails will only return a single srcset candidate, |
890 | 902 | * so we shouldn't return a srcset value in order to avoid unneeded markup. |
891 | 903 | */ |
892 | | $sizes = wp_get_attachment_image_srcset( self::$large_id, 'thumbnail' ); |
| 904 | $sizes = wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ); |
893 | 905 | |
894 | 906 | $this->assertFalse( $sizes ); |
895 | 907 | } |
… |
… |
EOF; |
899 | 911 | */ |
900 | 912 | function test_wp_get_attachment_image_sizes() { |
901 | 913 | // Test sizes against the default WP sizes. |
902 | | $intermediates = array('thumbnail', 'medium', 'large'); |
| 914 | $intermediates = array( 'thumbnail', 'medium', 'large' ); |
903 | 915 | |
904 | | foreach( $intermediates as $int ) { |
905 | | $width = get_option( $int . '_size_w' ); |
| 916 | foreach( $intermediates as $int_size ) { |
| 917 | $size_array = $this->_get_image_size_array_from_name( $int_size ); |
| 918 | list( $width, $height ) = $size_array; |
906 | 919 | |
907 | 920 | $expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px'; |
908 | | $sizes = wp_get_attachment_image_sizes( self::$large_id, $int ); |
| 921 | $sizes = wp_get_attachment_image_sizes( self::$large_id, $size_array ); |
909 | 922 | |
910 | | $this->assertSame($expected, $sizes); |
| 923 | $this->assertSame( $expected, $sizes ); |
911 | 924 | } |
912 | 925 | } |
913 | 926 | |
914 | 927 | /** |
915 | 928 | * @ticket 33641 |
916 | 929 | */ |
917 | | function test_wp_get_attachment_image_sizes_with_width() { |
918 | | $width = 350; |
919 | | |
920 | | $expected = '(max-width: 350px) 100vw, 350px'; |
921 | | $sizes = wp_get_attachment_image_sizes( self::$large_id, 'medium', $width ); |
922 | | |
923 | | $this->assertSame( $expected, $sizes ); |
924 | | } |
925 | | |
926 | | /** |
927 | | * @ticket 33641 |
928 | | */ |
929 | 930 | function test_wp_make_content_images_responsive() { |
930 | | $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, 'medium' ) ); |
931 | | $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, 'medium' ) ); |
| 931 | $image_meta = wp_get_attachment_metadata( self::$large_id ); |
| 932 | $size_array = $this->_get_image_size_array_from_name( 'medium' ); |
| 933 | |
| 934 | $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) ); |
| 935 | $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) ); |
932 | 936 | |
933 | 937 | // Function used to build HTML for the editor. |
934 | 938 | $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); |