From f23c1b205b24e377cce240b2fa09525495793919 Mon Sep 17 00:00:00 2001 From: abhishek kumar Date: Mon, 26 Oct 2026 01:39:59 +0530 Subject: [PATCH] Media: Prevent PHP warnings in `wp_get_missing_image_subsizes()` when image dimensions are missing. When the attachment metadata does not contain a `width` or `height` value, for example for SVG files added by plugins, broken uploads, or migrated content, `wp_get_missing_image_subsizes()` would trigger "Undefined array key" warnings. Missing dimensions now fall back to `0`. The return value is unchanged: no sub-sizes are considered possible without both dimensions, so an empty array is still returned and passed through the `wp_get_missing_image_subsizes` filter. Includes unit tests. See #57813. --- src/wp-admin/includes/image.php | 5 +- .../tests/media/wpGetMissingImageSubsizes.php | 149 ++++++++++++++++++ 2 files changed, 152 insertions(+), 2 deletions(-) create mode 100644 tests/phpunit/tests/media/wpGetMissingImageSubsizes.php diff --git a/src/wp-admin/includes/image.php b/src/wp-admin/includes/image.php index a05531c6d9557..fb11f88766771 100644 --- a/src/wp-admin/includes/image.php +++ b/src/wp-admin/includes/image.php @@ -107,8 +107,9 @@ function wp_get_missing_image_subsizes( $attachment_id ) { $full_width = $imagesize[0]; $full_height = $imagesize[1]; } else { - $full_width = (int) $image_meta['width']; - $full_height = (int) $image_meta['height']; + // Missing dimensions fall back to 0, so no sub-sizes are considered possible. + $full_width = isset( $image_meta['width'] ) ? (int) $image_meta['width'] : 0; + $full_height = isset( $image_meta['height'] ) ? (int) $image_meta['height'] : 0; } $possible_sizes = array(); diff --git a/tests/phpunit/tests/media/wpGetMissingImageSubsizes.php b/tests/phpunit/tests/media/wpGetMissingImageSubsizes.php new file mode 100644 index 0000000000000..79d30e557ce75 --- /dev/null +++ b/tests/phpunit/tests/media/wpGetMissingImageSubsizes.php @@ -0,0 +1,149 @@ +attachment->create_object( + array( + 'file' => $file, + 'post_mime_type' => $mime_type, + ) + ); + + wp_update_attachment_metadata( $attachment_id, $meta ); + + return $attachment_id; + } + + /** + * Tests that missing width or height in the image meta does not trigger + * an "Undefined array key" warning, and that no sub-sizes are reported + * as missing when the full-size dimensions are unknown. + * + * @ticket 57813 + * + * @dataProvider data_meta_without_dimensions + * + * @param string $file Attached file name. + * @param string $mime_type Attachment mime type. + * @param array $meta Attachment metadata missing width and/or height. + */ + public function test_should_not_warn_when_dimensions_are_missing( $file, $mime_type, $meta ) { + $attachment_id = $this->create_attachment_with_meta( $file, $mime_type, $meta ); + + $this->assertSame( array(), wp_get_missing_image_subsizes( $attachment_id ) ); + } + + /** + * Data provider. + * + * @return array[] + */ + public function data_meta_without_dimensions() { + return array( + 'svg with no dimensions' => array( + 'file' => 'image.svg', + 'mime_type' => 'image/svg+xml', + 'meta' => array( + 'file' => 'image.svg', + 'filesize' => 1024, + ), + ), + 'jpeg with no dimensions' => array( + 'file' => 'image.jpg', + 'mime_type' => 'image/jpeg', + 'meta' => array( + 'file' => 'image.jpg', + 'sizes' => array(), + ), + ), + 'jpeg with no width' => array( + 'file' => 'image.jpg', + 'mime_type' => 'image/jpeg', + 'meta' => array( + 'file' => 'image.jpg', + 'height' => 900, + 'sizes' => array(), + ), + ), + 'jpeg with no height' => array( + 'file' => 'image.jpg', + 'mime_type' => 'image/jpeg', + 'meta' => array( + 'file' => 'image.jpg', + 'width' => 1200, + 'sizes' => array(), + ), + ), + ); + } + + /** + * Tests that the `wp_get_missing_image_subsizes` filter still runs + * when the image meta lacks width and height. + * + * @ticket 57813 + */ + public function test_should_apply_filter_when_dimensions_are_missing() { + $attachment_id = $this->create_attachment_with_meta( + 'image.jpg', + 'image/jpeg', + array( + 'file' => 'image.jpg', + 'sizes' => array(), + ) + ); + + $filter = new MockAction(); + add_filter( 'wp_get_missing_image_subsizes', array( $filter, 'filter' ), 10, 3 ); + + wp_get_missing_image_subsizes( $attachment_id ); + + $this->assertSame( 1, $filter->get_call_count(), 'The filter should be called once.' ); + + $args = $filter->get_args(); + $this->assertSame( array(), $args[0][0], 'The filter should receive an empty array of missing sizes.' ); + $this->assertSame( $attachment_id, $args[0][2], 'The filter should receive the attachment ID.' ); + } + + /** + * Tests that sub-sizes are still reported as missing when the meta + * has valid dimensions, so the guard for missing dimensions does not + * short-circuit the normal path. + * + * @ticket 57813 + */ + public function test_should_return_missing_sizes_when_dimensions_are_present() { + $attachment_id = $this->create_attachment_with_meta( + 'image.jpg', + 'image/jpeg', + array( + 'file' => 'image.jpg', + 'width' => 1200, + 'height' => 900, + 'sizes' => array(), + ) + ); + + $missing_sizes = wp_get_missing_image_subsizes( $attachment_id ); + + $this->assertNotEmpty( $missing_sizes, 'Sub-sizes should be reported as missing for an image with valid dimensions.' ); + $this->assertArrayHasKey( 'thumbnail', $missing_sizes, 'The thumbnail size should be reported as missing.' ); + } +}