Ticket #35030: 35030.1.patch
File 35030.1.patch, 9.4 KB (added by , 9 years ago) |
---|
-
src/wp-includes/media.php
diff --git src/wp-includes/media.php src/wp-includes/media.php index 1c8387b..8eb5268 100644
function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 1052 1052 */ 1053 1053 $max_srcset_image_width = apply_filters( 'max_srcset_image_width', 1600, $size_array ); 1054 1054 1055 // Array to hold URL candidates.1056 $sources = array();1055 // Arrays to hold URL candidates. 1056 $sources = $src = $alternatives = array(); 1057 1057 1058 1058 /** 1059 1059 * To make sure the ID matches our image src, we will check to see if any sizes in our attachment … … function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 1067 1067 * versions of the same edit. 1068 1068 */ 1069 1069 foreach ( $image_sizes as $image ) { 1070 $is_src = false; 1070 1071 1071 1072 // Check if image meta isn't corrupted. 1072 1073 if ( ! is_array( $image ) ) { … … function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 1076 1077 // If the file name is part of the `src`, we've confirmed a match. 1077 1078 if ( ! $src_matched && false !== strpos( $image_src, $dirname . $image['file'] ) ) { 1078 1079 $src_matched = true; 1080 $is_src = true; 1079 1081 } 1080 1082 1081 1083 // Filter out images that are from previous edits. … … function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 1087 1089 * Filter out images that are wider than '$max_srcset_image_width' unless 1088 1090 * that file is in the 'src' attribute. 1089 1091 */ 1090 if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width && 1091 false === strpos( $image_src, $image['file'] ) ) { 1092 1092 if ( $max_srcset_image_width && $image['width'] > $max_srcset_image_width && ! $is_src ) { 1093 1093 continue; 1094 1094 } 1095 1095 … … function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac 1109 1109 // If the image dimensions are within 1px of the expected size, use it. 1110 1110 if ( abs( $constrained_size[0] - $expected_size[0] ) <= 1 && abs( $constrained_size[1] - $expected_size[1] ) <= 1 ) { 1111 1111 // Add the URL, descriptor, and value to the sources array to be returned. 1112 $source s[ $image['width'] ]= array(1112 $source = array( 1113 1113 'url' => $image_baseurl . $image['file'], 1114 1114 'descriptor' => 'w', 1115 1115 'value' => $image['width'], 1116 1116 ); 1117 1118 /* 1119 * Use two arrays to make sure the 'src' image becomes first in the 'srcset', because 1120 * of a bug in iOS8 that makes it always select the first source from the set. See #35030. 1121 */ 1122 if ( $is_src ) { 1123 $src[ $image['width'] ] = $source; 1124 } else { 1125 $alternatives[ $image['width'] ] = $source; 1126 } 1117 1127 } 1118 1128 } 1119 1129 1130 $sources = $src + $alternatives; 1131 1120 1132 /** 1121 1133 * Filter an image's 'srcset' sources. 1122 1134 * -
tests/phpunit/tests/media.php
diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php index a58360a..31b0b9f 100644
EOF; 845 845 return array( 1024, 768 ); 846 846 case 'full': 847 847 return array( 1600, 1200 ); // actual size of ../data/images/test-image-large.png 848 case 'twentyfifteen-logo': 849 return array( 248, 186 ); 848 850 default: 849 851 return array( 800, 600 ); // soft-resized image 850 852 } 851 853 } 852 854 853 855 /** 856 * Helper function to move the src image to the first position in the expected srcset string. 857 */ 858 function _src_first( $srcset, $src_url, $src_width ) { 859 $src_string = $src_url . ' ' . $src_width . 'w'; 860 $src_not_first = ', ' . $src_string; 861 862 if ( strpos( $srcset, $src_not_first ) ) { 863 $srcset = str_replace( $src_not_first, '', $srcset ); 864 $srcset = $src_string . ', ' . $srcset; 865 } 866 867 return $srcset; 868 } 869 870 /** 854 871 * @ticket 33641 855 872 */ 856 873 function test_wp_calculate_image_srcset() { … … EOF; 885 902 foreach ( $intermediates as $int ) { 886 903 $image_url = wp_get_attachment_image_url( self::$large_id, $int ); 887 904 $size_array = $this->_get_image_size_array_from_name( $int ); 888 $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 905 $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] ); 906 $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 889 907 } 890 908 } 891 909 … … EOF; 929 947 foreach ( $intermediates as $int ) { 930 948 $size_array = $this->_get_image_size_array_from_name( $int ); 931 949 $image_url = wp_get_attachment_image_url( $id, $int ); 932 $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 950 $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] ); 951 $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 933 952 } 934 953 935 954 // Remove the attachment … … EOF; 1008 1027 foreach ( $intermediates as $int ) { 1009 1028 $image_url = wp_get_attachment_image_url( self::$large_id, $int ); 1010 1029 $size_array = $this->_get_image_size_array_from_name( $int ); 1011 $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 1030 $expected_srcset = $this->_src_first( $expected, $image_url, $size_array[0] ); 1031 $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 1012 1032 } 1013 1033 } 1014 1034 … … EOF; 1123 1143 ), 1124 1144 ); 1125 1145 1126 $expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test -300x150.png 300w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 2000w';1146 $expected_srcset = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test.png 2000w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-300x150.png 300w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-768x384.png 768w, http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/2015/12/test-1024x512.png 1024w'; 1127 1147 1128 1148 $this->assertSame( $expected_srcset, wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) ); 1129 1149 } … … EOF; 1202 1222 } 1203 1223 1204 1224 /** 1225 * Helper filter function to shuffle the image meta sizes array. 1226 */ 1227 function _shuffle_sizes( $image_meta ) { 1228 $sizes = $image_meta['sizes']; 1229 $keys = array_keys( $sizes ); 1230 $image_meta_sizes = array(); 1231 1232 shuffle( $keys ); 1233 1234 foreach( $keys as $key ) { 1235 $image_meta_sizes[ $key ] = $sizes[ $key ]; 1236 } 1237 1238 $image_meta['sizes'] = $image_meta_sizes; 1239 1240 return $image_meta; 1241 } 1242 1243 /** 1244 * @ticket 35030 1245 */ 1246 function test_wp_calculate_image_srcset_src_first() { 1247 global $_wp_additional_image_sizes; 1248 1249 $year_month = date('Y/m'); 1250 $image_meta = wp_get_attachment_metadata( self::$large_id ); 1251 $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/'; 1252 1253 // Set up test cases for all expected size names. 1254 $intermediates = array( 'medium', 'medium_large', 'large', 'full' ); 1255 1256 // Add any soft crop intermediate sizes. 1257 foreach ( $_wp_additional_image_sizes as $name => $additional_size ) { 1258 if ( ! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) { 1259 $intermediates[] = $name; 1260 } 1261 } 1262 1263 $expected = ""; 1264 1265 foreach( $image_meta['sizes'] as $name => $size ) { 1266 // Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4. 1267 if ( in_array( $name, $intermediates ) ) { 1268 $expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, '; 1269 } 1270 } 1271 1272 // Add the full size width at the end. 1273 $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] .'w'; 1274 1275 foreach ( $intermediates as $int ) { 1276 $image_url = wp_get_attachment_image_url( self::$large_id, $int ); 1277 $size_array = $this->_get_image_size_array_from_name( $int ); 1278 1279 // The expected first source in the srcset is the src image. 1280 $expected_first = $image_url . ' ' . $size_array[0] . 'w'; 1281 1282 // Shuffle the sizes array used by 'wp_calculate_image_srcset()' to create the srcset. 1283 add_filter( 'wp_calculate_image_srcset_meta', array( $this, '_shuffle_sizes' ) ); 1284 1285 // Get the first source from the calculated srcset and compare with the expected first source. 1286 preg_match( '/[^,]+/', wp_calculate_image_srcset( $size_array, $image_url, $image_meta ), $srcset_first ); 1287 $this->assertSame( $expected_first, $srcset_first[0] ); 1288 1289 remove_filter( 'wp_calculate_image_srcset_meta', array( $this, '_shuffle_sizes' ) ); 1290 } 1291 } 1292 1293 /** 1205 1294 * @ticket 33641 1206 1295 */ 1207 1296 function test_wp_get_attachment_image_srcset() { … … EOF; 1235 1324 1236 1325 $expected .= $uploads_dir . $image_meta['file'] . ' ' . $image_meta['width'] .'w'; 1237 1326 1238 $this->assertSame( $expected, $srcset ); 1327 $expected_srcset = $this->_src_first( $expected, $uploads_dir . $image_meta['file'], $size_array[0] ); 1328 1329 $this->assertSame( $expected_srcset, $srcset ); 1239 1330 } 1240 1331 1241 1332 /** … … EOF; 1517 1608 1518 1609 $_SERVER['HTTPS'] = 'on'; 1519 1610 1520 $expected = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test- 300x150.jpg 300w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-1024x512.jpg 1024w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg 1200w';1611 $expected = 'https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-1024x512.jpg 1024w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test-300x150.jpg 300w, https://' . WP_TESTS_DOMAIN . '/wp-content/uploads/test.jpg 1200w'; 1521 1612 $actual = wp_calculate_image_srcset( $size_array, $image_url, $image_meta ); 1522 1613 1523 1614 $this->assertSame( $expected, $actual );