Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Also apply to pages
  • Loading branch information
ockham committed Dec 4, 2024
commit 838a75e7350f6d69183fa1eb522dbd88645f13d2
29 changes: 11 additions & 18 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -1296,33 +1296,26 @@ function insert_hooked_blocks_into_rest_response( $response, $post ) {
);
}

$post_type_to_wrapper_block_mappings = array(
'wp_navigation' => 'core/navigation',
'wp_post' => 'core/post-content',
);

if ( isset( $post_type_to_wrapper_block_mappings[ $post->post_type ] ) ) {
$wrapper_block_type = $post_type_to_wrapper_block_mappings[ $post->post_type ];

$content = get_comment_delimited_block_content(
$wrapper_block_type,
$attributes,
$response->data['content']['raw']
);
if ( 'wp_navigation' === $post->post_type ) {
Copy link
Member

Choose a reason for hiding this comment

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

What about synced patterns wp/block? Should it be extensible? These post types are special do maybe it’s fine as is but a filter here could be useful.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, good point. I guess the wp_block post type should map to core/block.

A filter might make sense here 👍 Something to map post types to block types. (I wonder if we already have a function or data structure to do that somewhere 🤔 )

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've finally tested this with a synced pattern, and it's currently not inserting hooked blocks.

Simply adding elseif ( 'wp_block' === $post->post_type ) { $wrapper_block_type = 'core/block'; } didn't do the trick, so I'll have to look into this a bit more. However, since this PR has been open for a while and seems to be ready otherwise, I'll merge it and tackle synced patterns in a follow-up 😊

Copy link
Member

Choose a reason for hiding this comment

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

That's fair. It was also discussed in https://github.com/WordPress/wordpress-develop/pull/7898/files#r1867470796 to add support for every post type. The challenge with Patterns is that they are handled out of the box if they come from files bundled from the theme or when registered with PHP. The last missing puzzle would be a synced version of the pattern.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Update: Support for synced patterns was implemented in WordPress/gutenberg#68058 and #8015, respectively.

I've filed a Trac ticket for other CPTs (and filterable post type -- block type mappings): https://core.trac.wordpress.org/ticket/62715

$wrapper_block_type = 'core/navigation';
} else {
$content = $response->data['content']['raw'];
$wrapper_block_type = 'core/post-content';
}

$content = get_comment_delimited_block_content(
$wrapper_block_type,
$attributes,
$response->data['content']['raw']
);

$content = apply_block_hooks_to_content(
$content,
$post,
'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata'
);

if ( isset( $post_type_to_wrapper_block_mappings[ $post->post_type ] ) ) {
// Remove mock block wrapper.
$content = remove_serialized_parent_block( $content );
}
// Remove mock block wrapper.
$content = remove_serialized_parent_block( $content );

$response->data['content']['raw'] = $content;

Expand Down
5 changes: 3 additions & 2 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -763,8 +763,9 @@
// 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 post and wp_navigation post type REST response.
add_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response', 10, 2 );
// Inject hooked blocks into the Posts endpoint REST response for some given post types.
add_filter( 'rest_prepare_page', 'insert_hooked_blocks_into_rest_response', 10, 2 );
add_filter( 'rest_prepare_post', 'insert_hooked_blocks_into_rest_response', 10, 2 );
add_filter( 'rest_prepare_wp_navigation', 'insert_hooked_blocks_into_rest_response', 10, 2 );

unset( $filter, $action );