PluginProbe
Gutenberg / 20.0.1
Gutenberg v20.0.1
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / block-library / blocks / navigation-submenu.php

navigation-submenu.php in Gutenberg 20.0.1, at build/block-library/blocks/navigation-submenu.php

284 lines 9.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 font sizes
10 * which will be applied to the navigation markup in the front-end.
11 *
12 * @since 5.9.0
13 *
14 * @param array $context Navigation block context.
15 * @return array Font size CSS classes and inline styles.
16 */
17 function gutenberg_block_core_navigation_submenu_build_css_font_sizes( $context ) {
18 // CSS classes.
19 $font_sizes = array(
20 'css_classes' => array(),
21 'inline_styles' => '',
22 );
23
24 $has_named_font_size = array_key_exists( 'fontSize', $context );
25 $has_custom_font_size = isset( $context['style']['typography']['fontSize'] );
26
27 if ( $has_named_font_size ) {
28 // Add the font size class.
29 $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $context['fontSize'] );
30 } elseif ( $has_custom_font_size ) {
31 // Add the custom font size inline style.
32 $font_sizes['inline_styles'] = sprintf(
33 'font-size: %s;',
34 gutenberg_get_typography_font_size_value(
35 array(
36 'size' => $context['style']['typography']['fontSize'],
37 )
38 )
39 );
40 }
41
42 return $font_sizes;
43 }
44
45 /**
46 * Returns the top-level submenu SVG chevron icon.
47 *
48 * @since 5.9.0
49 *
50 * @return string
51 */
52 function gutenberg_block_core_navigation_submenu_render_submenu_icon() {
53 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>';
54 }
55
56 /**
57 * Renders the `core/navigation-submenu` block.
58 *
59 * @since 5.9.0
60 *
61 * @param array $attributes The block attributes.
62 * @param string $content The saved content.
63 * @param WP_Block $block The parsed block.
64 *
65 * @return string Returns the post content with the legacy widget added.
66 */
67 function gutenberg_render_block_core_navigation_submenu( $attributes, $content, $block ) {
68 $navigation_link_has_id = isset( $attributes['id'] ) && is_numeric( $attributes['id'] );
69 $is_post_type = isset( $attributes['kind'] ) && 'post-type' === $attributes['kind'];
70 $is_post_type = $is_post_type || isset( $attributes['type'] ) && ( 'post' === $attributes['type'] || 'page' === $attributes['type'] );
71
72 // Don't render the block's subtree if it is a draft.
73 if ( $is_post_type && $navigation_link_has_id && 'publish' !== get_post_status( $attributes['id'] ) ) {
74 return '';
75 }
76
77 // Don't render the block's subtree if it has no label.
78 if ( empty( $attributes['label'] ) ) {
79 return '';
80 }
81
82 $font_sizes = gutenberg_block_core_navigation_submenu_build_css_font_sizes( $block->context );
83 $style_attribute = $font_sizes['inline_styles'];
84
85 $css_classes = trim( implode( ' ', $font_sizes['css_classes'] ) );
86 $has_submenu = count( $block->inner_blocks ) > 0;
87 $kind = empty( $attributes['kind'] ) ? 'post_type' : str_replace( '-', '_', $attributes['kind'] );
88 $is_active = ! empty( $attributes['id'] ) && get_queried_object_id() === (int) $attributes['id'] && ! empty( get_queried_object()->$kind );
89
90 if ( is_post_type_archive() ) {
91 $queried_archive_link = get_post_type_archive_link( get_queried_object()->name );
92 if ( $attributes['url'] === $queried_archive_link ) {
93 $is_active = true;
94 }
95 }
96
97 $show_submenu_indicators = isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'];
98 $open_on_click = isset( $block->context['openSubmenusOnClick'] ) && $block->context['openSubmenusOnClick'];
99 $open_on_hover_and_click = isset( $block->context['openSubmenusOnClick'] ) && ! $block->context['openSubmenusOnClick'] &&
100 $show_submenu_indicators;
101
102 $wrapper_attributes = get_block_wrapper_attributes(
103 array(
104 'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) .
105 ( $open_on_click ? ' open-on-click' : '' ) . ( $open_on_hover_and_click ? ' open-on-hover-click' : '' ) .
106 ( $is_active ? ' current-menu-item' : '' ),
107 'style' => $style_attribute,
108 )
109 );
110
111 $label = '';
112
113 if ( isset( $attributes['label'] ) ) {
114 $label .= wp_kses_post( $attributes['label'] );
115 }
116
117 $aria_label = sprintf(
118 /* translators: Accessibility text. %s: Parent page title. */
119 __( '%s submenu' ),
120 wp_strip_all_tags( $label )
121 );
122
123 $html = '<li ' . $wrapper_attributes . '>';
124
125 // If Submenus open on hover, we render an anchor tag with attributes.
126 // If submenu icons are set to show, we also render a submenu button, so the submenu can be opened on click.
127 if ( ! $open_on_click ) {
128 $item_url = isset( $attributes['url'] ) ? $attributes['url'] : '';
129 // Start appending HTML attributes to anchor tag.
130 $html .= '<a class="wp-block-navigation-item__content"';
131
132 // The href attribute on a and area elements is not required;
133 // when those elements do not have href attributes they do not create hyperlinks.
134 // But also The href attribute must have a value that is a valid URL potentially
135 // surrounded by spaces.
136 // see: https://html.spec.whatwg.org/multipage/links.html#links-created-by-a-and-area-elements.
137 if ( ! empty( $item_url ) ) {
138 $html .= ' href="' . esc_url( $item_url ) . '"';
139 }
140
141 if ( $is_active ) {
142 $html .= ' aria-current="page"';
143 }
144
145 if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
146 $html .= ' target="_blank" ';
147 }
148
149 if ( isset( $attributes['rel'] ) ) {
150 $html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
151 } elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) {
152 $html .= ' rel="nofollow"';
153 }
154
155 if ( isset( $attributes['title'] ) ) {
156 $html .= ' title="' . esc_attr( $attributes['title'] ) . '"';
157 }
158
159 $html .= '>';
160 // End appending HTML attributes to anchor tag.
161
162 $html .= '<span class="wp-block-navigation-item__label">';
163 $html .= $label;
164 $html .= '</span>';
165
166 // Add description if available.
167 if ( ! empty( $attributes['description'] ) ) {
168 $html .= '<span class="wp-block-navigation-item__description">';
169 $html .= wp_kses_post( $attributes['description'] );
170 $html .= '</span>';
171 }
172
173 $html .= '</a>';
174 // End anchor tag content.
175
176 if ( $show_submenu_indicators ) {
177 // The submenu icon is rendered in a button here
178 // so that there's a clickable element to open the submenu.
179 $html .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation__submenu-icon wp-block-navigation-submenu__toggle" aria-expanded="false">' . gutenberg_block_core_navigation_submenu_render_submenu_icon() . '</button>';
180 }
181 } else {
182 // If menus open on click, we render the parent as a button.
183 $html .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation-item__content wp-block-navigation-submenu__toggle" aria-expanded="false">';
184
185 // Wrap title with span to isolate it from submenu icon.
186 $html .= '<span class="wp-block-navigation-item__label">';
187
188 $html .= $label;
189
190 $html .= '</span>';
191
192 // Add description if available.
193 if ( ! empty( $attributes['description'] ) ) {
194 $html .= '<span class="wp-block-navigation-item__description">';
195 $html .= wp_kses_post( $attributes['description'] );
196 $html .= '</span>';
197 }
198
199 $html .= '</button>';
200
201 $html .= '<span class="wp-block-navigation__submenu-icon">' . gutenberg_block_core_navigation_submenu_render_submenu_icon() . '</span>';
202
203 }
204
205 if ( $has_submenu ) {
206 // Copy some attributes from the parent block to this one.
207 // Ideally this would happen in the client when the block is created.
208 if ( array_key_exists( 'overlayTextColor', $block->context ) ) {
209 $attributes['textColor'] = $block->context['overlayTextColor'];
210 }
211 if ( array_key_exists( 'overlayBackgroundColor', $block->context ) ) {
212 $attributes['backgroundColor'] = $block->context['overlayBackgroundColor'];
213 }
214 if ( array_key_exists( 'customOverlayTextColor', $block->context ) ) {
215 $attributes['style']['color']['text'] = $block->context['customOverlayTextColor'];
216 }
217 if ( array_key_exists( 'customOverlayBackgroundColor', $block->context ) ) {
218 $attributes['style']['color']['background'] = $block->context['customOverlayBackgroundColor'];
219 }
220
221 // This allows us to be able to get a response from gutenberg_apply_colors_support.
222 $block->block_type->supports['color'] = true;
223 $colors_supports = gutenberg_apply_colors_support( $block->block_type, $attributes );
224 $css_classes = 'wp-block-navigation__submenu-container';
225 if ( array_key_exists( 'class', $colors_supports ) ) {
226 $css_classes .= ' ' . $colors_supports['class'];
227 }
228
229 $style_attribute = '';
230 if ( array_key_exists( 'style', $colors_supports ) ) {
231 $style_attribute = $colors_supports['style'];
232 }
233
234 $inner_blocks_html = '';
235 foreach ( $block->inner_blocks as $inner_block ) {
236 $inner_blocks_html .= $inner_block->render();
237 }
238
239 if ( strpos( $inner_blocks_html, 'current-menu-item' ) ) {
240 $tag_processor = new WP_HTML_Tag_Processor( $html );
241 while ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-navigation-item' ) ) ) {
242 $tag_processor->add_class( 'current-menu-ancestor' );
243 }
244 $html = $tag_processor->get_updated_html();
245 }
246
247 $wrapper_attributes = get_block_wrapper_attributes(
248 array(
249 'class' => $css_classes,
250 'style' => $style_attribute,
251 )
252 );
253
254 $html .= sprintf(
255 '<ul %s>%s</ul>',
256 $wrapper_attributes,
257 $inner_blocks_html
258 );
259
260 }
261
262 $html .= '</li>';
263
264 return $html;
265 }
266
267 /**
268 * Register the navigation submenu block.
269 *
270 * @since 5.9.0
271 *
272 * @uses gutenberg_render_block_core_navigation_submenu()
273 * @throws WP_Error An WP_Error exception parsing the block definition.
274 */
275 function gutenberg_register_block_core_navigation_submenu() {
276 register_block_type_from_metadata(
277 __DIR__ . '/navigation-submenu',
278 array(
279 'render_callback' => 'gutenberg_render_block_core_navigation_submenu',
280 )
281 );
282 }
283 add_action( 'init', 'gutenberg_register_block_core_navigation_submenu', 20 );
284