PluginProbe
Gutenberg / 10.5.3
Gutenberg v10.5.3
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.php

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

205 lines 7.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` block.
4 *
5 * @package gutenberg
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 $attributes Navigation block attributes.
13 * @return array Colors CSS classes and inline styles.
14 */
15 function gutenberg_block_core_navigation_build_css_colors( $attributes ) {
16 $colors = array(
17 'css_classes' => array(),
18 'inline_styles' => '',
19 );
20
21 // Text color.
22 $has_named_text_color = array_key_exists( 'textColor', $attributes );
23 $has_custom_text_color = array_key_exists( 'customTextColor', $attributes );
24
25 // If has text color.
26 if ( $has_custom_text_color || $has_named_text_color ) {
27 // Add has-text-color class.
28 $colors['css_classes'][] = 'has-text-color';
29 }
30
31 if ( $has_named_text_color ) {
32 // Add the color class.
33 $colors['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] );
34 } elseif ( $has_custom_text_color ) {
35 // Add the custom color inline style.
36 $colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['customTextColor'] );
37 }
38
39 // Background color.
40 $has_named_background_color = array_key_exists( 'backgroundColor', $attributes );
41 $has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
42
43 // If has background color.
44 if ( $has_custom_background_color || $has_named_background_color ) {
45 // Add has-background class.
46 $colors['css_classes'][] = 'has-background';
47 }
48
49 if ( $has_named_background_color ) {
50 // Add the background-color class.
51 $colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
52 } elseif ( $has_custom_background_color ) {
53 // Add the custom background-color inline style.
54 $colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] );
55 }
56
57 return $colors;
58 }
59
60 /**
61 * Build an array with CSS classes and inline styles defining the font sizes
62 * which will be applied to the navigation markup in the front-end.
63 *
64 * @param array $attributes Navigation block attributes.
65 * @return array Font size CSS classes and inline styles.
66 */
67 function gutenberg_block_core_navigation_build_css_font_sizes( $attributes ) {
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', $attributes );
75 $has_custom_font_size = array_key_exists( 'customFontSize', $attributes );
76
77 if ( $has_named_font_size ) {
78 // Add the font size class.
79 $font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] );
80 } elseif ( $has_custom_font_size ) {
81 // Add the custom font size inline style.
82 $font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $attributes['customFontSize'] );
83 }
84
85 return $font_sizes;
86 }
87
88 /**
89 * Returns the top-level submenu SVG chevron icon.
90 *
91 * @return string
92 */
93 function gutenberg_block_core_navigation_render_submenu_icon() {
94 return '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none" role="img" aria-hidden="true" focusable="false"><path d="M1.50002 4L6.00002 8L10.5 4" stroke-width="1.5"></path></svg>';
95 }
96
97 /**
98 * Renders the `core/navigation` block on server.
99 *
100 * @param array $attributes The block attributes.
101 * @param array $content The saved content.
102 * @param array $block The parsed block.
103 *
104 * @return string Returns the post content with the legacy widget added.
105 */
106 function gutenberg_render_block_core_navigation( $attributes, $content, $block ) {
107 /**
108 * Deprecated:
109 * The rgbTextColor and rgbBackgroundColor attributes
110 * have been deprecated in favor of
111 * customTextColor and customBackgroundColor ones.
112 * Move the values from old attrs to the new ones.
113 */
114 if ( isset( $attributes['rgbTextColor'] ) && empty( $attributes['textColor'] ) ) {
115 $attributes['customTextColor'] = $attributes['rgbTextColor'];
116 }
117
118 if ( isset( $attributes['rgbBackgroundColor'] ) && empty( $attributes['backgroundColor'] ) ) {
119 $attributes['customBackgroundColor'] = $attributes['rgbBackgroundColor'];
120 }
121
122 unset( $attributes['rgbTextColor'], $attributes['rgbBackgroundColor'] );
123
124 if ( empty( $block->inner_blocks ) ) {
125 return '';
126 }
127
128 $colors = gutenberg_block_core_navigation_build_css_colors( $attributes );
129 $font_sizes = gutenberg_block_core_navigation_build_css_font_sizes( $attributes );
130 $classes = array_merge(
131 $colors['css_classes'],
132 $font_sizes['css_classes'],
133 ( isset( $attributes['orientation'] ) && 'vertical' === $attributes['orientation'] ) ? array( 'is-vertical' ) : array(),
134 isset( $attributes['itemsJustification'] ) ? array( 'items-justified-' . $attributes['itemsJustification'] ) : array()
135 );
136
137 $inner_blocks_html = '';
138 foreach ( $block->inner_blocks as $inner_block ) {
139 $inner_blocks_html .= $inner_block->render();
140 }
141
142 $block_styles = isset( $attributes['styles'] ) ? $attributes['styles'] : '';
143
144 $wrapper_attributes = get_block_wrapper_attributes(
145 array(
146 'class' => implode( ' ', $classes ),
147 'style' => $block_styles . $colors['inline_styles'] . $font_sizes['inline_styles'],
148 )
149 );
150
151 return sprintf(
152 '<nav %1$s><ul class="wp-block-navigation__container">%2$s</ul></nav>',
153 $wrapper_attributes,
154 $inner_blocks_html
155 );
156 }
157
158 /**
159 * Register the navigation block.
160 *
161 * @uses gutenberg_render_block_core_navigation()
162 * @throws WP_Error An WP_Error exception parsing the block definition.
163 */
164 function gutenberg_register_block_core_navigation() {
165 register_block_type_from_metadata(
166 __DIR__ . '/navigation',
167 array(
168 'render_callback' => 'gutenberg_render_block_core_navigation',
169 )
170 );
171 }
172
173 add_action( 'init', 'gutenberg_register_block_core_navigation', 20 );
174
175 /**
176 * Filter that changes the parsed attribute values of navigation blocks contain typographic presets to contain the values directly.
177 *
178 * @param array $parsed_block The block being rendered.
179 * @return array The block being rendered without typographic presets.
180 */
181 function gutenberg_block_core_navigation_typographic_presets_backcompatibility( $parsed_block ) {
182 if ( 'core/navigation' === $parsed_block['blockName'] ) {
183 $attribute_to_prefix_map = array(
184 'fontStyle' => 'var:preset|font-style|',
185 'fontWeight' => 'var:preset|font-weight|',
186 'textDecoration' => 'var:preset|text-decoration|',
187 'textTransform' => 'var:preset|text-transform|',
188 );
189 foreach ( $attribute_to_prefix_map as $style_attribute => $prefix ) {
190 if ( ! empty( $parsed_block['attrs']['style']['typography'][ $style_attribute ] ) ) {
191 $prefix_len = strlen( $prefix );
192 $attribute_value = &$parsed_block['attrs']['style']['typography'][ $style_attribute ];
193 if ( 0 === strncmp( $attribute_value, $prefix, $prefix_len ) ) {
194 $attribute_value = substr( $attribute_value, $prefix_len );
195 }
196 if ( 'textDecoration' === $style_attribute && 'strikethrough' === $attribute_value ) {
197 $attribute_value = 'line-through';
198 }
199 }
200 }
201 }
202 return $parsed_block;
203 }
204 add_filter( 'render_block_data', 'gutenberg_block_core_navigation_typographic_presets_backcompatibility' );
205