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

244 lines 7.3 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 24 24" transform="rotate(90)"><path d="M8 5v14l11-7z"/><path d="M0 0h24v24H0z" fill="none"/></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 // Don't render the block's subtree if it is a draft.
108 if (
109 isset( $attributes['id'] ) &&
110 is_numeric( $attributes['id'] ) &&
111 isset( $attributes['type'] ) &&
112 ( 'post' === $attributes['type'] || 'page' === $attributes['type'] )
113 ) {
114 $post = get_post( $attributes['id'] );
115 if ( 'publish' !== $post->post_status ) {
116 return '';
117 }
118 }
119
120 // Don't render the block's subtree if it has no label.
121 if ( empty( $attributes['label'] ) ) {
122 return '';
123 }
124
125 $colors = gutenberg_block_core_navigation_link_build_css_colors( $block->context );
126 $font_sizes = gutenberg_block_core_navigation_link_build_css_font_sizes( $block->context );
127 $classes = array_merge(
128 $colors['css_classes'],
129 $font_sizes['css_classes']
130 );
131 $style_attribute = ( $colors['inline_styles'] . $font_sizes['inline_styles'] );
132
133 $css_classes = trim( implode( ' ', $classes ) );
134 $has_submenu = count( $block->inner_blocks ) > 0;
135 $is_active = ! empty( $attributes['id'] ) && ( get_the_ID() === $attributes['id'] );
136
137 $class_name = ! empty( $attributes['className'] ) ? implode( ' ', (array) $attributes['className'] ) : false;
138
139 if ( false !== $class_name ) {
140 $css_classes .= ' ' . $class_name;
141 };
142
143 $wrapper_attributes = get_block_wrapper_attributes(
144 array(
145 'class' => $css_classes . ( $has_submenu ? ' has-child' : '' ) .
146 ( $is_active ? ' current-menu-item' : '' ),
147 'style' => $style_attribute,
148 )
149 );
150 $html = '<li ' . $wrapper_attributes . '>' .
151 '<a class="wp-block-navigation-link__content" ';
152
153 // Start appending HTML attributes to anchor tag.
154 if ( isset( $attributes['url'] ) ) {
155 $html .= ' href="' . esc_url( $attributes['url'] ) . '"';
156 }
157
158 if ( isset( $attributes['opensInNewTab'] ) && true === $attributes['opensInNewTab'] ) {
159 $html .= ' target="_blank" ';
160 }
161
162 if ( isset( $attributes['rel'] ) ) {
163 $html .= ' rel="' . esc_attr( $attributes['rel'] ) . '"';
164 } elseif ( isset( $attributes['nofollow'] ) && $attributes['nofollow'] ) {
165 $html .= ' rel="nofollow"';
166 }
167
168 if ( isset( $attributes['title'] ) ) {
169 $html .= ' title="' . esc_attr( $attributes['title'] ) . '"';
170 }
171
172 // End appending HTML attributes to anchor tag.
173
174 // Start anchor tag content.
175 $html .= '>' .
176 // Wrap title with span to isolate it from submenu icon.
177 '<span class="wp-block-navigation-link__label">';
178
179 if ( isset( $attributes['label'] ) ) {
180 $html .= wp_kses(
181 $attributes['label'],
182 array(
183 'code' => array(),
184 'em' => array(),
185 'img' => array(
186 'scale' => array(),
187 'class' => array(),
188 'style' => array(),
189 'src' => array(),
190 'alt' => array(),
191 ),
192 's' => array(),
193 'span' => array(
194 'style' => array(),
195 ),
196 'strong' => array(),
197 )
198 );
199 }
200
201 $html .= '</span>';
202
203 $html .= '</a>';
204 // End anchor tag content.
205
206 if ( $block->context['showSubmenuIcon'] && $has_submenu ) {
207 // The submenu icon can be hidden by a CSS rule on the Navigation Block.
208 $html .= '<span class="wp-block-navigation-link__submenu-icon">' . gutenberg_block_core_navigation_link_render_submenu_icon() . '</span>';
209 }
210
211 if ( $has_submenu ) {
212 $inner_blocks_html = '';
213 foreach ( $block->inner_blocks as $inner_block ) {
214 $inner_blocks_html .= $inner_block->render();
215 }
216
217 // TODO - classname is wrong!
218 $html .= sprintf(
219 '<ul class="wp-block-navigation__container">%s</ul>',
220 $inner_blocks_html
221 );
222 }
223
224 $html .= '</li>';
225
226 return $html;
227 }
228
229 /**
230 * Register the navigation link block.
231 *
232 * @uses render_block_core_navigation()
233 * @throws WP_Error An WP_Error exception parsing the block definition.
234 */
235 function gutenberg_register_block_core_navigation_link() {
236 register_block_type_from_metadata(
237 __DIR__ . '/navigation-link',
238 array(
239 'render_callback' => 'gutenberg_render_block_core_navigation_link',
240 )
241 );
242 }
243 add_action( 'init', 'gutenberg_register_block_core_navigation_link', 20 );
244