REST API: Prevent fatal error when a null template reaches prepare_item_for_response() - #13375
REST API: Prevent fatal error when a null template reaches prepare_item_for_response()#13375ramonjd wants to merge 1 commit into
Conversation
…em_for_response(). WP_REST_Templates_Controller::update_item() passes its get_block_template() refetches to prepare_item_for_response() without checking them, both after writing an update and on its revert-to-theme path, which force-deletes the template's post first. When a refetch returns null, reading $item->content is a fatal error. Return a rest_template_not_found error (404) instead when the template is null. Backports the guard from Gutenberg PR 82374. See #66032.
|
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 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. |
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. |
Trac ticket: https://core.trac.wordpress.org/ticket/66032
Backports the guard from WordPress/gutenberg#82374.
What
Prevents an uncaught fatal error (
Attempt to assign property "content" on null) inWP_REST_Templates_Controller::prepare_item_for_response()when it receives anulltemplate.Why
WP_REST_Templates_Controller::update_item()passes itsget_block_template()refetches toprepare_item_for_response()without checking them, both after writing an update and on its revert-to-theme path, which force-deletes the template's post before checking that a theme or plugin version of the template exists. When a refetch returnsnull, the method fatals reading$item->content.What changed
prepare_item_for_response()returns arest_template_not_foundWP_Error(404) when the template isnull, instead of crashing.prepare_item_for_response( null, ... )directly, the exact condition that fatals on trunk. Without the guard the call fatals and the run aborts, so the test can only pass through the guard.Manual testing
The revert path is the easiest way to reach the fatal:
POST /wp/v2/templateswith{ "slug": "db-only", "title": "T", "content": "" }.PUT /wp/v2/templates/<theme>//db-onlywith{ "source": "theme" }.rest_template_not_found.