WordPress Version: 6.5
/**
* Retrieves the appropriate fallback to be used on the front of the
* site when there is no menu assigned to the Nav block.
*
* This aims to mirror how the fallback mechanic for wp_nav_menu works.
* See https://developer.wordpress.org/reference/functions/wp_nav_menu/#more-information.
*
* @return array the array of blocks to be used as a fallback.
*/
function block_core_navigation_get_fallback_blocks()
{
$page_list_fallback = array(array('blockName' => 'core/page-list', 'innerContent' => array(), 'attrs' => array()));
$registry = WP_Block_Type_Registry::get_instance();
// If `core/page-list` is not registered then return empty blocks.
$fallback_blocks = $registry->is_registered('core/page-list') ? $page_list_fallback : array();
$navigation_post = WP_Navigation_Fallback::get_fallback();
// Use the first non-empty Navigation as fallback if available.
if ($navigation_post) {
$parsed_blocks = parse_blocks($navigation_post->post_content);
$maybe_fallback = block_core_navigation_filter_out_empty_blocks($parsed_blocks);
// Normalizing blocks may result in an empty array of blocks if they were all `null` blocks.
// In this case default to the (Page List) fallback.
$fallback_blocks = (!empty($maybe_fallback)) ? $maybe_fallback : $fallback_blocks;
if (function_exists('set_ignored_hooked_blocks_metadata')) {
// Run Block Hooks algorithm to inject hooked blocks.
// We have to run it here because we need the post ID of the Navigation block to track ignored hooked blocks.
$markup = block_core_navigation_insert_hooked_blocks($fallback_blocks, $navigation_post);
$blocks = parse_blocks($markup);
if (isset($blocks[0]['innerBlocks'])) {
$fallback_blocks = $blocks[0]['innerBlocks'];
}
}
}
/**
* Filters the fallback experience for the Navigation block.
*
* Returning a falsey value will opt out of the fallback and cause the block not to render.
* To customise the blocks provided return an array of blocks - these should be valid
* children of the `core/navigation` block.
*
* @since 5.9.0
*
* @param array[] $fallback_blocks default fallback blocks provided by the default block mechanic.
*/
return apply_filters('block_core_navigation_render_fallback', $fallback_blocks);
}