From 01e9de382e2e8cb9f06c06393839db98ee1e3cca Mon Sep 17 00:00:00 2001 From: Andrew Ozz Date: Wed, 4 Nov 2015 00:21:23 +0000 Subject: [PATCH] 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. git-svn-id: https://develop.svn.wordpress.org/trunk@35498 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/media.php | 51 ++++++++++++++++++++--------------- tests/phpunit/tests/media.php | 13 ++++++--- 2 files changed, 39 insertions(+), 25 deletions(-) diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 91a3e52edd..ae36fcb8ef 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -886,14 +886,16 @@ function wp_get_attachment_image_url( $attachment_id, $size = 'thumbnail', $icon * @return string The base URL, cached. */ function _wp_upload_dir_baseurl() { - static $baseurl = null; + static $baseurl = array(); - if ( ! $baseurl ) { + $blog_id = get_current_blog_id(); + + if ( empty( $baseurl[$blog_id] ) ) { $uploads_dir = wp_upload_dir(); - $baseurl = $uploads_dir['baseurl']; + $baseurl[$blog_id] = $uploads_dir['baseurl']; } - return $baseurl; + return $baseurl[$blog_id]; } /** @@ -945,13 +947,13 @@ function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag $image_meta = get_post_meta( $attachment_id, '_wp_attachment_metadata', true ); } - $image_url = $image[0]; + $image_src = $image[0]; $size_array = array( absint( $image[1] ), absint( $image[2] ) ); - return wp_calculate_image_srcset( $image_url, $size_array, $image_meta, $attachment_id ); + return wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attachment_id ); } /** @@ -959,13 +961,13 @@ function wp_get_attachment_image_srcset( $attachment_id, $size = 'medium', $imag * * @since 4.4.0 * - * @param string $image_name The file name, path, URL, or partial path or URL, of the image being matched. + * @param string $image_src The 'src' of the image. * @param array $size_array Array of width and height values in pixels (in that order). * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. * @param int $attachment_id Optional. The image attachment ID to pass to the filter. * @return string|bool The 'srcset' attribute value. False on error or when only one source exists. */ -function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $attachment_id = 0 ) { +function wp_calculate_image_srcset( $image_src, $size_array, $image_meta, $attachment_id = 0 ) { if ( empty( $image_meta['sizes'] ) ) { return false; } @@ -981,20 +983,27 @@ function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta return false; } + $image_basename = wp_basename( $image_meta['file'] ); + $image_baseurl = _wp_upload_dir_baseurl(); + // Add full size to the '$image_sizes' array. $image_sizes['full'] = array( 'width' => $image_meta['width'], 'height' => $image_meta['height'], - 'file' => wp_basename( $image_meta['file'] ), + 'file' => $image_basename, ); - $image_baseurl = _wp_upload_dir_baseurl(); - $dirname = dirname( $image_meta['file'] ); + // Uploads are (or have been) in year/month sub-directories. + if ( $image_basename !== $image_meta['file'] ) { + $dirname = dirname( $image_meta['file'] ); - if ( $dirname !== '.' ) { - $image_baseurl = path_join( $image_baseurl, $dirname ); + if ( $dirname !== '.' ) { + $image_baseurl = trailingslashit( $image_baseurl ) . $dirname; + } } + $image_baseurl = trailingslashit( $image_baseurl ); + // Calculate the image aspect ratio. $image_ratio = $image_height / $image_width; @@ -1003,7 +1012,7 @@ function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta * contain a unique hash. Look for that hash and use it later to filter * out images that are leftovers from previous versions. */ - $image_edited = preg_match( '/-e[0-9]{13}/', $image_name, $image_edit_hash ); + $image_edited = preg_match( '/-e[0-9]{13}/', wp_basename( $image_src ), $image_edit_hash ); /** * Filter the maximum image width to be included in a 'srcset' attribute. @@ -1034,8 +1043,6 @@ function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta continue; } - $candidate_url = $image['file']; - // Calculate the new image ratio. if ( $image['width'] ) { $image_ratio_compare = $image['height'] / $image['width']; @@ -1044,10 +1051,10 @@ function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta } // If the new ratio differs by less than 0.01, use it. - if ( abs( $image_ratio - $image_ratio_compare ) < 0.01 && ! array_key_exists( $candidate_url, $sources ) ) { + if ( abs( $image_ratio - $image_ratio_compare ) < 0.01 ) { // Add the URL, descriptor, and value to the sources array to be returned. $sources[ $image['width'] ] = array( - 'url' => path_join( $image_baseurl, $candidate_url ), + 'url' => $image_baseurl . $image['file'], 'descriptor' => 'w', 'value' => $image['width'], ); @@ -1100,11 +1107,11 @@ function wp_calculate_image_srcset( $image_name, $size_array, $image_meta, $atta * @param array $image_meta Optional. The image meta data as returned by 'wp_get_attachment_metadata()'. * @param int $attachment_id Optional. Image attachment ID. Either `$image_meta` or `$attachment_id` is needed * when using the image size name as argument for `$size`. - * @param string $image_url Optional. The URL to the image file. + * @param string $image_src Optional. The URL to the image file. * * @return string|bool A valid source size value for use in a 'sizes' attribute or false. */ -function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $image_url = null ) { +function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_id = 0, $image_src = null ) { $width = 0; if ( is_array( $size ) ) { @@ -1139,9 +1146,9 @@ function wp_get_attachment_image_sizes( $size, $image_meta = null, $attachment_i * values in pixels (in that order). * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. * @param int $attachment_id Image attachment ID of the original image. - * @param string $image_url Optional. The URL to the image file. + * @param string $image_src Optional. The URL to the image file. */ - return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $image_url ); + return apply_filters( 'wp_get_attachment_image_sizes', $sizes, $size, $image_meta, $attachment_id, $image_src ); } /** diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index aa39a5e928..627d251413 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -785,10 +785,13 @@ EOF; foreach ( $sizes as $size ) { $size_array = $this->_get_image_size_array_from_name( $size ); - $image_url = wp_get_attachment_image_url( self::$large_id, $size ); + $image_url = wp_get_attachment_image_url( $id, $size ); $this->assertSame( $expected, wp_calculate_image_srcset( $image_url, $size_array, $image_meta ) ); } + // Remove the attachment + wp_delete_attachment( $id ); + // Leave the uploads option the way you found it. update_option( 'uploads_use_yearmonth_folders', $uploads_use_yearmonth_folders ); } @@ -800,14 +803,18 @@ EOF; // For this test we're going to mock metadata changes from an edit. // Start by getting the attachment metadata. $image_meta = wp_get_attachment_metadata( self::$large_id ); - $image_url = wp_get_attachment_image_url( self::$large_id ); + $image_url = wp_get_attachment_image_url( self::$large_id, 'medium' ); $size_array = $this->_get_image_size_array_from_name( 'medium' ); // Copy hash generation method used in wp_save_image(). $hash = 'e' . time() . rand(100, 999); - // Replace file paths for full and medium sizes with hashed versions. $filename_base = basename( $image_meta['file'], '.png' ); + + // Add the hash to the image URL + $image_url = str_replace( $filename_base, $filename_base . '-' . $hash, $image_url ); + + // Replace file paths for full and medium sizes with hashed versions. $image_meta['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['file'] ); $image_meta['sizes']['medium']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium']['file'] ); $image_meta['sizes']['medium_large']['file'] = str_replace( $filename_base, $filename_base . '-' . $hash, $image_meta['sizes']['medium_large']['file'] );