Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Synced Patterns: Apply Block Hooks #68058

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 3 additions & 0 deletions backport-changelog/6.8/8015.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/8015

* https://github.com/WordPress/gutenberg/pull/68058
13 changes: 12 additions & 1 deletion lib/compat/wordpress-6.8/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function gutenberg_apply_block_hooks_to_post_content( $content ) {
* @return WP_REST_Response The response object.
*/
function gutenberg_insert_hooked_blocks_into_rest_response( $response, $post ) {
if ( empty( $response->data['content']['raw'] ) || empty( $response->data['content']['rendered'] ) ) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out that synced patterns don't have rendered content set, so I had to loosen the criterion here.

if ( empty( $response->data['content']['raw'] ) ) {
return $response;
}

Expand All @@ -185,6 +185,8 @@ function gutenberg_insert_hooked_blocks_into_rest_response( $response, $post ) {

if ( 'wp_navigation' === $post->post_type ) {
$wrapper_block_type = 'core/navigation';
} elseif ( 'wp_block' === $post->post_type ) {
$wrapper_block_type = 'core/block';
} else {
$wrapper_block_type = 'core/post-content';
}
Expand All @@ -206,6 +208,11 @@ function gutenberg_insert_hooked_blocks_into_rest_response( $response, $post ) {

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

// If the rendered content was previously empty, we leave it like that.
if ( empty( $response->data['content']['rendered'] ) ) {
Copy link
Member

Choose a reason for hiding this comment

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

Makes perfect sense together with other changes 👍🏻

return $response;
}

// No need to inject hooked blocks twice.
$priority = has_filter( 'the_content', 'apply_block_hooks_to_content' );
if ( false !== $priority ) {
Expand All @@ -224,6 +231,7 @@ function gutenberg_insert_hooked_blocks_into_rest_response( $response, $post ) {
}
add_filter( 'rest_prepare_page', 'gutenberg_insert_hooked_blocks_into_rest_response', 10, 2 );
add_filter( 'rest_prepare_post', 'gutenberg_insert_hooked_blocks_into_rest_response', 10, 2 );
add_filter( 'rest_prepare_wp_block', 'gutenberg_insert_hooked_blocks_into_rest_response', 10, 2 );

/**
* Updates the wp_postmeta with the list of ignored hooked blocks
Expand Down Expand Up @@ -272,6 +280,8 @@ function gutenberg_update_ignored_hooked_blocks_postmeta( $post ) {

if ( 'wp_navigation' === $post->post_type ) {
$wrapper_block_type = 'core/navigation';
} elseif ( 'wp_block' === $post->post_type ) {
$wrapper_block_type = 'core/block';
} else {
$wrapper_block_type = 'core/post-content';
}
Expand Down Expand Up @@ -311,3 +321,4 @@ function gutenberg_update_ignored_hooked_blocks_postmeta( $post ) {
}
add_filter( 'rest_pre_insert_page', 'gutenberg_update_ignored_hooked_blocks_postmeta' );
add_filter( 'rest_pre_insert_post', 'gutenberg_update_ignored_hooked_blocks_postmeta' );
add_filter( 'rest_pre_insert_wp_block', 'gutenberg_update_ignored_hooked_blocks_postmeta' );
20 changes: 20 additions & 0 deletions packages/block-library/src/block/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,26 @@ function render_block_core_block( $attributes ) {
add_filter( 'render_block_context', $filter_block_context, 1 );
}

$ignored_hooked_blocks = get_post_meta( $attributes['ref'], '_wp_ignored_hooked_blocks', true );
Copy link
Member

Choose a reason for hiding this comment

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

I couldn't find a better place, so I will ask here. How feasible would it be to move the logic to apply_block_hooks_to_content in WordPress core? A very similar logic exists now in a few places. As the helper function gets the current post object as param, it should be able to handle everything as it happens inside the rest_pre_insert_{$post_type} filter.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hah, I just had a similar thought yesterday!

I'll need to check if we can really absorb this logic into apply_block_hooks_to_content. Note that templates and template parts are still handled quite differently from posts, synced patterns, and navigation blocks:

  1. They're instances of the WP_Block_Template class rather than WP_Post.
  2. While first/last child insertion is implemented for template parts, it is not implemented for templates. (The reason is that there is a Template Part block, core/template-part, that can be used as an anchor block; but of course, there's no Template block, so it cannot be used as an anchor block for Block Hooks.)
  3. I need to think a bit more about patterns.

Anyway, I'll file a ticket for this and will look into it in January 😊

Copy link
Member

Choose a reason for hiding this comment

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

They're instances of the WP_Block_Template class rather than WP_Post.

I missed the nuance of the templates. Anyway, it could still be context-based, so the additional logic would only run if the WP_Post instance is provided.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Anyway, I'll file a ticket for this and will look into it in January 😊

https://core.trac.wordpress.org/ticket/62716

Copy link
Contributor Author

Choose a reason for hiding this comment

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

PR (WIP): #68926

if ( ! empty( $ignored_hooked_blocks ) ) {
$ignored_hooked_blocks = json_decode( $ignored_hooked_blocks, true );
$attributes['metadata'] = array(
'ignoredHookedBlocks' => $ignored_hooked_blocks,
);
}

// Wrap in "Block" block so the Block Hooks algorithm can insert blocks
// that are hooked as first or last child of `core/block`.
$content = get_comment_delimited_block_content(
'core/block',
$attributes,
$content
);
// Apply Block Hooks.
$content = apply_block_hooks_to_content( $content, $reusable_block );
// Remove block wrapper.
$content = remove_serialized_parent_block( $content );

$content = do_blocks( $content );
unset( $seen_refs[ $attributes['ref'] ] );

Expand Down
Loading