Ticket #34430: 34430.11.diff
File 34430.11.diff, 7.6 KB (added by , 9 years ago) |
---|
-
src/wp-includes/media.php
886 886 * @return string The base URL, cached. 887 887 */ 888 888 function _wp_upload_dir_baseurl() { 889 static $baseurl = null;889 static $baseurl = array(); 890 890 891 if ( ! $baseurl ) { 891 $blog_id = get_current_blog_id(); 892 893 if ( empty( $baseurl[$blog_id] ) ) { 892 894 $uploads_dir = wp_upload_dir(); 893 $baseurl = $uploads_dir['baseurl'];895 $baseurl[$blog_id] = $uploads_dir['baseurl']; 894 896 } 895 897 896 return $baseurl ;898 return $baseurl[$blog_id]; 897 899 } 898 900 899 901 /** … … 945 947 $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); 946 948 } 947 949 948 $image_ url= $image[0];950 $image_src = $image[0]; 949 951 $size_array = array( 950 952 absint( $image[1] ), 951 953 absint( $image[2] ) 952 954 ); 953 955 954 return wp_calculate_image_srcset( $image_ url, $size_array, $image_meta, $attachment_id );956 return wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attachment_id ); 955 957 } 956 958 957 959 /** … … 959 961 * 960 962 * @since 4.4.0 961 963 * 962 * @param string $image_ name The file name, path, URL, or partial path or URL, of the image being matched.964 * @param string $image_src The 'src' of the image. 963 965 * @param array $size_array Array of width and height values in pixels (in that order). 964 966 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 965 967 * @param int $attachment_id Optional. The image attachment ID to pass to the filter. 966 968 * @return string|bool The 'srcset' attribute value. False on error or when only one source exists. 967 969 */ 968 function wp_calculate_image_srcset( $image_ name, $size_array, $image_meta, $attachment_id = 0 ) {970 function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attachment_id = 0 ) { 969 971 if ( empty( $image_meta['sizes'] ) ) { 970 972 return false; 971 973 } … … 981 983 return false; 982 984 } 983 985 986 $image_basename = wp_basename( $image_meta['file'] ); 987 $image_baseurl = _wp_upload_dir_baseurl(); 988 984 989 // Add full size to the '$image_sizes' array. 985 990 $image_sizes['full'] = array( 986 991 'width' => $image_meta['width'], 987 992 'height' => $image_meta['height'], 988 'file' => wp_basename( $image_meta['file'] ),993 'file' => $image_basename, 989 994 ); 990 995 991 $image_baseurl = _wp_upload_dir_baseurl(); 992 $dirname = dirname( $image_meta['file'] ); 996 // Uploads are (or have been) in year/month sub-directories. 997 if ( $image_basename !== $image_meta['file'] ) { 998 $dirname = dirname( $image_meta['file'] ); 993 999 994 if ( $dirname !== '.' ) { 995 $image_baseurl = path_join( $image_baseurl, $dirname ); 1000 if ( $dirname !== '.' ) { 1001 $image_baseurl = trailingslashit( $image_baseurl ) . $dirname; 1002 } 996 1003 } 997 1004 1005 $image_baseurl = trailingslashit( $image_baseurl ); 1006 998 1007 // Calculate the image aspect ratio. 999 1008 $image_ratio = $image_height / $image_width; 1000 1009 … … 1003 1012 * contain a unique hash. Look for that hash and use it later to filter 1004 1013 * out images that are leftovers from previous versions. 1005 1014 */ 1006 $image_edited = preg_match( '/-e[0-9]{13}/', $image_name, $image_edit_hash );1015 $image_edited = preg_match( '/-e[0-9]{13}/', wp_basename( $image_src ), $image_edit_hash ); 1007 1016 1008 1017 /** 1009 1018 * Filter the maximum image width to be included in a 'srcset' attribute. … … 1034 1043 continue; 1035 1044 } 1036 1045 1037 $candidate_url = $image['file'];1038 1039 1046 // Calculate the new image ratio. 1040 1047 if ( $image['width'] ) { 1041 1048 $image_ratio_compare = $image['height'] / $image['width']; … … 1044 1051 } 1045 1052 1046 1053 // If the new ratio differs by less than 0.01, use it. 1047 if ( abs( $image_ratio - $image_ratio_compare ) < 0.01 && ! array_key_exists( $candidate_url, $sources )) {1054 if ( abs( $image_ratio - $image_ratio_compare ) < 0.01 ) { 1048 1055 // Add the URL, descriptor, and value to the sources array to be returned. 1049 1056 $sources[ $image['width'] ] = array( 1050 'url' => path_join( $image_baseurl, $candidate_url ),1057 'url' => $image_baseurl . $image['file'], 1051 1058 'descriptor' => 'w', 1052 1059 'value' => $image['width'], 1053 1060 ); … … 1100 1107 * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. 1101 1108 * @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` is needed 1102 1109 * when using the image size name as argument for `$size`. 1103 * @param string $image_ urlOptional. The URL to the image file.1110 * @param string $image_src Optional. The URL to the image file. 1104 1111 * 1105 1112 * @return string|bool A valid source size value for use in a 'sizes' attribute or false. 1106 1113 */ 1107 function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $image_ url= null ) {1114 function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $image_src = null ) { 1108 1115 $width = 0; 1109 1116 1110 1117 if ( is_array( $size ) ) { … … 1139 1146 * values in pixels (in that order). 1140 1147 * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. 1141 1148 * @param int $attachment_id Image attachment ID of the original image. 1142 * @param string $image_ urlOptional. The URL to the image file.1149 * @param string $image_src Optional. The URL to the image file. 1143 1150 */ 1144 return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $image_ url);1151 return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $image_src ); 1145 1152 } 1146 1153 1147 1154 /** -
tests/phpunit/tests/media.php
785 785 786 786 foreach ( $sizes as $size ) { 787 787 $size_array = $this->_get_image_size_array_from_name( $size ); 788 $image_url = wp_get_attachment_image_url( self::$large_id, $size );788 $image_url = wp_get_attachment_image_url( $id, $size ); 789 789 $this->assertSame( $expected, wp_calculate_image_srcset( $image_url, $size_array, $image_meta ) ); 790 790 } 791 791 792 // Remove the attachment 793 wp_delete_attachment( $id ); 794 792 795 // Leave the uploads option the way you found it. 793 796 update_option( 'uploads_use_yearmonth_folders', $uploads_use_yearmonth_folders ); 794 797 } … … 800 803 // For this test we're going to mock metadata changes from an edit. 801 804 // Start by getting the attachment metadata. 802 805 $image_meta = wp_get_attachment_metadata( self::$large_id ); 803 $image_url = wp_get_attachment_image_url( self::$large_id );806 $image_url = wp_get_attachment_image_url( self::$large_id, 'medium' ); 804 807 $size_array = $this->_get_image_size_array_from_name( 'medium' ); 805 808 806 809 // Copy hash generation method used in wp_save_image(). 807 810 $hash = 'e' . time() . rand(100, 999); 808 811 812 $filename_base = basename( $image_meta['file'], '.png' ); 813 814 // Add the hash to the image URL 815 $image_url = str_replace( $filename_base, $filename_base . '-' . $hash, $image_url ); 816 809 817 // Replace file paths for full and medium sizes with hashed versions. 810 $filename_base = basename( $image_meta['file'], '.png' );811 818 $image_meta['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['file'] ); 812 819 $image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium']['file'] ); 813 820 $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium_large']['file'] );