Skip to content

Image editor: fatal error when attachment metadata has no sizes array - #12744

Open
josephscott wants to merge 1 commit into
WordPress:trunkfrom
josephscott:65748/image-edit-meta-sizes
Open

Image editor: fatal error when attachment metadata has no sizes array#12744
josephscott wants to merge 1 commit into
WordPress:trunkfrom
josephscott:65748/image-edit-meta-sizes

Conversation

@josephscott

Copy link
Copy Markdown
Contributor

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.

Copilot AI review requested due to automatic review settings July 28, 2026 20:14
@github-actions

github-actions Bot commented Jul 28, 2026

Copy link
Copy Markdown

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 props-bot label.

Unlinked Accounts

The 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:

Props josephscott, westonruter, mukesh27.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 in wp_save_image() before it’s passed to array_merge().
  • Normalize $meta and $meta['sizes'] in wp_restore_image() to avoid “Cannot use a scalar value as an array” fatals.
  • Add PHPUnit data-driven tests covering missing / scalar / null sizes metadata 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.

Comment on lines +828 to +834
if ( ! is_array( $meta ) ) {
$meta = array();
}

if ( ! isset( $meta['sizes'] ) || ! is_array( $meta['sizes'] ) ) {
$meta['sizes'] = array();
}
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Comment thread tests/phpunit/tests/ajax/wpAjaxImageEditor.php
Comment on lines +314 to +316
* @return array[]
*/
public function data_save_image_with_unusable_sizes_metadata() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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 ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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 ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function test_save_image_with_unusable_sizes_metadata( $meta ) {
public function test_save_image_with_unusable_sizes_metadata( array $meta ) {

Comment on lines +207 to +213
* @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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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.

Comment on lines +265 to +271
* @ticket 65748
*
* @dataProvider data_save_image_with_unusable_sizes_metadata
*
* @param array $meta Replacement `sizes` metadata to store before restoring.
*
* @covers ::wp_restore_image

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* @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.

@nazmulasif

Copy link
Copy Markdown

Test Report

Patch tested: #12744

Environment

  • WordPress: 7.1-beta3-20260728.201755
  • Subdirectory: No
  • PHP: 7.4.33
  • Server: PHP.wasm
  • Database: WP_SQLite_Driver (Server: 8.0.38 / Client: 3.51.0)
  • Browser: Chrome 150.0.0.0
  • OS: macOS
  • Theme: Twenty Twenty-Five 1.5
  • MU Plugins: None activated
  • Plugins:
    • Test Reports 1.3.0

Steps taken

  1. Launched the WordPress Playground instance built from PR Image editor: fatal error when attachment metadata has no sizes array #12744.
  2. Navigated to Media → Library and uploaded a test image.
  3. Clicked Edit Image to open the core image editor interface.
  4. Performed Crop and Scale operations, then saved the modifications.
  5. Clicked Restore Original Image to revert the image back to its original state.
  6. Verified that no PHP fatal errors or warnings occurred during the process.
  7. ✅ Patch is solving the problem

Expected result

  • Image saving, cropping, scaling, and restoring operations should execute successfully without throwing PHP type errors or fatal errors when processing attachment metadata.

Additional Notes

  • Tested via WordPress Playground instance. Crop, scale, and restore functionalities work as expected without any errors.

Screenshots/Screencast with results

  • Screenshot/Screencast before: N/A
  • Screenshot/Screencast after: N/A

Support Content

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants