Ticket #35106: 35106.diff
File 35106.diff, 9.6 KB (added by , 9 years ago) |
---|
-
src/wp-includes/media.php
function wp_get_attachment_image($attach 866 866 * 867 867 * @since 4.4.0 868 868 * 869 869 * @param int $attachment_id Image attachment ID. 870 870 * @param string|array $size Optional. Image size to retrieve. Accepts any valid image size, or an array 871 871 * of width and height values in pixels (in that order). Default 'thumbnail'. 872 872 * @param bool $icon Optional. Whether the image should be treated as an icon. Default false. 873 873 * @return string|false Attachment URL or false if no image is available. 874 874 */ 875 875 function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon = false ) { 876 876 $image = wp_get_attachment_image_src( $attachment_id, $size, $icon ); 877 877 return isset( $image['0'] ) ? $image['0'] : false; 878 878 } 879 879 880 880 /** 881 * Get the attachment path relative to the upload directory. 882 * 883 * @since 4.4.1 884 * @access private 885 * 886 * @param string $file Attachment file name. 887 * @return string Attachment path relative to the upload directory. 888 */ 889 function _wp_get_attachment_relative_path( $file ) { 890 $dirname = dirname( $file ); 891 892 if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) { 893 // Get the directory name relative to the upload directory (back compat for pre-2.7 uploads) 894 $dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 ); 895 $dirname = ltrim( $dirname, '/' ); 896 } 897 898 if ( '.' === $dirname ) { 899 return ''; 900 } 901 902 return $dirname; 903 } 904 905 /** 881 906 * Caches and returns the base URL of the uploads directory. 882 907 * 883 908 * @since 4.4.0 884 909 * @access private 885 910 * 886 911 * @return string The base URL, cached. 887 912 */ 888 913 function _wp_upload_dir_baseurl() { 889 914 static $baseurl = array(); 890 915 891 916 $blog_id = get_current_blog_id(); 892 917 893 918 if ( empty( $baseurl[$blog_id] ) ) { 894 919 $uploads_dir = wp_upload_dir(); 895 920 $baseurl[$blog_id] = $uploads_dir['baseurl']; … … function wp_calculate_image_srcset( $siz 994 1019 * To avoid hiding animation in user content, if src is a full size GIF, a srcset attribute is not generated. 995 1020 * If src is an intermediate size GIF, the full size is excluded from srcset to keep a flattened GIF from becoming animated. 996 1021 */ 997 1022 if ( ! isset( $image_sizes['thumbnail']['mime-type'] ) || 'image/gif' !== $image_sizes['thumbnail']['mime-type'] ) { 998 1023 $image_sizes['full'] = array( 999 1024 'width' => $image_meta['width'], 1000 1025 'height' => $image_meta['height'], 1001 1026 'file' => $image_basename, 1002 1027 ); 1003 1028 } elseif ( strpos( $image_src, $image_meta['file'] ) ) { 1004 1029 return false; 1005 1030 } 1006 1031 1007 1032 // Uploads are (or have been) in year/month sub-directories. 1008 1033 if ( $image_basename !== $image_meta['file'] ) { 1009 $dirname = dirname( $image_meta['file'] );1034 $dirname = _wp_get_attachment_relative_path( $image_meta['file'] ); 1010 1035 1011 if ( $dirname !== '.') {1036 if ( $dirname ) { 1012 1037 $image_baseurl = trailingslashit( $image_baseurl ) . $dirname; 1013 1038 } 1014 1039 } 1015 1040 1016 1041 $image_baseurl = trailingslashit( $image_baseurl ); 1017 1042 1018 1043 /* 1019 1044 * Images that have been edited in WordPress after being uploaded will 1020 1045 * contain a unique hash. Look for that hash and use it later to filter 1021 1046 * out images that are leftovers from previous versions. 1022 1047 */ 1023 1048 $image_edited = preg_match( '/-e[0-9]{13}/', wp_basename( $image_src ), $image_edit_hash ); 1024 1049 1025 1050 /** 1026 1051 * Filter the maximum image width to be included in a 'srcset' attribute. … … function wp_image_add_srcset_and_sizes( 1277 1302 // Return early if we couldn't get the image source. 1278 1303 if ( ! $image_src ) { 1279 1304 return $image; 1280 1305 } 1281 1306 1282 1307 // Bail early if an image has been inserted and later edited. 1283 1308 if ( preg_match( '/-e[0-9]{13}/', $image_meta['file'], $img_edit_hash ) && 1284 1309 strpos( wp_basename( $image_src ), $img_edit_hash[0] ) === false ) { 1285 1310 1286 1311 return $image; 1287 1312 } 1288 1313 1289 1314 $base_url = trailingslashit( _wp_upload_dir_baseurl() ); 1290 1315 $image_base_url = $base_url; 1291 1316 1292 $dirname = dirname( $image_meta['file'] );1293 if ( $dirname !== '.') {1317 $dirname = _wp_get_attachment_relative_path( $image_meta['file'] ); 1318 if ( $dirname ) { 1294 1319 $image_base_url .= trailingslashit( $dirname ); 1295 1320 } 1296 1321 1297 1322 $all_sizes = wp_list_pluck( $image_meta['sizes'], 'file' ); 1298 1323 1299 1324 foreach ( $all_sizes as $key => $file ) { 1300 1325 $all_sizes[ $key ] = $image_base_url . $file; 1301 1326 } 1302 1327 1303 1328 // Add the original image. 1304 $all_sizes[] = $ base_url . $image_meta['file'];1329 $all_sizes[] = $image_base_url . basename( $image_meta['file'] ); 1305 1330 1306 1331 // Bail early if the image src doesn't match any of the known image sizes. 1307 1332 if ( ! in_array( $image_src, $all_sizes ) ) { 1308 1333 return $image; 1309 1334 } 1310 1335 1311 1336 $width = preg_match( '/ width="([0-9]+)"/', $image, $match_width ) ? (int) $match_width[1] : 0; 1312 1337 $height = preg_match( '/ height="([0-9]+)"/', $image, $match_height ) ? (int) $match_height[1] : 0; 1313 1338 1314 1339 if ( ! $width || ! $height ) { 1315 1340 /* 1316 1341 * If attempts to parse the size value failed, attempt to use the image meta data to match 1317 1342 * the image file name from 'src' against the available sizes for an attachment. 1318 1343 */ 1319 1344 $image_filename = wp_basename( $image_src ); -
src/wp-includes/post.php
function wp_get_attachment_url( $post_id 4867 4867 return false; 4868 4868 4869 4869 if ( 'attachment' != $post->post_type ) 4870 4870 return false; 4871 4871 4872 4872 $url = ''; 4873 4873 // Get attached file. 4874 4874 if ( $file = get_post_meta( $post->ID, '_wp_attached_file', true) ) { 4875 4875 // Get upload directory. 4876 4876 if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { 4877 4877 // Check that the upload base exists in the file location. 4878 4878 if ( 0 === strpos( $file, $uploads['basedir'] ) ) { 4879 4879 // Replace file location with url location. 4880 4880 $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); 4881 4881 } elseif ( false !== strpos($file, 'wp-content/uploads') ) { 4882 $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 ); 4882 // Get the directory name relative to the basedir (back compat for pre-2.7 uploads) 4883 $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . basename( $file ); 4883 4884 } else { 4884 4885 // It's a newly-uploaded file, therefore $file is relative to the basedir. 4885 4886 $url = $uploads['baseurl'] . "/$file"; 4886 4887 } 4887 4888 } 4888 4889 } 4889 4890 4890 4891 /* 4891 4892 * If any of the above options failed, Fallback on the GUID as used pre-2.7, 4892 4893 * not recommended to rely upon this. 4893 4894 */ 4894 4895 if ( empty($url) ) { 4895 4896 $url = get_the_guid( $post->ID ); 4896 4897 } 4897 4898 -
tests/phpunit/tests/media.php
EOF; 854 854 $image_meta['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['file'] ); 855 855 $image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium']['file'] ); 856 856 $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium_large']['file'] ); 857 857 $image_meta['sizes']['large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['large']['file'] ); 858 858 859 859 // Calculate a srcset array. 860 860 $sizes = explode( ', ', wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 861 861 862 862 // Test to confirm all sources in the array include the same edit hash. 863 863 foreach ( $sizes as $size ) { 864 864 $this->assertTrue( false !== strpos( $size, $hash ) ); 865 865 } 866 866 } 867 867 868 868 /** 869 * @ticket 35106 870 */ 871 function test_wp_calculate_image_srcset_with_absolute_path_in_meta() { 872 global $_wp_additional_image_sizes; 873 874 $year_month = date('Y/m'); 875 $image_meta = wp_get_attachment_metadata( self::$large_id ); 876 $uploads_dir_url = 'http://' . WP_TESTS_DOMAIN . '/wp-content/uploads/'; 877 878 // Set up test cases for all expected size names. 879 $intermediates = array( 'medium', 'medium_large', 'large', 'full' ); 880 881 // Add any soft crop intermediate sizes. 882 foreach ( $_wp_additional_image_sizes as $name => $additional_size ) { 883 if ( ! $_wp_additional_image_sizes[$name]['crop'] || 0 === $_wp_additional_image_sizes[$name]['height'] ) { 884 $intermediates[] = $name; 885 } 886 } 887 888 $expected = ''; 889 890 foreach( $image_meta['sizes'] as $name => $size ) { 891 // Whitelist the sizes that should be included so we pick up 'medium_large' in 4.4. 892 if ( in_array( $name, $intermediates ) ) { 893 $expected .= $uploads_dir_url . $year_month . '/' . $size['file'] . ' ' . $size['width'] . 'w, '; 894 } 895 } 896 897 // Add the full size width at the end. 898 $expected .= $uploads_dir_url . $image_meta['file'] . ' ' . $image_meta['width'] .'w'; 899 900 // Prepend an absolute path to simulate a pre-2.7 upload 901 $image_meta['file'] = 'H:\home\wordpress\trunk/wp-content/uploads/' . $image_meta['file']; 902 903 foreach ( $intermediates as $int ) { 904 $image_url = wp_get_attachment_image_url( self::$large_id, $int ); 905 $size_array = $this->_get_image_size_array_from_name( $int ); 906 $this->assertSame( $expected, wp_calculate_image_srcset( $size_array, $image_url, $image_meta ) ); 907 } 908 } 909 910 /** 869 911 * @ticket 33641 870 912 */ 871 913 function test_wp_calculate_image_srcset_false() { 872 914 $sizes = wp_calculate_image_srcset( array( 400, 300 ), 'file.png', array() ); 873 915 874 916 // For canola.jpg we should return 875 917 $this->assertFalse( $sizes ); 876 918 } 877 919 878 920 /** 879 921 * @ticket 33641 880 922 */ 881 923 function test_wp_calculate_image_srcset_no_width() { 882 924 $file = get_attached_file( self::$large_id ); 883 925 $image_url = wp_get_attachment_image_url( self::$large_id, 'medium' );