navigation-submenu.php 10 KB

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