Changeset 35569
- Timestamp:
- 11/07/2015 09:35:34 PM (9 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/media.php
r35567 r35569 827 827 if ( is_array( $image_meta ) ) { 828 828 $size_array = array( absint( $width ), absint( $height ) ); 829 $srcset = wp_calculate_image_srcset( $s rc, $size_array, $image_meta, $attachment_id );830 $sizes = wp_ get_attachment_image_sizes( $size_array, $image_meta, $attachment_id, $src);829 $srcset = wp_calculate_image_srcset( $size_array, $src, $image_meta, $attachment_id ); 830 $sizes = wp_calculate_image_sizes( $size_array, $src, $image_meta, $attachment_id ); 831 831 832 832 if ( $srcset && ( $sizes || ! empty( $attr['sizes'] ) ) ) { … … 934 934 * 935 935 * @param int $attachment_id Image attachment ID. 936 * @param array|string $size Image size. Accepts any valid image size, or an array of width and height937 * values in pixels (in that order). Default 'medium'.936 * @param array|string $size Optional. Image size. Accepts any valid image size, or an array of 937 * width and height values in pixels (in that order). Default 'medium'. 938 938 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. 939 939 * @return string|bool A 'srcset' value string or false. … … 954 954 ); 955 955 956 return wp_calculate_image_srcset( $ image_src, $size_array, $image_meta, $attachment_id );956 return wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); 957 957 } 958 958 … … 962 962 * @since 4.4.0 963 963 * 964 * @param array $size_array Array of width and height values in pixels (in that order). 964 965 * @param string $image_src The 'src' of the image. 965 * @param array $size_array Array of width and height values in pixels (in that order).966 966 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 967 967 * @param int $attachment_id Optional. The image attachment ID to pass to the filter. 968 968 * @return string|bool The 'srcset' attribute value. False on error or when only one source exists. 969 969 */ 970 function wp_calculate_image_srcset( $ image_src, $size_array, $image_meta, $attachment_id = 0 ) {970 function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id = 0 ) { 971 971 if ( empty( $image_meta['sizes'] ) ) { 972 972 return false; … … 1107 1107 1108 1108 /** 1109 * Retrieves the value for an image attachment's 'sizes' attribute. 1110 * 1111 * @since 4.4.0 1112 * 1113 * @param int $attachment_id Image attachment ID. 1114 * @param array|string $size Optional. Image size. Accepts any valid image size, or an array of width 1115 * and height values in pixels (in that order). Default 'medium'. 1116 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. 1117 * @return string|bool A 'srcset' value string or false. 1118 */ 1119 function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) { 1120 if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) { 1121 return false; 1122 } 1123 1124 if ( ! is_array( $image_meta ) ) { 1125 $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); 1126 } 1127 1128 $image_src = $image[0]; 1129 $size_array = array( 1130 absint( $image[1] ), 1131 absint( $image[2] ) 1132 ); 1133 1134 return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); 1135 } 1136 1137 /** 1109 1138 * Create 'sizes' attribute value for an image. 1110 1139 * 1111 1140 * @since 4.4.0 1112 1141 * 1113 * @param array|string $size Image size . Accepts any valid image size name ('thumbnail', 'medium', etc.),1114 * o r an array of width and height values in pixels (in that order).1142 * @param array|string $size Image size to retrieve. Accepts any valid image size, or an array 1143 * of width and height values in pixels (in that order). Default 'medium'. 1115 1144 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. 1116 1145 * @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` is needed … … 1120 1149 * @return string|bool A valid source size value for use in a 'sizes' attribute or false. 1121 1150 */ 1122 function wp_ get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $image_src = null) {1151 function wp_calculate_image_sizes( $size, $image_src, $image_meta, $attachment_id = 0 ) { 1123 1152 $width = 0; 1124 1153 … … 1146 1175 1147 1176 /** 1148 * Filter the output of 'wp_ get_attachment_image_sizes()'.1177 * Filter the output of 'wp_calculate_image_sizes()'. 1149 1178 * 1150 1179 * @since 4.4.0 1151 1180 * 1152 1181 * @param string $sizes A source size value for use in a 'sizes' attribute. 1153 * @param array|string $size Image size. Image size name, or an array of width and height 1154 * values in pixels (in that order). 1182 * @param array|string $size Requested size. Image size or array of width and height values 1183 * in pixels (in that order). Default 'medium'. 1184 * @param string $image_src The URL to the image file. 1155 1185 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 1156 1186 * @param int $attachment_id Image attachment ID of the original image. 1157 * @param string $image_src Optional. The URL to the image file. 1158 */ 1159 return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $image_src ); 1187 */ 1188 return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id ); 1160 1189 } 1161 1190 … … 1226 1255 } 1227 1256 1228 $ src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : '';1229 list( $ src ) = explode( '?', $src );1257 $image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; 1258 list( $image_src ) = explode( '?', $image_src ); 1230 1259 1231 1260 // Return early if we couldn't get the image source. 1232 if ( ! $ src ) {1261 if ( ! $image_src ) { 1233 1262 return $image; 1234 1263 } … … 1236 1265 // Bail early if an image has been inserted and later edited. 1237 1266 if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && 1238 strpos( wp_basename( $ src ), $img_edit_hash[0] ) === false ) {1267 strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) { 1239 1268 1240 1269 return $image; … … 1249 1278 * the image file name from 'src' against the available sizes for an attachment. 1250 1279 */ 1251 $image_filename = wp_basename( $ src );1280 $image_filename = wp_basename( $image_src ); 1252 1281 1253 1282 if ( $image_filename === wp_basename( $image_meta['file'] ) ) { … … 1270 1299 1271 1300 $size_array = array( $width, $height ); 1272 $srcset = wp_calculate_image_srcset( $s rc, $size_array, $image_meta, $attachment_id );1301 $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); 1273 1302 1274 1303 if ( $srcset ) { 1275 $sizes = wp_ get_attachment_image_sizes( $size_array, $image_meta, $attachment_id, $src);1304 $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); 1276 1305 } 1277 1306 -
trunk/tests/phpunit/tests/media.php
r35561 r35569 755 755 $image_url = wp_get_attachment_image_url( self::$large_id, $size ); 756 756 $size_array = $this->_get_image_size_array_from_name( $size ); 757 $this->assertSame( $expected, wp_calculate_image_srcset( $ image_url, $size_array, $image_meta ) );757 $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 758 758 } 759 759 } … … 787 787 $size_array = $this->_get_image_size_array_from_name( $size ); 788 788 $image_url = wp_get_attachment_image_url( $id, $size ); 789 $this->assertSame( $expected, wp_calculate_image_srcset( $ image_url, $size_array, $image_meta ) );789 $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 790 790 } 791 791 … … 822 822 823 823 // Calculate a srcset array. 824 $sizes = explode( ', ', wp_calculate_image_srcset( $ image_url, $size_array, $image_meta ) );824 $sizes = explode( ', ', wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 825 825 826 826 // Test to confirm all sources in the array include the same edit hash. … … 834 834 */ 835 835 function test_wp_calculate_image_srcset_false() { 836 $sizes = wp_calculate_image_srcset( 'file.png', array( 400, 300 ), array() );836 $sizes = wp_calculate_image_srcset( array( 400, 300 ), 'file.png', array() ); 837 837 838 838 // For canola.jpg we should return … … 850 850 $size_array = array(0, 0); 851 851 852 $srcset = wp_calculate_image_srcset( $ image_url, $size_array, $image_meta );852 $srcset = wp_calculate_image_srcset( $size_array, $image_url, $image_meta ); 853 853 854 854 // The srcset should be false. … … 915 915 // Test sizes against the default WP sizes. 916 916 $intermediates = array('thumbnail', 'medium', 'medium_large', 'large'); 917 918 foreach( $intermediates as $int_size ) { 919 $image = wp_get_attachment_image_src( self::$large_id, $int_size ); 920 921 $expected = '(max-width: ' . $image[1] . 'px) 100vw, ' . $image[1] . 'px'; 922 $sizes = wp_get_attachment_image_sizes( self::$large_id, $int_size ); 923 924 $this->assertSame( $expected, $sizes ); 925 } 926 } 927 928 /** 929 * @ticket 33641 930 */ 931 function test_wp_calculate_image_sizes() { 932 // Test sizes against the default WP sizes. 933 $intermediates = array('thumbnail', 'medium', 'medium_large', 'large'); 917 934 $image_meta = wp_get_attachment_metadata( self::$large_id ); 918 935 919 936 foreach( $intermediates as $int_size ) { 920 937 $size_array = $this->_get_image_size_array_from_name( $int_size ); 938 $image_src = $image_meta['sizes'][$int_size]['file']; 921 939 list( $width, $height ) = $size_array; 922 940 923 941 $expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px'; 924 $sizes = wp_ get_attachment_image_sizes( $size_array, $image_meta );942 $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta ); 925 943 926 944 $this->assertSame( $expected, $sizes ); … … 936 954 937 955 $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) ); 938 $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( $size_array, $image_meta, self::$large_id) );956 $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) ); 939 957 940 958 // Function used to build HTML for the editor. … … 1020 1038 1021 1039 // Full size GIFs should not return a srcset. 1022 $this->assertFalse( wp_calculate_image_srcset( $ full_src, $size_array, $image_meta ) );1040 $this->assertFalse( wp_calculate_image_srcset( $size_array, $full_src, $image_meta ) ); 1023 1041 // Intermediate sized GIFs should not include the full size in the srcset. 1024 $this->assertFalse( strpos( wp_calculate_image_srcset( $ large_src, $size_array, $image_meta ), $full_src ) );1042 $this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) ); 1025 1043 } 1026 1044 }
Note: See TracChangeset
for help on using the changeset viewer.