[ Index ]

PHP Cross Reference of WordPress

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   * Build an array with CSS classes and inline styles defining the colors
  10   * which will be applied to the navigation markup in the front-end.
  11   *
  12   * @param  array $context    Navigation block context.
  13   * @param  array $attributes Block attributes.
  14   * @return array Colors CSS classes and inline styles.
  15   */
  16  function block_core_navigation_submenu_build_css_colors( $context, $attributes ) {
  17      $colors = array(
  18          'css_classes'   => array(),
  19          'inline_styles' => '',
  20      );
  21  
  22      $is_sub_menu = isset( $attributes['isTopLevelItem'] ) ? ( ! $attributes['isTopLevelItem'] ) : false;
  23  
  24      // Text color.
  25      $named_text_color  = null;
  26      $custom_text_color = null;
  27  
  28      if ( $is_sub_menu && array_key_exists( 'customOverlayTextColor', $context ) ) {
  29          $custom_text_color = $context['customOverlayTextColor'];
  30      } elseif ( $is_sub_menu && array_key_exists( 'overlayTextColor', $context ) ) {
  31          $named_text_color = $context['overlayTextColor'];
  32      } elseif ( array_key_exists( 'customTextColor', $context ) ) {
  33          $custom_text_color = $context['customTextColor'];
  34      } elseif ( array_key_exists( 'textColor', $context ) ) {
  35          $named_text_color = $context['textColor'];
  36      } elseif ( isset( $context['style']['color']['text'] ) ) {
  37          $custom_text_color = $context['style']['color']['text'];
  38      }
  39  
  40      // If has text color.
  41      if ( ! is_null( $named_text_color ) ) {
  42          // Add the color class.
  43          array_push( $colors['css_classes'], 'has-text-color', sprintf( 'has-%s-color', $named_text_color ) );
  44      } elseif ( ! is_null( $custom_text_color ) ) {
  45          // Add the custom color inline style.
  46          $colors['css_classes'][]  = 'has-text-color';
  47          $colors['inline_styles'] .= sprintf( 'color: %s;', $custom_text_color );
  48      }
  49  
  50      // Background color.
  51      $named_background_color  = null;
  52      $custom_background_color = null;
  53  
  54      if ( $is_sub_menu && array_key_exists( 'customOverlayBackgroundColor', $context ) ) {
  55          $custom_background_color = $context['customOverlayBackgroundColor'];
  56      } elseif ( $is_sub_menu && array_key_exists( 'overlayBackgroundColor', $context ) ) {
  57          $named_background_color = $context['overlayBackgroundColor'];
  58      } elseif ( array_key_exists( 'customBackgroundColor', $context ) ) {
  59          $custom_background_color = $context['customBackgroundColor'];
  60      } elseif ( array_key_exists( 'backgroundColor', $context ) ) {
  61          $named_background_color = $context['backgroundColor'];
  62      } elseif ( isset( $context['style']['color']['background'] ) ) {
  63          $custom_background_color = $context['style']['color']['background'];
  64      }
  65  
  66      // If has background color.
  67      if ( ! is_null( $named_background_color ) ) {
  68          // Add the background-color class.
  69          array_push( $colors['css_classes'], 'has-background', sprintf( 'has-%s-background-color', $named_background_color ) );
  70      } elseif ( ! is_null( $custom_background_color ) ) {
  71          // Add the custom background-color inline style.
  72          $colors['css_classes'][]  = 'has-background';
  73          $colors['inline_styles'] .= sprintf( 'background-color: %s;', $custom_background_color );
  74      }
  75  
  76      return $colors;
  77  }
  78  
  79  /**
  80   * Build an array with CSS classes and inline styles defining the font sizes
  81   * which will be applied to the navigation markup in the front-end.
  82   *
  83   * @param  array $context Navigation block context.
  84   * @return array Font size CSS classes and inline styles.
  85   */
  86  function block_core_navigation_submenu_build_css_font_sizes( $context ) {
  87      // CSS classes.
  88      $font_sizes = array(
  89          'css_classes'   => array(),
  90          'inline_styles' => '',
  91      );
  92  
  93      $has_named_font_size  = array_key_exists( 'fontSize', $context );
  94      $has_custom_font_size = isset( $context['style']['typography']['fontSize'] );
  95  
  96      if ( $has_named_font_size ) {
  97          // Add the font size class.
  98          $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] );
  99      } elseif ( $has_custom_font_size ) {
 100          // Add the custom font size inline style.
 101          $font_sizes['inline_styles'] = sprintf( 'font-size: %s;', $context['style']['typography']['fontSize'] );
 102      }
 103  
 104      return $font_sizes;
 105  }
 106  
 107  /**
 108   * Returns the top-level submenu SVG chevron icon.
 109   *
 110   * @return string
 111   */
 112  function block_core_navigation_submenu_render_submenu_icon() {
 113      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>';
 114  }
 115  
 116  /**
 117   * Renders the `core/navigation-submenu` block.
 118   *
 119   * @param array    $attributes The block attributes.
 120   * @param string   $content    The saved content.
 121   * @param WP_Block $block      The parsed block.
 122   *
 123   * @return string Returns the post content with the legacy widget added.
 124   */
 125  function render_block_core_navigation_submenu( $attributes, $content, $block ) {
 126  
 127      $navigation_link_has_id = isset( $attributes['id'] ) && is_numeric( $attributes['id'] );
 128      $is_post_type           = isset( $attributes['kind'] ) && 'post-type' === $attributes['kind'];
 129      $is_post_type           = $is_post_type || isset( $attributes['type'] ) && ( 'post' === $attributes['type'] || 'page' === $attributes['type'] );
 130  
 131      // Don't render the block's subtree if it is a draft.
 132      if ( $is_post_type && $navigation_link_has_id && 'publish' !== get_post_status( $attributes['id'] ) ) {
 133          return '';
 134      }
 135  
 136      // Don't render the block's subtree if it has no label.
 137      if ( empty( $attributes['label'] ) ) {
 138          return '';
 139      }
 140  
 141      $colors          = block_core_navigation_submenu_build_css_colors( $block->context, $attributes );
 142      $font_sizes      = block_core_navigation_submenu_build_css_font_sizes( $block->context );
 143      $classes         = array_merge(
 144          $colors['css_classes'],
 145          $font_sizes['css_classes']
 146      );
 147      $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
 148  
 149      $css_classes = trim( implode( ' ', $classes ) );
 150      $has_submenu = count( $block->inner_blocks ) > 0;
 151      $is_active   = ! empty( $attributes['id'] ) && ( get_the_ID() === $attributes['id'] );
 152  
 153      $show_submenu_indicators = isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'];
 154      $open_on_click           = isset( $block->context['openSubmenusOnClick'] ) && $block->context['openSubmenusOnClick'];
 155      $open_on_hover_and_click = isset( $block->context['openSubmenusOnClick'] ) && ! $block->context['openSubmenusOnClick'] &&
 156          $show_submenu_indicators;
 157  
 158      $wrapper_attributes = get_block_wrapper_attributes(
 159          array(
 160              'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) .
 161              ( $open_on_click ? ' open-on-click' : '' ) . ( $open_on_hover_and_click ? ' open-on-hover-click' : '' ) .
 162              ( $is_active ? ' current-menu-item' : '' ),
 163              'style' => $style_attribute,
 164          )
 165      );
 166  
 167      $label = '';
 168  
 169      if ( isset( $attributes['label'] ) ) {
 170          $label .= wp_kses_post( $attributes['label'] );
 171      }
 172  
 173      $aria_label = sprintf(
 174          /* translators: Accessibility text. %s: Parent page title. */
 175          __( '%s submenu' ),
 176          wp_strip_all_tags( $label )
 177      );
 178  
 179      $html = '<li ' . $wrapper_attributes . '>';
 180  
 181      // If Submenus open on hover, we render an anchor tag with attributes.
 182      // If submenu icons are set to show, we also render a submenu button, so the submenu can be opened on click.
 183      if ( ! $open_on_click ) {
 184          $item_url = isset( $attributes['url'] ) ? $attributes['url'] : '';
 185          // Start appending HTML attributes to anchor tag.
 186          $html .= '<a class="wp-block-navigation-item__content" href="' . esc_url( $item_url ) . '"';
 187  
 188          if ( $is_active ) {
 189              $html .= ' aria-current="page"';
 190          }
 191  
 192          if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
 193              $html .= ' target="_blank"  ';
 194          }
 195  
 196          if ( isset( $attributes['rel'] ) ) {
 197              $html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
 198          } elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) {
 199              $html .= ' rel="nofollow"';
 200          }
 201  
 202          if ( isset( $attributes['title'] ) ) {
 203              $html .= ' title="' . esc_attr( $attributes['title'] ) . '"';
 204          }
 205  
 206          $html .= '>';
 207          // End appending HTML attributes to anchor tag.
 208  
 209          $html .= $label;
 210  
 211          $html .= '</a>';
 212          // End anchor tag content.
 213  
 214          if ( $show_submenu_indicators ) {
 215              // The submenu icon is rendered in a button here
 216              // so that there's a clickable element to open the submenu.
 217              $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_submenu_render_submenu_icon() . '</button>';
 218          }
 219      } else {
 220          // If menus open on click, we render the parent as a button.
 221          $html .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation-item__content wp-block-navigation-submenu__toggle" aria-expanded="false">';
 222  
 223          // Wrap title with span to isolate it from submenu icon.
 224          $html .= '<span class="wp-block-navigation-item__label">';
 225  
 226          $html .= $label;
 227  
 228          $html .= '</span>';
 229  
 230          $html .= '</button>';
 231  
 232          $html .= '<span class="wp-block-navigation__submenu-icon">' . block_core_navigation_submenu_render_submenu_icon() . '</span>';
 233  
 234      }
 235  
 236      if ( $has_submenu ) {
 237          $inner_blocks_html = '';
 238          foreach ( $block->inner_blocks as $inner_block ) {
 239              $inner_blocks_html .= $inner_block->render();
 240          }
 241  
 242          $html .= sprintf(
 243              '<ul class="wp-block-navigation__submenu-container">%s</ul>',
 244              $inner_blocks_html
 245          );
 246      }
 247  
 248      $html .= '</li>';
 249  
 250      return $html;
 251  }
 252  
 253  /**
 254   * Register the navigation submenu block.
 255   *
 256   * @uses render_block_core_navigation_submenu()
 257   * @throws WP_Error An WP_Error exception parsing the block definition.
 258   */
 259  function register_block_core_navigation_submenu() {
 260      register_block_type_from_metadata(
 261          __DIR__ . '/navigation-submenu',
 262          array(
 263              'render_callback' => 'render_block_core_navigation_submenu',
 264          )
 265      );
 266  }
 267  add_action( 'init', 'register_block_core_navigation_submenu' );


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1