block_core_navigation_insert_hooked_blocks()WP 6.5.0

Insert hooked blocks into a Navigation block.

Given a Navigation block's inner blocks and its corresponding wp_navigation post object, this function inserts hooked blocks into it, and returns the serialized inner blocks in a mock Navigation block wrapper.

If there are any hooked blocks that need to be inserted as the Navigation block's first or last children, the wp_navigation post's _wp_ignored_hooked_blocks meta is checked to see if any of those hooked blocks should be exempted from insertion.

No Hooks.

Return

String. Serialized inner blocks in mock Navigation block wrapper, with hooked blocks inserted, if any.

Usage

block_core_navigation_insert_hooked_blocks( $inner_blocks, $post );
$inner_blocks(array) (required)
Parsed inner blocks of a Navigation block.
$post(WP_Post) (required)
wp_navigation post object corresponding to the block.

Changelog

Since 6.5.0 Introduced.

block_core_navigation_insert_hooked_blocks() code WP 6.7.1

function block_core_navigation_insert_hooked_blocks( $inner_blocks, $post ) {
	$mock_navigation_block = block_core_navigation_mock_parsed_block( $inner_blocks, $post );

	if ( function_exists( 'apply_block_hooks_to_content' ) ) {
		$mock_navigation_block_markup = serialize_block( $mock_navigation_block );
		return apply_block_hooks_to_content( $mock_navigation_block_markup, $post, 'insert_hooked_blocks' );
	}

	$hooked_blocks        = get_hooked_blocks();
	$before_block_visitor = null;
	$after_block_visitor  = null;

	if ( ! empty( $hooked_blocks ) || has_filter( 'hooked_block_types' ) ) {
		$before_block_visitor = make_before_block_visitor( $hooked_blocks, $post, 'insert_hooked_blocks' );
		$after_block_visitor  = make_after_block_visitor( $hooked_blocks, $post, 'insert_hooked_blocks' );
	}

	return traverse_and_serialize_block( $mock_navigation_block, $before_block_visitor, $after_block_visitor );
}