PluginProbe
Gutenberg / 10.4.1
Gutenberg v10.4.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 10.4.1, at build/block-library/blocks/navigation-link.php

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