@@ -912,7 +912,7 @@ function insert_hooked_blocks( &$parsed_anchor_block, $relative_position, $hooke
912912 * @param string $relative_position The relative position of the hooked blocks.
913913 * Can be one of 'before', 'after', 'first_child', or 'last_child'.
914914 * @param string $anchor_block_type The anchor block type.
915- * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type ,
915+ * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object ,
916916 * or pattern that the anchor block belongs to.
917917 */
918918 $ 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
935935 * @param string $hooked_block_type The hooked block type name.
936936 * @param string $relative_position The relative position of the hooked block.
937937 * @param array $parsed_anchor_block The anchor block, in parsed block array format.
938- * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type ,
938+ * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object ,
939939 * or pattern that the anchor block belongs to.
940940 */
941941 $ 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
951951 * @param string $hooked_block_type The hooked block type name.
952952 * @param string $relative_position The relative position of the hooked block.
953953 * @param array $parsed_anchor_block The anchor block, in parsed block array format.
954- * @param WP_Block_Template|WP_Post|array $context The block template, template part, `wp_navigation` post type ,
954+ * @param WP_Block_Template|WP_Post|array $context The block template, template part, post object ,
955955 * or pattern that the anchor block belongs to.
956956 */
957957 $ parsed_hooked_block = apply_filters ( "hooked_block_ {$ hooked_block_type }" , $ parsed_hooked_block , $ hooked_block_type , $ relative_position , $ parsed_anchor_block , $ context );
@@ -1039,17 +1039,25 @@ function set_ignored_hooked_blocks_metadata( &$parsed_anchor_block, $relative_po
10391039 *
10401040 * @since 6.6.0
10411041 * @since 6.7.0 Injects the `theme` attribute into Template Part blocks, even if no hooked blocks are registered.
1042+ * @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.
10421043 * @access private
10431044 *
1044- * @param string $content Serialized content.
1045- * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object,
1046- * or pattern that the blocks belong to.
1047- * @param callable $callback A function that will be called for each block to generate
1048- * the markup for a given list of blocks that are hooked to it.
1049- * Default: 'insert_hooked_blocks'.
1045+ * @param string $content Serialized content.
1046+ * @param WP_Block_Template|WP_Post|array|null $context A block template, template part, post object, or pattern
1047+ * that the blocks belong to. If set to `null`, `get_post()`
1048+ * will be called to use the current post as context.
1049+ * Default: `null`.
1050+ * @param callable $callback A function that will be called for each block to generate
1051+ * the markup for a given list of blocks that are hooked to it.
1052+ * Default: 'insert_hooked_blocks'.
10501053 * @return string The serialized markup.
10511054 */
1052- function apply_block_hooks_to_content ( $ content , $ context , $ callback = 'insert_hooked_blocks ' ) {
1055+ function apply_block_hooks_to_content ( $ content , $ context = null , $ callback = 'insert_hooked_blocks ' ) {
1056+ // Default to the current post if no context is provided.
1057+ if ( null === $ context ) {
1058+ $ context = get_post ();
1059+ }
1060+
10531061 $ hooked_blocks = get_hooked_blocks ();
10541062
10551063 $ before_block_visitor = '_inject_theme_attribute_in_template_part_block ' ;
@@ -1165,36 +1173,37 @@ function extract_serialized_parent_block( $serialized_block ) {
11651173}
11661174
11671175/**
1168- * Updates the wp_postmeta with the list of ignored hooked blocks where the inner blocks are stored as post content.
1169- * Currently only supports `wp_navigation` post types .
1176+ * Updates the wp_postmeta with the list of ignored hooked blocks
1177+ * where the inner blocks are stored as post content .
11701178 *
11711179 * @since 6.6.0
1180+ * @since 6.8.0 Support non-`wp_navigation` post types.
11721181 * @access private
11731182 *
11741183 * @param stdClass $post Post object.
11751184 * @return stdClass The updated post object.
11761185 */
11771186function update_ignored_hooked_blocks_postmeta ( $ post ) {
11781187 /*
1179- * In this scenario the user has likely tried to create a navigation via the REST API.
1188+ * In this scenario the user has likely tried to create a new post object via the REST API.
11801189 * In which case we won't have a post ID to work with and store meta against.
11811190 */
11821191 if ( empty ( $ post ->ID ) ) {
11831192 return $ post ;
11841193 }
11851194
11861195 /*
1187- * Skip meta generation when consumers intentionally update specific Navigation fields
1196+ * Skip meta generation when consumers intentionally update specific fields
11881197 * and omit the content update.
11891198 */
11901199 if ( ! isset ( $ post ->post_content ) ) {
11911200 return $ post ;
11921201 }
11931202
11941203 /*
1195- * Skip meta generation when the post content is not a navigation block .
1204+ * Skip meta generation if post type is not set .
11961205 */
1197- if ( ! isset ( $ post ->post_type ) || ' wp_navigation ' !== $ post -> post_type ) {
1206+ if ( ! isset ( $ post ->post_type ) ) {
11981207 return $ post ;
11991208 }
12001209
@@ -1208,8 +1217,14 @@ function update_ignored_hooked_blocks_postmeta( $post ) {
12081217 );
12091218 }
12101219
1220+ if ( 'wp_navigation ' === $ post ->post_type ) {
1221+ $ wrapper_block_type = 'core/navigation ' ;
1222+ } else {
1223+ $ wrapper_block_type = 'core/post-content ' ;
1224+ }
1225+
12111226 $ markup = get_comment_delimited_block_content (
1212- ' core/navigation ' ,
1227+ $ wrapper_block_type ,
12131228 $ attributes ,
12141229 $ post ->post_content
12151230 );
@@ -1266,16 +1281,17 @@ function insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata( &$parsed_a
12661281}
12671282
12681283/**
1269- * Hooks into the REST API response for the core/navigation block and adds the first and last inner blocks.
1284+ * Hooks into the REST API response for the Posts endpoint and adds the first and last inner blocks.
12701285 *
12711286 * @since 6.6.0
1287+ * @since 6.8.0 Support non-`wp_navigation` post types.
12721288 *
12731289 * @param WP_REST_Response $response The response object.
12741290 * @param WP_Post $post Post object.
12751291 * @return WP_REST_Response The response object.
12761292 */
12771293function insert_hooked_blocks_into_rest_response ( $ response , $ post ) {
1278- if ( ! isset ( $ response ->data ['content ' ]['raw ' ] ) || ! isset ( $ response ->data ['content ' ]['rendered ' ] ) ) {
1294+ if ( empty ( $ response ->data ['content ' ]['raw ' ] ) || empty ( $ response ->data ['content ' ]['rendered ' ] ) ) {
12791295 return $ response ;
12801296 }
12811297
@@ -1287,22 +1303,44 @@ function insert_hooked_blocks_into_rest_response( $response, $post ) {
12871303 'ignoredHookedBlocks ' => $ ignored_hooked_blocks ,
12881304 );
12891305 }
1306+
1307+ if ( 'wp_navigation ' === $ post ->post_type ) {
1308+ $ wrapper_block_type = 'core/navigation ' ;
1309+ } else {
1310+ $ wrapper_block_type = 'core/post-content ' ;
1311+ }
1312+
12901313 $ content = get_comment_delimited_block_content (
1291- ' core/navigation ' ,
1314+ $ wrapper_block_type ,
12921315 $ attributes ,
12931316 $ response ->data ['content ' ]['raw ' ]
12941317 );
12951318
1296- $ content = apply_block_hooks_to_content ( $ content , $ post );
1319+ $ content = apply_block_hooks_to_content (
1320+ $ content ,
1321+ $ post ,
1322+ 'insert_hooked_blocks_and_set_ignored_hooked_blocks_metadata '
1323+ );
12971324
1298- // Remove mock Navigation block wrapper.
1325+ // Remove mock block wrapper.
12991326 $ content = remove_serialized_parent_block ( $ content );
13001327
13011328 $ response ->data ['content ' ]['raw ' ] = $ content ;
13021329
1330+ // `apply_block_hooks_to_content` is called above. Ensure it is not called again as a filter.
1331+ $ priority = has_filter ( 'the_content ' , 'apply_block_hooks_to_content ' );
1332+ if ( false !== $ priority ) {
1333+ remove_filter ( 'the_content ' , 'apply_block_hooks_to_content ' , $ priority );
1334+ }
1335+
13031336 /** This filter is documented in wp-includes/post-template.php */
13041337 $ response ->data ['content ' ]['rendered ' ] = apply_filters ( 'the_content ' , $ content );
13051338
1339+ // Restore the filter if it was set initially.
1340+ if ( false !== $ priority ) {
1341+ add_filter ( 'the_content ' , 'apply_block_hooks_to_content ' , $ priority );
1342+ }
1343+
13061344 return $ response ;
13071345}
13081346
@@ -1320,7 +1358,7 @@ function insert_hooked_blocks_into_rest_response( $response, $post ) {
13201358 * @access private
13211359 *
13221360 * @param array $hooked_blocks An array of blocks hooked to another given block.
1323- * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object,
1361+ * @param WP_Block_Template|WP_Post|array $context A block template, template part, post object,
13241362 * or pattern that the blocks belong to.
13251363 * @param callable $callback A function that will be called for each block to generate
13261364 * the markup for a given list of blocks that are hooked to it.
@@ -1377,7 +1415,7 @@ function make_before_block_visitor( $hooked_blocks, $context, $callback = 'inser
13771415 * @access private
13781416 *
13791417 * @param array $hooked_blocks An array of blocks hooked to another block.
1380- * @param WP_Block_Template|WP_Post|array $context A block template, template part, `wp_navigation` post object,
1418+ * @param WP_Block_Template|WP_Post|array $context A block template, template part, post object,
13811419 * or pattern that the blocks belong to.
13821420 * @param callable $callback A function that will be called for each block to generate
13831421 * the markup for a given list of blocks that are hooked to it.
0 commit comments