PluginProbe
Gutenberg / 12.6.0
Gutenberg v12.6.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 7.4.0 All 402 releases
gutenberg / build / block-library / blocks / navigation-submenu.php

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

286 lines 10.0 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 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 gutenberg_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 gutenberg_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 gutenberg_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 gutenberg_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 = gutenberg_block_core_navigation_submenu_build_css_colors( $block->context, $attributes );
142 $font_sizes = gutenberg_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(
171 $attributes['label'],
172 array(
173 'code' => array(),
174 'em' => array(),
175 'img' => array(
176 'scale' => array(),
177 'class' => array(),
178 'style' => array(),
179 'src' => array(),
180 'alt' => array(),
181 ),
182 's' => array(),
183 'span' => array(
184 'style' => array(),
185 ),
186 'strong' => array(),
187 )
188 );
189 }
190
191 $aria_label = sprintf(
192 /* translators: Accessibility text. %s: Parent page title. */
193 __( '%s submenu' ),
194 wp_strip_all_tags( $label )
195 );
196
197 $html = '<li ' . $wrapper_attributes . '>';
198
199 // If Submenus open on hover, we render an anchor tag with attributes.
200 // If submenu icons are set to show, we also render a submenu button, so the submenu can be opened on click.
201 if ( ! $open_on_click ) {
202 $item_url = isset( $attributes['url'] ) ? $attributes['url'] : '';
203 // Start appending HTML attributes to anchor tag.
204 $html .= '<a class="wp-block-navigation-item__content" href="' . esc_url( $item_url ) . '"';
205
206 if ( $is_active ) {
207 $html .= ' aria-current="page"';
208 }
209
210 if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
211 $html .= ' target="_blank" ';
212 }
213
214 if ( isset( $attributes['rel'] ) ) {
215 $html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
216 } elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) {
217 $html .= ' rel="nofollow"';
218 }
219
220 if ( isset( $attributes['title'] ) ) {
221 $html .= ' title="' . esc_attr( $attributes['title'] ) . '"';
222 }
223
224 $html .= '>';
225 // End appending HTML attributes to anchor tag.
226
227 $html .= $label;
228
229 $html .= '</a>';
230 // End anchor tag content.
231
232 if ( $show_submenu_indicators ) {
233 // The submenu icon is rendered in a button here
234 // so that there's a clickable element to open the submenu.
235 $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>';
236 }
237 } else {
238 // If menus open on click, we render the parent as a button.
239 $html .= '<button aria-label="' . esc_attr( $aria_label ) . '" class="wp-block-navigation-item__content wp-block-navigation-submenu__toggle" aria-expanded="false">';
240
241 // Wrap title with span to isolate it from submenu icon.
242 $html .= '<span class="wp-block-navigation-item__label">';
243
244 $html .= $label;
245
246 $html .= '</span>';
247
248 $html .= '<span class="wp-block-navigation__submenu-icon">' . gutenberg_block_core_navigation_submenu_render_submenu_icon() . '</span>';
249
250 $html .= '</button>';
251
252 }
253
254 if ( $has_submenu ) {
255 $inner_blocks_html = '';
256 foreach ( $block->inner_blocks as $inner_block ) {
257 $inner_blocks_html .= $inner_block->render();
258 }
259
260 $html .= sprintf(
261 '<ul class="wp-block-navigation__submenu-container">%s</ul>',
262 $inner_blocks_html
263 );
264 }
265
266 $html .= '</li>';
267
268 return $html;
269 }
270
271 /**
272 * Register the navigation submenu block.
273 *
274 * @uses gutenberg_render_block_core_navigation_submenu()
275 * @throws WP_Error An WP_Error exception parsing the block definition.
276 */
277 function gutenberg_register_block_core_navigation_submenu() {
278 register_block_type_from_metadata(
279 __DIR__ . '/navigation-submenu',
280 array(
281 'render_callback' => 'gutenberg_render_block_core_navigation_submenu',
282 )
283 );
284 }
285 add_action( 'init', 'gutenberg_register_block_core_navigation_submenu', 20 );
286