[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * Server-side rendering of the `core/navigation-submenu` block.
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Returns the submenu visibility value with backward compatibility
  10   * for the deprecated openSubmenusOnClick attribute.
  11   *
  12   * This function centralizes the migration logic from the boolean
  13   * openSubmenusOnClick to the new submenuVisibility enum.
  14   *
  15   * Backward compatibility handling:
  16   * - Legacy blocks (saved before migration, never opened in editor):
  17   *   Have openSubmenusOnClick in database. Parent Navigation block passes it via context.
  18   *   We prioritize openSubmenusOnClick to preserve the original behavior.
  19   *
  20   * - Migrated blocks (opened in editor after migration):
  21   *   JavaScript deprecation removes openSubmenusOnClick and sets submenuVisibility.
  22   *   We use submenuVisibility since openSubmenusOnClick is null.
  23   *
  24   * - New blocks (created after migration):
  25   *   Only have submenuVisibility, openSubmenusOnClick is null.
  26   *   We use submenuVisibility.
  27   *
  28   * @since 6.9.0
  29   *
  30   * @param array $context Block context from parent Navigation block.
  31   * @return string The visibility mode: 'hover', 'click', or 'always'.
  32   */
  33  function block_core_navigation_submenu_get_submenu_visibility( $context ) {
  34      $deprecated_open_submenus_on_click = $context['openSubmenusOnClick'] ?? null;
  35  
  36      // For backward compatibility, prioritize the legacy attribute if present. If it has been loaded and saved in the editor, then
  37      // the deprecated attribute will be replaced by submenuVisibility.
  38      if ( null !== $deprecated_open_submenus_on_click ) {
  39          // Convert boolean to string: true -> 'click', false -> 'hover'.
  40          return ! empty( $deprecated_open_submenus_on_click ) ? 'click' : 'hover';
  41      }
  42  
  43      $submenu_visibility = $context['submenuVisibility'] ?? null;
  44  
  45      // Use submenuVisibility for migrated/new blocks.
  46      return $submenu_visibility ?? 'hover';
  47  }
  48  
  49  // Path differs between source and build: '../navigation-link/shared/' in source, './navigation-link/shared/' in build.
  50  if ( file_exists( __DIR__ . '/../navigation-link/shared/item-should-render.php' ) ) {
  51      require_once __DIR__ . '/../navigation-link/shared/item-should-render.php';
  52      require_once __DIR__ . '/../navigation-link/shared/render-submenu-icon.php';
  53  } else {
  54      require_once  __DIR__ . '/navigation-link/shared/item-should-render.php';
  55      require_once  __DIR__ . '/navigation-link/shared/render-submenu-icon.php';
  56  }
  57  
  58  /**
  59   * Build an array with CSS classes and inline styles defining the font sizes
  60   * which will be applied to the navigation markup in the front-end.
  61   *
  62   * @since 5.9.0
  63   *
  64   * @param  array $context Navigation block context.
  65   * @return array Font size CSS classes and inline styles.
  66   */
  67  function block_core_navigation_submenu_build_css_font_sizes( $context ) {
  68      // CSS classes.
  69      $font_sizes = array(
  70          'css_classes'   => array(),
  71          'inline_styles' => '',
  72      );
  73  
  74      $has_named_font_size  = array_key_exists( 'fontSize', $context );
  75      $has_custom_font_size = isset( $context['style']['typography']['fontSize'] );
  76  
  77      if ( $has_named_font_size ) {
  78          // Add the font size class.
  79          $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] );
  80      } elseif ( $has_custom_font_size ) {
  81          // Add the custom font size inline style.
  82          $font_sizes['inline_styles'] = sprintf(
  83              'font-size: %s;',
  84              wp_get_typography_font_size_value(
  85                  array(
  86                      'size' => $context['style']['typography']['fontSize'],
  87                  )
  88              )
  89          );
  90      }
  91  
  92      return $font_sizes;
  93  }
  94  
  95  /**
  96   * Renders the `core/navigation-submenu` block.
  97   *
  98   * @since 5.9.0
  99   *
 100   * @param array    $attributes The block attributes.
 101   * @param string   $content    The saved content.
 102   * @param WP_Block $block      The parsed block.
 103   *
 104   * @return string Returns the post content with the legacy widget added.
 105   */
 106  function render_block_core_navigation_submenu( $attributes, $content, $block ) {
 107      // Check if this navigation item should render based on post status.
 108      if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) {
 109          if ( ! gutenberg_block_core_shared_navigation_item_should_render( $attributes, $block ) ) {
 110              return '';
 111          }
 112      }
 113  
 114      // Don't render the block's subtree if it has no label.
 115      if ( empty( $attributes['label'] ) ) {
 116          return '';
 117      }
 118  
 119      $font_sizes      = block_core_navigation_submenu_build_css_font_sizes( $block->context );
 120      $style_attribute = $font_sizes['inline_styles'];
 121  
 122      // Render inner blocks first to check if any menu items will actually display.
 123      $inner_blocks_html = '';
 124      foreach ( $block->inner_blocks as $inner_block ) {
 125          $inner_blocks_html .= $inner_block->render();
 126      }
 127      $has_submenu = ! empty( trim( $inner_blocks_html ) );
 128  
 129      $kind      = empty( $attributes['kind'] ) ? 'post_type' : str_replace( '-', '_', $attributes['kind'] );
 130      $is_active = ! empty( $attributes['id'] ) && get_queried_object_id() === (int) $attributes['id'] && ! empty( get_queried_object()->$kind );
 131  
 132      if ( is_post_type_archive() && ! empty( $attributes['url'] ) ) {
 133          $queried_archive_link = get_post_type_archive_link( get_queried_object()->name );
 134          if ( $attributes['url'] === $queried_archive_link ) {
 135              $is_active = true;
 136          }
 137      }
 138  
 139      $show_submenu_indicators = isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'];
 140      $computed_visibility     = block_core_navigation_submenu_get_submenu_visibility( $block->context );
 141      $open_on_click           = 'click' === $computed_visibility;
 142      $open_on_hover           = 'hover' === $computed_visibility;
 143      $open_on_hover_and_click = $open_on_hover && $show_submenu_indicators;
 144  
 145      $classes = array(
 146          'wp-block-navigation-item',
 147      );
 148      $classes = array_merge(
 149          $classes,
 150          $font_sizes['css_classes']
 151      );
 152      if ( $has_submenu ) {
 153          $classes[] = 'has-child';
 154      }
 155      if ( $open_on_click ) {
 156          $classes[] = 'open-on-click';
 157      }
 158      if ( $open_on_hover_and_click ) {
 159          $classes[] = 'open-on-hover-click';
 160      }
 161      if ( 'always' === $computed_visibility ) {
 162          $classes[] = 'open-always';
 163      }
 164      if ( $is_active ) {
 165          $classes[] = 'current-menu-item';
 166      }
 167  
 168      $wrapper_attributes = get_block_wrapper_attributes(
 169          array(
 170              'class' => implode( ' ', $classes ),
 171              'style' => $style_attribute,
 172          )
 173      );
 174  
 175      $label = '';
 176  
 177      if ( isset( $attributes['label'] ) ) {
 178          $label .= wp_kses_post( $attributes['label'] );
 179      }
 180  
 181      $aria_label = sprintf(
 182          /* translators: Accessibility text. %s: Parent page title. */
 183          __( '%s submenu' ),
 184          wp_strip_all_tags( $label )
 185      );
 186  
 187      $html = '<li ' . $wrapper_attributes . '>';
 188  
 189      // If Submenus open on hover or are always open, we render an anchor tag with attributes.
 190      // If submenu icons are set to show, we also render a submenu button, so the submenu can be opened on click.
 191      if ( ! $open_on_click ) {
 192          $item_url = $attributes['url'] ?? '';
 193          // Start appending HTML attributes to anchor tag.
 194          $html .= '<a class="wp-block-navigation-item__content"';
 195  
 196          // The href attribute on a and area elements is not required;
 197          // when those elements do not have href attributes they do not create hyperlinks.
 198          // But also The href attribute must have a value that is a valid URL potentially
 199          // surrounded by spaces.
 200          // see: https://html.spec.whatwg.org/multipage/links.html#links-created-by-a-and-area-elements.
 201          if ( ! empty( $item_url ) ) {
 202              $html .= ' href="' . esc_url( $item_url ) . '"';
 203          }
 204  
 205          if ( $is_active ) {
 206              $html .= ' aria-current="page"';
 207          }
 208  
 209          if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
 210              $html .= ' target="_blank"  ';
 211          }
 212  
 213          if ( isset( $attributes['rel'] ) ) {
 214              $html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
 215          } elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) {
 216              $html .= ' rel="nofollow"';
 217          }
 218  
 219          if ( isset( $attributes['title'] ) ) {
 220              $html .= ' title="' . esc_attr( $attributes['title'] ) . '"';
 221          }
 222  
 223          $html .= '>';
 224          // End appending HTML attributes to anchor tag.
 225  
 226          $html .= '<span class="wp-block-navigation-item__label">';
 227          $html .= $label;
 228          $html .= '</span>';
 229  
 230          // Add description if available.
 231          if ( ! empty( $attributes['description'] ) ) {
 232              $html .= '<span class="wp-block-navigation-item__description">';
 233              $html .= wp_kses_post( $attributes['description'] );
 234              $html .= '</span>';
 235          }
 236  
 237          $html .= '</a>';
 238          // End anchor tag content.
 239  
 240          if ( $show_submenu_indicators && $has_submenu ) {
 241              // The submenu icon is rendered in a button here
 242              // so that there's a clickable element to open the submenu.
 243              $html .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">' . block_core_navigation_render_submenu_icon() . '</button>';
 244          }
 245      } else {
 246          $html .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation-item__content wp-block-navigation-submenu__toggle" aria-expanded="false">';
 247  
 248          // Wrap title with span to isolate it from submenu icon.
 249          $html .= '<span class="wp-block-navigation-item__label">';
 250  
 251          $html .= $label;
 252  
 253          $html .= '</span>';
 254  
 255          // Add description if available.
 256          if ( ! empty( $attributes['description'] ) ) {
 257              $html .= '<span class="wp-block-navigation-item__description">';
 258              $html .= wp_kses_post( $attributes['description'] );
 259              $html .= '</span>';
 260          }
 261  
 262          $html .= '</button>';
 263  
 264          if ( $has_submenu ) {
 265              $html .= '<span class="wp-block-navigation__submenu-icon">' . block_core_navigation_render_submenu_icon() . '</span>';
 266          }
 267      }
 268  
 269      if ( $has_submenu ) {
 270          // Copy some attributes from the parent block to this one.
 271          // Ideally this would happen in the client when the block is created.
 272          if ( array_key_exists( 'overlayTextColor', $block->context ) ) {
 273              $attributes['textColor'] = $block->context['overlayTextColor'];
 274          }
 275          if ( array_key_exists( 'overlayBackgroundColor', $block->context ) ) {
 276              $attributes['backgroundColor'] = $block->context['overlayBackgroundColor'];
 277          }
 278          if ( array_key_exists( 'customOverlayTextColor', $block->context ) ) {
 279              $attributes['style']['color']['text'] = $block->context['customOverlayTextColor'];
 280          }
 281          if ( array_key_exists( 'customOverlayBackgroundColor', $block->context ) ) {
 282              $attributes['style']['color']['background'] = $block->context['customOverlayBackgroundColor'];
 283          }
 284  
 285          // This allows us to be able to get a response from wp_apply_colors_support.
 286          $block->block_type->supports['color'] = true;
 287          $colors_supports                      = wp_apply_colors_support( $block->block_type, $attributes );
 288          $css_classes                          = 'wp-block-navigation__submenu-container';
 289          if ( array_key_exists( 'class', $colors_supports ) ) {
 290              $css_classes .= ' ' . $colors_supports['class'];
 291          }
 292  
 293          $style_attribute = '';
 294          if ( array_key_exists( 'style', $colors_supports ) ) {
 295              $style_attribute = $colors_supports['style'];
 296          }
 297  
 298          if ( strpos( $inner_blocks_html, 'current-menu-item' ) ) {
 299              $tag_processor = new WP_HTML_Tag_Processor( $html );
 300              while ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-navigation-item' ) ) ) {
 301                  $tag_processor->add_class( 'current-menu-ancestor' );
 302              }
 303              $html = $tag_processor->get_updated_html();
 304          }
 305  
 306          $wrapper_attributes = get_block_wrapper_attributes(
 307              array(
 308                  'class' => $css_classes,
 309                  'style' => $style_attribute,
 310              )
 311          );
 312  
 313          $html .= sprintf(
 314              '<ul %s>%s</ul>',
 315              $wrapper_attributes,
 316              $inner_blocks_html
 317          );
 318  
 319      }
 320  
 321      $html .= '</li>';
 322  
 323      return $html;
 324  }
 325  
 326  /**
 327   * Register the navigation submenu block.
 328   *
 329   * @since 5.9.0
 330   *
 331   * @uses render_block_core_navigation_submenu()
 332   * @throws WP_Error An WP_Error exception parsing the block definition.
 333   */
 334  function register_block_core_navigation_submenu() {
 335      register_block_type_from_metadata(
 336          __DIR__ . '/navigation-submenu',
 337          array(
 338              'render_callback' => 'render_block_core_navigation_submenu',
 339          )
 340      );
 341  }
 342  add_action( 'init', 'register_block_core_navigation_submenu' );


Generated : Sat Jun 20 08:20:11 2026 Cross-referenced by PHPXref