PluginProbe
Gutenberg / 12.7.1
Gutenberg v12.7.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-link.php

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

332 lines 10.9 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-link` 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_link_build_css_colors( $context, $attributes ) {
17 $colors = array(
18 'css_classes' => array(),
19 'inline_styles' => '',
20 );
21
22 $is_sub_menu = isset( $attributes['isTopLevelLink'] ) ? ( ! $attributes['isTopLevelLink'] ) : 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_link_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_link_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-link` 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_link( $attributes, $content, $block ) {
126 $navigation_link_has_id = isset( $attributes['id'] ) && is_numeric( $attributes['id'] );
127 $is_post_type = isset( $attributes['kind'] ) && 'post-type' === $attributes['kind'];
128 $is_post_type = $is_post_type || isset( $attributes['type'] ) && ( 'post' === $attributes['type'] || 'page' === $attributes['type'] );
129
130 // Don't render the block's subtree if it is a draft or if the ID does not exist.
131 if ( $is_post_type && $navigation_link_has_id ) {
132 $post = get_post( $attributes['id'] );
133 if ( ! $post || 'publish' !== $post->post_status ) {
134 return '';
135 }
136 }
137
138 // Don't render the block's subtree if it has no label.
139 if ( empty( $attributes['label'] ) ) {
140 return '';
141 }
142
143 $colors = gutenberg_block_core_navigation_link_build_css_colors( $block->context, $attributes );
144 $font_sizes = gutenberg_block_core_navigation_link_build_css_font_sizes( $block->context );
145 $classes = array_merge(
146 $colors['css_classes'],
147 $font_sizes['css_classes']
148 );
149 $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
150
151 $css_classes = trim( implode( ' ', $classes ) );
152 $has_submenu = count( $block->inner_blocks ) > 0;
153 $is_active = ! empty( $attributes['id'] ) && ( get_the_ID() === $attributes['id'] );
154
155 $wrapper_attributes = get_block_wrapper_attributes(
156 array(
157 'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) .
158 ( $is_active ? ' current-menu-item' : '' ),
159 'style' => $style_attribute,
160 )
161 );
162 $html = '<li ' . $wrapper_attributes . '>' .
163 '<a class="wp-block-navigation-item__content" ';
164
165 // Start appending HTML attributes to anchor tag.
166 if ( isset( $attributes['url'] ) ) {
167 $html .= ' href="' . esc_url( $attributes['url'] ) . '"';
168 }
169
170 if ( $is_active ) {
171 $html .= ' aria-current="page"';
172 }
173
174 if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
175 $html .= ' target="_blank" ';
176 }
177
178 if ( isset( $attributes['rel'] ) ) {
179 $html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
180 } elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) {
181 $html .= ' rel="nofollow"';
182 }
183
184 if ( isset( $attributes['title'] ) ) {
185 $html .= ' title="' . esc_attr( $attributes['title'] ) . '"';
186 }
187
188 // End appending HTML attributes to anchor tag.
189
190 // Start anchor tag content.
191 $html .= '>' .
192 // Wrap title with span to isolate it from submenu icon.
193 '<span class="wp-block-navigation-item__label">';
194
195 if ( isset( $attributes['label'] ) ) {
196 $html .= wp_kses_post( $attributes['label'] );
197 }
198
199 $html .= '</span>';
200 $html .= '</a>';
201 // End anchor tag content.
202
203 if ( isset( $block->context['showSubmenuIcon'] ) && $block->context['showSubmenuIcon'] && $has_submenu ) {
204 // The submenu icon can be hidden by a CSS rule on the Navigation Block.
205 $html .= '<span class="wp-block-navigation__submenu-icon">' . gutenberg_block_core_navigation_link_render_submenu_icon() . '</span>';
206 }
207
208 if ( $has_submenu ) {
209 $inner_blocks_html = '';
210 foreach ( $block->inner_blocks as $inner_block ) {
211 $inner_blocks_html .= $inner_block->render();
212 }
213
214 $html .= sprintf(
215 '<ul class="wp-block-navigation__submenu-container">%s</ul>',
216 $inner_blocks_html
217 );
218 }
219
220 $html .= '</li>';
221
222 return $html;
223 }
224
225 /**
226 * Returns a navigation link variation
227 *
228 * @param WP_Taxonomy|WP_Post_Type $entity post type or taxonomy entity.
229 * @param string $kind string of value 'taxonomy' or 'post-type'.
230 *
231 * @return array
232 */
233 function gutenberg_build_variation_for_navigation_link( $entity, $kind ) {
234 $title = '';
235 $description = '';
236
237 if ( property_exists( $entity->labels, 'item_link' ) ) {
238 $title = $entity->labels->item_link;
239 }
240 if ( property_exists( $entity->labels, 'item_link_description' ) ) {
241 $description = $entity->labels->item_link_description;
242 }
243
244 $variation = array(
245 'name' => $entity->name,
246 'title' => $title,
247 'description' => $description,
248 'attributes' => array(
249 'type' => $entity->name,
250 'kind' => $kind,
251 ),
252 );
253
254 // Tweak some value for the variations.
255 $variation_overrides = array(
256 'post_tag' => array(
257 'name' => 'tag',
258 'attributes' => array(
259 'type' => 'tag',
260 'kind' => $kind,
261 ),
262 ),
263 'post_format' => array(
264 // The item_link and item_link_description for post formats is the
265 // same as for tags, so need to be overridden.
266 'title' => __( 'Post Format Link' ),
267 'description' => __( 'A link to a post format' ),
268 'attributes' => array(
269 'type' => 'post_format',
270 'kind' => $kind,
271 ),
272 ),
273 );
274
275 if ( array_key_exists( $entity->name, $variation_overrides ) ) {
276 $variation = array_merge(
277 $variation,
278 $variation_overrides[ $entity->name ]
279 );
280 }
281
282 return $variation;
283 }
284
285 /**
286 * Register the navigation link block.
287 *
288 * @uses render_block_core_navigation()
289 * @throws WP_Error An WP_Error exception parsing the block definition.
290 */
291 function gutenberg_register_block_core_navigation_link() {
292 $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' );
293 $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' );
294
295 // Use two separate arrays as a way to order the variations in the UI.
296 // Known variations (like Post Link and Page Link) are added to the
297 // `built_ins` array. Variations for custom post types and taxonomies are
298 // added to the `variations` array and will always appear after `built-ins.
299 $built_ins = array();
300 $variations = array();
301
302 if ( $post_types ) {
303 foreach ( $post_types as $post_type ) {
304 $variation = gutenberg_build_variation_for_navigation_link( $post_type, 'post-type' );
305 if ( $post_type->_builtin ) {
306 $built_ins[] = $variation;
307 } else {
308 $variations[] = $variation;
309 }
310 }
311 }
312 if ( $taxonomies ) {
313 foreach ( $taxonomies as $taxonomy ) {
314 $variation = gutenberg_build_variation_for_navigation_link( $taxonomy, 'taxonomy' );
315 if ( $taxonomy->_builtin ) {
316 $built_ins[] = $variation;
317 } else {
318 $variations[] = $variation;
319 }
320 }
321 }
322
323 register_block_type_from_metadata(
324 __DIR__ . '/navigation-link',
325 array(
326 'render_callback' => 'gutenberg_render_block_core_navigation_link',
327 'variations' => array_merge( $built_ins, $variations ),
328 )
329 );
330 }
331 add_action( 'init', 'gutenberg_register_block_core_navigation_link', 20 );
332