Ticket #34379: 34379.patch
| File 34379.patch, 9.8 KB (added by , 10 years ago) |
|---|
-
src/wp-includes/media.php
diff --git src/wp-includes/media.php src/wp-includes/media.php index 6fa51f8..06021ab 100644
function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = fa 814 814 // Generate srcset and sizes if not already present. 815 815 if ( empty( $attr['srcset'] ) && $srcset = wp_get_attachment_image_srcset( $attachment_id, $size ) ) { 816 816 $attr['srcset'] = $srcset; 817 $sizes_args = array( 818 'height' => $height, 819 'width' => $width, 820 ); 821 $attr['sizes'] = wp_get_attachment_image_sizes( $attachment_id, $size, $sizes_args ); 817 818 if ( empty( $attr['sizes'] ) && $sizes = wp_get_attachment_image_sizes( $attachment_id, $size, $width ) ) { 819 $attr['sizes'] = $sizes; 820 } 822 821 } 823 822 824 823 /** … … function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium' ) { 1019 1018 * 1020 1019 * @since 4.4.0 1021 1020 * 1022 * @param int $attachment_id Image attachment ID. 1023 * @param string $size Optional. Name of image size. Default value: 'medium'. 1024 * @param array $args { 1025 * Optional. Arguments to retrieve attachments. 1026 * 1027 * @type array|string $sizes An array or string containing of size information. 1028 * @type int $width A single width value used in the default `sizes` string. 1029 * } 1021 * @param int $attachment_id Image attachment ID. 1022 * @param array|string $size Image size. Accepts any valid image size, or an array of width and height 1023 * values in pixels (in that order). Default 'medium'. 1024 * @param int $width Optional. Display width of the image. 1030 1025 * @return string|bool A valid source size value for use in a 'sizes' attribute or false. 1031 1026 */ 1032 function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $args = null ) { 1033 $img_width = 0; 1034 // Try to get the image width from $args. 1035 if ( is_array( $args ) && ! empty( $args['width'] ) ) { 1036 $img_width = (int) $args['width']; 1037 } elseif ( $img = image_get_intermediate_size( $attachment_id, $size ) ) { 1038 list( $img_width ) = image_constrain_size_for_editor( $img['width'], $img['height'], $size ); 1027 function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $width = null ) { 1028 // Try to get the image width from $args parameter. 1029 if ( is_numeric( $width ) ) { 1030 $img_width = (int) $width; 1031 // Next, use see if a width value was passed in the $size parameter. 1032 } elseif ( is_array( $size ) ) { 1033 $img_width = $size[0]; 1034 // Finally, use the $size name to return the width of the image. 1035 } else { 1036 $image = image_get_intermediate_size( $attachment_id, $size ); 1037 $img_width = $image ? $image['width'] : false; 1039 1038 } 1040 1039 1041 1040 // Bail early if $image_width isn't set. … … function wp_get_attachment_image_sizes( $attachment_id, $size = 'medium', $args 1043 1042 return false; 1044 1043 } 1045 1044 1046 // Set the image width in pixels. 1047 $img_width = $img_width . 'px'; 1048 1049 // Set up our default values. 1050 $defaults = array( 1051 'sizes' => array( 1052 array( 1053 'size_value' => '100vw', 1054 'mq_value' => $img_width, 1055 'mq_name' => 'max-width' 1056 ), 1057 array( 1058 'size_value' => $img_width 1059 ), 1060 ) 1061 ); 1062 1063 $args = wp_parse_args( $args, $defaults ); 1045 // Setup the default sizes attribute. 1046 $sizes = sprintf( '(max-width: %1$dpx) 100vw, %1$dpx', $img_width ); 1064 1047 1065 1048 /** 1066 * Filter arguments used to create 'sizes' attribute.1049 * Filter the output of wp_get_attachment_image_sizes(). 1067 1050 * 1068 1051 * @since 4.4.0 1069 1052 * 1070 * @param array $args An array of arguments used to create a 'sizes' attribute. 1071 * @param int $attachment_id Post ID of the original image. 1072 * @param string $size Name of the image size being used. 1053 * @param string $sizes A source size value for use in a 'sizes' attribute. 1054 * @param int $attachment_id Post ID of the original image. 1055 * @param array|string $size Image size. Accepts any valid image size, or an array of width and height 1056 * values in pixels (in that order). Default 'medium'. 1057 * @param int $width Display width of the image. 1073 1058 */ 1074 $args = apply_filters( 'wp_image_sizes_args', $args, $attachment_id, $size ); 1075 1076 // If sizes is passed as a string, just use the string. 1077 if ( is_string( $args['sizes'] ) ) { 1078 $size_list = $args['sizes']; 1079 1080 // Otherwise, breakdown the array and build a sizes string. 1081 } elseif ( is_array( $args['sizes'] ) ) { 1082 1083 $size_list = ''; 1084 1085 foreach ( $args['sizes'] as $size ) { 1086 1087 // Use 100vw as the size value unless something else is specified. 1088 $size_value = ( $size['size_value'] ) ? $size['size_value'] : '100vw'; 1089 1090 // If a media length is specified, build the media query. 1091 if ( ! empty( $size['mq_value'] ) ) { 1092 1093 $media_length = $size['mq_value']; 1094 1095 // Use max-width as the media condition unless min-width is specified. 1096 $media_condition = ( ! empty( $size['mq_name'] ) ) ? $size['mq_name'] : 'max-width'; 1097 1098 // If a media_length was set, create the media query. 1099 $media_query = '(' . $media_condition . ": " . $media_length . ') '; 1100 1101 } else { 1102 // If no media length was set, $media_query is blank. 1103 $media_query = ''; 1104 } 1105 1106 // Add to the source size list string. 1107 $size_list .= $media_query . $size_value . ', '; 1108 } 1109 1110 // Remove the trailing comma and space from the end of the string. 1111 $size_list = substr( $size_list, 0, -2 ); 1112 } 1113 1114 // Return the sizes value as $size_list or false. 1115 return ( $size_list ) ? $size_list : false; 1059 return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $attachment_id, $size, $width ); 1116 1060 } 1117 1061 1118 1062 /** … … function wp_img_add_srcset_and_sizes( $image ) { 1177 1121 $id = preg_match( '/wp-image-([0-9]+)/i', $image, $match_id ) ? (int) $match_id[1] : false; 1178 1122 $size = preg_match( '/size-([^\s|"]+)/i', $image, $match_size ) ? $match_size[1] : false; 1179 1123 $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : false; 1180 $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false;1181 1124 1182 1125 if ( $id && false === $size ) { 1183 $size = array( $width, $height ); 1126 $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : false; 1127 1128 if ( $width && $height ) { 1129 $size = array( $width, $height ); 1130 } 1184 1131 } 1185 1132 1186 1133 /* … … function wp_img_add_srcset_and_sizes( $image ) { 1215 1162 1216 1163 } 1217 1164 1218 // If ID and size, try for 'srcset' and 'sizes' and update the markup. 1219 if ( $id && $size && $srcset = wp_get_attachment_image_srcset( $id, $size ) ) { 1220 1221 /* 1222 * Pass the 'height' and 'width' to 'wp_get_attachment_image_sizes()' to avoid 1223 * recalculating the image size. 1224 */ 1225 $args = array( 1226 'height' => $height, 1227 'width' => $width, 1228 ); 1229 1230 $sizes = wp_get_attachment_image_sizes( $id, $size, $args ); 1231 1165 // If ID and size exist, try for 'srcset' and 'sizes' and update the markup. 1166 if ( $id && $size && ( $srcset = wp_get_attachment_image_srcset( $id, $size ) ) && ( $sizes = wp_get_attachment_image_sizes( $id, $size, $width ) ) ) { 1232 1167 // Format the srcset and sizes string and escape attributes. 1233 1168 $srcset_and_sizes = sprintf( ' srcset="%s" sizes="%s"', esc_attr( $srcset ), esc_attr( $sizes) ); 1234 1169 -
tests/phpunit/tests/media.php
diff --git tests/phpunit/tests/media.php tests/phpunit/tests/media.php index 24fbfd4..9fee749 100644
EOF; 898 898 * @ticket 33641 899 899 */ 900 900 function test_wp_get_attachment_image_sizes() { 901 global $content_width;902 903 901 // Test sizes against the default WP sizes. 904 902 $intermediates = array('thumbnail', 'medium', 'large'); 905 903 906 904 foreach( $intermediates as $int ) { 907 905 $width = get_option( $int . '_size_w' ); 908 906 909 // The sizes width gets constrained to $content_width by default.910 if ( $content_width > 0 ) {911 $width = ( $width > $content_width ) ? $content_width : $width;912 }913 914 907 $expected = '(max-width: ' . $width . 'px) 100vw, ' . $width . 'px'; 915 908 $sizes = wp_get_attachment_image_sizes( self::$large_id, $int ); 916 909 … … EOF; 921 914 /** 922 915 * @ticket 33641 923 916 */ 924 function test_wp_get_attachment_image_sizes_with_args() { 925 $args = array( 926 'sizes' => array( 927 array( 928 'size_value' => '10em', 929 'mq_value' => '60em', 930 'mq_name' => 'min-width' 931 ), 932 array( 933 'size_value' => '20em', 934 'mq_value' => '30em', 935 'mq_name' => 'min-width' 936 ), 937 array( 938 'size_value' => 'calc(100vm - 30px)' 939 ), 940 ) 941 ); 917 function test_wp_get_attachment_image_sizes_with_width() { 918 $width = 350; 942 919 943 $expected = '(m in-width: 60em) 10em, (min-width: 30em) 20em, calc(100vm - 30px)';944 $sizes = wp_get_attachment_image_sizes( self::$large_id, 'medium', $ args);920 $expected = '(max-width: 350px) 100vw, 350px'; 921 $sizes = wp_get_attachment_image_sizes( self::$large_id, 'medium', $width ); 945 922 946 923 $this->assertSame( $expected, $sizes ); 947 924 } … … EOF; 949 926 /** 950 927 * @ticket 33641 951 928 */ 952 function test_wp_get_attachment_image_sizes_with_filtered_args() {953 // Add our test filter.954 add_filter( 'wp_image_sizes_args', array( $this, '_test_wp_image_sizes_args' ) );955 956 $sizes = wp_get_attachment_image_sizes( self::$large_id, 'medium' );957 958 remove_filter( 'wp_image_sizes_args', array( $this, '_test_wp_image_sizes_args' ) );959 960 // Evaluate that the sizes returned is what we expected.961 $this->assertSame( $sizes, '100vm');962 }963 964 /**965 * A simple test filter for wp_get_attachment_image_sizes().966 */967 function _test_wp_image_sizes_args( $args ) {968 $args['sizes'] = "100vm";969 return $args;970 }971 972 /**973 * @ticket 33641974 */975 929 function test_wp_make_content_images_responsive() { 976 930 $srcset = sprintf( 'srcset="%s"', wp_get_attachment_image_srcset( self::$large_id, 'medium' ) ); 977 931 $sizes = sprintf( 'sizes="%s"', wp_get_attachment_image_sizes( self::$large_id, 'medium' ) );