Make WordPress Core

Changeset 35498


Ignore:
Timestamp:
11/04/2015 12:21:23 AM (9 years ago)
Author:
azaozz
Message:

Responsive images:

  • Fix _wp_upload_dir_baseurl() to cache by blog_id.
  • Replace path_join() with trailingslashit(), it's much faster.
  • Rename $image_url to $image_src for consistency (used at about 50 other places).
  • Couple of tests fixes.

See #34430.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/media.php

    r35491 r35498  
    887887 */
    888888function _wp_upload_dir_baseurl() {
    889     static $baseurl = null;
    890 
    891     if ( ! $baseurl ) {
     889    static $baseurl = array();
     890
     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'];
    894     }
    895 
    896     return $baseurl;
     895        $baseurl[$blog_id] = $uploads_dir['baseurl'];
     896    }
     897
     898    return $baseurl[$blog_id];
    897899}
    898900
     
    946948    }
    947949
    948     $image_url = $image[0];
     950    $image_src = $image[0];
    949951    $size_array = array(
    950952        absint( $image[1] ),
     
    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
     
    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()'.
     
    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;
     
    981983        return false;
    982984    }
     985
     986    $image_basename = wp_basename( $image_meta['file'] );
     987    $image_baseurl = _wp_upload_dir_baseurl();
    983988
    984989    // Add full size to the '$image_sizes' 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'] );
    993 
    994     if ( $dirname !== '.' ) {
    995         $image_baseurl = path_join( $image_baseurl, $dirname );
    996     }
     996    // Uploads are (or have been) in year/month sub-directories.
     997    if ( $image_basename !== $image_meta['file'] ) {
     998        $dirname = dirname( $image_meta['file'] );
     999
     1000        if ( $dirname !== '.' ) {
     1001            $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
     1002        }
     1003    }
     1004
     1005    $image_baseurl = trailingslashit( $image_baseurl );
    9971006
    9981007    // Calculate the image aspect ratio.
     
    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    /**
     
    10341043            continue;
    10351044        }
    1036 
    1037         $candidate_url = $image['file'];
    10381045
    10391046        // Calculate the new image ratio.
     
    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'],
     
    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
     
    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.
    1143      */
    1144     return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $image_url );
     1149     * @param string       $image_src     Optional. The URL to the image file.
     1150     */
     1151    return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $image_src );
    11451152}
    11461153
  • trunk/tests/phpunit/tests/media.php

    r35479 r35498  
    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        }
     791
     792        // Remove the attachment
     793        wp_delete_attachment( $id );
    791794
    792795        // Leave the uploads option the way you found it.
     
    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
     
    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'] );
Note: See TracChangeset for help on using the changeset viewer.