WordPress.org

Make WordPress Core

Changeset 36120


Ignore:
Timestamp:
12/30/2015 12:05:07 AM (5 years ago)
Author:
azaozz
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().

Props dd32, SergeyBiryukov.
Fixes #35106 for trunk.

Location:
trunk
Files:
3 edited

Legend:

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

    r36110 r36120  
    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        }
     
    12901315    $image_base_url = $base_url;
    12911316
    1292     $dirname = dirname( $image_meta['file'] );
    1293     if ( $dirname !== '.' ) {
     1317    $dirname = _wp_get_attachment_relative_path( $image_meta['file'] );
     1318    if ( $dirname ) {
    12941319        $image_base_url .= trailingslashit( $dirname );
    12951320    }
     
    13021327
    13031328    // Add the original image.
    1304     $all_sizes[] = $base_url . $image_meta['file'];
     1329    $all_sizes[] = $image_base_url . basename( $image_meta['file'] );
    13051330
    13061331    // Bail early if the image src doesn't match any of the known image sizes.
  • trunk/src/wp-includes/post.php

    r36094 r36120  
    48804880                $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file);
    48814881            } 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 );
    48834884            } else {
    48844885                // It's a newly-uploaded file, therefore $file is relative to the basedir.
  • trunk/tests/phpunit/tests/media.php

    r36110 r36120  
    864864            $this->assertTrue( false !== strpos( $size, $hash ) );
    865865        }
     866    }
     867
     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        }
    866908    }
    867909
Note: See TracChangeset for help on using the changeset viewer.