render_block_core_navigation() – Renders the `core/navigation` block on server.

You appear to be a bot. Output may be restricted

Description

Renders the core/navigation block on server.

Usage

$string = render_block_core_navigation( $attributes, $content, $block );

Parameters

$attributes
( array ) required – The block attributes.
$content
( string ) required – The saved content.
$block
( WP_Block ) required – The parsed block.

Returns

string Returns the post content with the legacy widget added.

Source

File name: wordpress/wp-includes/blocks/navigation.php


Lines:

1 to 100 of 246
function render_block_core_navigation( $attributes, $content, $block ) {

  static $seen_menu_names = array();

  // Flag used to indicate whether the rendered output is considered to be
  // a fallback (i.e. the block has no menu associated with it).
  $is_fallback = false;

  $nav_menu_name = '';

  
/**
 * Deprecated:
 * The rgbTextColor and rgbBackgroundColor attributes
 * have been deprecated in favor of
 * customTextColor and customBackgroundColor ones.
 * Move the values from old attrs to the new ones.
 */
  if ( isset( $attributes['rgbTextColor'] ) && empty( $attributes['textColor'] ) ) {
    $attributes['customTextColor'] = $attributes['rgbTextColor'];
  }

  if ( isset( $attributes['rgbBackgroundColor'] ) && empty( $attributes['backgroundColor'] ) ) {
    $attributes['customBackgroundColor'] = $attributes['rgbBackgroundColor'];
  }

  unset( $attributes['rgbTextColor'], $attributes['rgbBackgroundColor'] );

  
/**
 * This is for backwards compatibility after `isResponsive` attribute has been removed.
 */
  $has_old_responsive_attribute = ! empty( $attributes['isResponsive'] ) && $attributes['isResponsive'];
  $is_responsive_menu           = isset( $attributes['overlayMenu'] ) && 'never' !== $attributes['overlayMenu'] || $has_old_responsive_attribute;
  $should_load_view_script      = ! wp_script_is( 'wp-block-navigation-view' ) && ( $is_responsive_menu || $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] );
  if ( $should_load_view_script ) {
    wp_enqueue_script( 'wp-block-navigation-view' );
  }

  $inner_blocks = $block->inner_blocks;

  // Ensure that blocks saved with the legacy ref attribute name (navigationMenuId) continue to render.
  if ( array_key_exists( 'navigationMenuId', $attributes ) ) {
    $attributes['ref'] = $attributes['navigationMenuId'];
  }

  // If:
  // - the gutenberg plugin is active
  // - `__unstableLocation` is defined
  // - we have menu items at the defined location
  // - we don't have a relationship to a `wp_navigation` Post (via `ref`).
  // ...then create inner blocks from the classic menu assigned to that location.
  if (
    defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN &&
    array_key_exists( '__unstableLocation', $attributes ) &&
    ! array_key_exists( 'ref', $attributes ) &&
    ! empty( block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] ) )
  ) {
    $menu_items = block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] );
    if ( empty( $menu_items ) ) {
      return '';
    }

    $menu_items_by_parent_id = block_core_navigation_sort_menu_items_by_parent_id( $menu_items );
    $parsed_blocks           = block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[0], $menu_items_by_parent_id );
    $inner_blocks            = new WP_Block_List( $parsed_blocks, $attributes );
  }

  // Load inner blocks from the navigation post.
  if ( array_key_exists( 'ref', $attributes ) ) {
    $navigation_post = get_post( $attributes['ref'] );
    if ( ! isset( $navigation_post ) ) {
      return '';
    }

    $nav_menu_name = $navigation_post->post_title;

    if ( isset( $seen_menu_names[ $nav_menu_name ] ) ) {
      ++$seen_menu_names[ $nav_menu_name ];
    } else {
      $seen_menu_names[ $nav_menu_name ] = 1;
    }

    $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.
    $compacted_blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks );

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

  // If there are no inner blocks then fallback to rendering an appropriate fallback.
  if ( empty( $inner_blocks ) ) {
    $is_fallback = true; // indicate we are rendering the fallback.

    $fallback_blocks = block_core_navigation_get_fallback_blocks();

    // Fallback my have been filtered so do basic test for validity.

 View on GitHub View on Trac