WordPress/wp-includes/rest-api/endpoints
Bernhard Reiter 725f302121 Block Hooks: Pass correct context to filters.
The `$context` argument passed to filters such as `hooked_block_types`, `hooked_block`, and `hooked_block_{$hooked_block_type}` allows them to conditionally insert a hooked block. If the anchor block is contained in a template or template part, `$context` will be set to a `WP_Block_Template` object reflecting that template or part.

The aforementioned filters are applied when hooked block insertion is run upon reading a template (or part) from the DB (and before sending the template/part content with hooked blocks inserted over the REST API to the client), but also upon writing to the DB, as that's when the `ignoredHookedBlocks` metadata attribute is set.

Prior to this changeset, the `$context` passed to Block Hooks related filters in the latter case reflected the template/part that was already stored in the database (if any), which is a bug; instead, it needs to reflect the template/part that will result from the incoming `POST` network request that will trigger a database update.

Those incoming changes are encapsulated in the `$changes` argument passed to the `reset_pre_insert_template` and  `reset_pre_insert_template_part` filters, respectively, and thus to the `inject_ignored_hooked_blocks_metadata_attributes` function that is hooked to them. `$changes` is of type `stdClass` and only contains the fields that need to be updated. That means that in order to create a `WP_Block_Template` object, a two-step process is needed:

- Emulate what the updated `wp_template` or `wp_template_part` post object in the database will look like by merging `$changes` on top of the existing `$post` object fetched from the DB, or from the theme's block template (part) file, if any.
- Create a `WP_Block_Template` from the resulting object.

To achieve the latter, a new helper method (`_build_block_template_object_from_post_object`) is extracted from the existing `_build_block_template_result_from_post` function. (The latter cannot be used directly as it includes a few database calls that will fail if no post object for the template has existed yet in the database.)

While somewhat complicated to implement, the overall change allows for better separation of concerns and isolation of entities. This is visible e.g. in the fact that `inject_ignored_hooked_blocks_metadata_attributes` no longer requires a `$request` argument, which is reflected by unit tests no longer needing to create a `$request` object to pass to it, thus decoupling the function from the templates endpoint controller.

Unit tests for `inject_ignored_hooked_blocks_metadata_attributes` have been moved to a new, separate file. Test coverage has been added such that now, all three relevant scenarios are covered:

- The template doesn't exist in the DB, nor is there a block theme template file for it.
- The template doesn't exist in the DB, but there is a block theme template file for it.
- The template already exists in the DB.

Those scenarios also correspond to the logical branching inside `WP_REST_Templates_Controller::prepare_item_for_database`, which is where `inject_ignored_hooked_blocks_metadata_attributes` gets its data from.

Props tomjcafferkey, bernhard-reiter, gziolo, swissspidy.
Fixes #60754.
Built from https://develop.svn.wordpress.org/trunk@57919


git-svn-id: http://core.svn.wordpress.org/trunk@57420 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2024-04-03 15:11:14 +00:00
..
class-wp-rest-application-passwords-controller.php
class-wp-rest-attachments-controller.php REST API: Add featured_media field to attachments endpoint. 2024-02-12 22:17:11 +00:00
class-wp-rest-autosaves-controller.php
class-wp-rest-block-directory-controller.php
class-wp-rest-block-pattern-categories-controller.php
class-wp-rest-block-patterns-controller.php
class-wp-rest-block-renderer-controller.php
class-wp-rest-block-types-controller.php Coding standards: Apply some changes after composer format. 2024-03-05 06:55:08 +00:00
class-wp-rest-blocks-controller.php Coding Standards: Remove extra space in a comment in WP_REST_Blocks_Controller. 2023-10-31 14:23:21 +00:00
class-wp-rest-comments-controller.php General: Consistently cast return value to int in functions that use ceil(). 2024-02-17 15:24:08 +00:00
class-wp-rest-controller.php
class-wp-rest-edit-site-export-controller.php
class-wp-rest-font-collections-controller.php Editor: Ensure font collection metadata can be properly localized. 2024-02-21 19:27:14 +00:00
class-wp-rest-font-faces-controller.php Editor: Prevent font folder naive filtering causing infinite loops. 2024-03-22 23:01:10 +00:00
class-wp-rest-font-families-controller.php REST API: Introduce the necessary endpoints for the font library. 2024-02-07 09:20:08 +00:00
class-wp-rest-global-styles-controller.php REST API: Revert the refactor of global styles endpoints in REST API in [57624]. 2024-02-13 15:13:17 +00:00
class-wp-rest-global-styles-revisions-controller.php General: Consistently cast return value to int in functions that use ceil(). 2024-02-17 15:24:08 +00:00
class-wp-rest-menu-items-controller.php
class-wp-rest-menu-locations-controller.php
class-wp-rest-menus-controller.php
class-wp-rest-navigation-fallback-controller.php
class-wp-rest-pattern-directory-controller.php
class-wp-rest-plugins-controller.php
class-wp-rest-post-statuses-controller.php
class-wp-rest-post-types-controller.php
class-wp-rest-posts-controller.php General: Consistently cast return value to int in functions that use ceil(). 2024-02-17 15:24:08 +00:00
class-wp-rest-revisions-controller.php General: Consistently cast return value to int in functions that use ceil(). 2024-02-17 15:24:08 +00:00
class-wp-rest-search-controller.php REST API: Prevent error when passing invalid type parameter to search endpoint. 2024-03-15 11:25:06 +00:00
class-wp-rest-settings-controller.php
class-wp-rest-sidebars-controller.php
class-wp-rest-site-health-controller.php
class-wp-rest-taxonomies-controller.php
class-wp-rest-template-autosaves-controller.php
class-wp-rest-template-revisions-controller.php
class-wp-rest-templates-controller.php Block Hooks: Pass correct context to filters. 2024-04-03 15:11:14 +00:00
class-wp-rest-terms-controller.php General: Consistently cast return value to int in functions that use ceil(). 2024-02-17 15:24:08 +00:00
class-wp-rest-themes-controller.php
class-wp-rest-url-details-controller.php Bootstrap/Load: Introduce functions to check whether WordPress is serving a REST API request. 2024-01-19 17:40:09 +00:00
class-wp-rest-users-controller.php General: Consistently cast return value to int in functions that use ceil(). 2024-02-17 15:24:08 +00:00
class-wp-rest-widget-types-controller.php
class-wp-rest-widgets-controller.php