Skip to content

REST API: Use wp_get_image_editor_output_format() for image_output_format and image_quality fields - #14614

Open
robelsust wants to merge 1 commit into
WordPress:trunkfrom
robelsust:fix/64677-rest-image-editor-output-format
Open

robelsust wants to merge 1 commit into
WordPress:trunkfrom
robelsust:fix/64677-rest-image-editor-output-format

Conversation

@robelsust

Copy link
Copy Markdown

Description

In WP_REST_Attachments_Controller::prepare_item_for_response(), the image_output_format and image_quality fields directly called apply_filters( 'image_editor_output_format', array( $mime_type => $mime_type ), ... ).

Seeding the filter with array( $mime_type => $mime_type ) causes issues for plugins that add mappings using array union (+) rather than key assignment (e.g., add_filter( 'image_editor_output_format', fn( $formats ) => $formats + [ 'image/jpeg' => 'image/webp' ] )). Because $mime_type was already present as a key in the seeded array, the union operator would preserve the original MIME type instead of the plugin's mapped format.

This PR replaces those direct apply_filters() calls with wp_get_image_editor_output_format( $filename ? $filename : '', $mime_type ). This aligns the REST API behavior with WP_Image_Editor::get_output_format(), which also relies on wp_get_image_editor_output_format().

Changes

  • Updated WP_REST_Attachments_Controller::prepare_item_for_response() in src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php to use wp_get_image_editor_output_format().
  • Updated inline comments to correctly reference WP_Image_Editor::get_output_format().
  • Added unit test test_image_output_format_with_array_union_filter() in tests/phpunit/tests/rest-api/rest-attachments-controller.php covering filters using array union (+).

Testing Instructions

  1. Run PHPUnit tests:
    vendor/bin/phpunit --filter test_image_output_format
  2. Verify all 5 tests pass without regressions:
    • test_image_output_format_and_progressive_defaults_in_create_response
    • test_image_output_format_with_custom_filter
    • test_image_output_format_with_array_union_filter
    • test_image_save_progressive_with_custom_filter
    • test_image_output_format_skipped_for_non_image
  3. Run PHPCS to verify coding standards:
    vendor/bin/phpcs src/wp-includes/rest-api/endpoints/class-wp-rest-attachments-controller.php tests/phpunit/tests/rest-api/rest-attachments-controller.php

Trac ticket: https://core.trac.wordpress.org/ticket/64677

…rmat and image_quality fields.

In WP_REST_Attachments_Controller::prepare_item_for_response(), use wp_get_image_editor_output_format() to retrieve output formats instead of manually seeding apply_filters( 'image_editor_output_format' ). This provides full parity with WP_Image_Editor::get_output_format(), ensuring plugins using array union (+) without overwriting keys are evaluated correctly.

See #64677.
@github-actions

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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props therssoftware.

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

@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.

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.

1 participant