From 3061a680a9c038ce51dcb216475fc1ac91e50a04 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 21 Jul 2020 16:55:08 +0000 Subject: [PATCH] Media: Pass the attachment ID to the `wp_image_file_matches_image_meta` filter. Props spacedmonkey. Fixes #50722. Built from https://develop.svn.wordpress.org/trunk@48547 git-svn-id: http://core.svn.wordpress.org/trunk@48309 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 25 +++++++++++-------- .../class-wp-rest-attachments-controller.php | 2 +- wp-includes/version.php | 2 +- 3 files changed, 16 insertions(+), 13 deletions(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index c25ac99ed9..8a3844e75e 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -1507,11 +1507,12 @@ function wp_calculate_image_sizes( $size, $image_src = null, $image_meta = null, * * @since 5.5.0 * - * @param string $image_location The full path or URI to the image file. - * @param array $image_meta The attachment meta data as returned by 'wp_get_attachment_metadata()'. + * @param string $image_location The full path or URI to the image file. + * @param array $image_meta The attachment meta data as returned by 'wp_get_attachment_metadata()'. + * @param int $attachment_id Optional. The image attachment ID. Default 0. * @return bool Whether the image meta is for this image file. */ -function wp_image_file_matches_image_meta( $image_location, $image_meta ) { +function wp_image_file_matches_image_meta( $image_location, $image_meta, $attachment_id = 0 ) { $match = false; // Ensure the $image_meta is valid. @@ -1559,9 +1560,10 @@ function wp_image_file_matches_image_meta( $image_location, $image_meta ) { * @param bool $match Whether the image relative path from the image meta * matches the end of the URI or path to the image file. * @param string $image_location Full path or URI to the tested image file. - * @param array $image_meta The image meta data being tested. + * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. + * @param int $attachment_id The image attachment ID or 0 if not supplied. */ - return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta ); + return apply_filters( 'wp_image_file_matches_image_meta', $match, $image_location, $image_meta, $attachment_id ); } /** @@ -1569,13 +1571,14 @@ function wp_image_file_matches_image_meta( $image_location, $image_meta ) { * * @since 5.5.0 * - * @param string $image_src The image source file. - * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. + * @param string $image_src The image source file. + * @param array $image_meta The image meta data as returned by 'wp_get_attachment_metadata()'. + * @param int $attachment_id Optional. The image attachment ID. Default 0. * @return array|false Array with first element being the width and second element being the height, * or false if dimensions cannot be determined. */ -function wp_image_src_get_dimensions( $image_src, $image_meta ) { - if ( ! wp_image_file_matches_image_meta( $image_src, $image_meta ) ) { +function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = 0 ) { + if ( ! wp_image_file_matches_image_meta( $image_src, $image_meta, $attachment_id ) ) { return false; } @@ -1643,7 +1646,7 @@ function wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ) { if ( $width && $height ) { $size_array = array( $width, $height ); } else { - $size_array = wp_image_src_get_dimensions( $image_src, $image_meta ); + $size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id ); if ( ! $size_array ) { return $image; } @@ -1863,7 +1866,7 @@ function wp_img_tag_add_width_and_height_attr( $image, $context, $attachment_id if ( true === $add ) { $image_meta = wp_get_attachment_metadata( $attachment_id ); - $size_array = wp_image_src_get_dimensions( $image_src, $image_meta ); + $size_array = wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id ); if ( $size_array ) { $hw = trim( image_hwstring( $size_array[0], $size_array[1] ) ); diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php index f5468c256d..5b2abc794a 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php @@ -424,7 +424,7 @@ class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller { if ( ! $image_meta || ! $image_file || - ! wp_image_file_matches_image_meta( $request['src'], $image_meta ) + ! wp_image_file_matches_image_meta( $request['src'], $image_meta, $attachment_id ) ) { return new WP_Error( 'rest_unknown_attachment', diff --git a/wp-includes/version.php b/wp-includes/version.php index 8b7b41039e..5c6ff7f859 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.5-beta2-48546'; +$wp_version = '5.5-beta2-48547'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.