Block Hooks: Move Posts controller hooked blocks injection logic.

In a similar vein as [58291], this changeset introduces a new `insert_hooked_blocks_into_rest_response` function and hooks it to the `rest_prepare_wp_navigation` filter.

This is part of an ongoing effort to move Block Hooks related code out of the Navigation block. Specifically, `insert_hooked_blocks_into_rest_response` is based on `block_core_navigation_insert_hooked_blocks_into_rest_response`. Eventually, it will be possible to deprecate the latter.

Follow-up to [58291].

See #60759.
Built from https://develop.svn.wordpress.org/trunk@58292


git-svn-id: http://core.svn.wordpress.org/trunk@57752 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Bernhard Reiter 2024-06-03 12:37:14 +00:00
parent b62b7a599e
commit cdbb9bf2b4
3 changed files with 45 additions and 1 deletions

View File

@ -1135,6 +1135,47 @@ function insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata( &$parsed_a
return $markup;
}
/**
* Hooks into the REST API response for the core/navigation block and adds the first and last inner blocks.
*
* @since 6.6.0
*
* @param WP_REST_Response $response The response object.
* @param WP_Post $post Post object.
* @return WP_REST_Response The response object.
*/
function insert_hooked_blocks_into_rest_response( $response, $post ) {
if ( ! isset( $response->data['content']['raw'] ) || ! isset( $response->data['content']['rendered'] ) ) {
return $response;
}
$attributes = array();
$ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true );
if ( ! empty( $ignored_hooked_blocks ) ) {
$ignored_hooked_blocks = json_decode( $ignored_hooked_blocks, true );
$attributes['metadata'] = array(
'ignoredHookedBlocks' => $ignored_hooked_blocks,
);
}
$content = get_comment_delimited_block_content(
'core/navigation',
$attributes,
$response->data['content']['raw']
);
$content = apply_block_hooks_to_content( $content, $post );
// Remove mock Navigation block wrapper.
$content = remove_serialized_parent_block( $content );
$response->data['content']['raw'] = $content;
/** This filter is documented in wp-includes/post-template.php */
$response->data['content']['rendered'] = apply_filters( 'the_content', $content );
return $response;
}
/**
* Returns a function that injects the theme attribute into, and hooked blocks before, a given block.
*

View File

@ -760,4 +760,7 @@ add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_me
// Update ignoredHookedBlocks postmeta for wp_navigation post type.
add_filter( 'rest_pre_insert_wp_navigation', 'update_ignored_hooked_blocks_postmeta' );
// Inject hooked blocks into the wp_navigation post type REST response.
add_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response', 10, 2 );
unset( $filter, $action );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.6-alpha-58291';
$wp_version = '6.6-alpha-58292';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.