diff --git src/wp-includes/media.php src/wp-includes/media.php
index 562f283..e686d6a 100644
|
|
|
function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa |
| 826 | 826 | |
| 827 | 827 | if ( is_array( $image_meta ) ) { |
| 828 | 828 | $size_array = array( absint( $width ), absint( $height ) ); |
| 829 | | $srcset = wp_calculate_image_srcset( $src, $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'] ) ) ) { |
| 833 | 833 | $attr['srcset'] = $srcset; |
| … |
… |
function _wp_get_image_size_from_meta( $size_name, $image_meta ) { |
| 933 | 933 | * @since 4.4.0 |
| 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 height |
| 937 | | * 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. |
| 940 | 940 | */ |
| … |
… |
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag |
| 953 | 953 | absint( $image[2] ) |
| 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 | |
| 959 | 959 | /** |
| … |
… |
function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag |
| 961 | 961 | * |
| 962 | 962 | * @since 4.4.0 |
| 963 | 963 | * |
| 964 | | * @param string $image_src The 'src' of the image. |
| 965 | 964 | * @param array $size_array Array of width and height values in pixels (in that order). |
| | 965 | * @param string $image_src The 'src' of the image. |
| 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; |
| 973 | 973 | } |
| … |
… |
function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attac |
| 1105 | 1105 | } |
| 1106 | 1106 | |
| 1107 | 1107 | /** |
| | 1108 | * Retrieves the value for an image attachment's 'sizes' attribute. |
| | 1109 | * |
| | 1110 | * @since 4.4.0 |
| | 1111 | * |
| | 1112 | * @param int $attachment_id Image attachment ID. |
| | 1113 | * @param array|string $size Optional. Image size. Accepts any valid image size, or an array of width |
| | 1114 | * and height values in pixels (in that order). Default 'medium'. |
| | 1115 | * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
| | 1116 | * @return string|bool A 'srcset' value string or false. |
| | 1117 | */ |
| | 1118 | function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $image_meta = null ) { |
| | 1119 | if ( ! $image = wp_get_attachment_image_src( $attachment_id, $size ) ) { |
| | 1120 | return false; |
| | 1121 | } |
| | 1122 | |
| | 1123 | if ( ! is_array( $image_meta ) ) { |
| | 1124 | $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); |
| | 1125 | } |
| | 1126 | |
| | 1127 | $image_src = $image[0]; |
| | 1128 | $size_array = array( |
| | 1129 | absint( $image[1] ), |
| | 1130 | absint( $image[2] ) |
| | 1131 | ); |
| | 1132 | |
| | 1133 | return wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); |
| | 1134 | } |
| | 1135 | |
| | 1136 | /** |
| 1108 | 1137 | * Create 'sizes' attribute value for an image. |
| 1109 | 1138 | * |
| 1110 | 1139 | * @since 4.4.0 |
| 1111 | 1140 | * |
| 1112 | | * @param array|string $size Image size. Accepts any valid image size name ('thumbnail', 'medium', etc.), |
| 1113 | | * or an array of width and height values in pixels (in that order). |
| | 1141 | * @param array|string $size Image size to retrieve. Accepts any valid image size, or an array |
| | 1142 | * of width and height values in pixels (in that order). Default 'medium'. |
| 1114 | 1143 | * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. |
| 1115 | 1144 | * @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` is needed |
| 1116 | 1145 | * when using the image size name as argument for `$size`. |
| … |
… |
function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attac |
| 1118 | 1147 | * |
| 1119 | 1148 | * @return string|bool A valid source size value for use in a 'sizes' attribute or false. |
| 1120 | 1149 | */ |
| 1121 | | function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $image_src = null ) { |
| | 1150 | function wp_calculate_image_sizes( $size, $image_src, $image_meta, $attachment_id = 0 ) { |
| 1122 | 1151 | $width = 0; |
| 1123 | 1152 | |
| 1124 | 1153 | if ( is_array( $size ) ) { |
| … |
… |
function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_i |
| 1144 | 1173 | $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $width ); |
| 1145 | 1174 | |
| 1146 | 1175 | /** |
| 1147 | | * Filter the output of 'wp_get_attachment_image_sizes()'. |
| | 1176 | * Filter the output of 'wp_calculate_image_sizes()'. |
| 1148 | 1177 | * |
| 1149 | 1178 | * @since 4.4.0 |
| 1150 | 1179 | * |
| 1151 | 1180 | * @param string $sizes A source size value for use in a 'sizes' attribute. |
| 1152 | | * @param array|string $size Image size. Image size name, or an array of width and height |
| 1153 | | * values in pixels (in that order). |
| | 1181 | * @param array|string $size Requested size. Image size or array of width and height values |
| | 1182 | * in pixels (in that order). Default 'medium'. |
| | 1183 | * @param string $image_src The URL to the image file. |
| 1154 | 1184 | * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. |
| 1155 | 1185 | * @param int $attachment_id Image attachment ID of the original image. |
| 1156 | | * @param string $image_src Optional. The URL to the image file. |
| 1157 | 1186 | */ |
| 1158 | | return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $image_src ); |
| | 1187 | return apply_filters( 'wp_calculate_image_sizes', $sizes, $size, $image_src, $image_meta, $attachment_id ); |
| 1159 | 1188 | } |
| 1160 | 1189 | |
| 1161 | 1190 | /** |
| … |
… |
function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { |
| 1224 | 1253 | return $image; |
| 1225 | 1254 | } |
| 1226 | 1255 | |
| 1227 | | $src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; |
| 1228 | | list( $src ) = explode( '?', $src ); |
| | 1256 | $image_src = preg_match( '/src="([^"]+)"/', $image, $match_src ) ? $match_src[1] : ''; |
| | 1257 | list( $image_src ) = explode( '?', $image_src ); |
| 1229 | 1258 | |
| 1230 | 1259 | // Return early if we couldn't get the image source. |
| 1231 | | if ( ! $src ) { |
| | 1260 | if ( ! $image_src ) { |
| 1232 | 1261 | return $image; |
| 1233 | 1262 | } |
| 1234 | 1263 | |
| 1235 | 1264 | // Bail early if an image has been inserted and later edited. |
| 1236 | 1265 | if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && |
| 1237 | | strpos( wp_basename( $src ), $img_edit_hash[0] ) === false ) { |
| | 1266 | strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) { |
| 1238 | 1267 | |
| 1239 | 1268 | return $image; |
| 1240 | 1269 | } |
| … |
… |
function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { |
| 1247 | 1276 | * If attempts to parse the size value failed, attempt to use the image meta data to match |
| 1248 | 1277 | * the image file name from 'src' against the available sizes for an attachment. |
| 1249 | 1278 | */ |
| 1250 | | $image_filename = wp_basename( $src ); |
| | 1279 | $image_filename = wp_basename( $image_src ); |
| 1251 | 1280 | |
| 1252 | 1281 | if ( $image_filename === wp_basename( $image_meta['file'] ) ) { |
| 1253 | 1282 | $width = (int) $image_meta['width']; |
| … |
… |
function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { |
| 1268 | 1297 | } |
| 1269 | 1298 | |
| 1270 | 1299 | $size_array = array( $width, $height ); |
| 1271 | | $srcset = wp_calculate_image_srcset( $src, $size_array, $image_meta, $attachment_id ); |
| | 1300 | $srcset = wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attachment_id ); |
| 1272 | 1301 | |
| 1273 | 1302 | if ( $srcset ) { |
| 1274 | | $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta, $attachment_id, $src ); |
| | 1303 | $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta, $attachment_id ); |
| 1275 | 1304 | } |
| 1276 | 1305 | |
| 1277 | 1306 | if ( $srcset && $sizes ) { |
diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php
index 5734f3e..c666fd8 100644
|
|
|
EOF; |
| 754 | 754 | foreach ( $sizes as $size ) { |
| 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 | } |
| 760 | 760 | |
| … |
… |
EOF; |
| 786 | 786 | foreach ( $sizes as $size ) { |
| 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 | |
| 792 | 792 | // Remove the attachment |
| … |
… |
EOF; |
| 821 | 821 | $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file'] ); |
| 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. |
| 827 | 827 | foreach ( $sizes as $size ) { |
| … |
… |
EOF; |
| 833 | 833 | * @ticket 33641 |
| 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 |
| 839 | 839 | $this->assertFalse( $sizes ); |
| … |
… |
EOF; |
| 849 | 849 | |
| 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. |
| 855 | 855 | $this->assertFalse( $srcset ); |
| … |
… |
EOF; |
| 898 | 898 | function test_wp_get_attachment_image_sizes() { |
| 899 | 899 | // Test sizes against the default WP sizes. |
| 900 | 900 | $intermediates = array('thumbnail', 'medium', 'medium_large', 'large'); |
| | 901 | |
| | 902 | foreach( $intermediates as $int_size ) { |
| | 903 | $image = wp_get_attachment_image_src( self::$large_id, $int_size ); |
| | 904 | |
| | 905 | $expected = '(max-width: ' . $image[1] . 'px) 100vw, ' . $image[1] . 'px'; |
| | 906 | $sizes = wp_get_attachment_image_sizes( self::$large_id, $int_size ); |
| | 907 | |
| | 908 | $this->assertSame( $expected, $sizes ); |
| | 909 | } |
| | 910 | } |
| | 911 | |
| | 912 | /** |
| | 913 | * @ticket 33641 |
| | 914 | */ |
| | 915 | function test_wp_calculate_image_sizes() { |
| | 916 | // Test sizes against the default WP sizes. |
| | 917 | $intermediates = array('thumbnail', 'medium', 'medium_large', 'large'); |
| 901 | 918 | $image_meta = wp_get_attachment_metadata( self::$large_id ); |
| 902 | 919 | |
| 903 | 920 | foreach( $intermediates as $int_size ) { |
| 904 | 921 | $size_array = $this->_get_image_size_array_from_name( $int_size ); |
| | 922 | $image_src = $image_meta['sizes'][$int_size]['file']; |
| 905 | 923 | list( $width, $height ) = $size_array; |
| 906 | 924 | |
| 907 | 925 | $expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px'; |
| 908 | | $sizes = wp_get_attachment_image_sizes( $size_array, $image_meta ); |
| | 926 | $sizes = wp_calculate_image_sizes( $size_array, $image_src, $image_meta ); |
| 909 | 927 | |
| 910 | 928 | $this->assertSame( $expected, $sizes ); |
| 911 | 929 | } |
| … |
… |
EOF; |
| 919 | 937 | $size_array = $this->_get_image_size_array_from_name( 'medium' ); |
| 920 | 938 | |
| 921 | 939 | $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, $size_array, $image_meta ) ); |
| 922 | | $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( $size_array, $image_meta, self::$large_id ) ); |
| | 940 | $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, $size_array, $image_meta ) ); |
| 923 | 941 | |
| 924 | 942 | // Function used to build HTML for the editor. |
| 925 | 943 | $img = get_image_tag( self::$large_id, '', '', '', 'medium' ); |
| … |
… |
EOF; |
| 1000 | 1018 | // Test with soft resized size array. |
| 1001 | 1019 | $size_array = array(900, 450); |
| 1002 | 1020 | |
| 1003 | | $this->assertFalse( wp_calculate_image_srcset( $image_src, $size_array, $image_meta ) ); |
| | 1021 | $this->assertFalse( wp_calculate_image_srcset( $size_array, $image_src, $image_meta ) ); |
| 1004 | 1022 | } |
| 1005 | 1023 | } |