Ticket #34430: 34430.1.diff
File 34430.1.diff, 29.8 KB (added by , 9 years ago) |
---|
-
src/wp-includes/media.php
812 812 $attr = wp_parse_args($attr, $default_attr); 813 813 814 814 // Generate srcset and sizes if not already present. 815 if ( empty( $attr['srcset'] ) && ( $srcset = wp_get_attachment_image_srcset( $attachment_id, $size ) ) && ( $sizes = wp_get_attachment_image_sizes( $attachment_id, $size, $width ) ) ) { 816 $attr['srcset'] = $srcset; 815 if ( empty( $attr['srcset'] ) ) { 816 $image_meta = wp_get_attachment_metadata( $attachment_id ); 817 $size_array = array( absint( $width ), absint( $height ) ); 817 818 818 if ( empty( $attr['sizes'] ) ) { 819 if ( $srcset = wp_get_attachment_image_srcset( $size_array, $image_meta, $attachment_id ) ) { 820 $attr['srcset'] = $srcset; 821 } 822 823 if ( empty( $attr['sizes'] ) && ( $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id ) ) ) { 819 824 $attr['sizes'] = $sizes; 820 825 } 821 826 } … … 859 864 } 860 865 861 866 /** 867 * Private, do not use 868 */ 869 function _wp_upload_dir_baseurl() { 870 static $baseurl = null; 871 872 if ( ! $baseurl ) { 873 $uploads_dir = wp_upload_dir(); 874 $baseurl = $uploads_dir['baseurl']; 875 } 876 877 return $baseurl; 878 } 879 880 // TODO: needed?? 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 /** 862 901 * Retrieves an array of URLs and pixel widths representing sizes of an image. 863 902 * 864 903 * The purpose is to populate a source set when creating responsive image markup. … … 865 904 * 866 905 * @since 4.4.0 867 906 * 868 * @param int $attachment_id Image attachment ID. 869 * @param array|string $size Image size. Accepts any valid image size, or an array of width and height 870 * values in pixels (in that order). Default 'medium'. 907 * @param array $size_array Array of width and height values in pixels (in that order). 908 * @param array $image_meta The image meta data. 909 * @param int $attachment_id Optional. Image attachment ID. Used only for the filter. 910 * 871 911 * @return array|bool $sources { 872 912 * Array image candidate values containing a URL, descriptor type, and 873 913 * descriptor value. False if none exist. … … 881 921 * } 882 922 * 883 923 */ 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 888 // Get the post meta. 889 $img_meta = wp_get_attachment_metadata( $attachment_id ); 890 if ( ! is_array( $img_meta ) ) { 924 function wp_get_attachment_image_srcset_array( $size_array, $image_meta, $attachment_id = 0 ) { 925 if ( empty( $image_meta['sizes'] ) ) { 891 926 return false; 892 927 } 893 928 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']; 929 $img_sizes = $image_meta['sizes']; 897 930 898 // Bail early if the width isn't greater than zero. 899 if ( ! $img_width > 0 ) { 931 // Get the height and width for the image. 932 $img_width = (int) $size_array[0]; 933 $img_height = (int) $size_array[1]; 934 935 // Bail early if error/no width. 936 if ( $img_width < 1 ) { 900 937 return false; 901 938 } 902 939 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'];906 } 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;910 }911 912 $img_sizes = $img_meta['sizes'];913 914 940 // Add full size to the img_sizes array. 915 941 $img_sizes['full'] = array( 916 'width' => $im g_meta['width'],917 'height' => $im g_meta['height'],918 'file' => wp_basename( $im g_meta['file'] )942 'width' => $image_meta['width'], 943 'height' => $image_meta['height'], 944 'file' => wp_basename( $image_meta['file'] ), 919 945 ); 920 946 947 $img_baseurl = _wp_upload_dir_baseurl(); 948 $dirname = dirname( $image_meta['file'] ); 949 950 if ( $dirname !== '.' ) { 951 $img_baseurl = path_join( $img_baseurl, $dirname ); 952 } 953 921 954 // Calculate the image aspect ratio. 922 955 $img_ratio = $img_height / $img_width; 923 956 … … 926 959 * contain a unique hash. Look for that hash and use it later to filter 927 960 * out images that are leftovers from previous versions. 928 961 */ 929 $img_edited = preg_match( '/-e[0-9]{13}/', $im g_url, $img_edit_hash );962 $img_edited = preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ); 930 963 931 964 /** 932 965 * Filter the maximum width included in a srcset attribute. … … 933 966 * 934 967 * @since 4.4.0 935 968 * 936 * @param array|string $size Size of image, either array or string.969 * @param array|string $size_array Size of image, either array or string. 937 970 */ 938 $max_srcset_width = apply_filters( 'max_srcset_image_width', 1600, $size );971 $max_srcset_width = apply_filters( 'max_srcset_image_width', 1600, $size_array ); 939 972 940 /* 941 * Set up arrays to hold url candidates and matched image sources so 942 * we can avoid duplicates without looping through the full sources array 943 */ 944 $candidates = $sources = array(); 973 // Array to hold url candidates. 974 $sources = array(); 945 975 946 976 /* 947 * Loop through available images and only use images that are resized977 * Loop through available images. Only use images that are resized 948 978 * versions of the same rendition. 949 979 */ 950 980 foreach ( $img_sizes as $img ) { 951 981 952 // Filter out images that are leftoversfrom previous renditions.982 // Filter out images that are from previous renditions. 953 983 if ( $img_edited && ! strpos( $img['file'], $img_edit_hash[0] ) ) { 954 984 continue; 955 985 } … … 959 989 continue; 960 990 } 961 991 962 $candidate_url = path_join( dirname( $img_url ), $img['file'] );992 $candidate_url = path_join( $img_baseurl, $img['file'] ); 963 993 964 994 // Calculate the new image ratio. 965 995 if ( $img['width'] ) { … … 969 999 } 970 1000 971 1001 // 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; 975 1002 if ( abs( $img_ratio - $img_ratio_compare ) < 0.01 && ! array_key_exists( $candidate_url, $sources ) ) { 976 1003 // Add the url, descriptor, and value to the sources array to be returned. 977 $sources[ ] = array(1004 $sources[ $candidate_url ] = array( 978 1005 'url' => $candidate_url, 979 1006 'descriptor' => 'w', 980 1007 'value' => $img['width'], … … 987 1014 * 988 1015 * @since 4.4.0 989 1016 * 990 * @param array 991 * @param int $attachment_id Attachment ID for image.992 * @param array |string $size Image size. Accepts any valid image size, or an array of width and height993 * values in pixels (in that order). Default 'medium'.1017 * @param array $sources An array of image urls and widths. 1018 * @param array $size_array Image size. Array of width and height values in pixels (in that order). 1019 * @param array $image_meta The image meta data. 1020 * @param int $attachment_id Attachment ID for image. 994 1021 */ 995 return apply_filters( 'wp_get_attachment_image_srcset_array', $sources, $attachment_id, $size);1022 return apply_filters( 'wp_get_attachment_image_srcset_array', array_values( $sources ), $size_array, $image_meta, $attachment_id ); 996 1023 } 997 1024 998 1025 /** … … 1000 1027 * 1001 1028 * @since 4.4.0 1002 1029 * 1003 * @param int $attachment_id Image attachment ID. 1004 * @param array|string $size Image size. Accepts any valid image size, or an array of width and height 1005 * values in pixels (in that order). Default 'medium'. 1006 * @return string|bool A 'srcset' value string or false. 1030 * @param int $image_meta The image meta data. 1031 * @param array $size_array Image size. An array of width and height values in pixels (in that order). 1032 * @return string|bool A 'srcset' value string or false. 1007 1033 */ 1008 function wp_get_attachment_image_srcset( $ attachment_id, $size = 'medium') {1009 $srcset_array = wp_get_attachment_image_srcset_array( $ attachment_id, $size);1034 function wp_get_attachment_image_srcset( $size_array, $image_meta, $attachment_id = 0 ) { 1035 $srcset_array = wp_get_attachment_image_srcset_array( $size_array, $image_meta, $attachment_id ); 1010 1036 1011 1037 // Only return a srcset value if there is more than one source. 1012 if ( count( $srcset_array ) < = 1) {1038 if ( count( $srcset_array ) < 2 ) { 1013 1039 return false; 1014 1040 } 1015 1041 … … 1023 1049 * 1024 1050 * @since 4.4.0 1025 1051 * 1026 * @param string 1027 * @param int $attachment_id Attachment ID for image.1028 * @param array |string $size Image size. Accepts any valid image size, or an array of width and height1029 * values in pixels (in that order). Default 'medium'.1052 * @param string $srcset A source set formated for a `srcset` attribute. 1053 * @param array $size_array Image size. Array of width and height values in pixels (in that order). 1054 * @param array $image_meta The image meta data. 1055 * @param int $attachment_id Image attachment ID. 1030 1056 */ 1031 return apply_filters( 'wp_get_attachment_image_srcset', rtrim( $srcset, ', ' ), $ attachment_id, $size);1057 return apply_filters( 'wp_get_attachment_image_srcset', rtrim( $srcset, ', ' ), $size_array, $image_meta, $attachment_id ); 1032 1058 } 1033 1059 1034 1060 /** 1035 * Retrieves a source size attribute for an image from an array of values.1061 * Create `sizes` attribute value for an image. 1036 1062 * 1037 1063 * @since 4.4.0 1038 1064 * 1039 * @param int $attachment_id Image attachment ID.1040 * @param array |string $size Image size. Accepts any valid image size, or an array of width and height1041 * values in pixels (in that order). Default 'medium'.1042 * @param int $width Optional. Display width of the image.1065 * @param int|array $size The width of the image or an array( $width, $height ). 1066 * @param array $image_meta Optional. The image meta data. 1067 * @param int $attachment_id Optional. Image attachment ID. 1068 * 1043 1069 * @return string|bool A valid source size value for use in a 'sizes' attribute or false. 1044 1070 */ 1045 function wp_get_attachment_image_sizes( $ attachment_id, $size = 'medium', $width = null) {1046 // Try to get the image width from $args parameter.1047 if ( is_numeric( $width ) ) { 1048 $img_width = (int) $width;1049 // Next, use see if a width value was passed in the $size parameter.1071 function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0 ) { 1072 $width = 0; 1073 1074 if ( is_numeric( $size ) ) { 1075 $width = absint( $size ); 1050 1076 } elseif ( is_array( $size ) ) { 1051 $img_width = $size[0]; 1052 // Finally, use the $size name to return the width of the image. 1053 } else { 1054 $image = image_get_intermediate_size( $attachment_id, $size ); 1055 $img_width = $image ? $image['width'] : false; 1077 $width = absint( $size[0] ); 1056 1078 } 1057 1079 1058 // Bail early if $image_width isn't set. 1059 if ( ! $img_width ) { 1080 if ( ! $width ) { 1060 1081 return false; 1061 1082 } 1062 1083 1063 1084 // Setup the default sizes attribute. 1064 $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $img_width );1085 $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', (int) $width ); 1065 1086 1066 1087 /** 1067 1088 * Filter the output of wp_get_attachment_image_sizes(). … … 1074 1095 * values in pixels (in that order). Default 'medium'. 1075 1096 * @param int $width Display width of the image. 1076 1097 */ 1077 return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $ attachment_id, $size, $width);1098 return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $width, $image_meta, $attachment_id ); 1078 1099 } 1079 1100 1080 1101 /** … … 1082 1103 * 1083 1104 * @since 4.4.0 1084 1105 * 1085 * @see wp_im g_add_srcset_and_sizes()1106 * @see wp_image_add_srcset_and_sizes() 1086 1107 * 1087 1108 * @param string $content The raw post content to be filtered. 1088 1109 * @return string Converted content with 'srcset' and 'sizes' attributes added to images. … … 1090 1111 function wp_make_content_images_responsive( $content ) { 1091 1112 $images = get_media_embedded_in_content( $content, 'img' ); 1092 1113 1093 $attachment_ids = array(); 1114 foreach( $images as $image ) { 1115 if ( false === strpos( $image, ' srcset="' ) && preg_match( '/wp-image-([0-9]+)/i', $image, $class_id ) && 1116 ( $attachment_id = absint( $class_id[1] ) ) ) { 1094 1117 1095 foreach( $images as $image ) { 1096 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 } 1118 $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); 1119 $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content ); 1101 1120 } 1102 1121 } 1103 1122 1104 if ( 0 < count( $attachment_ids ) ) {1105 /*1106 * Warm object caches for use with wp_get_attachment_metadata.1107 *1108 * To avoid making a database call for each image, a single query1109 * warms the object cache with the meta information for all images.1110 */1111 _prime_post_caches( $attachment_ids, false, true );1112 }1113 1114 foreach( $images as $image ) {1115 $content = str_replace( $image, wp_img_add_srcset_and_sizes( $image ), $content );1116 }1117 1118 1123 return $content; 1119 1124 } 1120 1125 … … 1126 1131 * @see wp_get_attachment_image_srcset() 1127 1132 * @see wp_get_attachment_image_sizes() 1128 1133 * 1129 * @param string $image An HTML 'img' element to be filtered. 1134 * @param string $image An HTML 'img' element to be filtered. 1135 * @param array $image_meta The image meta data. 1136 * @param int $attachment_id Image attachment ID. 1130 1137 * @return string Converted 'img' element with `srcset` and `sizes` attributes added. 1131 1138 */ 1132 function wp_im g_add_srcset_and_sizes( $image) {1133 // Return early if a 'srcset' attribute already exists.1134 if ( false !== strpos( $image, ' srcset="') ) {1139 function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { 1140 // Ensure the image meta exists 1141 if ( empty( $image_meta['sizes'] ) ) { 1135 1142 return $image; 1136 1143 } 1137 1144 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; 1145 $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; 1146 list( $src ) = explode( '?', $src ); 1142 1147 1143 if ( $id && false === $size ) { 1144 $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false; 1148 // Return early if we coudn't get the image source. 1149 if ( ! $src ) { 1150 return $image; 1151 } 1145 1152 1146 if ( $width && $height ) { 1147 $size = array( $width, $height ); 1148 } 1153 // Bail early when an image has been inserted and later edited. 1154 // We don't have the previous image meta to generate srcset. 1155 if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && 1156 strpos( wp_basename( $src ), $img_edit_hash[0] ) === false ) { 1157 1158 return $image; 1149 1159 } 1150 1160 1151 /* 1152 * If attempts to parse the size value failed, attempt to use the image 1153 * metadata to match the 'src' against the available sizes for an attachment. 1154 */ 1155 if ( ! $size && ! empty( $id ) && is_array( $meta = wp_get_attachment_metadata( $id ) ) ) { 1156 // Parse the image src value from the img element. 1157 $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : false; 1161 $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; 1162 $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0; 1158 1163 1159 // Return early if the src value is empty. 1160 if ( ! $src ) { 1161 return $image; 1162 } 1163 1164 if ( ! $width || ! $height ) { 1164 1165 /* 1165 * First, see if the file is the full size image. If not, loop through1166 * the intermediate sizes until we find a file that matches.1166 * If attempts to parse the size value failed, attempt to use the image 1167 * metadata to match the 'src' against the available sizes for an attachment. 1167 1168 */ 1168 1169 $image_filename = wp_basename( $src ); 1169 1170 1170 if ( $image_filename === basename( $meta['file'] ) ) { 1171 $size = 'full'; 1171 if ( $image_filename === basename( $image_meta['file'] ) ) { 1172 $width = (int) $image_meta['width']; 1173 $height = (int) $image_meta['height']; 1172 1174 } else { 1173 foreach( $ meta['sizes'] as $image_size =>$image_size_data ) {1175 foreach( $image_meta['sizes'] as $image_size_data ) { 1174 1176 if ( $image_filename === $image_size_data['file'] ) { 1175 $size = $image_size; 1177 $width = (int) $image_size_data['width']; 1178 $height = (int) $image_size_data['height']; 1176 1179 break; 1177 1180 } 1178 1181 } 1179 1182 } 1183 } 1180 1184 1185 if ( ! $width || ! $height ) { 1186 return $image; 1181 1187 } 1182 1188 1183 // 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 ) ) ) { 1189 $size_array = array( $width, $height ); 1190 $srcset = wp_get_attachment_image_srcset( $size_array, $image_meta, $attachment_id ); 1191 $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id ); 1192 1193 if ( $srcset && $sizes ) { 1185 1194 // Format the srcset and sizes string and escape attributes. 1186 $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes ) );1195 $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes ) ); 1187 1196 1188 1197 // Add srcset and sizes attributes to the image markup. 1189 $image = preg_replace( '/<img ([^>]+ )[\s?][\/?]>/', '<img $1' . $srcset_and_sizes . ' />', $image );1198 $image = preg_replace( '/<img ([^>]+?)[\/ ]*>/', '<img $1' . $srcset_and_sizes . ' />', $image ); 1190 1199 } 1191 1200 1192 1201 return $image; -
tests/phpunit/tests/media.php
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 … … 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( $size_array, $image_meta, $id ) ); 748 767 } 749 768 } 750 769 … … 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 … … 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( $size_array, $image_meta, $id ) ); 790 810 } 791 811 792 812 // Leave the uploads option the way you found it. … … 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'] ); 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'] ); 811 833 812 // Save edited metadata.813 wp_update_attachment_metadata( self::$large_id, $meta );814 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( $size_array, $image_meta, self::$large_id ); 817 836 818 837 // Test to confirm all sources in the array include the same edit hash. 819 838 foreach ( $sizes as $size ) { … … 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( array( 400, 300 ), array(), 99999 ); 829 848 830 849 // For canola.jpg we should return 831 850 $this->assertFalse( $sizes ); … … 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 $s rcset = 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( $size_array, $image_meta, self::$large_id ); 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 871 880 872 $image = wp_get_attachment_metadata( self::$large_id ); 881 $sizes = wp_get_attachment_image_srcset( $size_array, $image_meta, self::$large_id ); 882 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 } … … 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( $size_array, $image_meta, self::$large_id ); 893 905 894 906 $this->assertFalse( $sizes ); 895 907 } … … 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( $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; 930 function test_wp_make_content_images_responsive() { 931 $image_meta = wp_get_attachment_metadata( self::$large_id ); 932 $size_array = $this->_get_image_size_array_from_name( 'medium' ); 919 933 920 $ expected = '(max-width: 350px) 100vw, 350px';921 $sizes = wp_get_attachment_image_sizes( self::$large_id, 'medium', $width);934 $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( $size_array, $image_meta, self::$large_id ) ); 935 $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( $size_array, $image_meta, self::$large_id ) ); 922 936 923 $this->assertSame( $expected, $sizes );924 }925 926 /**927 * @ticket 33641928 */929 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' ) );932 933 937 // Function used to build HTML for the editor. 934 938 $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); 935 939 $img_no_size = str_replace( 'size-', '', $img );