WP_Navigation_Block_Renderer::get_inner_blocks_from_navigation_postprivate staticWP 6.5.0

Gets the inner blocks for the navigation block from the navigation post.

Method of the class: WP_Navigation_Block_Renderer{}

No Hooks.

Returns

WP_Block_List. Returns the inner blocks for the navigation block.

Usage

$result = WP_Navigation_Block_Renderer::get_inner_blocks_from_navigation_post( $attributes );
$attributes(array) (required)
The block attributes.

Changelog

Since 6.5.0 Introduced.

WP_Navigation_Block_Renderer::get_inner_blocks_from_navigation_post() code WP 6.9.1

private static function get_inner_blocks_from_navigation_post( $attributes ) {
	$navigation_post = get_post( $attributes['ref'] );
	if ( ! isset( $navigation_post ) ) {
		return new WP_Block_List( array(), $attributes );
	}

	// Only published posts are valid. If this is changed then a corresponding change
	// must also be implemented in `use-navigation-menu.js`.
	if ( 'publish' === $navigation_post->post_status ) {
		$parsed_blocks = parse_blocks( $navigation_post->post_content );

		// 'parse_blocks' includes a null block with '\n\n' as the content when
		// it encounters whitespace. This code strips it.
		$blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks );

		// Re-serialize, and run Block Hooks algorithm to inject hooked blocks.
		// TODO: See if we can move the apply_block_hooks_to_content_from_post_object() call
		// before the parse_blocks() call further above, to avoid the extra serialization/parsing.
		$markup = serialize_blocks( $blocks );
		$markup = apply_block_hooks_to_content_from_post_object( $markup, $navigation_post );
		$blocks = parse_blocks( $markup );

		// TODO - this uses the full navigation block attributes for the
		// context which could be refined.
		return new WP_Block_List( $blocks, $attributes );
	}
}