From 0e88fe4ade519c569085f4bfe792a2b17feece5e Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 14 Feb 2024 16:37:43 +0100 Subject: [PATCH 1/4] Block Hooks: Fix in Navigation block --- .../block-library/src/navigation/index.php | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index f1292e5d2e723a..6c3d3da069b4f3 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -1383,7 +1383,7 @@ function block_core_navigation_get_most_recently_published_navigation() { * @param WP_Post $post `wp_navigation` post object corresponding to the block. * @return string Serialized inner blocks in mock Navigation block wrapper, with hooked blocks inserted, if any. */ -function block_core_navigation_insert_hooked_blocks( $inner_blocks, $post ) { +function block_core_navigation_insert_hooked_blocks( $inner_blocks, $post, $callback = 'insert_hooked_blocks' ) { $before_block_visitor = null; $after_block_visitor = null; $hooked_blocks = get_hooked_blocks(); @@ -1409,8 +1409,8 @@ function block_core_navigation_insert_hooked_blocks( $inner_blocks, $post ) { $after_block_visitor = null; if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) { - $before_block_visitor = make_before_block_visitor( $hooked_blocks, $post ); - $after_block_visitor = make_after_block_visitor( $hooked_blocks, $post ); + $before_block_visitor = make_before_block_visitor( $hooked_blocks, $post, $callback ); + $after_block_visitor = make_after_block_visitor( $hooked_blocks, $post, $callback ); } return traverse_and_serialize_block( $mock_anchor_parent_block, $before_block_visitor, $after_block_visitor ); @@ -1422,12 +1422,11 @@ function block_core_navigation_insert_hooked_blocks( $inner_blocks, $post ) { * @param WP_Post $post Post object. */ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) { - // We run the Block Hooks mechanism so it will return the list of ignored hooked blocks - // in the mock root Navigation block's metadata attribute. - // We ignore the rest of the returned `$markup`; `$post->post_content` already has the hooked - // blocks inserted, whereas `$markup` will have them inserted twice. - $blocks = parse_blocks( $post->post_content ); - $markup = block_core_navigation_insert_hooked_blocks( $blocks, $post ); + // We run the Block Hooks mechanism to inject the `metadata.ignoredHookedBlocks` attribute into + // all anchor blocks. For the root level, we create a mock Navigation and extract them from there. + $blocks = parse_blocks( $post->post_content ); + $markup = block_core_navigation_insert_hooked_blocks( $blocks, $post, 'set_ignored_hooked_blocks_metadata' );) + $root_nav_block = parse_blocks( $markup )[0]; $ignored_hooked_blocks = isset( $root_nav_block['attrs']['metadata']['ignoredHookedBlocks'] ) ? $root_nav_block['attrs']['metadata']['ignoredHookedBlocks'] @@ -1441,6 +1440,10 @@ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) { } update_post_meta( $post->ID, '_wp_ignored_hooked_blocks', json_encode( $ignored_hooked_blocks ) ); } + + // TODO: wp_update_post() to set the post_content to the updated markup (to include the ignoredHookedBlocks metadata). + // We need to remove the markup for the root Nav block wrapper like we do in block_core_navigation_insert_hooked_blocks_into_rest_response, + // or alternatively via serialize_blocks( $root_nav_block['innerBlocks'] ), but that's probably more expensive. } // Before adding our filter, we verify if it's already added in Core. From faf444861fe9cd321c1f0574196db89c10bd3208 Mon Sep 17 00:00:00 2001 From: Tom Cafferkey Date: Thu, 15 Feb 2024 14:43:00 +0000 Subject: [PATCH 2/4] Update wp_navigation post with ignoredHookedBlocks meta and feature gate --- .../block-library/src/navigation/index.php | 35 ++++++++++++++----- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 6c3d3da069b4f3..8db6e9c00ce557 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -218,7 +218,7 @@ private static function get_inner_blocks_from_navigation_post( $attributes ) { // it encounters whitespace. This code strips it. $blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks ); - if ( function_exists( 'get_hooked_block_markup' ) ) { + if ( function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { // Run Block Hooks algorithm to inject hooked blocks. $markup = block_core_navigation_insert_hooked_blocks( $blocks, $navigation_post ); $root_nav_block = parse_blocks( $markup )[0]; @@ -1024,7 +1024,7 @@ function block_core_navigation_get_fallback_blocks() { // In this case default to the (Page List) fallback. $fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks; - if ( function_exists( 'get_hooked_block_markup' ) ) { + if ( function_exists( 'set_ignored_hooked_blocks_metadata' ) ) { // Run Block Hooks algorithm to inject hooked blocks. // We have to run it here because we need the post ID of the Navigation block to track ignored hooked blocks. $markup = block_core_navigation_insert_hooked_blocks( $fallback_blocks, $navigation_post ); @@ -1425,7 +1425,7 @@ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) { // We run the Block Hooks mechanism to inject the `metadata.ignoredHookedBlocks` attribute into // all anchor blocks. For the root level, we create a mock Navigation and extract them from there. $blocks = parse_blocks( $post->post_content ); - $markup = block_core_navigation_insert_hooked_blocks( $blocks, $post, 'set_ignored_hooked_blocks_metadata' );) + $markup = block_core_navigation_insert_hooked_blocks( $blocks, $post, 'set_ignored_hooked_blocks_metadata' ); $root_nav_block = parse_blocks( $markup )[0]; $ignored_hooked_blocks = isset( $root_nav_block['attrs']['metadata']['ignoredHookedBlocks'] ) @@ -1441,6 +1441,15 @@ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) { update_post_meta( $post->ID, '_wp_ignored_hooked_blocks', json_encode( $ignored_hooked_blocks ) ); } + $serialized_inner_blocks = block_core_navigation_remove_serialized_parent_block( $markup ); + + wp_update_post( + array( + 'ID' => $post->ID, + 'post_content' => $serialized_inner_blocks, + ) + ); + // TODO: wp_update_post() to set the post_content to the updated markup (to include the ignoredHookedBlocks metadata). // We need to remove the markup for the root Nav block wrapper like we do in block_core_navigation_insert_hooked_blocks_into_rest_response, // or alternatively via serialize_blocks( $root_nav_block['innerBlocks'] ), but that's probably more expensive. @@ -1453,7 +1462,7 @@ function block_core_navigation_update_ignore_hooked_blocks_meta( $post ) { // Injection of hooked blocks into the Navigation block relies on some functions present in WP >= 6.5 // that are not present in Gutenberg's WP 6.5 compatibility layer. -if ( function_exists( 'get_hooked_block_markup' ) && ! has_filter( 'rest_insert_wp_navigation', $rest_insert_wp_navigation_core_callback ) ) { +if ( function_exists( 'set_ignored_hooked_blocks_metadata' ) && ! has_filter( 'rest_insert_wp_navigation', $rest_insert_wp_navigation_core_callback ) ) { add_action( 'rest_insert_wp_navigation', 'block_core_navigation_update_ignore_hooked_blocks_meta', 10, 3 ); } @@ -1473,9 +1482,7 @@ function block_core_navigation_insert_hooked_blocks_into_rest_response( $respons $content = block_core_navigation_insert_hooked_blocks( $parsed_blocks, $post ); // Remove mock Navigation block wrapper. - $start = strpos( $content, '-->' ) + strlen( '-->' ); - $end = strrpos( $content, '' ) + strlen( '-->' ); + $end = strrpos( $serialized_block, '' ) + strlen( '-->' ); + $end = strrpos( $serialized_block, '' ) + strlen( '-->' ); - $end = strrpos( $serialized_block, '