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
This commit is contained in:
Andrew Ozz 2015-12-30 00:06:28 +00:00
parent 09688bd59c
commit 597bbf0318
3 changed files with 33 additions and 7 deletions

View File

@ -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 ) ) {

View File

@ -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";

View File

@ -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.