[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> navigation.php (source)

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/navigation` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Helper functions used to render the navigation block.
  10   *
  11   * @since 6.5.0
  12   */
  13  class WP_Navigation_Block_Renderer {
  14  
  15      /**
  16       * Used to determine whether or not a navigation has submenus.
  17       *
  18       * @since 6.5.0
  19       */
  20      private static $has_submenus = false;
  21  
  22      /**
  23       * Used to determine which blocks need an <li> wrapper.
  24       *
  25       * @since 6.5.0
  26       *
  27       * @var array
  28       */
  29      private static $needs_list_item_wrapper = array(
  30          'core/site-title',
  31          'core/site-logo',
  32          'core/social-links',
  33      );
  34  
  35      /**
  36       * Keeps track of all the navigation names that have been seen.
  37       *
  38       * @since 6.5.0
  39       *
  40       * @var array
  41       */
  42      private static $seen_menu_names = array();
  43  
  44      /**
  45       * Returns whether or not this is responsive navigation.
  46       *
  47       * @since 6.5.0
  48       *
  49       * @param array $attributes The block attributes.
  50       * @return bool Returns whether or not this is responsive navigation.
  51       */
  52  	private static function is_responsive( $attributes ) {
  53          /**
  54           * This is for backwards compatibility after the `isResponsive` attribute was been removed.
  55           */
  56  
  57          $has_old_responsive_attribute = ! empty( $attributes['isResponsive'] ) && $attributes['isResponsive'];
  58          return isset( $attributes['overlayMenu'] ) && 'never' !== $attributes['overlayMenu'] || $has_old_responsive_attribute;
  59      }
  60  
  61      /**
  62       * Returns whether or not a navigation has a submenu.
  63       *
  64       * @since 6.5.0
  65       *
  66       * @param WP_Block_List $inner_blocks The list of inner blocks.
  67       * @return bool Returns whether or not a navigation has a submenu and also sets the member variable.
  68       */
  69  	private static function has_submenus( $inner_blocks ) {
  70          if ( true === static::$has_submenus ) {
  71              return static::$has_submenus;
  72          }
  73  
  74          foreach ( $inner_blocks as $inner_block ) {
  75              // If this is a page list then work out if any of the pages have children.
  76              if ( 'core/page-list' === $inner_block->name ) {
  77                  $all_pages = get_pages(
  78                      array(
  79                          'sort_column' => 'menu_order,post_title',
  80                          'order'       => 'asc',
  81                      )
  82                  );
  83                  foreach ( (array) $all_pages as $page ) {
  84                      if ( $page->post_parent ) {
  85                          static::$has_submenus = true;
  86                          break;
  87                      }
  88                  }
  89              }
  90              // If this is a navigation submenu then we know we have submenus.
  91              if ( 'core/navigation-submenu' === $inner_block->name ) {
  92                  static::$has_submenus = true;
  93                  break;
  94              }
  95          }
  96  
  97          return static::$has_submenus;
  98      }
  99  
 100      /**
 101       * Determine whether the navigation blocks is interactive.
 102       *
 103       * @since 6.5.0
 104       *
 105       * @param array         $attributes   The block attributes.
 106       * @param WP_Block_List $inner_blocks The list of inner blocks.
 107       * @return bool Returns whether or not to load the view script.
 108       */
 109  	private static function is_interactive( $attributes, $inner_blocks ) {
 110          $has_submenus       = static::has_submenus( $inner_blocks );
 111          $is_responsive_menu = static::is_responsive( $attributes );
 112          return ( $has_submenus && ( $attributes['openSubmenusOnClick'] || $attributes['showSubmenuIcon'] ) ) || $is_responsive_menu;
 113      }
 114  
 115      /**
 116       * Returns whether or not a block needs a list item wrapper.
 117       *
 118       * @since 6.5.0
 119       *
 120       * @param WP_Block $block The block.
 121       * @return bool Returns whether or not a block needs a list item wrapper.
 122       */
 123  	private static function does_block_need_a_list_item_wrapper( $block ) {
 124  
 125          /**
 126           * Filter the list of blocks that need a list item wrapper.
 127           *
 128           * Affords the ability to customize which blocks need a list item wrapper when rendered
 129           * within a core/navigation block.
 130           * This is useful for blocks that are not list items but should be wrapped in a list
 131           * item when used as a child of a navigation block.
 132           *
 133           * @since 6.5.0
 134           *
 135           * @param array $needs_list_item_wrapper The list of blocks that need a list item wrapper.
 136           * @return array The list of blocks that need a list item wrapper.
 137           */
 138          $needs_list_item_wrapper = apply_filters( 'block_core_navigation_listable_blocks', static::$needs_list_item_wrapper );
 139  
 140          return in_array( $block->name, $needs_list_item_wrapper, true );
 141      }
 142  
 143      /**
 144       * Returns the markup for a single inner block.
 145       *
 146       * @since 6.5.0
 147       *
 148       * @param WP_Block $inner_block The inner block.
 149       * @return string Returns the markup for a single inner block.
 150       */
 151  	private static function get_markup_for_inner_block( $inner_block ) {
 152          $inner_block_content = $inner_block->render();
 153          if ( ! empty( $inner_block_content ) ) {
 154              if ( static::does_block_need_a_list_item_wrapper( $inner_block ) ) {
 155                  return '<li class="wp-block-navigation-item">' . $inner_block_content . '</li>';
 156              }
 157          }
 158  
 159          return $inner_block_content;
 160      }
 161  
 162      /**
 163       * Returns the html for the inner blocks of the navigation block.
 164       *
 165       * @since 6.5.0
 166       *
 167       * @param array         $attributes   The block attributes.
 168       * @param WP_Block_List $inner_blocks The list of inner blocks.
 169       * @return string Returns the html for the inner blocks of the navigation block.
 170       */
 171  	private static function get_inner_blocks_html( $attributes, $inner_blocks ) {
 172          $has_submenus   = static::has_submenus( $inner_blocks );
 173          $is_interactive = static::is_interactive( $attributes, $inner_blocks );
 174  
 175          $style                = static::get_styles( $attributes );
 176          $class                = static::get_classes( $attributes );
 177          $container_attributes = get_block_wrapper_attributes(
 178              array(
 179                  'class' => 'wp-block-navigation__container ' . $class,
 180                  'style' => $style,
 181              )
 182          );
 183  
 184          $inner_blocks_html = '';
 185          $is_list_open      = false;
 186  
 187          foreach ( $inner_blocks as $inner_block ) {
 188              $inner_block_markup = static::get_markup_for_inner_block( $inner_block );
 189              $p                  = new WP_HTML_Tag_Processor( $inner_block_markup );
 190              $is_list_item       = $p->next_tag( 'LI' );
 191  
 192              if ( $is_list_item && ! $is_list_open ) {
 193                  $is_list_open       = true;
 194                  $inner_blocks_html .= sprintf(
 195                      '<ul %1$s>',
 196                      $container_attributes
 197                  );
 198              }
 199  
 200              if ( ! $is_list_item && $is_list_open ) {
 201                  $is_list_open       = false;
 202                  $inner_blocks_html .= '</ul>';
 203              }
 204  
 205              $inner_blocks_html .= $inner_block_markup;
 206          }
 207  
 208          if ( $is_list_open ) {
 209              $inner_blocks_html .= '</ul>';
 210          }
 211  
 212          // Add directives to the submenu if needed.
 213          if ( $has_submenus && $is_interactive ) {
 214              $tags              = new WP_HTML_Tag_Processor( $inner_blocks_html );
 215              $inner_blocks_html = block_core_navigation_add_directives_to_submenu( $tags, $attributes );
 216          }
 217  
 218          return $inner_blocks_html;
 219      }
 220  
 221      /**
 222       * Gets the inner blocks for the navigation block from the navigation post.
 223       *
 224       * @since 6.5.0
 225       *
 226       * @param array $attributes The block attributes.
 227       * @return WP_Block_List Returns the inner blocks for the navigation block.
 228       */
 229  	private static function get_inner_blocks_from_navigation_post( $attributes ) {
 230          $navigation_post = get_post( $attributes['ref'] );
 231          if ( ! isset( $navigation_post ) ) {
 232              return new WP_Block_List( array(), $attributes );
 233          }
 234  
 235          // Only published posts are valid. If this is changed then a corresponding change
 236          // must also be implemented in `use-navigation-menu.js`.
 237          if ( 'publish' === $navigation_post->post_status ) {
 238              $parsed_blocks = parse_blocks( $navigation_post->post_content );
 239  
 240              // 'parse_blocks' includes a null block with '\n\n' as the content when
 241              // it encounters whitespace. This code strips it.
 242              $blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks );
 243  
 244              // Re-serialize, and run Block Hooks algorithm to inject hooked blocks.
 245              // TODO: See if we can move the apply_block_hooks_to_content_from_post_object() call
 246              // before the parse_blocks() call further above, to avoid the extra serialization/parsing.
 247              $markup = serialize_blocks( $blocks );
 248              $markup = apply_block_hooks_to_content_from_post_object( $markup, $navigation_post );
 249              $blocks = parse_blocks( $markup );
 250  
 251              // TODO - this uses the full navigation block attributes for the
 252              // context which could be refined.
 253              return new WP_Block_List( $blocks, $attributes );
 254          }
 255      }
 256  
 257      /**
 258       * Gets the inner blocks for the navigation block from the fallback.
 259       *
 260       * @since 6.5.0
 261       *
 262       * @param array $attributes The block attributes.
 263       * @return WP_Block_List Returns the inner blocks for the navigation block.
 264       */
 265  	private static function get_inner_blocks_from_fallback( $attributes ) {
 266          $fallback_blocks = block_core_navigation_get_fallback_blocks();
 267  
 268          // Fallback my have been filtered so do basic test for validity.
 269          if ( empty( $fallback_blocks ) || ! is_array( $fallback_blocks ) ) {
 270              return new WP_Block_List( array(), $attributes );
 271          }
 272  
 273          return new WP_Block_List( $fallback_blocks, $attributes );
 274      }
 275  
 276      /**
 277       * Gets the inner blocks for the navigation block.
 278       *
 279       * @since 6.5.0
 280       *
 281       * @param array    $attributes The block attributes.
 282       * @param WP_Block $block The parsed block.
 283       * @return WP_Block_List Returns the inner blocks for the navigation block.
 284       */
 285  	private static function get_inner_blocks( $attributes, $block ) {
 286          $inner_blocks = $block->inner_blocks;
 287  
 288          // Ensure that blocks saved with the legacy ref attribute name (navigationMenuId) continue to render.
 289          if ( array_key_exists( 'navigationMenuId', $attributes ) ) {
 290              $attributes['ref'] = $attributes['navigationMenuId'];
 291          }
 292  
 293          // If:
 294          // - the gutenberg plugin is active
 295          // - `__unstableLocation` is defined
 296          // - we have menu items at the defined location
 297          // - we don't have a relationship to a `wp_navigation` Post (via `ref`).
 298          // ...then create inner blocks from the classic menu assigned to that location.
 299          if (
 300              defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN &&
 301              array_key_exists( '__unstableLocation', $attributes ) &&
 302              ! array_key_exists( 'ref', $attributes ) &&
 303              ! empty( block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] ) )
 304          ) {
 305              $inner_blocks = block_core_navigation_get_inner_blocks_from_unstable_location( $attributes );
 306          }
 307  
 308          // Load inner blocks from the navigation post.
 309          if ( array_key_exists( 'ref', $attributes ) ) {
 310              $inner_blocks = static::get_inner_blocks_from_navigation_post( $attributes );
 311          }
 312  
 313          // If there are no inner blocks then fallback to rendering an appropriate fallback.
 314          if ( empty( $inner_blocks ) ) {
 315              $inner_blocks = static::get_inner_blocks_from_fallback( $attributes );
 316          }
 317  
 318          /**
 319           * Filter navigation block $inner_blocks.
 320           * Allows modification of a navigation block menu items.
 321           *
 322           * @since 6.1.0
 323           *
 324           * @param \WP_Block_List $inner_blocks
 325           */
 326          $inner_blocks = apply_filters( 'block_core_navigation_render_inner_blocks', $inner_blocks );
 327  
 328          $post_ids = block_core_navigation_get_post_ids( $inner_blocks );
 329          if ( $post_ids ) {
 330              _prime_post_caches( $post_ids, false, false );
 331          }
 332  
 333          return $inner_blocks;
 334      }
 335  
 336      /**
 337       * Gets the name of the current navigation, if it has one.
 338       *
 339       * @since 6.5.0
 340       *
 341       * @param array $attributes The block attributes.
 342       * @return string Returns the name of the navigation.
 343       */
 344  	private static function get_navigation_name( $attributes ) {
 345  
 346          $navigation_name = $attributes['ariaLabel'] ?? '';
 347  
 348          if ( ! empty( $navigation_name ) ) {
 349              return $navigation_name;
 350          }
 351  
 352          // Load the navigation post.
 353          if ( array_key_exists( 'ref', $attributes ) ) {
 354              $navigation_post = get_post( $attributes['ref'] );
 355              if ( ! isset( $navigation_post ) ) {
 356                  return $navigation_name;
 357              }
 358  
 359              // Only published posts are valid. If this is changed then a corresponding change
 360              // must also be implemented in `use-navigation-menu.js`.
 361              if ( 'publish' === $navigation_post->post_status ) {
 362                  $navigation_name = $navigation_post->post_title;
 363  
 364                  // This is used to count the number of times a navigation name has been seen,
 365                  // so that we can ensure every navigation has a unique id.
 366                  if ( isset( static::$seen_menu_names[ $navigation_name ] ) ) {
 367                      ++static::$seen_menu_names[ $navigation_name ];
 368                  } else {
 369                      static::$seen_menu_names[ $navigation_name ] = 1;
 370                  }
 371              }
 372          }
 373  
 374          return $navigation_name;
 375      }
 376  
 377      /**
 378       * Returns the layout class for the navigation block.
 379       *
 380       * @since 6.5.0
 381       *
 382       * @param array $attributes The block attributes.
 383       * @return string Returns the layout class for the navigation block.
 384       */
 385  	private static function get_layout_class( $attributes ) {
 386          $layout_justification = array(
 387              'left'          => 'items-justified-left',
 388              'right'         => 'items-justified-right',
 389              'center'        => 'items-justified-center',
 390              'space-between' => 'items-justified-space-between',
 391          );
 392  
 393          $layout_class = '';
 394          if (
 395              isset( $attributes['layout']['justifyContent'] ) &&
 396              isset( $layout_justification[ $attributes['layout']['justifyContent'] ] )
 397          ) {
 398              $layout_class .= $layout_justification[ $attributes['layout']['justifyContent'] ];
 399          }
 400          if ( isset( $attributes['layout']['orientation'] ) && 'vertical' === $attributes['layout']['orientation'] ) {
 401              $layout_class .= ' is-vertical';
 402          }
 403  
 404          if ( isset( $attributes['layout']['flexWrap'] ) && 'nowrap' === $attributes['layout']['flexWrap'] ) {
 405              $layout_class .= ' no-wrap';
 406          }
 407          return $layout_class;
 408      }
 409  
 410      /**
 411       * Return classes for the navigation block.
 412       *
 413       * @since 6.5.0
 414       *
 415       * @param array $attributes The block attributes.
 416       * @return string Returns the classes for the navigation block.
 417       */
 418  	private static function get_classes( $attributes ) {
 419          // Restore legacy classnames for submenu positioning.
 420          $layout_class       = static::get_layout_class( $attributes );
 421          $colors             = block_core_navigation_build_css_colors( $attributes );
 422          $font_sizes         = block_core_navigation_build_css_font_sizes( $attributes );
 423          $is_responsive_menu = static::is_responsive( $attributes );
 424  
 425          // Manually add block support text decoration as CSS class.
 426          $text_decoration       = $attributes['style']['typography']['textDecoration'] ?? null;
 427          $text_decoration_class = sprintf( 'has-text-decoration-%s', $text_decoration );
 428  
 429          $classes = array_merge(
 430              $colors['css_classes'],
 431              $font_sizes['css_classes'],
 432              $is_responsive_menu ? array( 'is-responsive' ) : array(),
 433              $layout_class ? array( $layout_class ) : array(),
 434              $text_decoration ? array( $text_decoration_class ) : array()
 435          );
 436          return implode( ' ', $classes );
 437      }
 438  
 439      /**
 440       * Get styles for the navigation block.
 441       *
 442       * @since 6.5.0
 443       *
 444       * @param array $attributes The block attributes.
 445       * @return string Returns the styles for the navigation block.
 446       */
 447  	private static function get_styles( $attributes ) {
 448          $colors       = block_core_navigation_build_css_colors( $attributes );
 449          $font_sizes   = block_core_navigation_build_css_font_sizes( $attributes );
 450          $block_styles = isset( $attributes['styles'] ) ? $attributes['styles'] : '';
 451          return $block_styles . $colors['inline_styles'] . $font_sizes['inline_styles'];
 452      }
 453  
 454      /**
 455       * Get the responsive container markup
 456       *
 457       * @since 6.5.0
 458       *
 459       * @param array         $attributes The block attributes.
 460       * @param WP_Block_List $inner_blocks The list of inner blocks.
 461       * @param string        $inner_blocks_html The markup for the inner blocks.
 462       * @return string Returns the container markup.
 463       */
 464  	private static function get_responsive_container_markup( $attributes, $inner_blocks, $inner_blocks_html ) {
 465          $is_interactive  = static::is_interactive( $attributes, $inner_blocks );
 466          $colors          = block_core_navigation_build_css_colors( $attributes );
 467          $modal_unique_id = wp_unique_id( 'modal-' );
 468  
 469          $is_hidden_by_default = isset( $attributes['overlayMenu'] ) && 'always' === $attributes['overlayMenu'];
 470  
 471          $responsive_container_classes = array(
 472              'wp-block-navigation__responsive-container',
 473              $is_hidden_by_default ? 'hidden-by-default' : '',
 474              implode( ' ', $colors['overlay_css_classes'] ),
 475          );
 476          $open_button_classes          = array(
 477              'wp-block-navigation__responsive-container-open',
 478              $is_hidden_by_default ? 'always-shown' : '',
 479          );
 480  
 481          $should_display_icon_label = isset( $attributes['hasIcon'] ) && true === $attributes['hasIcon'];
 482          $toggle_button_icon        = '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><rect x="4" y="7.5" width="16" height="1.5" /><rect x="4" y="15" width="16" height="1.5" /></svg>';
 483          if ( isset( $attributes['icon'] ) ) {
 484              if ( 'menu' === $attributes['icon'] ) {
 485                  $toggle_button_icon = '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M5 5v1.5h14V5H5zm0 7.8h14v-1.5H5v1.5zM5 19h14v-1.5H5V19z" /></svg>';
 486              }
 487          }
 488          $toggle_button_content       = $should_display_icon_label ? $toggle_button_icon : __( 'Menu' );
 489          $toggle_close_button_icon    = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path d="m13.06 12 6.47-6.47-1.06-1.06L12 10.94 5.53 4.47 4.47 5.53 10.94 12l-6.47 6.47 1.06 1.06L12 13.06l6.47 6.47 1.06-1.06L13.06 12Z"></path></svg>';
 490          $toggle_close_button_content = $should_display_icon_label ? $toggle_close_button_icon : __( 'Close' );
 491          $toggle_aria_label_open      = $should_display_icon_label ? 'aria-label="' . __( 'Open menu' ) . '"' : ''; // Open button label.
 492          $toggle_aria_label_close     = $should_display_icon_label ? 'aria-label="' . __( 'Close menu' ) . '"' : ''; // Close button label.
 493  
 494          // Add Interactivity API directives to the markup if needed.
 495          $open_button_directives          = '';
 496          $responsive_container_directives = '';
 497          $responsive_dialog_directives    = '';
 498          $close_button_directives         = '';
 499          if ( $is_interactive ) {
 500              $open_button_directives                  = '
 501                  data-wp-on-async--click="actions.openMenuOnClick"
 502                  data-wp-on--keydown="actions.handleMenuKeydown"
 503              ';
 504              $responsive_container_directives         = '
 505                  data-wp-class--has-modal-open="state.isMenuOpen"
 506                  data-wp-class--is-menu-open="state.isMenuOpen"
 507                  data-wp-watch="callbacks.initMenu"
 508                  data-wp-on--keydown="actions.handleMenuKeydown"
 509                  data-wp-on-async--focusout="actions.handleMenuFocusout"
 510                  tabindex="-1"
 511              ';
 512              $responsive_dialog_directives            = '
 513                  data-wp-bind--aria-modal="state.ariaModal"
 514                  data-wp-bind--aria-label="state.ariaLabel"
 515                  data-wp-bind--role="state.roleAttribute"
 516              ';
 517              $close_button_directives                 = '
 518                  data-wp-on-async--click="actions.closeMenuOnClick"
 519              ';
 520              $responsive_container_content_directives = '
 521                  data-wp-watch="callbacks.focusFirstElement"
 522              ';
 523          }
 524  
 525          $overlay_inline_styles = esc_attr( safecss_filter_attr( $colors['overlay_inline_styles'] ) );
 526  
 527          return sprintf(
 528              '<button aria-haspopup="dialog" %3$s class="%6$s" %10$s>%8$s</button>
 529                  <div class="%5$s" %7$s id="%1$s" %11$s>
 530                      <div class="wp-block-navigation__responsive-close" tabindex="-1">
 531                          <div class="wp-block-navigation__responsive-dialog" %12$s>
 532                              <button %4$s class="wp-block-navigation__responsive-container-close" %13$s>%9$s</button>
 533                              <div class="wp-block-navigation__responsive-container-content" %14$s id="%1$s-content">
 534                                  %2$s
 535                              </div>
 536                          </div>
 537                      </div>
 538                  </div>',
 539              esc_attr( $modal_unique_id ),
 540              $inner_blocks_html,
 541              $toggle_aria_label_open,
 542              $toggle_aria_label_close,
 543              esc_attr( trim( implode( ' ', $responsive_container_classes ) ) ),
 544              esc_attr( trim( implode( ' ', $open_button_classes ) ) ),
 545              ( ! empty( $overlay_inline_styles ) ) ? "style=\"$overlay_inline_styles\"" : '',
 546              $toggle_button_content,
 547              $toggle_close_button_content,
 548              $open_button_directives,
 549              $responsive_container_directives,
 550              $responsive_dialog_directives,
 551              $close_button_directives,
 552              $responsive_container_content_directives
 553          );
 554      }
 555  
 556      /**
 557       * Get the wrapper attributes
 558       *
 559       * @since 6.5.0
 560       *
 561       * @param array         $attributes    The block attributes.
 562       * @param WP_Block_List $inner_blocks  A list of inner blocks.
 563       * @return string Returns the navigation block markup.
 564       */
 565  	private static function get_nav_wrapper_attributes( $attributes, $inner_blocks ) {
 566          $nav_menu_name      = static::get_unique_navigation_name( $attributes );
 567          $is_interactive     = static::is_interactive( $attributes, $inner_blocks );
 568          $is_responsive_menu = static::is_responsive( $attributes );
 569          $style              = static::get_styles( $attributes );
 570          $class              = static::get_classes( $attributes );
 571          $extra_attributes   = array(
 572              'class' => $class,
 573              'style' => $style,
 574          );
 575          if ( ! empty( $nav_menu_name ) ) {
 576              $extra_attributes['aria-label'] = $nav_menu_name;
 577          }
 578          $wrapper_attributes = get_block_wrapper_attributes( $extra_attributes );
 579  
 580          if ( $is_responsive_menu ) {
 581              $nav_element_directives = static::get_nav_element_directives( $is_interactive );
 582              $wrapper_attributes    .= ' ' . $nav_element_directives;
 583          }
 584  
 585          return $wrapper_attributes;
 586      }
 587  
 588      /**
 589       * Gets the nav element directives.
 590       *
 591       * @since 6.5.0
 592       *
 593       * @param bool $is_interactive Whether the block is interactive.
 594       * @return string the directives for the navigation element.
 595       */
 596  	private static function get_nav_element_directives( $is_interactive ) {
 597          if ( ! $is_interactive ) {
 598              return '';
 599          }
 600          // When adding to this array be mindful of security concerns.
 601          $nav_element_context    = wp_interactivity_data_wp_context(
 602              array(
 603                  'overlayOpenedBy' => array(
 604                      'click' => false,
 605                      'hover' => false,
 606                      'focus' => false,
 607                  ),
 608                  'type'            => 'overlay',
 609                  'roleAttribute'   => '',
 610                  'ariaLabel'       => __( 'Menu' ),
 611              )
 612          );
 613          $nav_element_directives = '
 614           data-wp-interactive="core/navigation" '
 615          . $nav_element_context;
 616  
 617          return $nav_element_directives;
 618      }
 619  
 620      /**
 621       * Handle view script module loading.
 622       *
 623       * @since 6.5.0
 624       *
 625       * @param array         $attributes   The block attributes.
 626       * @param WP_Block      $block        The parsed block.
 627       * @param WP_Block_List $inner_blocks The list of inner blocks.
 628       */
 629  	private static function handle_view_script_module_loading( $attributes, $block, $inner_blocks ) {
 630          if ( static::is_interactive( $attributes, $inner_blocks ) ) {
 631              wp_enqueue_script_module( '@wordpress/block-library/navigation/view' );
 632          }
 633      }
 634  
 635      /**
 636       * Returns the markup for the navigation block.
 637       *
 638       * @since 6.5.0
 639       *
 640       * @param array         $attributes The block attributes.
 641       * @param WP_Block_List $inner_blocks The list of inner blocks.
 642       * @return string Returns the navigation wrapper markup.
 643       */
 644  	private static function get_wrapper_markup( $attributes, $inner_blocks ) {
 645          $inner_blocks_html = static::get_inner_blocks_html( $attributes, $inner_blocks );
 646          if ( static::is_responsive( $attributes ) ) {
 647              return static::get_responsive_container_markup( $attributes, $inner_blocks, $inner_blocks_html );
 648          }
 649          return $inner_blocks_html;
 650      }
 651  
 652      /**
 653       * Returns a unique name for the navigation.
 654       *
 655       * @since 6.5.0
 656       *
 657       * @param array $attributes The block attributes.
 658       * @return string Returns a unique name for the navigation.
 659       */
 660  	private static function get_unique_navigation_name( $attributes ) {
 661          $nav_menu_name = static::get_navigation_name( $attributes );
 662  
 663          // If the menu name has been used previously then append an ID
 664          // to the name to ensure uniqueness across a given post.
 665          if ( isset( static::$seen_menu_names[ $nav_menu_name ] ) && static::$seen_menu_names[ $nav_menu_name ] > 1 ) {
 666              $count         = static::$seen_menu_names[ $nav_menu_name ];
 667              $nav_menu_name = $nav_menu_name . ' ' . ( $count );
 668          }
 669  
 670          return $nav_menu_name;
 671      }
 672  
 673      /**
 674       * Renders the navigation block.
 675       *
 676       * @since 6.5.0
 677       *
 678       * @param array    $attributes The block attributes.
 679       * @param string   $content    The saved content.
 680       * @param WP_Block $block      The parsed block.
 681       * @return string Returns the navigation block markup.
 682       */
 683  	public static function render( $attributes, $content, $block ) {
 684          /**
 685           * Deprecated:
 686           * The rgbTextColor and rgbBackgroundColor attributes
 687           * have been deprecated in favor of
 688           * customTextColor and customBackgroundColor ones.
 689           * Move the values from old attrs to the new ones.
 690           */
 691          if ( isset( $attributes['rgbTextColor'] ) && empty( $attributes['textColor'] ) ) {
 692              $attributes['customTextColor'] = $attributes['rgbTextColor'];
 693          }
 694  
 695          if ( isset( $attributes['rgbBackgroundColor'] ) && empty( $attributes['backgroundColor'] ) ) {
 696              $attributes['customBackgroundColor'] = $attributes['rgbBackgroundColor'];
 697          }
 698  
 699          unset( $attributes['rgbTextColor'], $attributes['rgbBackgroundColor'] );
 700  
 701          $inner_blocks = static::get_inner_blocks( $attributes, $block );
 702          // Prevent navigation blocks referencing themselves from rendering.
 703          if ( block_core_navigation_block_contains_core_navigation( $inner_blocks ) ) {
 704              return '';
 705          }
 706  
 707          static::handle_view_script_module_loading( $attributes, $block, $inner_blocks );
 708  
 709          return sprintf(
 710              '<nav %1$s>%2$s</nav>',
 711              static::get_nav_wrapper_attributes( $attributes, $inner_blocks ),
 712              static::get_wrapper_markup( $attributes, $inner_blocks )
 713          );
 714      }
 715  }
 716  
 717  // These functions are used for the __unstableLocation feature and only active
 718  // when the gutenberg plugin is active.
 719  if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
 720      /**
 721       * Returns the menu items for a WordPress menu location.
 722       *
 723       * @since 5.9.0
 724       *
 725       * @param string $location The menu location.
 726       * @return array Menu items for the location.
 727       */
 728      function block_core_navigation_get_menu_items_at_location( $location ) {
 729          if ( empty( $location ) ) {
 730              return;
 731          }
 732  
 733          // Build menu data. The following approximates the code in
 734          // `wp_nav_menu()` and `gutenberg_output_block_nav_menu`.
 735  
 736          // Find the location in the list of locations, returning early if the
 737          // location can't be found.
 738          $locations = get_nav_menu_locations();
 739          if ( ! isset( $locations[ $location ] ) ) {
 740              return;
 741          }
 742  
 743          // Get the menu from the location, returning early if there is no
 744          // menu or there was an error.
 745          $menu = wp_get_nav_menu_object( $locations[ $location ] );
 746          if ( ! $menu || is_wp_error( $menu ) ) {
 747              return;
 748          }
 749  
 750          $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) );
 751          _wp_menu_item_classes_by_context( $menu_items );
 752  
 753          return $menu_items;
 754      }
 755  
 756  
 757      /**
 758       * Sorts a standard array of menu items into a nested structure keyed by the
 759       * id of the parent menu.
 760       *
 761       * @since 5.9.0
 762       *
 763       * @param array $menu_items Menu items to sort.
 764       * @return array An array keyed by the id of the parent menu where each element
 765       *               is an array of menu items that belong to that parent.
 766       */
 767      function block_core_navigation_sort_menu_items_by_parent_id( $menu_items ) {
 768          $sorted_menu_items = array();
 769          foreach ( (array) $menu_items as $menu_item ) {
 770              $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
 771          }
 772          unset( $menu_items, $menu_item );
 773  
 774          $menu_items_by_parent_id = array();
 775          foreach ( $sorted_menu_items as $menu_item ) {
 776              $menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item;
 777          }
 778  
 779          return $menu_items_by_parent_id;
 780      }
 781  
 782      /**
 783       * Gets the inner blocks for the navigation block from the unstable location attribute.
 784       *
 785       * @since 6.5.0
 786       *
 787       * @param array $attributes The block attributes.
 788       * @return WP_Block_List Returns the inner blocks for the navigation block.
 789       */
 790      function block_core_navigation_get_inner_blocks_from_unstable_location( $attributes ) {
 791          $menu_items = block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] );
 792          if ( empty( $menu_items ) ) {
 793              return new WP_Block_List( array(), $attributes );
 794          }
 795  
 796          $menu_items_by_parent_id = block_core_navigation_sort_menu_items_by_parent_id( $menu_items );
 797          $parsed_blocks           = block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[0], $menu_items_by_parent_id );
 798          return new WP_Block_List( $parsed_blocks, $attributes );
 799      }
 800  }
 801  
 802  /**
 803   * Add Interactivity API directives to the navigation-submenu and page-list
 804   * blocks markup using the Tag Processor.
 805   *
 806   * @since 6.3.0
 807   *
 808   * @param WP_HTML_Tag_Processor $tags             Markup of the navigation block.
 809   * @param array                 $block_attributes Block attributes.
 810   *
 811   * @return string Submenu markup with the directives injected.
 812   */
 813  function block_core_navigation_add_directives_to_submenu( $tags, $block_attributes ) {
 814      while ( $tags->next_tag(
 815          array(
 816              'tag_name'   => 'LI',
 817              'class_name' => 'has-child',
 818          )
 819      ) ) {
 820          // Add directives to the parent `<li>`.
 821          $tags->set_attribute( 'data-wp-interactive', 'core/navigation' );
 822          $tags->set_attribute( 'data-wp-context', '{ "submenuOpenedBy": { "click": false, "hover": false, "focus": false }, "type": "submenu", "modal": null }' );
 823          $tags->set_attribute( 'data-wp-watch', 'callbacks.initMenu' );
 824          $tags->set_attribute( 'data-wp-on--focusout', 'actions.handleMenuFocusout' );
 825          $tags->set_attribute( 'data-wp-on--keydown', 'actions.handleMenuKeydown' );
 826  
 827          // This is a fix for Safari. Without it, Safari doesn't change the active
 828          // element when the user clicks on a button. It can be removed once we add
 829          // an overlay to capture the clicks, instead of relying on the focusout
 830          // event.
 831          $tags->set_attribute( 'tabindex', '-1' );
 832  
 833          if ( ! isset( $block_attributes['openSubmenusOnClick'] ) || false === $block_attributes['openSubmenusOnClick'] ) {
 834              $tags->set_attribute( 'data-wp-on-async--mouseenter', 'actions.openMenuOnHover' );
 835              $tags->set_attribute( 'data-wp-on-async--mouseleave', 'actions.closeMenuOnHover' );
 836          }
 837  
 838          // Add directives to the toggle submenu button.
 839          if ( $tags->next_tag(
 840              array(
 841                  'tag_name'   => 'BUTTON',
 842                  'class_name' => 'wp-block-navigation-submenu__toggle',
 843              )
 844          ) ) {
 845              $tags->set_attribute( 'data-wp-on-async--click', 'actions.toggleMenuOnClick' );
 846              $tags->set_attribute( 'data-wp-bind--aria-expanded', 'state.isMenuOpen' );
 847              // The `aria-expanded` attribute for SSR is already added in the submenu block.
 848          }
 849          // Add directives to the submenu.
 850          if ( $tags->next_tag(
 851              array(
 852                  'tag_name'   => 'UL',
 853                  'class_name' => 'wp-block-navigation__submenu-container',
 854              )
 855          ) ) {
 856              $tags->set_attribute( 'data-wp-on-async--focus', 'actions.openMenuOnFocus' );
 857          }
 858  
 859          // Iterate through subitems if exist.
 860          block_core_navigation_add_directives_to_submenu( $tags, $block_attributes );
 861      }
 862      return $tags->get_updated_html();
 863  }
 864  
 865  /**
 866   * Build an array with CSS classes and inline styles defining the colors
 867   * which will be applied to the navigation markup in the front-end.
 868   *
 869   * @since 5.9.0
 870   *
 871   * @param array $attributes Navigation block attributes.
 872   *
 873   * @return array Colors CSS classes and inline styles.
 874   */
 875  function block_core_navigation_build_css_colors( $attributes ) {
 876      $colors = array(
 877          'css_classes'           => array(),
 878          'inline_styles'         => '',
 879          'overlay_css_classes'   => array(),
 880          'overlay_inline_styles' => '',
 881      );
 882  
 883      // Text color.
 884      $has_named_text_color  = array_key_exists( 'textColor', $attributes );
 885      $has_custom_text_color = array_key_exists( 'customTextColor', $attributes );
 886  
 887      // If has text color.
 888      if ( $has_custom_text_color || $has_named_text_color ) {
 889          // Add has-text-color class.
 890          $colors['css_classes'][] = 'has-text-color';
 891      }
 892  
 893      if ( $has_named_text_color ) {
 894          // Add the color class.
 895          $colors['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] );
 896      } elseif ( $has_custom_text_color ) {
 897          // Add the custom color inline style.
 898          $colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['customTextColor'] );
 899      }
 900  
 901      // Background color.
 902      $has_named_background_color  = array_key_exists( 'backgroundColor', $attributes );
 903      $has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
 904  
 905      // If has background color.
 906      if ( $has_custom_background_color || $has_named_background_color ) {
 907          // Add has-background class.
 908          $colors['css_classes'][] = 'has-background';
 909      }
 910  
 911      if ( $has_named_background_color ) {
 912          // Add the background-color class.
 913          $colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
 914      } elseif ( $has_custom_background_color ) {
 915          // Add the custom background-color inline style.
 916          $colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] );
 917      }
 918  
 919      // Overlay text color.
 920      $has_named_overlay_text_color  = array_key_exists( 'overlayTextColor', $attributes );
 921      $has_custom_overlay_text_color = array_key_exists( 'customOverlayTextColor', $attributes );
 922  
 923      // If has overlay text color.
 924      if ( $has_custom_overlay_text_color || $has_named_overlay_text_color ) {
 925          // Add has-text-color class.
 926          $colors['overlay_css_classes'][] = 'has-text-color';
 927      }
 928  
 929      if ( $has_named_overlay_text_color ) {
 930          // Add the overlay color class.
 931          $colors['overlay_css_classes'][] = sprintf( 'has-%s-color', $attributes['overlayTextColor'] );
 932      } elseif ( $has_custom_overlay_text_color ) {
 933          // Add the custom overlay color inline style.
 934          $colors['overlay_inline_styles'] .= sprintf( 'color: %s;', $attributes['customOverlayTextColor'] );
 935      }
 936  
 937      // Overlay background color.
 938      $has_named_overlay_background_color  = array_key_exists( 'overlayBackgroundColor', $attributes );
 939      $has_custom_overlay_background_color = array_key_exists( 'customOverlayBackgroundColor', $attributes );
 940  
 941      // If has overlay background color.
 942      if ( $has_custom_overlay_background_color || $has_named_overlay_background_color ) {
 943          // Add has-background class.
 944          $colors['overlay_css_classes'][] = 'has-background';
 945      }
 946  
 947      if ( $has_named_overlay_background_color ) {
 948          // Add the overlay background-color class.
 949          $colors['overlay_css_classes'][] = sprintf( 'has-%s-background-color', $attributes['overlayBackgroundColor'] );
 950      } elseif ( $has_custom_overlay_background_color ) {
 951          // Add the custom overlay background-color inline style.
 952          $colors['overlay_inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customOverlayBackgroundColor'] );
 953      }
 954  
 955      return $colors;
 956  }
 957  
 958  /**
 959   * Build an array with CSS classes and inline styles defining the font sizes
 960   * which will be applied to the navigation markup in the front-end.
 961   *
 962   * @since 5.9.0
 963   *
 964   * @param array $attributes Navigation block attributes.
 965   *
 966   * @return array Font size CSS classes and inline styles.
 967   */
 968  function block_core_navigation_build_css_font_sizes( $attributes ) {
 969      // CSS classes.
 970      $font_sizes = array(
 971          'css_classes'   => array(),
 972          'inline_styles' => '',
 973      );
 974  
 975      $has_named_font_size  = array_key_exists( 'fontSize', $attributes );
 976      $has_custom_font_size = array_key_exists( 'customFontSize', $attributes );
 977  
 978      if ( $has_named_font_size ) {
 979          // Add the font size class.
 980          $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] );
 981      } elseif ( $has_custom_font_size ) {
 982          // Add the custom font size inline style.
 983          $font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $attributes['customFontSize'] );
 984      }
 985  
 986      return $font_sizes;
 987  }
 988  
 989  /**
 990   * Returns the top-level submenu SVG chevron icon.
 991   *
 992   * @since 5.9.0
 993   *
 994   * @return string
 995   */
 996  function block_core_navigation_render_submenu_icon() {
 997      return '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg>';
 998  }
 999  
1000  /**
1001   * Filter out empty "null" blocks from the block list.
1002   * 'parse_blocks' includes a null block with '\n\n' as the content when
1003   * it encounters whitespace. This is not a bug but rather how the parser
1004   * is designed.
1005   *
1006   * @since 5.9.0
1007   *
1008   * @param array $parsed_blocks the parsed blocks to be normalized.
1009   * @return array the normalized parsed blocks.
1010   */
1011  function block_core_navigation_filter_out_empty_blocks( $parsed_blocks ) {
1012      $filtered = array_filter(
1013          $parsed_blocks,
1014          static function ( $block ) {
1015              return isset( $block['blockName'] );
1016          }
1017      );
1018  
1019      // Reset keys.
1020      return array_values( $filtered );
1021  }
1022  
1023  /**
1024   * Returns true if the navigation block contains a nested navigation block.
1025   *
1026   * @since 6.2.0
1027   *
1028   * @param WP_Block_List $inner_blocks Inner block instance to be normalized.
1029   * @return bool true if the navigation block contains a nested navigation block.
1030   */
1031  function block_core_navigation_block_contains_core_navigation( $inner_blocks ) {
1032      foreach ( $inner_blocks as $block ) {
1033          if ( 'core/navigation' === $block->name ) {
1034              return true;
1035          }
1036          if ( $block->inner_blocks && block_core_navigation_block_contains_core_navigation( $block->inner_blocks ) ) {
1037              return true;
1038          }
1039      }
1040  
1041      return false;
1042  }
1043  
1044  /**
1045   * Retrieves the appropriate fallback to be used on the front of the
1046   * site when there is no menu assigned to the Nav block.
1047   *
1048   * This aims to mirror how the fallback mechanic for wp_nav_menu works.
1049   * See https://developer.wordpress.org/reference/functions/wp_nav_menu/#more-information.
1050   *
1051   * @since 5.9.0
1052   *
1053   * @return array the array of blocks to be used as a fallback.
1054   */
1055  function block_core_navigation_get_fallback_blocks() {
1056      $page_list_fallback = array(
1057          array(
1058              'blockName'    => 'core/page-list',
1059              'innerContent' => array(),
1060              'attrs'        => array(),
1061          ),
1062      );
1063  
1064      $registry = WP_Block_Type_Registry::get_instance();
1065  
1066      // If `core/page-list` is not registered then return empty blocks.
1067      $fallback_blocks = $registry->is_registered( 'core/page-list' ) ? $page_list_fallback : array();
1068      $navigation_post = WP_Navigation_Fallback::get_fallback();
1069  
1070      // Use the first non-empty Navigation as fallback if available.
1071      if ( $navigation_post ) {
1072          $parsed_blocks  = parse_blocks( $navigation_post->post_content );
1073          $maybe_fallback = block_core_navigation_filter_out_empty_blocks( $parsed_blocks );
1074  
1075          // Normalizing blocks may result in an empty array of blocks if they were all `null` blocks.
1076          // In this case default to the (Page List) fallback.
1077          $fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks;
1078  
1079          // Run Block Hooks algorithm to inject hooked blocks.
1080          // We have to run it here because we need the post ID of the Navigation block to track ignored hooked blocks.
1081          // TODO: See if we can move the apply_block_hooks_to_content_from_post_object() call
1082          // before the parse_blocks() call further above, to avoid the extra serialization/parsing.
1083          $markup          = serialize_blocks( $fallback_blocks );
1084          $markup          = apply_block_hooks_to_content_from_post_object( $markup, $navigation_post );
1085          $fallback_blocks = parse_blocks( $markup );
1086      }
1087  
1088      /**
1089       * Filters the fallback experience for the Navigation block.
1090       *
1091       * Returning a falsey value will opt out of the fallback and cause the block not to render.
1092       * To customise the blocks provided return an array of blocks - these should be valid
1093       * children of the `core/navigation` block.
1094       *
1095       * @since 5.9.0
1096       *
1097       * @param array[] $fallback_blocks default fallback blocks provided by the default block mechanic.
1098       */
1099      return apply_filters( 'block_core_navigation_render_fallback', $fallback_blocks );
1100  }
1101  
1102  /**
1103   * Iterate through all inner blocks recursively and get navigation link block's post IDs.
1104   *
1105   * @since 6.0.0
1106   *
1107   * @param WP_Block_List $inner_blocks Block list class instance.
1108   *
1109   * @return array Array of post IDs.
1110   */
1111  function block_core_navigation_get_post_ids( $inner_blocks ) {
1112      $post_ids = array_map( 'block_core_navigation_from_block_get_post_ids', iterator_to_array( $inner_blocks ) );
1113      return array_unique( array_merge( ...$post_ids ) );
1114  }
1115  
1116  /**
1117   * Get post IDs from a navigation link block instance.
1118   *
1119   * @since 6.0.0
1120   *
1121   * @param WP_Block $block Instance of a block.
1122   *
1123   * @return array Array of post IDs.
1124   */
1125  function block_core_navigation_from_block_get_post_ids( $block ) {
1126      $post_ids = array();
1127  
1128      if ( $block->inner_blocks ) {
1129          $post_ids = block_core_navigation_get_post_ids( $block->inner_blocks );
1130      }
1131  
1132      if ( 'core/navigation-link' === $block->name || 'core/navigation-submenu' === $block->name ) {
1133          if ( $block->attributes && isset( $block->attributes['kind'] ) && 'post-type' === $block->attributes['kind'] && isset( $block->attributes['id'] ) ) {
1134              $post_ids[] = $block->attributes['id'];
1135          }
1136      }
1137  
1138      return $post_ids;
1139  }
1140  
1141  /**
1142   * Renders the `core/navigation` block on server.
1143   *
1144   * @since 5.9.0
1145   *
1146   * @param array    $attributes The block attributes.
1147   * @param string   $content    The saved content.
1148   * @param WP_Block $block      The parsed block.
1149   *
1150   * @return string Returns the navigation block markup.
1151   */
1152  function render_block_core_navigation( $attributes, $content, $block ) {
1153      return WP_Navigation_Block_Renderer::render( $attributes, $content, $block );
1154  }
1155  
1156  /**
1157   * Register the navigation block.
1158   *
1159   * @since 5.9.0
1160   *
1161   * @uses render_block_core_navigation()
1162   * @throws WP_Error An WP_Error exception parsing the block definition.
1163   */
1164  function register_block_core_navigation() {
1165      register_block_type_from_metadata(
1166          __DIR__ . '/navigation',
1167          array(
1168              'render_callback' => 'render_block_core_navigation',
1169          )
1170      );
1171  }
1172  
1173  add_action( 'init', 'register_block_core_navigation' );
1174  
1175  /**
1176   * Filter that changes the parsed attribute values of navigation blocks contain typographic presets to contain the values directly.
1177   *
1178   * @since 5.9.0
1179   *
1180   * @param array $parsed_block The block being rendered.
1181   *
1182   * @return array The block being rendered without typographic presets.
1183   */
1184  function block_core_navigation_typographic_presets_backcompatibility( $parsed_block ) {
1185      if ( 'core/navigation' === $parsed_block['blockName'] ) {
1186          $attribute_to_prefix_map = array(
1187              'fontStyle'      => 'var:preset|font-style|',
1188              'fontWeight'     => 'var:preset|font-weight|',
1189              'textDecoration' => 'var:preset|text-decoration|',
1190              'textTransform'  => 'var:preset|text-transform|',
1191          );
1192          foreach ( $attribute_to_prefix_map as $style_attribute => $prefix ) {
1193              if ( ! empty( $parsed_block['attrs']['style']['typography'][ $style_attribute ] ) ) {
1194                  $prefix_len      = strlen( $prefix );
1195                  $attribute_value = &$parsed_block['attrs']['style']['typography'][ $style_attribute ];
1196                  if ( 0 === strncmp( $attribute_value, $prefix, $prefix_len ) ) {
1197                      $attribute_value = substr( $attribute_value, $prefix_len );
1198                  }
1199                  if ( 'textDecoration' === $style_attribute && 'strikethrough' === $attribute_value ) {
1200                      $attribute_value = 'line-through';
1201                  }
1202              }
1203          }
1204      }
1205  
1206      return $parsed_block;
1207  }
1208  
1209  add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' );
1210  
1211  /**
1212   * Turns menu item data into a nested array of parsed blocks
1213   *
1214   * @since 5.9.0
1215   *
1216   * @deprecated 6.3.0 Use WP_Navigation_Fallback::parse_blocks_from_menu_items() instead.
1217   *
1218   * @param array $menu_items               An array of menu items that represent
1219   *                                        an individual level of a menu.
1220   * @param array $menu_items_by_parent_id  An array keyed by the id of the
1221   *                                        parent menu where each element is an
1222   *                                        array of menu items that belong to
1223   *                                        that parent.
1224   * @return array An array of parsed block data.
1225   */
1226  function block_core_navigation_parse_blocks_from_menu_items( $menu_items, $menu_items_by_parent_id ) {
1227  
1228      _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Navigation_Fallback::parse_blocks_from_menu_items' );
1229  
1230      if ( empty( $menu_items ) ) {
1231          return array();
1232      }
1233  
1234      $blocks = array();
1235  
1236      foreach ( $menu_items as $menu_item ) {
1237          $class_name       = ! empty( $menu_item->classes ) ? implode( ' ', (array) $menu_item->classes ) : null;
1238          $id               = ( null !== $menu_item->object_id && 'custom' !== $menu_item->object ) ? $menu_item->object_id : null;
1239          $opens_in_new_tab = null !== $menu_item->target && '_blank' === $menu_item->target;
1240          $rel              = ( null !== $menu_item->xfn && '' !== $menu_item->xfn ) ? $menu_item->xfn : null;
1241          $kind             = null !== $menu_item->type ? str_replace( '_', '-', $menu_item->type ) : 'custom';
1242  
1243          $block = array(
1244              'blockName' => isset( $menu_items_by_parent_id[ $menu_item->ID ] ) ? 'core/navigation-submenu' : 'core/navigation-link',
1245              'attrs'     => array(
1246                  'className'     => $class_name,
1247                  'description'   => $menu_item->description,
1248                  'id'            => $id,
1249                  'kind'          => $kind,
1250                  'label'         => $menu_item->title,
1251                  'opensInNewTab' => $opens_in_new_tab,
1252                  'rel'           => $rel,
1253                  'title'         => $menu_item->attr_title,
1254                  'type'          => $menu_item->object,
1255                  'url'           => $menu_item->url,
1256              ),
1257          );
1258  
1259          $block['innerBlocks']  = isset( $menu_items_by_parent_id[ $menu_item->ID ] )
1260              ? block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[ $menu_item->ID ], $menu_items_by_parent_id )
1261              : array();
1262          $block['innerContent'] = array_map( 'serialize_block', $block['innerBlocks'] );
1263  
1264          $blocks[] = $block;
1265      }
1266  
1267      return $blocks;
1268  }
1269  
1270  /**
1271   * Get the classic navigation menu to use as a fallback.
1272   *
1273   * @since 6.2.0
1274   *
1275   * @deprecated 6.3.0 Use WP_Navigation_Fallback::get_classic_menu_fallback() instead.
1276   *
1277   * @return object WP_Term The classic navigation.
1278   */
1279  function block_core_navigation_get_classic_menu_fallback() {
1280  
1281      _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Navigation_Fallback::get_classic_menu_fallback' );
1282  
1283      $classic_nav_menus = wp_get_nav_menus();
1284  
1285      // If menus exist.
1286      if ( $classic_nav_menus && ! is_wp_error( $classic_nav_menus ) ) {
1287          // Handles simple use case where user has a classic menu and switches to a block theme.
1288  
1289          // Returns the menu assigned to location `primary`.
1290          $locations = get_nav_menu_locations();
1291          if ( isset( $locations['primary'] ) ) {
1292              $primary_menu = wp_get_nav_menu_object( $locations['primary'] );
1293              if ( $primary_menu ) {
1294                  return $primary_menu;
1295              }
1296          }
1297  
1298          // Returns a menu if `primary` is its slug.
1299          foreach ( $classic_nav_menus as $classic_nav_menu ) {
1300              if ( 'primary' === $classic_nav_menu->slug ) {
1301                  return $classic_nav_menu;
1302              }
1303          }
1304  
1305          // Otherwise return the most recently created classic menu.
1306          usort(
1307              $classic_nav_menus,
1308              static function ( $a, $b ) {
1309                  return $b->term_id - $a->term_id;
1310              }
1311          );
1312          return $classic_nav_menus[0];
1313      }
1314  }
1315  
1316  /**
1317   * Converts a classic navigation to blocks.
1318   *
1319   * @since 6.2.0
1320   *
1321   * @deprecated 6.3.0 Use WP_Navigation_Fallback::get_classic_menu_fallback_blocks() instead.
1322   *
1323   * @param  object $classic_nav_menu WP_Term The classic navigation object to convert.
1324   * @return array the normalized parsed blocks.
1325   */
1326  function block_core_navigation_get_classic_menu_fallback_blocks( $classic_nav_menu ) {
1327  
1328      _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Navigation_Fallback::get_classic_menu_fallback_blocks' );
1329  
1330      // BEGIN: Code that already exists in wp_nav_menu().
1331      $menu_items = wp_get_nav_menu_items( $classic_nav_menu->term_id, array( 'update_post_term_cache' => false ) );
1332  
1333      // Set up the $menu_item variables.
1334      _wp_menu_item_classes_by_context( $menu_items );
1335  
1336      $sorted_menu_items = array();
1337      foreach ( (array) $menu_items as $menu_item ) {
1338          $sorted_menu_items[ $menu_item->menu_order ] = $menu_item;
1339      }
1340  
1341      unset( $menu_items, $menu_item );
1342  
1343      // END: Code that already exists in wp_nav_menu().
1344  
1345      $menu_items_by_parent_id = array();
1346      foreach ( $sorted_menu_items as $menu_item ) {
1347          $menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item;
1348      }
1349  
1350      $inner_blocks = block_core_navigation_parse_blocks_from_menu_items(
1351          isset( $menu_items_by_parent_id[0] )
1352              ? $menu_items_by_parent_id[0]
1353              : array(),
1354          $menu_items_by_parent_id
1355      );
1356  
1357      return serialize_blocks( $inner_blocks );
1358  }
1359  
1360  /**
1361   * If there's a classic menu then use it as a fallback.
1362   *
1363   * @since 6.2.0
1364   *
1365   * @deprecated 6.3.0 Use WP_Navigation_Fallback::create_classic_menu_fallback() instead.
1366   *
1367   * @return array the normalized parsed blocks.
1368   */
1369  function block_core_navigation_maybe_use_classic_menu_fallback() {
1370  
1371      _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Navigation_Fallback::create_classic_menu_fallback' );
1372  
1373      // See if we have a classic menu.
1374      $classic_nav_menu = block_core_navigation_get_classic_menu_fallback();
1375  
1376      if ( ! $classic_nav_menu ) {
1377          return;
1378      }
1379  
1380      // If we have a classic menu then convert it to blocks.
1381      $classic_nav_menu_blocks = block_core_navigation_get_classic_menu_fallback_blocks( $classic_nav_menu );
1382  
1383      if ( empty( $classic_nav_menu_blocks ) ) {
1384          return;
1385      }
1386  
1387      // Create a new navigation menu from the classic menu.
1388      $wp_insert_post_result = wp_insert_post(
1389          array(
1390              'post_content' => $classic_nav_menu_blocks,
1391              'post_title'   => $classic_nav_menu->name,
1392              'post_name'    => $classic_nav_menu->slug,
1393              'post_status'  => 'publish',
1394              'post_type'    => 'wp_navigation',
1395          ),
1396          true // So that we can check whether the result is an error.
1397      );
1398  
1399      if ( is_wp_error( $wp_insert_post_result ) ) {
1400          return;
1401      }
1402  
1403      // Fetch the most recently published navigation which will be the classic one created above.
1404      return block_core_navigation_get_most_recently_published_navigation();
1405  }
1406  
1407  /**
1408   * Finds the most recently published `wp_navigation` Post.
1409   *
1410   * @since 6.1.0
1411   *
1412   * @deprecated 6.3.0 Use WP_Navigation_Fallback::get_most_recently_published_navigation() instead.
1413   *
1414   * @return WP_Post|null the first non-empty Navigation or null.
1415   */
1416  function block_core_navigation_get_most_recently_published_navigation() {
1417  
1418      _deprecated_function( __FUNCTION__, '6.3.0', 'WP_Navigation_Fallback::get_most_recently_published_navigation' );
1419  
1420      // Default to the most recently created menu.
1421      $parsed_args = array(
1422          'post_type'              => 'wp_navigation',
1423          'no_found_rows'          => true,
1424          'update_post_meta_cache' => false,
1425          'update_post_term_cache' => false,
1426          'order'                  => 'DESC',
1427          'orderby'                => 'date',
1428          'post_status'            => 'publish',
1429          'posts_per_page'         => 1, // get only the most recent.
1430      );
1431  
1432      $navigation_post = new WP_Query( $parsed_args );
1433      if ( count( $navigation_post->posts ) > 0 ) {
1434          return $navigation_post->posts[0];
1435      }
1436  
1437      return null;
1438  }


Generated : Sun Mar 9 08:20:01 2025 Cross-referenced by PHPXref