From 0711c61b81f3b1f293d0691c9a985fde6bf615ec Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 25 Nov 2024 12:54:34 +0100 Subject: [PATCH 01/20] Block Hooks: Apply to Post Content (on frontend) --- src/wp-includes/blocks.php | 15 +++++++++++++++ src/wp-includes/default-filters.php | 1 + 2 files changed, 16 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 94c4dfa43afb6..a029e72ab9fa4 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1132,6 +1132,21 @@ function apply_block_hooks_to_content( $content, $context, $callback = 'insert_h return $content; } +/** + * Runs the Block Hooks algorithm on the post content of the current post. + * + * @since 6.8.0 + * + * @param string $content + * @return string The post content, with Block Hooks applied. + */ +function apply_block_hooks_to_post_content( $content ) { + // The `the_content` filter does not provide the post that the content is coming from. + // However, we can infer it by calling `get_post()`, which will return the current post + // if no post ID is provided. + return apply_block_hooks_to_content( $content, get_post(), 'insert_hooked_blocks' ); +} + /** * Accepts the serialized markup of a block and its inner blocks, and returns serialized markup of the inner blocks. * diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index ae654605e8f4b..8a67743a25424 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -192,6 +192,7 @@ add_filter( 'the_title', 'convert_chars' ); add_filter( 'the_title', 'trim' ); +add_filter( 'the_content', 'apply_block_hooks_to_post_content', 8 ); // BEFORE do_blocks(). add_filter( 'the_content', 'do_blocks', 9 ); add_filter( 'the_content', 'wptexturize' ); add_filter( 'the_content', 'convert_smilies', 20 ); From b40dd94593a4d50e25688e737eedafd1b2d592b3 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 25 Nov 2024 13:01:54 +0100 Subject: [PATCH 02/20] Solve by using a default instead --- src/wp-includes/blocks.php | 37 ++++++++++++----------------- src/wp-includes/default-filters.php | 2 +- 2 files changed, 16 insertions(+), 23 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index a029e72ab9fa4..a91d2f2b80344 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1039,17 +1039,25 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po * * @since 6.6.0 * @since 6.7.0 Injects the `theme` attribute into Template Part blocks, even if no hooked blocks are registered. + * @since 6.8.0 Have the `$context` parameter default to `null`, in which case the current post will be used. * @access private * - * @param string $content Serialized content. - * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object, - * or pattern that the blocks belong to. - * @param callable $callback A function that will be called for each block to generate - * the markup for a given list of blocks that are hooked to it. - * Default: 'insert_hooked_blocks'. + * @param string $content Serialized content. + * @param WP_Block_Template|WP_Post|array|null $context A block template, template part, `wp_navigation` post object, + * or pattern that the blocks belong to. If set to `null`, the + * current post is used. + * Default: `null`. + * @param callable $callback A function that will be called for each block to generate + * the markup for a given list of blocks that are hooked to it. + * Default: 'insert_hooked_blocks'. * @return string The serialized markup. */ -function apply_block_hooks_to_content( $content, $context, $callback = 'insert_hooked_blocks' ) { +function apply_block_hooks_to_content( $content, $context = null, $callback = 'insert_hooked_blocks' ) { + // Default to the current post if no context is provided. + if ( null === $context ) { + $context = get_post(); + } + $hooked_blocks = get_hooked_blocks(); $before_block_visitor = '_inject_theme_attribute_in_template_part_block'; @@ -1132,21 +1140,6 @@ function apply_block_hooks_to_content( $content, $context, $callback = 'insert_h return $content; } -/** - * Runs the Block Hooks algorithm on the post content of the current post. - * - * @since 6.8.0 - * - * @param string $content - * @return string The post content, with Block Hooks applied. - */ -function apply_block_hooks_to_post_content( $content ) { - // The `the_content` filter does not provide the post that the content is coming from. - // However, we can infer it by calling `get_post()`, which will return the current post - // if no post ID is provided. - return apply_block_hooks_to_content( $content, get_post(), 'insert_hooked_blocks' ); -} - /** * Accepts the serialized markup of a block and its inner blocks, and returns serialized markup of the inner blocks. * diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 8a67743a25424..0a502a4b4f8ea 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -192,7 +192,7 @@ add_filter( 'the_title', 'convert_chars' ); add_filter( 'the_title', 'trim' ); -add_filter( 'the_content', 'apply_block_hooks_to_post_content', 8 ); // BEFORE do_blocks(). +add_filter( 'the_content', 'apply_block_hooks_to_content', 8 ); // BEFORE do_blocks(). add_filter( 'the_content', 'do_blocks', 9 ); add_filter( 'the_content', 'wptexturize' ); add_filter( 'the_content', 'convert_smilies', 20 ); From 20a547069c199315d8c0dcfba6969693b75f3cf7 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 26 Nov 2024 17:35:16 +0100 Subject: [PATCH 03/20] Insert hooked block into editor --- src/wp-includes/blocks.php | 26 ++++++++++++++++++++------ src/wp-includes/default-filters.php | 3 ++- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index a91d2f2b80344..808bb8da15eff 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1295,16 +1295,30 @@ function insert_hooked_blocks_into_rest_response( $response, $post ) { 'ignoredHookedBlocks' => $ignored_hooked_blocks, ); } - $content = get_comment_delimited_block_content( - 'core/navigation', - $attributes, - $response->data['content']['raw'] + + $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'] + ); + } else { + $content = $response->data['content']['raw']; + } + $content = apply_block_hooks_to_content( $content, $post ); - // Remove mock Navigation block wrapper. - $content = remove_serialized_parent_block( $content ); + if ( isset( $post_type_to_wrapper_block_mappings[ $post->post_type ] ) ) { + // Remove mock block wrapper. + $content = remove_serialized_parent_block( $content ); + } $response->data['content']['raw'] = $content; diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 0a502a4b4f8ea..6645e14e18672 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -763,7 +763,8 @@ // 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 wp_navigation post type REST response. +// 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 ); +add_filter( 'rest_prepare_post', 'insert_hooked_blocks_into_rest_response', 10, 2 ); unset( $filter, $action ); From 90abff4cd41ab47e2c561adb01701d934f2a4660 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 26 Nov 2024 21:38:46 +0100 Subject: [PATCH 04/20] Update PHPDoc --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 808bb8da15eff..2e0eee4027ac3 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1274,7 +1274,7 @@ function insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata( &$parsed_a } /** - * Hooks into the REST API response for the core/navigation block and adds the first and last inner blocks. + * Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks. * * @since 6.6.0 * From 0b844b337356d7c16bc0972cc285ff77f3383b30 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 26 Nov 2024 21:39:04 +0100 Subject: [PATCH 05/20] Set ignoredHookedBlocks in REST API response --- src/wp-includes/blocks.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 2e0eee4027ac3..3c0829e2aa61a 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1313,7 +1313,11 @@ function insert_hooked_blocks_into_rest_response( $response, $post ) { $content = $response->data['content']['raw']; } - $content = apply_block_hooks_to_content( $content, $post ); + $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. From 838a75e7350f6d69183fa1eb522dbd88645f13d2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Nov 2024 15:53:47 +0100 Subject: [PATCH 06/20] Also apply to pages --- src/wp-includes/blocks.php | 29 +++++++++++------------------ src/wp-includes/default-filters.php | 5 +++-- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 3c0829e2aa61a..67692bc196cd7 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -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 ) { + $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; diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 6645e14e18672..77b515fbf0ecd 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -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 ); From 6793b5575521cd1a42b1ee7b9f76a36faea057c0 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Nov 2024 15:57:00 +0100 Subject: [PATCH 07/20] Update PHPDoc --- src/wp-includes/blocks.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 67692bc196cd7..05beea7093f48 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -912,7 +912,7 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke * @param string $relative_position The relative position of the hooked blocks. * Can be one of 'before', 'after', 'first_child', or 'last_child'. * @param string $anchor_block_type The anchor block type. - * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type, + * @param WP_Block_Template|WP_Post|array $context The block template, template part, post type, * or pattern that the anchor block belongs to. */ $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); @@ -935,7 +935,7 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke * @param string $hooked_block_type The hooked block type name. * @param string $relative_position The relative position of the hooked block. * @param array $parsed_anchor_block The anchor block, in parsed block array format. - * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type, + * @param WP_Block_Template|WP_Post|array $context The block template, template part, post type, * or pattern that the anchor block belongs to. */ $parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ); @@ -951,7 +951,7 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke * @param string $hooked_block_type The hooked block type name. * @param string $relative_position The relative position of the hooked block. * @param array $parsed_anchor_block The anchor block, in parsed block array format. - * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type, + * @param WP_Block_Template|WP_Post|array $context The block template, template part, post type, * or pattern that the anchor block belongs to. */ $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ); @@ -1043,7 +1043,7 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po * @access private * * @param string $content Serialized content. - * @param WP_Block_Template|WP_Post|array|null $context A block template, template part, `wp_navigation` post object, + * @param WP_Block_Template|WP_Post|array|null $context A block template, template part, post object, * or pattern that the blocks belong to. If set to `null`, the * current post is used. * Default: `null`. @@ -1339,7 +1339,7 @@ function insert_hooked_blocks_into_rest_response( $response, $post ) { * @access private * * @param array $hooked_blocks An array of blocks hooked to another given block. - * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object, + * @param WP_Block_Template|WP_Post|array $context A block template, template part, post object, * or pattern that the blocks belong to. * @param callable $callback A function that will be called for each block to generate * the markup for a given list of blocks that are hooked to it. @@ -1396,7 +1396,7 @@ function make_before_block_visitor( $hooked_blocks, $context, $callback = 'inser * @access private * * @param array $hooked_blocks An array of blocks hooked to another block. - * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object, + * @param WP_Block_Template|WP_Post|array $context A block template, template part, post object, * or pattern that the blocks belong to. * @param callable $callback A function that will be called for each block to generate * the markup for a given list of blocks that are hooked to it. From 2958c64d38ef47b41bd15cb74576f85b3d54cba2 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Nov 2024 15:58:03 +0100 Subject: [PATCH 08/20] Tweak PHPDoc --- src/wp-includes/blocks.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 05beea7093f48..763168000dc06 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -912,7 +912,7 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke * @param string $relative_position The relative position of the hooked blocks. * Can be one of 'before', 'after', 'first_child', or 'last_child'. * @param string $anchor_block_type The anchor block type. - * @param WP_Block_Template|WP_Post|array $context The block template, template part, post type, + * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object, * or pattern that the anchor block belongs to. */ $hooked_block_types = apply_filters( 'hooked_block_types', $hooked_block_types, $relative_position, $anchor_block_type, $context ); @@ -935,7 +935,7 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke * @param string $hooked_block_type The hooked block type name. * @param string $relative_position The relative position of the hooked block. * @param array $parsed_anchor_block The anchor block, in parsed block array format. - * @param WP_Block_Template|WP_Post|array $context The block template, template part, post type, + * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object, * or pattern that the anchor block belongs to. */ $parsed_hooked_block = apply_filters( 'hooked_block', $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ); @@ -951,7 +951,7 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke * @param string $hooked_block_type The hooked block type name. * @param string $relative_position The relative position of the hooked block. * @param array $parsed_anchor_block The anchor block, in parsed block array format. - * @param WP_Block_Template|WP_Post|array $context The block template, template part, post type, + * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object, * or pattern that the anchor block belongs to. */ $parsed_hooked_block = apply_filters( "hooked_block_{$hooked_block_type}", $parsed_hooked_block, $hooked_block_type, $relative_position, $parsed_anchor_block, $context ); From 74b39fa51e7cf4a88b29f677accaa8c14601d691 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Nov 2024 16:00:03 +0100 Subject: [PATCH 09/20] More PHPDoc tweaking --- src/wp-includes/blocks.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 763168000dc06..227d93c9a9821 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1043,10 +1043,9 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po * @access private * * @param string $content Serialized content. - * @param WP_Block_Template|WP_Post|array|null $context A block template, template part, post object, - * or pattern that the blocks belong to. If set to `null`, the - * current post is used. - * Default: `null`. + * @param WP_Block_Template|WP_Post|array|null $context A block template, template part, post object, or pattern + * that the blocks belong to. If set to `null`, the current + * post is used. Default: `null`. * @param callable $callback A function that will be called for each block to generate * the markup for a given list of blocks that are hooked to it. * Default: 'insert_hooked_blocks'. From 007d7a451491b7efec40e48846b11369e70ee84f Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Nov 2024 18:40:47 +0100 Subject: [PATCH 10/20] Temporarily remove filter --- src/wp-includes/blocks.php | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 227d93c9a9821..f292b7552789d 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1318,9 +1318,20 @@ function insert_hooked_blocks_into_rest_response( $response, $post ) { $response->data['content']['raw'] = $content; + // No need to inject hooked blocks twice. + $priority = has_filter( 'the_content', 'apply_block_hooks_to_content' ); + if ( false !== $priority ) { + remove_filter( 'the_content', 'apply_block_hooks_to_content', $priority ); + } + /** This filter is documented in wp-includes/post-template.php */ $response->data['content']['rendered'] = apply_filters( 'the_content', $content ); + // Add back the filter. + if ( false !== $priority ) { + add_filter( 'the_content', 'apply_block_hooks_to_content', $priority ); + } + return $response; } From 1d98a276c00dbdf8e997df512c5aa3338c359cf9 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Nov 2024 19:35:38 +0100 Subject: [PATCH 11/20] Bail early if no content is given --- src/wp-includes/blocks.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index f292b7552789d..125d55771ef3e 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1052,6 +1052,10 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po * @return string The serialized markup. */ function apply_block_hooks_to_content( $content, $context = null, $callback = 'insert_hooked_blocks' ) { + if ( ! $content ) { + return $content; + } + // Default to the current post if no context is provided. if ( null === $context ) { $context = get_post(); From c6c848189a831f6f5c33984529e20d3d5c906ddc Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Nov 2024 19:39:55 +0100 Subject: [PATCH 12/20] Revert "Bail early if no content is given" This reverts commit 41d13d09e5376fd949683756d42afd209b6c0056. --- src/wp-includes/blocks.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 125d55771ef3e..f292b7552789d 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1052,10 +1052,6 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po * @return string The serialized markup. */ function apply_block_hooks_to_content( $content, $context = null, $callback = 'insert_hooked_blocks' ) { - if ( ! $content ) { - return $content; - } - // Default to the current post if no context is provided. if ( null === $context ) { $context = get_post(); From f29b288e98e352870e9197d29c7fa138458c525a Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 28 Nov 2024 19:40:22 +0100 Subject: [PATCH 13/20] Tighten checks --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index f292b7552789d..eb098638f35d0 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1282,7 +1282,7 @@ function insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata( &$parsed_a * @return WP_REST_Response The response object. */ function insert_hooked_blocks_into_rest_response( $response, $post ) { - if ( ! isset( $response->data['content']['raw'] ) || ! isset( $response->data['content']['rendered'] ) ) { + if ( empty( $response->data['content']['raw'] ) || empty( $response->data['content']['rendered'] ) ) { return $response; } From a5cb5985e22cce642ae8e38aaa5dd0855f3bdac8 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 3 Dec 2024 14:14:03 +0100 Subject: [PATCH 14/20] Add since PHPDoc --- src/wp-includes/blocks.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index eb098638f35d0..8d5d00c24175f 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1276,6 +1276,7 @@ function insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata( &$parsed_a * Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks. * * @since 6.6.0 + * @since 6.8.0 Support non-`wp_navigation` post types. * * @param WP_REST_Response $response The response object. * @param WP_Post $post Post object. From 886d3758ab7c7fdccd92954cfcfab7539e0cb9b6 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 3 Dec 2024 14:50:57 +0100 Subject: [PATCH 15/20] Update ignoredHookedBlocks post meta on save --- src/wp-includes/blocks.php | 24 ++++++++++++------------ src/wp-includes/default-filters.php | 2 ++ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 8d5d00c24175f..b503bac911201 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1172,10 +1172,11 @@ function extract_serialized_parent_block( $serialized_block ) { } /** - * Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content. - * Currently only supports `wp_navigation` post types. + * Updates the wp_postmeta with the list of ignored hooked blocks + * where the inner blocks are stored as post content. * * @since 6.6.0 + * @since 6.8.0 Support non-`wp_navigation` post types. * @access private * * @param stdClass $post Post object. @@ -1183,7 +1184,7 @@ function extract_serialized_parent_block( $serialized_block ) { */ function update_ignored_hooked_blocks_postmeta( $post ) { /* - * In this scenario the user has likely tried to create a navigation via the REST API. + * In this scenario the user has likely tried to create a new post object via the REST API. * In which case we won't have a post ID to work with and store meta against. */ if ( empty( $post->ID ) ) { @@ -1191,20 +1192,13 @@ function update_ignored_hooked_blocks_postmeta( $post ) { } /* - * Skip meta generation when consumers intentionally update specific Navigation fields + * Skip meta generation when consumers intentionally update specific fields * and omit the content update. */ if ( ! isset( $post->post_content ) ) { return $post; } - /* - * Skip meta generation when the post content is not a navigation block. - */ - if ( ! isset( $post->post_type ) || 'wp_navigation' !== $post->post_type ) { - return $post; - } - $attributes = array(); $ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true ); @@ -1215,8 +1209,14 @@ function update_ignored_hooked_blocks_postmeta( $post ) { ); } + if ( 'wp_navigation' === $post->post_type ) { + $wrapper_block_type = 'core/navigation'; + } else { + $wrapper_block_type = 'core/post-content'; + } + $markup = get_comment_delimited_block_content( - 'core/navigation', + $wrapper_block_type, $attributes, $post->post_content ); diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 77b515fbf0ecd..18ef8517edce2 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -761,6 +761,8 @@ add_filter( 'rest_pre_insert_wp_template_part', 'inject_ignored_hooked_blocks_metadata_attributes' ); // Update ignoredHookedBlocks postmeta for wp_navigation post type. +add_filter( 'rest_pre_insert_page', 'update_ignored_hooked_blocks_postmeta' ); +add_filter( 'rest_pre_insert_post', 'update_ignored_hooked_blocks_postmeta' ); add_filter( 'rest_pre_insert_wp_navigation', 'update_ignored_hooked_blocks_postmeta' ); // Inject hooked blocks into the Posts endpoint REST response for some given post types. From 89b835d496b6244294b1ee20a51c0a72745a1c76 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 3 Dec 2024 18:28:52 +0100 Subject: [PATCH 16/20] Add back check for post_type --- src/wp-includes/blocks.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index b503bac911201..2841c6dc2978e 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1199,6 +1199,13 @@ function update_ignored_hooked_blocks_postmeta( $post ) { return $post; } + /* + * Skip meta generation if post type is not set. + */ + if ( ! isset( $post->post_type ) ) { + return $post; + } + $attributes = array(); $ignored_hooked_blocks = get_post_meta( $post->ID, '_wp_ignored_hooked_blocks', true ); From e14db01d3446f5d3406c261237efd63e53102480 Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Tue, 3 Dec 2024 18:49:32 +0100 Subject: [PATCH 17/20] Tweak @since PHPDoc --- src/wp-includes/blocks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 2841c6dc2978e..b09acebca159b 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1039,7 +1039,7 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po * * @since 6.6.0 * @since 6.7.0 Injects the `theme` attribute into Template Part blocks, even if no hooked blocks are registered. - * @since 6.8.0 Have the `$context` parameter default to `null`, in which case the current post will be used. + * @since 6.8.0 Have the `$context` parameter default to `null`, in which case `get_post()` will be called to use the current post as context. * @access private * * @param string $content Serialized content. From 7635d2380de76adb9b13dc345de8a311431b23d9 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 3 Dec 2024 18:51:34 +0100 Subject: [PATCH 18/20] Tweak param description --- src/wp-includes/blocks.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index b09acebca159b..20c24af80bc0a 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1044,8 +1044,9 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po * * @param string $content Serialized content. * @param WP_Block_Template|WP_Post|array|null $context A block template, template part, post object, or pattern - * that the blocks belong to. If set to `null`, the current - * post is used. Default: `null`. + * that the blocks belong to. If set to `null`, `get_post()` + * will be called to use the current post as context. + * Default: `null`. * @param callable $callback A function that will be called for each block to generate * the markup for a given list of blocks that are hooked to it. * Default: 'insert_hooked_blocks'. From 2b7d36a564f6c86bfe47173c71643c159aa7d40c Mon Sep 17 00:00:00 2001 From: Bernie Reiter <96308+ockham@users.noreply.github.com> Date: Mon, 16 Dec 2024 15:41:39 +0100 Subject: [PATCH 19/20] Add comments to clarify removal/re-adding of `the_content` filter Co-authored-by: Jon Surrell --- src/wp-includes/blocks.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/blocks.php b/src/wp-includes/blocks.php index 20c24af80bc0a..6a4ab4636c803 100644 --- a/src/wp-includes/blocks.php +++ b/src/wp-includes/blocks.php @@ -1327,7 +1327,7 @@ function insert_hooked_blocks_into_rest_response( $response, $post ) { $response->data['content']['raw'] = $content; - // No need to inject hooked blocks twice. + // `apply_block_hooks_to_content` is called above. Ensure it is not called again as a filter. $priority = has_filter( 'the_content', 'apply_block_hooks_to_content' ); if ( false !== $priority ) { remove_filter( 'the_content', 'apply_block_hooks_to_content', $priority ); @@ -1336,7 +1336,7 @@ function insert_hooked_blocks_into_rest_response( $response, $post ) { /** This filter is documented in wp-includes/post-template.php */ $response->data['content']['rendered'] = apply_filters( 'the_content', $content ); - // Add back the filter. + // Restore the filter if it was set initially. if ( false !== $priority ) { add_filter( 'the_content', 'apply_block_hooks_to_content', $priority ); } From a7693b07ac91f8c2ce267bdd8828da6a3d7a4081 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Mon, 16 Dec 2024 17:58:16 +0100 Subject: [PATCH 20/20] Add test case for context set to null --- .../tests/blocks/applyBlockHooksToContent.php | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/tests/phpunit/tests/blocks/applyBlockHooksToContent.php b/tests/phpunit/tests/blocks/applyBlockHooksToContent.php index f238ef6ff5266..22e3c6e8dffb5 100644 --- a/tests/phpunit/tests/blocks/applyBlockHooksToContent.php +++ b/tests/phpunit/tests/blocks/applyBlockHooksToContent.php @@ -91,6 +91,26 @@ public function test_apply_block_hooks_to_content_inserts_hooked_block() { ); } + /** + * @ticket 61074 + */ + public function test_apply_block_hooks_to_content_with_context_set_to_null() { + $content = ''; + + /* + * apply_block_hooks_to_content() will fall back to the global $post object (via get_post()) + * if the $context parameter is null. However, we'd also like to ensure that the function + * works as expected even when get_post() returns null. + */ + $this->assertNull( get_post() ); + + $actual = apply_block_hooks_to_content( $content, null, 'insert_hooked_blocks' ); + $this->assertSame( + '', + $actual + ); + } + /** * @ticket 61902 */