From e36d0969478e143f5319f8b14c4b92ade2c40f3f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 Jan 2024 14:59:47 +0100 Subject: [PATCH 1/4] Revert "Block Hooks API: Update Navigation block feature gate (#58388)" This reverts commit 7e7f042547132269615e4ae9d9792df97c4999bf. --- packages/block-library/src/navigation/index.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/block-library/src/navigation/index.php b/packages/block-library/src/navigation/index.php index 59b4a66a1babd1..2094b7de1607b1 100644 --- a/packages/block-library/src/navigation/index.php +++ b/packages/block-library/src/navigation/index.php @@ -192,7 +192,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( 'get_hooked_blocks' ) ) { // 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]; @@ -987,7 +987,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( 'get_hooked_blocks' ) ) { // 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 ); @@ -1413,9 +1413,9 @@ 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' ) ) { +// Injection of hooked blocks into the Navigation block relies on some functions present in WP >= 6.4 +// that are not present in Gutenberg's WP 6.4 compatibility layer. +if ( function_exists( 'get_hooked_blocks' ) ) { add_action( 'rest_insert_wp_navigation', 'block_core_navigation_update_ignore_hooked_blocks_meta', 10, 3 ); } @@ -1445,8 +1445,8 @@ function block_core_navigation_insert_hooked_blocks_into_rest_response( $respons return $response; } -// 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' ) ) { +// Injection of hooked blocks into the Navigation block relies on some functions present in WP >= 6.4 +// that are not present in Gutenberg's WP 6.4 compatibility layer. +if ( function_exists( 'get_hooked_blocks' ) ) { add_filter( 'rest_prepare_wp_navigation', 'block_core_navigation_insert_hooked_blocks_into_rest_response', 10, 3 ); } From 00a3ad4d7a795b7b6653c518653b850293824ab5 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 Jan 2024 17:03:13 +0100 Subject: [PATCH 2/4] Block Hooks: Try to make insertion into Navigation block work on WP 6.4 --- lib/compat/wordpress-6.5/block-hooks.php | 48 ++++++++++++++++++++++++ lib/load.php | 1 + 2 files changed, 49 insertions(+) create mode 100644 lib/compat/wordpress-6.5/block-hooks.php diff --git a/lib/compat/wordpress-6.5/block-hooks.php b/lib/compat/wordpress-6.5/block-hooks.php new file mode 100644 index 00000000000000..7403c8d552debe --- /dev/null +++ b/lib/compat/wordpress-6.5/block-hooks.php @@ -0,0 +1,48 @@ +post_type ) { + return $hooked_block_types; + } + + // Only apply this filter if the anchor block is a Navigation block. + if ( + 'core/navigation' !== $anchor_block_type || ( + 'first_child' !== $relative_position && + 'last_child' !== $relative_position + ) + ) { + return $hooked_block_types; + } + + $ignored_hooked_blocks = get_post_meta( $context->ID, '_wp_ignored_hooked_blocks', true ); + if ( ! empty( $ignored_hooked_blocks ) ) { + $ignored_hooked_blocks = json_decode( $ignored_hooked_blocks, true ); + } + + if ( ! isset( $ignored_hooked_blocks ) || ! is_array( $ignored_hooked_blocks ) ) { + $ignored_hooked_blocks = array(); + } + + $hooked_block_types = array_diff( $hooked_block_types, $ignored_hooked_blocks ); + + // FIXME. The problem is that we don't have any way of telling if this is being invoked + // upon read or write access. As a consequence, we're always writing the post meta, even + // if we're only reading. That means that the ignoredHookedBlocks meta will be set + // when first loading the frontend, and will cause hooked blocks to be omitted when loading + // it for the second time :( + $ignored_hooked_blocks = array_merge( $ignored_hooked_blocks, $hooked_block_types ); + update_post_meta( $context->ID, '_wp_ignored_hooked_blocks', json_encode( $ignored_hooked_blocks ) ); + + return $hooked_block_types; +} + +if ( version_compare( get_bloginfo( 'version' ), '6.5', '<' ) ) { + add_filter( 'hooked_block_types', 'respect_ignored_hooked_blocks_post_meta_on_wp_navigation_post', 10, 4 ); +} diff --git a/lib/load.php b/lib/load.php index f5c606727f9f47..297fce5bb6176d 100644 --- a/lib/load.php +++ b/lib/load.php @@ -105,6 +105,7 @@ function gutenberg_is_experiment_enabled( $name ) { // WordPress 6.5 compat. require __DIR__ . '/compat/wordpress-6.5/blocks.php'; +require __DIR__ . '/compat/wordpress-6.5/block-hooks.php'; require __DIR__ . '/compat/wordpress-6.5/block-patterns.php'; require __DIR__ . '/compat/wordpress-6.5/kses.php'; require __DIR__ . '/compat/wordpress-6.5/interactivity-api/class-wp-interactivity-api.php'; From 4286320a7eb32d3879c98fbf04632f3fd0d54858 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 Jan 2024 17:16:09 +0100 Subject: [PATCH 3/4] Whitespace --- lib/compat/wordpress-6.5/block-hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/block-hooks.php b/lib/compat/wordpress-6.5/block-hooks.php index 7403c8d552debe..40eb987ddc1b1e 100644 --- a/lib/compat/wordpress-6.5/block-hooks.php +++ b/lib/compat/wordpress-6.5/block-hooks.php @@ -5,7 +5,7 @@ * @package gutenberg */ -function respect_ignored_hooked_blocks_post_meta_on_wp_navigation_post ( $hooked_block_types, $relative_position, $anchor_block_type, $context ) { +function respect_ignored_hooked_blocks_post_meta_on_wp_navigation_post( $hooked_block_types, $relative_position, $anchor_block_type, $context ) { // Only apply this filter if $context is a wp_navigation post object. if ( ! $context instanceof WP_Post || 'wp_navigation' !== $context->post_type ) { return $hooked_block_types; From 0013e9ad3358113d9cbfe361416499afa2737f5a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 31 Jan 2024 17:20:45 +0100 Subject: [PATCH 4/4] Typo --- lib/compat/wordpress-6.5/block-hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/block-hooks.php b/lib/compat/wordpress-6.5/block-hooks.php index 40eb987ddc1b1e..990285ecb42643 100644 --- a/lib/compat/wordpress-6.5/block-hooks.php +++ b/lib/compat/wordpress-6.5/block-hooks.php @@ -1,6 +1,6 @@