From 597bbf03184ccf30a0a7ad6a18049c05ca944819 Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Wed, 30 Dec 2015 00:06:28 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@36120 git-svn-id: http://core.svn.wordpress.org/trunk@36086 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 35 ++++++++++++++++++++++++++++++----- wp-includes/post.php | 3 ++- wp-includes/version.php | 2 +- 3 files changed, 33 insertions(+), 7 deletions(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index d200d4ee91..9428b7e050 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -877,6 +877,31 @@ function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon return isset( $image['0'] ) ? $image['0'] : false; } +/** + * Get the attachment path relative to the upload directory. + * + * @since 4.4.1 + * @access private + * + * @param string $file Attachment file name. + * @return string Attachment path relative to the upload directory. + */ +function _wp_get_attachment_relative_path( $file ) { + $dirname = dirname( $file ); + + if ( '.' === $dirname ) { + return ''; + } + + if ( false !== strpos( $dirname, 'wp-content/uploads' ) ) { + // Get the directory name relative to the upload directory (back compat for pre-2.7 uploads) + $dirname = substr( $dirname, strpos( $dirname, 'wp-content/uploads' ) + 18 ); + $dirname = ltrim( $dirname, '/' ); + } + + return $dirname; +} + /** * Caches and returns the base URL of the uploads directory. * @@ -1006,9 +1031,9 @@ function wp_calculate_image_srcset( $size_array, $image_src, $image_meta, $attac // Uploads are (or have been) in year/month sub-directories. if ( $image_basename !== $image_meta['file'] ) { - $dirname = dirname( $image_meta['file'] ); + $dirname = _wp_get_attachment_relative_path( $image_meta['file'] ); - if ( $dirname !== '.' ) { + if ( $dirname ) { $image_baseurl = trailingslashit( $image_baseurl ) . $dirname; } } @@ -1289,8 +1314,8 @@ function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { $base_url = trailingslashit( _wp_upload_dir_baseurl() ); $image_base_url = $base_url; - $dirname = dirname( $image_meta['file'] ); - if ( $dirname !== '.' ) { + $dirname = _wp_get_attachment_relative_path( $image_meta['file'] ); + if ( $dirname ) { $image_base_url .= trailingslashit( $dirname ); } @@ -1301,7 +1326,7 @@ function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { } // Add the original image. - $all_sizes[] = $base_url . $image_meta['file']; + $all_sizes[] = $image_base_url . basename( $image_meta['file'] ); // Bail early if the image src doesn't match any of the known image sizes. if ( ! in_array( $image_src, $all_sizes ) ) { diff --git a/wp-includes/post.php b/wp-includes/post.php index 0fb1dfa348..bf60e5b156 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -4879,7 +4879,8 @@ function wp_get_attachment_url( $post_id = 0 ) { // Replace file location with url location. $url = str_replace($uploads['basedir'], $uploads['baseurl'], $file); } elseif ( false !== strpos($file, 'wp-content/uploads') ) { - $url = $uploads['baseurl'] . substr( $file, strpos($file, 'wp-content/uploads') + 18 ); + // Get the directory name relative to the basedir (back compat for pre-2.7 uploads) + $url = trailingslashit( $uploads['baseurl'] . '/' . _wp_get_attachment_relative_path( $file ) ) . basename( $file ); } else { // It's a newly-uploaded file, therefore $file is relative to the basedir. $url = $uploads['baseurl'] . "/$file"; diff --git a/wp-includes/version.php b/wp-includes/version.php index 6031a3a021..8f18c1ef4d 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.5-alpha-36119'; +$wp_version = '4.5-alpha-36120'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.