WordPress.org

Make WordPress Core

Ticket #34430: 34430.11.diff

File 34430.11.diff, 7.6 KB (added by azaozz, 5 years ago)
  • src/wp-includes/media.php

     
    886886 * @return string The base URL, cached.
    887887 */
    888888function _wp_upload_dir_baseurl() {
    889         static $baseurl = null;
     889        static $baseurl = array();
    890890
    891         if ( ! $baseurl ) {
     891        $blog_id = get_current_blog_id();
     892
     893        if ( empty( $baseurl[$blog_id] ) ) {
    892894                $uploads_dir = wp_upload_dir();
    893                 $baseurl = $uploads_dir['baseurl'];
     895                $baseurl[$blog_id] = $uploads_dir['baseurl'];
    894896        }
    895897
    896         return $baseurl;
     898        return $baseurl[$blog_id];
    897899}
    898900
    899901/**
     
    945947                $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true );
    946948        }
    947949
    948         $image_url = $image[0];
     950        $image_src = $image[0];
    949951        $size_array = array(
    950952                absint( $image[1] ),
    951953                absint( $image[2] )
    952954        );
    953955
    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 );
    955957}
    956958
    957959/**
     
    959961 *
    960962 * @since 4.4.0
    961963 *
    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.
    963965 * @param array  $size_array    Array of width and height values in pixels (in that order).
    964966 * @param array  $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    965967 * @param int    $attachment_id Optional. The image attachment ID to pass to the filter.
    966968 * @return string|bool          The 'srcset' attribute value. False on error or when only one source exists.
    967969 */
    968 function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $attachment_id = 0 ) {
     970function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attachment_id = 0 ) {
    969971        if ( empty( $image_meta['sizes'] ) ) {
    970972                return false;
    971973        }
     
    981983                return false;
    982984        }
    983985
     986        $image_basename = wp_basename( $image_meta['file'] );
     987        $image_baseurl = _wp_upload_dir_baseurl();
     988
    984989        // Add full size to the '$image_sizes' array.
    985990        $image_sizes['full'] = array(
    986991                'width'  => $image_meta['width'],
    987992                'height' => $image_meta['height'],
    988                 'file'   => wp_basename( $image_meta['file'] ),
     993                'file'   => $image_basename,
    989994        );
    990995
    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'] );
    993999
    994         if ( $dirname !== '.' ) {
    995                 $image_baseurl = path_join( $image_baseurl, $dirname );
     1000                if ( $dirname !== '.' ) {
     1001                        $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
     1002                }
    9961003        }
    9971004
     1005        $image_baseurl = trailingslashit( $image_baseurl );
     1006
    9981007        // Calculate the image aspect ratio.
    9991008        $image_ratio = $image_height / $image_width;
    10001009
     
    10031012         * contain a unique hash. Look for that hash and use it later to filter
    10041013         * out images that are leftovers from previous versions.
    10051014         */
    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 );
    10071016
    10081017        /**
    10091018         * Filter the maximum image width to be included in a 'srcset' attribute.
     
    10341043                        continue;
    10351044                }
    10361045
    1037                 $candidate_url = $image['file'];
    1038 
    10391046                // Calculate the new image ratio.
    10401047                if ( $image['width'] ) {
    10411048                        $image_ratio_compare = $image['height'] / $image['width'];
     
    10441051                }
    10451052
    10461053                // 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 ) {
    10481055                        // Add the URL, descriptor, and value to the sources array to be returned.
    10491056                        $sources[ $image['width'] ] = array(
    1050                                 'url'        => path_join( $image_baseurl, $candidate_url ),
     1057                                'url'        => $image_baseurl . $image['file'],
    10511058                                'descriptor' => 'w',
    10521059                                'value'      => $image['width'],
    10531060                        );
     
    11001107 * @param array        $image_meta    Optional. The image meta data as returned by 'wp_get_attachment_metadata()'.
    11011108 * @param int          $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` is needed
    11021109 *                                    when using the image size name as argument for `$size`.
    1103  * @param string       $image_url     Optional. The URL to the image file.
     1110 * @param string       $image_src     Optional. The URL to the image file.
    11041111 *
    11051112 * @return string|bool A valid source size value for use in a 'sizes' attribute or false.
    11061113 */
    1107 function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $image_url = null ) {
     1114function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $image_src = null ) {
    11081115        $width = 0;
    11091116
    11101117        if ( is_array( $size ) ) {
     
    11391146         *                                    values in pixels (in that order).
    11401147         * @param array        $image_meta    The image meta data as returned by 'wp_get_attachment_metadata()'.
    11411148         * @param int          $attachment_id Image attachment ID of the original image.
    1142          * @param string       $image_url     Optional. The URL to the image file.
     1149         * @param string       $image_src     Optional. The URL to the image file.
    11431150         */
    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 );
    11451152}
    11461153
    11471154/**
  • tests/phpunit/tests/media.php

     
    785785
    786786                foreach ( $sizes as $size ) {
    787787                        $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 );
    789789                        $this->assertSame( $expected, wp_calculate_image_srcset( $image_url, $size_array, $image_meta ) );
    790790                }
    791791
     792                // Remove the attachment
     793                wp_delete_attachment( $id );
     794
    792795                // Leave the uploads option the way you found it.
    793796                update_option( 'uploads_use_yearmonth_folders', $uploads_use_yearmonth_folders );
    794797        }
     
    800803                // For this test we're going to mock metadata changes from an edit.
    801804                // Start by getting the attachment metadata.
    802805                $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' );
    804807                $size_array = $this->_get_image_size_array_from_name( 'medium' );
    805808
    806809                // Copy hash generation method used in wp_save_image().
    807810                $hash = 'e' . time() . rand(100, 999);
    808811
     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
    809817                // Replace file paths for full and medium sizes with hashed versions.
    810                 $filename_base = basename( $image_meta['file'], '.png' );
    811818                $image_meta['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['file'] );
    812819                $image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium']['file'] );
    813820                $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium_large']['file'] );