-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.php
273 lines (242 loc) · 8.17 KB
/
index.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/**
* Server-side rendering of the `core/navigation` block.
*
* @package gutenberg
*/
/**
* Build an array with CSS classes and inline styles defining the colors
* which will be applied to the navigation markup in the front-end.
*
* @param array $attributes Navigation block attributes.
* @return array Colors CSS classes and inline styles.
*/
function build_css_colors( $attributes ) {
$colors = array(
'css_classes' => array(),
'inline_styles' => '',
);
// Text color.
$has_named_text_color = array_key_exists( 'textColor', $attributes );
$has_custom_text_color = array_key_exists( 'customTextColor', $attributes );
// If has text color.
if ( $has_custom_text_color || $has_named_text_color ) {
// Add has-text-color class.
$colors['css_classes'][] = 'has-text-color';
}
if ( $has_named_text_color ) {
// Add the color class.
$colors['css_classes'][] = sprintf( 'has-%s-color', $attributes['textColor'] );
} elseif ( $has_custom_text_color ) {
// Add the custom color inline style.
$colors['inline_styles'] .= sprintf( 'color: %s;', $attributes['customTextColor'] );
}
// Background color.
$has_named_background_color = array_key_exists( 'backgroundColor', $attributes );
$has_custom_background_color = array_key_exists( 'customBackgroundColor', $attributes );
// If has background color.
if ( $has_custom_background_color || $has_named_background_color ) {
// Add has-background-color class.
$colors['css_classes'][] = 'has-background-color';
}
if ( $has_named_background_color ) {
// Add the background-color class.
$colors['css_classes'][] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
} elseif ( $has_custom_background_color ) {
// Add the custom background-color inline style.
$colors['inline_styles'] .= sprintf( 'background-color: %s;', $attributes['customBackgroundColor'] );
}
return $colors;
}
/**
* Build an array with CSS classes and inline styles defining the font sizes
* which will be applied to the navigation markup in the front-end.
*
* @param array $attributes Navigation block attributes.
* @return array Font size CSS classes and inline styles.
*/
function build_css_font_sizes( $attributes ) {
// CSS classes.
$font_sizes = array(
'css_classes' => array(),
'inline_styles' => '',
);
$has_named_font_size = array_key_exists( 'fontSize', $attributes );
$has_custom_font_size = array_key_exists( 'customFontSize', $attributes );
if ( $has_named_font_size ) {
// Add the font size class.
$font_sizes['css_classes'][] = sprintf( 'has-%s-font-size', $attributes['fontSize'] );
} elseif ( $has_custom_font_size ) {
// Add the custom font size inline style.
$font_sizes['inline_styles'] = sprintf( 'font-size: %spx;', $attributes['customFontSize'] );
}
return $font_sizes;
}
/**
* Recursively filters out links with no labels to build a clean navigation block structure.
*
* @param array $blocks Navigation link inner blocks from the Navigation block.
* @return array Blocks that had valid labels
*/
function gutenberg_remove_empty_navigation_links_recursive( $blocks ) {
$blocks = array_filter(
$blocks,
function( $block ) {
return ! empty( $block['attrs']['label'] );
}
);
if ( ! empty( $blocks ) ) {
foreach ( $blocks as $key => $block ) {
if ( ! empty( $block['innerBlocks'] ) ) {
$blocks[ $key ]['innerBlocks'] = gutenberg_remove_empty_navigation_links_recursive( $block['innerBlocks'] );
}
}
}
return $blocks;
}
/**
* Renders the `core/navigation` block on server.
*
* @param array $attributes The block attributes.
* @param array $content The saved content.
* @param array $block The parsed block.
*
* @return string Returns the post content with the legacy widget added.
*/
function render_block_navigation( $attributes, $content, $block ) {
$block['innerBlocks'] = gutenberg_remove_empty_navigation_links_recursive( $block['innerBlocks'] );
if ( empty( $block['innerBlocks'] ) ) {
return '';
}
$colors = build_css_colors( $attributes );
$font_sizes = build_css_font_sizes( $attributes );
$classes = array_merge(
$colors['css_classes'],
$font_sizes['css_classes'],
array( 'wp-block-navigation' ),
isset( $attributes['className'] ) ? array( $attributes['className'] ) : array(),
isset( $attributes['itemsJustification'] ) ? array( 'items-justified-' . $attributes['itemsJustification'] ) : array(),
isset( $attributes['align'] ) ? array( 'align' . $attributes['align'] ) : array()
);
$class_attribute = sprintf( ' class="%s"', esc_attr( implode( ' ', $classes ) ) );
$style_attribute = ( $colors['inline_styles'] || $font_sizes['inline_styles'] )
? sprintf( ' style="%s"', esc_attr( $colors['inline_styles'] ) . esc_attr( $font_sizes['inline_styles'] ) )
: '';
return sprintf(
'<nav %1$s %2$s>%3$s</nav>',
$class_attribute,
$style_attribute,
build_navigation_html( $block, $colors, $font_sizes )
);
}
/**
* Walks the inner block structure and returns an HTML list for it.
*
* @param array $block The block.
* @param array $colors Contains inline styles and CSS classes to apply to navigation item.
* @param array $font_sizes Contains inline styles and CSS classes to apply to navigation item.
* @param bool $level_zero True whether is main menu (level zero). Otherwise, False.
*
* @return string Returns an HTML list from innerBlocks.
*/
function build_navigation_html( $block, $colors, $font_sizes, $level_zero = true ) {
$html = '';
$classes = array_merge(
$colors['css_classes'],
$font_sizes['css_classes']
);
$css_classes = implode( ' ', $classes );
$class_attribute = sprintf( ' class="wp-block-navigation-link__content %s"', esc_attr( trim( $css_classes ) ) );
$style_attribute = ( $colors['inline_styles'] || $font_sizes['inline_styles'] )
? sprintf( ' style="%s"', esc_attr( $colors['inline_styles'] ) . esc_attr( $font_sizes['inline_styles'] ) )
: '';
foreach ( (array) $block['innerBlocks'] as $key => $block ) {
$html .= '<li class="wp-block-navigation-link">' .
'<a';
if ( $level_zero ) {
$html .= $class_attribute . $style_attribute;
}
// Start appending HTML attributes to anchor tag.
if ( isset( $block['attrs']['url'] ) ) {
$html .= ' href="' . esc_url( $block['attrs']['url'] ) . '"';
}
if ( isset( $block['attrs']['title'] ) ) {
$html .= ' title="' . esc_attr( $block['attrs']['title'] ) . '"';
}
if ( isset( $block['attrs']['opensInNewTab'] ) && true === $block['attrs']['opensInNewTab'] ) {
$html .= ' target="_blank" ';
}
// End appending HTML attributes to anchor tag.
// Start anchor tag content.
$html .= '>';
if ( isset( $block['attrs']['label'] ) ) {
$html .= wp_kses(
$block['attrs']['label'],
array(
'code' => array(),
'em' => array(),
'img' => array(
'scale' => array(),
'class' => array(),
'style' => array(),
'src' => array(),
'alt' => array(),
),
's' => array(),
'span' => array(
'style' => array(),
),
'strong' => array(),
)
);
}
$html .= '</a>';
// End anchor tag content.
if ( count( (array) $block['innerBlocks'] ) > 0 ) {
$html .= build_navigation_html( $block, $colors, $font_sizes, false );
}
$html .= '</li>';
}
return '<ul>' . $html . '</ul>';
}
/**
* Register the navigation block.
*
* @uses render_block_navigation()
* @throws WP_Error An WP_Error exception parsing the block definition.
*/
function register_block_core_navigation() {
register_block_type(
'core/navigation',
array(
'attributes' => array(
'className' => array(
'type' => 'string',
),
'textColor' => array(
'type' => 'string',
),
'customTextColor' => array(
'type' => 'string',
),
'backgroundColor' => array(
'type' => 'string',
),
'customBackgroundColor' => array(
'type' => 'string',
),
'fontSize' => array(
'type' => 'string',
),
'customFontSize' => array(
'type' => 'number',
),
'itemsJustification' => array(
'type' => 'string',
),
),
'render_callback' => 'render_block_navigation',
)
);
}
add_action( 'init', 'register_block_core_navigation' );