Make WordPress Core


Ignore:
Timestamp:
01/02/2016 03:53:21 AM (10 years ago)
Author:
dd32
Message:

Responsive images: add compatibility for versions < 2.7 when the full image path was stored in the metadata. Introduces _wp_get_attachment_relative_path() and uses it in wp_get_attachment_url().

Merges [36120] to the 4.4 branch.
Props dd32, SergeyBiryukov.
Fixes #35106.

Location:
branches/4.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.4

  • branches/4.4/src/wp-includes/media.php

    r36150 r36151  
    879879
    880880/**
     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 */
     889function _wp_get_attachment_relative_path( $file ) {
     890    $dirname = dirname( $file );
     891
     892    if ( '.' === $dirname ) {
     893        return '';
     894    }
     895
     896    if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) {
     897        // Get the directory name relative to the upload directory (back compat for pre-2.7 uploads)
     898        $dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 );
     899        $dirname = ltrim( $dirname, '/' );
     900    }
     901
     902    return $dirname;
     903}
     904
     905/**
    881906 * Caches and returns the base URL of the uploads directory.
    882907 *
     
    10071032    // Uploads are (or have been) in year/month sub-directories.
    10081033    if ( $image_basename !== $image_meta['file'] ) {
    1009         $dirname = dirname( $image_meta['file'] );
    1010 
    1011         if ( $dirname !== '.' ) {
     1034        $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
     1035
     1036        if ( $dirname ) {
    10121037            $image_baseurl = trailingslashit( $image_baseurl ) . $dirname;
    10131038        }
     
    12871312    $image_base_url = $base_url;
    12881313
    1289     $dirname = dirname( $image_meta['file'] );
    1290     if ( $dirname !== '.' ) {
     1314    $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
     1315    if ( $dirname ) {
    12911316        $image_base_url .= trailingslashit( $dirname );
    12921317    }
     
    12991324
    13001325    // Add the original image.
    1301     $all_sizes[] = $base_url . $image_meta['file'];
     1326    $all_sizes[] = $image_base_url . basename( $image_meta['file'] );
    13021327
    13031328    // Bail early if the image src doesn't match any of the known image sizes.
Note: See TracChangeset for help on using the changeset viewer.