From 5ca780edb16c42aaa01f89534c0caae22f11f254 Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Tue, 2 Feb 2021 02:59:05 +0000 Subject: [PATCH] Media: Add filter to wp_image_src_get_dimensions. This adds a new filter, `wp_image_src_get_dimensions` to the `wp_image_src_get_dimensions()` function to correct the dimensions returned for a file whenever WordPress isn't able to correctly get the dimensions from attachment metadata. Fixes #51865. Built from https://develop.svn.wordpress.org/trunk@50134 git-svn-id: http://core.svn.wordpress.org/trunk@49813 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/media.php | 27 ++++++++++++++++++++------- wp-includes/version.php | 2 +- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/wp-includes/media.php b/wp-includes/media.php index d29426213e..42b3e23363 100644 --- a/wp-includes/media.php +++ b/wp-includes/media.php @@ -1599,32 +1599,45 @@ function wp_image_file_matches_image_meta( $image_location, $image_meta, $attach * or false if dimensions cannot be determined. */ 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; - } + $dimensions = false; // Is it a full size image? if ( strpos( $image_src, $image_meta['file'] ) !== false ) { - return array( + $dimensions = array( (int) $image_meta['width'], (int) $image_meta['height'], ); } - if ( ! empty( $image_meta['sizes'] ) ) { + if ( ! $dimensions && ! empty( $image_meta['sizes'] ) ) { $src_filename = wp_basename( $image_src ); foreach ( $image_meta['sizes'] as $image_size_data ) { if ( $src_filename === $image_size_data['file'] ) { - return array( + $dimensions = array( (int) $image_size_data['width'], (int) $image_size_data['height'], ); + + break; } } } - return false; + /** + * Filter the 'wp_image_src_get_dimensions' value. + * + * @since 5.7.0 + * + * @param array|false $dimensions Array with first element being the width + * and second element being the height, or + * false if dimensions could not be determined. + * @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 The image attachment ID. Default 0. + */ + return apply_filters( 'wp_image_src_get_dimensions', $dimensions, $image_src, $image_meta, $attachment_id ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index ebcac711dc..3b52bfca0b 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.7-alpha-50133'; +$wp_version = '5.7-alpha-50134'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.