WP_Navigation_Block_Renderer::get_overlay_blocks_from_template_part
Gets the inner blocks for the navigation block from an overlay template part.
Method of the class: WP_Navigation_Block_Renderer{}
No Hooks.
Returns
WP_Block_List. Returns the inner blocks for the overlay template part.
Usage
$result = WP_Navigation_Block_Renderer::get_overlay_blocks_from_template_part( $overlay_template_part_id, $attributes );
- $overlay_template_part_id(string) (required)
- The overlay template part ID in format "theme//slug".
- $attributes(array) (required)
- The block attributes.
Changelog
| Since 6.5.0 | Introduced. |
WP_Navigation_Block_Renderer::get_overlay_blocks_from_template_part() WP Navigation Block Renderer::get overlay blocks from template part code WP 7.0
private static function get_overlay_blocks_from_template_part( $overlay_template_part_id, $attributes ) {
if ( empty( $overlay_template_part_id ) || ! is_string( $overlay_template_part_id ) ) {
return new WP_Block_List( array(), $attributes );
}
// Parse the template part ID (format: "theme//slug").
// If it's just a slug, construct the full ID using the current theme.
$parts = explode( '//', $overlay_template_part_id, 2 );
if ( count( $parts ) === 2 ) {
// Already in "theme//slug" format (backward compatibility).
$theme = $parts[0];
$slug = $parts[1];
} else {
// Just a slug, use current theme.
$theme = get_stylesheet();
$slug = $overlay_template_part_id;
}
// Only query for template parts from the active theme.
if ( get_stylesheet() !== $theme ) {
return new WP_Block_List( array(), $attributes );
}
// Query for the template part post.
$template_part_query = new WP_Query(
array(
'post_type' => 'wp_template_part',
'post_status' => 'publish',
'post_name__in' => array( $slug ),
'tax_query' => array(
array(
'taxonomy' => 'wp_theme',
'field' => 'name',
'terms' => $theme,
),
),
'posts_per_page' => 1,
'no_found_rows' => true,
'lazy_load_term_meta' => false, // Do not lazy load term meta, as template parts only have one term.
)
);
$template_part_post = $template_part_query->have_posts() ? $template_part_query->next_post() : null;
if ( ! $template_part_post ) {
// Try to get from theme file if not in database.
// Construct the full template part ID for get_block_file_template.
$full_template_part_id = $theme . '//' . $slug;
$block_template = get_block_file_template( $full_template_part_id, 'wp_template_part' );
if ( isset( $block_template->content ) ) {
// Expand shortcodes before parsing blocks, matching the order in
// `render_block_core_template_part()`.
$content = shortcode_unautop( $block_template->content );
$content = do_shortcode( $content );
$parsed_blocks = parse_blocks( $content );
$blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks );
// Disable overlay menu for any navigation blocks within the overlay to prevent nested overlays.
$blocks = static::disable_overlay_menu_for_nested_navigation_blocks( $blocks );
return new WP_Block_List( $blocks, $attributes );
}
return new WP_Block_List( array(), $attributes );
}
// Get the template part content.
$block_template = _build_block_template_result_from_post( $template_part_post );
if ( ! isset( $block_template->content ) ) {
return new WP_Block_List( array(), $attributes );
}
$parsed_blocks = parse_blocks( $block_template->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.
$markup = serialize_blocks( $blocks );
$markup = apply_block_hooks_to_content_from_post_object( $markup, $template_part_post );
// Expand shortcodes before parsing blocks, matching the order in
// `render_block_core_template_part()`.
$markup = shortcode_unautop( $markup );
$markup = do_shortcode( $markup );
$blocks = parse_blocks( $markup );
// Disable overlay menu for any navigation blocks within the overlay to prevent nested overlays.
$blocks = static::disable_overlay_menu_for_nested_navigation_blocks( $blocks );
return new WP_Block_List( $blocks, $attributes );
}