From f30ac58044399f43b5d882409bf86f24f36dc187 Mon Sep 17 00:00:00 2001 From: abhishek kumar Date: Mon, 26 Oct 2026 02:17:20 +0530 Subject: [PATCH] Media: Avoid undefined array key warnings in wp_image_src_get_dimensions(). When the attachment meta for a full size image has a `file` key but is missing `width` or `height` (for example, metadata written by SVG plugins, importers, or custom upload handling), the function read those keys unguarded, raising "Undefined array key" warnings and returning `array( 0, 0 )`. The full size check now also requires `width` and `height` to be set, so the function returns `false` in this case, as its documented return value specifies. --- src/wp-includes/media.php | 2 +- .../tests/media/wpImageSrcGetDimensions.php | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/media/wpImageSrcGetDimensions.php diff --git a/src/wp-includes/media.php b/src/wp-includes/media.php index 3c675dd05d957..d5ed29d67ec43 100644 --- a/src/wp-includes/media.php +++ b/src/wp-includes/media.php @@ -1818,7 +1818,7 @@ function wp_image_src_get_dimensions( $image_src, $image_meta, $attachment_id = // Is it a full size image? if ( - isset( $image_meta['file'] ) && + isset( $image_meta['file'], $image_meta['width'], $image_meta['height'] ) && str_contains( $image_src, wp_basename( $image_meta['file'] ) ) ) { $dimensions = array( diff --git a/tests/phpunit/tests/media/wpImageSrcGetDimensions.php b/tests/phpunit/tests/media/wpImageSrcGetDimensions.php new file mode 100644 index 0000000000000..f3e107a149e34 --- /dev/null +++ b/tests/phpunit/tests/media/wpImageSrcGetDimensions.php @@ -0,0 +1,95 @@ +attachment->create_object( + array( + 'file' => '2024/10/image.svg', + 'post_mime_type' => 'image/svg+xml', + ) + ); + } + + /** + * Tests that `wp_image_src_get_dimensions()` returns the full size dimensions from the image meta. + * + * @ticket 62312 + */ + public function test_should_return_full_size_dimensions() { + wp_update_attachment_metadata( + self::$attachment_id, + array( + 'file' => '2024/10/image.svg', + 'width' => 640, + 'height' => 480, + ) + ); + + $this->assertSame( + array( 640, 480 ), + wp_image_src_get_dimensions( + 'http://example.org/wp-content/uploads/2024/10/image.svg', + wp_get_attachment_metadata( self::$attachment_id ), + self::$attachment_id + ) + ); + } + + /** + * Tests that `wp_image_src_get_dimensions()` returns false without raising warnings + * when the full size image meta is missing width or height. + * + * @ticket 62312 + * + * @dataProvider data_should_return_false_when_full_size_dimensions_are_missing + * + * @param array $image_meta The image meta data. + */ + public function test_should_return_false_when_full_size_dimensions_are_missing( $image_meta ) { + wp_update_attachment_metadata( self::$attachment_id, $image_meta ); + + $this->assertFalse( + wp_image_src_get_dimensions( + 'http://example.org/wp-content/uploads/2024/10/image.svg', + wp_get_attachment_metadata( self::$attachment_id ), + self::$attachment_id + ) + ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_should_return_false_when_full_size_dimensions_are_missing() { + return array( + 'width and height missing' => array( + 'image_meta' => array( + 'file' => '2024/10/image.svg', + ), + ), + 'width missing' => array( + 'image_meta' => array( + 'file' => '2024/10/image.svg', + 'height' => 480, + ), + ), + 'height missing' => array( + 'image_meta' => array( + 'file' => '2024/10/image.svg', + 'width' => 640, + ), + ), + ); + } +}