Skip to content

Commit

Permalink
Synced Patterns: Apply Block Hooks (WordPress#68058)
Browse files Browse the repository at this point in the history
Apply Block Hooks to synced patterns (i.e. `core/block` instances).

Co-authored-by: ockham <[email protected]>
Co-authored-by: gziolo <[email protected]>
  • Loading branch information
3 people authored Dec 19, 2024
1 parent 7e15fad commit 9034196
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
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'] ) ) {
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'] ) ) {
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 );
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

0 comments on commit 9034196

Please sign in to comment.