Image editor: fatal error when attachment metadata has no sizes array - #12744
Image editor: fatal error when attachment metadata has no sizes array#12744josephscott wants to merge 1 commit into
sizes array#12744Conversation
|
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Unlinked AccountsThe following contributors have not linked their GitHub and WordPress.org accounts: @nazmulasif. Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases. Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
There was a problem hiding this comment.
Pull request overview
Fixes a fatal error path in the image editor when an attachment’s metadata lacks a usable sizes array, ensuring wp_save_image() / wp_restore_image() can proceed without type errors and adding regression coverage in the Ajax image editor test suite.
Changes:
- Normalize
$meta['sizes']to an array inwp_save_image()before it’s passed toarray_merge(). - Normalize
$metaand$meta['sizes']inwp_restore_image()to avoid “Cannot use a scalar value as an array” fatals. - Add PHPUnit data-driven tests covering missing / scalar / null
sizesmetadata during save and restore flows.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
tests/phpunit/tests/ajax/wpAjaxImageEditor.php |
Adds regression tests (data provider + two test methods) to ensure saving/restoring doesn’t fatal when sizes metadata is missing or invalid. |
src/wp-admin/includes/image-edit.php |
Hardens wp_save_image() and wp_restore_image() against missing/non-array sizes metadata. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if ( ! is_array( $meta ) ) { | ||
| $meta = array(); | ||
| } | ||
|
|
||
| if ( ! isset( $meta['sizes'] ) || ! is_array( $meta['sizes'] ) ) { | ||
| $meta['sizes'] = array(); | ||
| } |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
| * @return array[] | ||
| */ | ||
| public function data_save_image_with_unusable_sizes_metadata() { |
There was a problem hiding this comment.
| * @return array[] | |
| */ | |
| public function data_save_image_with_unusable_sizes_metadata() { | |
| * @return array<non-empty-string, array{ 0: array{ sizes?: mixed } }> | |
| */ | |
| public function data_save_image_with_unusable_sizes_metadata(): array { |
| * | ||
| * @covers ::wp_restore_image | ||
| */ | ||
| public function test_restore_image_with_unusable_sizes_metadata( $meta ) { |
There was a problem hiding this comment.
| public function test_restore_image_with_unusable_sizes_metadata( $meta ) { | |
| public function test_restore_image_with_unusable_sizes_metadata( array $meta ) { |
| * | ||
| * @covers ::wp_save_image | ||
| */ | ||
| public function test_save_image_with_unusable_sizes_metadata( $meta ) { |
There was a problem hiding this comment.
| public function test_save_image_with_unusable_sizes_metadata( $meta ) { | |
| public function test_save_image_with_unusable_sizes_metadata( array $meta ) { |
| * @ticket 65748 | ||
| * | ||
| * @dataProvider data_save_image_with_unusable_sizes_metadata | ||
| * | ||
| * @param array $meta Attachment metadata to store before editing, minus the file-specific keys. | ||
| * | ||
| * @covers ::wp_save_image |
There was a problem hiding this comment.
| * @ticket 65748 | |
| * | |
| * @dataProvider data_save_image_with_unusable_sizes_metadata | |
| * | |
| * @param array $meta Attachment metadata to store before editing, minus the file-specific keys. | |
| * | |
| * @covers ::wp_save_image | |
| * @ticket 65748 | |
| * | |
| * @covers ::wp_save_image | |
| * | |
| * @dataProvider data_save_image_with_unusable_sizes_metadata | |
| * | |
| * @param array $meta Attachment metadata to store before editing, minus the file-specific keys. |
| * @ticket 65748 | ||
| * | ||
| * @dataProvider data_save_image_with_unusable_sizes_metadata | ||
| * | ||
| * @param array $meta Replacement `sizes` metadata to store before restoring. | ||
| * | ||
| * @covers ::wp_restore_image |
There was a problem hiding this comment.
| * @ticket 65748 | |
| * | |
| * @dataProvider data_save_image_with_unusable_sizes_metadata | |
| * | |
| * @param array $meta Replacement `sizes` metadata to store before restoring. | |
| * | |
| * @covers ::wp_restore_image | |
| * @ticket 65748 | |
| * | |
| * @covers ::wp_restore_image | |
| * | |
| * @dataProvider data_save_image_with_unusable_sizes_metadata | |
| * | |
| * @param array $meta Replacement `sizes` metadata to store before restoring. |
Test ReportPatch tested: #12744 Environment
Steps taken
Expected result
Additional Notes
Screenshots/Screencast with results
Support Content |
https://core.trac.wordpress.org/ticket/65748
AI assistance: Yes
Tool(s): Claude
Model(s): Opus 5
Used for: Writing the tests
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.