-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
index.php
432 lines (380 loc) · 15.7 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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
<?php
/**
* Server-side rendering of the `core/search` block.
*
* @package WordPress
*/
/**
* Dynamically renders the `core/search` block.
*
* @param array $attributes The block attributes.
*
* @return string The search block markup.
*/
function render_block_core_search( $attributes ) {
// Older versions of the Search block defaulted the label and buttonText
// attributes to `__( 'Search' )` meaning that many posts contain `<!--
// wp:search /-->`. Support these by defaulting an undefined label and
// buttonText to `__( 'Search' )`.
$attributes = wp_parse_args(
$attributes,
array(
'label' => __( 'Search' ),
'buttonText' => __( 'Search' ),
)
);
$input_id = wp_unique_id( 'wp-block-search__input-' );
$classnames = classnames_for_block_core_search( $attributes );
$show_label = ( ! empty( $attributes['showLabel'] ) ) ? true : false;
$use_icon_button = ( ! empty( $attributes['buttonUseIcon'] ) ) ? true : false;
$show_input = ( ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'] ) ? false : true;
$show_button = ( ! empty( $attributes['buttonPosition'] ) && 'no-button' === $attributes['buttonPosition'] ) ? false : true;
$query_params = ( ! empty( $attributes['query'] ) ) ? $attributes['query'] : array();
$input_markup = '';
$button_markup = '';
$query_params_markup = '';
$inline_styles = styles_for_block_core_search( $attributes );
$color_classes = get_color_classes_for_block_core_search( $attributes );
$is_button_inside = ! empty( $attributes['buttonPosition'] ) &&
'button-inside' === $attributes['buttonPosition'];
// Border color classes need to be applied to the elements that have a border color.
$border_color_classes = get_border_color_classes_for_block_core_search( $attributes );
$label_inner_html = empty( $attributes['label'] ) ? __( 'Search' ) : wp_kses_post( $attributes['label'] );
$label_markup = sprintf(
'<label for="%1$s" class="wp-block-search__label screen-reader-text">%2$s</label>',
esc_attr( $input_id ),
$label_inner_html
);
if ( $show_label && ! empty( $attributes['label'] ) ) {
$label_markup = sprintf(
'<label for="%1$s" class="wp-block-search__label">%2$s</label>',
$input_id,
$label_inner_html
);
}
if ( $show_input ) {
$input_classes = ! $is_button_inside ? $border_color_classes : '';
$input_markup = sprintf(
'<input type="search" id="%s" class="wp-block-search__input %s" name="s" value="%s" placeholder="%s" %s required />',
$input_id,
esc_attr( $input_classes ),
get_search_query(),
esc_attr( $attributes['placeholder'] ),
$inline_styles['input']
);
}
if ( count( $query_params ) > 0 ) {
foreach ( $query_params as $param => $value ) {
$query_params_markup .= sprintf(
'<input type="hidden" name="%s" value="%s" />',
esc_attr( $param ),
esc_attr( $value )
);
}
}
if ( $show_button ) {
$button_internal_markup = '';
$button_classes = $color_classes;
$aria_label = '';
if ( ! $is_button_inside ) {
$button_classes .= ' ' . $border_color_classes;
}
if ( ! $use_icon_button ) {
if ( ! empty( $attributes['buttonText'] ) ) {
$button_internal_markup = wp_kses_post( $attributes['buttonText'] );
}
} else {
$aria_label = sprintf( 'aria-label="%s"', esc_attr( wp_strip_all_tags( $attributes['buttonText'] ) ) );
$button_classes .= ' has-icon';
$button_internal_markup =
'<svg class="search-icon" viewBox="0 0 24 24" width="24" height="24">
<path d="M13.5 6C10.5 6 8 8.5 8 11.5c0 1.1.3 2.1.9 3l-3.4 3 1 1.1 3.4-2.9c1 .9 2.2 1.4 3.6 1.4 3 0 5.5-2.5 5.5-5.5C19 8.5 16.5 6 13.5 6zm0 9.5c-2.2 0-4-1.8-4-4s1.8-4 4-4 4 1.8 4 4-1.8 4-4 4z"></path>
</svg>';
}
// Include the button element class.
$button_classes .= ' ' . WP_Theme_JSON_GUTENBERG::__EXPERIMENTAL_ELEMENT_BUTTON_CLASS_NAME;
$button_markup = sprintf(
'<button type="submit" class="wp-block-search__button %s" %s %s>%s</button>',
esc_attr( $button_classes ),
$inline_styles['button'],
$aria_label,
$button_internal_markup
);
}
$field_markup_classes = $is_button_inside ? $border_color_classes : '';
$field_markup = sprintf(
'<div class="wp-block-search__inside-wrapper %s" %s>%s</div>',
esc_attr( $field_markup_classes ),
$inline_styles['wrapper'],
$input_markup . $query_params_markup . $button_markup
);
$wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) );
return sprintf(
'<form role="search" method="get" action="%s" %s>%s</form>',
esc_url( home_url( '/' ) ),
$wrapper_attributes,
$label_markup . $field_markup
);
}
/**
* Registers the `core/search` block on the server.
*/
function register_block_core_search() {
register_block_type_from_metadata(
__DIR__ . '/search',
array(
'render_callback' => 'render_block_core_search',
)
);
}
add_action( 'init', 'register_block_core_search' );
/**
* Builds the correct top level classnames for the 'core/search' block.
*
* @param array $attributes The block attributes.
*
* @return string The classnames used in the block.
*/
function classnames_for_block_core_search( $attributes ) {
$classnames = array();
if ( ! empty( $attributes['buttonPosition'] ) ) {
if ( 'button-inside' === $attributes['buttonPosition'] ) {
$classnames[] = 'wp-block-search__button-inside';
}
if ( 'button-outside' === $attributes['buttonPosition'] ) {
$classnames[] = 'wp-block-search__button-outside';
}
if ( 'no-button' === $attributes['buttonPosition'] ) {
$classnames[] = 'wp-block-search__no-button';
}
if ( 'button-only' === $attributes['buttonPosition'] ) {
$classnames[] = 'wp-block-search__button-only';
}
}
if ( isset( $attributes['buttonUseIcon'] ) ) {
if ( ! empty( $attributes['buttonPosition'] ) && 'no-button' !== $attributes['buttonPosition'] ) {
if ( $attributes['buttonUseIcon'] ) {
$classnames[] = 'wp-block-search__icon-button';
} else {
$classnames[] = 'wp-block-search__text-button';
}
}
}
return implode( ' ', $classnames );
}
/**
* This generates a CSS rule for the given border property and side if provided.
* Based on whether the Search block is configured to display the button inside
* or not, the generated rule is injected into the appropriate collection of
* styles for later application in the block's markup.
*
* @param array $attributes The block attributes.
* @param string $property Border property to generate rule for e.g. width or color.
* @param string $side Optional side border. The dictates the value retrieved and final CSS property.
* @param array $wrapper_styles Current collection of wrapper styles.
* @param array $button_styles Current collection of button styles.
* @param array $input_styles Current collection of input styles.
*
* @return void
*/
function apply_block_core_search_border_style( $attributes, $property, $side, &$wrapper_styles, &$button_styles, &$input_styles ) {
$is_button_inside = 'button-inside' === _wp_array_get( $attributes, array( 'buttonPosition' ), false );
$path = array( 'style', 'border', $property );
if ( $side ) {
array_splice( $path, 2, 0, $side );
}
$value = _wp_array_get( $attributes, $path, false );
if ( empty( $value ) ) {
return;
}
if ( 'color' === $property && $side ) {
$has_color_preset = strpos( $value, 'var:preset|color|' ) !== false;
if ( $has_color_preset ) {
$named_color_value = substr( $value, strrpos( $value, '|' ) + 1 );
$value = sprintf( 'var(--wp--preset--color--%s)', $named_color_value );
}
}
$property_suffix = $side ? sprintf( '%s-%s', $side, $property ) : $property;
if ( $is_button_inside ) {
$wrapper_styles[] = sprintf( 'border-%s: %s;', $property_suffix, esc_attr( $value ) );
} else {
$button_styles[] = sprintf( 'border-%s: %s;', $property_suffix, esc_attr( $value ) );
$input_styles[] = sprintf( 'border-%s: %s;', $property_suffix, esc_attr( $value ) );
}
}
/**
* This adds CSS rules for a given border property e.g. width or color. It
* injects rules into the provided wrapper, button and input style arrays for
* uniform "flat" borders or those with individual sides configured.
*
* @param array $attributes The block attributes.
* @param string $property Border property to generate rule for e.g. width or color.
* @param array $wrapper_styles Current collection of wrapper styles.
* @param array $button_styles Current collection of button styles.
* @param array $input_styles Current collection of input styles.
*
* @return void
*/
function apply_block_core_search_border_styles( $attributes, $property, &$wrapper_styles, &$button_styles, &$input_styles ) {
apply_block_core_search_border_style( $attributes, $property, null, $wrapper_styles, $button_styles, $input_styles );
apply_block_core_search_border_style( $attributes, $property, 'top', $wrapper_styles, $button_styles, $input_styles );
apply_block_core_search_border_style( $attributes, $property, 'right', $wrapper_styles, $button_styles, $input_styles );
apply_block_core_search_border_style( $attributes, $property, 'bottom', $wrapper_styles, $button_styles, $input_styles );
apply_block_core_search_border_style( $attributes, $property, 'left', $wrapper_styles, $button_styles, $input_styles );
}
/**
* Builds an array of inline styles for the search block.
*
* The result will contain one entry for shared styles such as those for the
* inner input or button and a second for the inner wrapper should the block
* be positioning the button "inside".
*
* @param array $attributes The block attributes.
*
* @return array Style HTML attribute.
*/
function styles_for_block_core_search( $attributes ) {
$wrapper_styles = array();
$button_styles = array();
$input_styles = array();
$is_button_inside = ! empty( $attributes['buttonPosition'] ) &&
'button-inside' === $attributes['buttonPosition'];
// Add width styles.
$has_width = ! empty( $attributes['width'] ) && ! empty( $attributes['widthUnit'] );
$button_only = ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'];
if ( $has_width && ! $button_only ) {
$wrapper_styles[] = sprintf(
'width: %d%s;',
esc_attr( $attributes['width'] ),
esc_attr( $attributes['widthUnit'] )
);
}
// Add border width and color styles.
apply_block_core_search_border_styles( $attributes, 'width', $wrapper_styles, $button_styles, $input_styles );
apply_block_core_search_border_styles( $attributes, 'color', $wrapper_styles, $button_styles, $input_styles );
apply_block_core_search_border_styles( $attributes, 'style', $wrapper_styles, $button_styles, $input_styles );
// Add border radius styles.
$has_border_radius = ! empty( $attributes['style']['border']['radius'] );
if ( $has_border_radius ) {
$default_padding = '4px';
$border_radius = $attributes['style']['border']['radius'];
if ( is_array( $border_radius ) ) {
// Apply styles for individual corner border radii.
foreach ( $border_radius as $key => $value ) {
if ( null !== $value ) {
// Convert camelCase key to kebab-case.
$name = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $key ) );
// Add shared styles for individual border radii for input & button.
$border_style = sprintf(
'border-%s-radius: %s;',
esc_attr( $name ),
esc_attr( $value )
);
$input_styles[] = $border_style;
$button_styles[] = $border_style;
// Add adjusted border radius styles for the wrapper element
// if button is positioned inside.
if ( $is_button_inside && intval( $value ) !== 0 ) {
$wrapper_styles[] = sprintf(
'border-%s-radius: calc(%s + %s);',
esc_attr( $name ),
esc_attr( $value ),
$default_padding
);
}
}
}
} else {
// Numeric check is for backwards compatibility purposes.
$border_radius = is_numeric( $border_radius ) ? $border_radius . 'px' : $border_radius;
$border_style = sprintf( 'border-radius: %s;', esc_attr( $border_radius ) );
$input_styles[] = $border_style;
$button_styles[] = $border_style;
if ( $is_button_inside && intval( $border_radius ) !== 0 ) {
// Adjust wrapper border radii to maintain visual consistency
// with inner elements when button is positioned inside.
$wrapper_styles[] = sprintf(
'border-radius: calc(%s + %s);',
esc_attr( $border_radius ),
$default_padding
);
}
}
}
// Add color styles.
$has_text_color = ! empty( $attributes['style']['color']['text'] );
if ( $has_text_color ) {
$button_styles[] = sprintf( 'color: %s;', esc_attr( $attributes['style']['color']['text'] ) );
}
$has_background_color = ! empty( $attributes['style']['color']['background'] );
if ( $has_background_color ) {
$button_styles[] = sprintf( 'background-color: %s;', esc_attr( $attributes['style']['color']['background'] ) );
}
$has_custom_gradient = ! empty( $attributes['style']['color']['gradient'] );
if ( $has_custom_gradient ) {
$button_styles[] = sprintf( 'background: %s;', $attributes['style']['color']['gradient'] );
}
return array(
'input' => ! empty( $input_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $input_styles ) ) ) : '',
'button' => ! empty( $button_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $button_styles ) ) ) : '',
'wrapper' => ! empty( $wrapper_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $wrapper_styles ) ) ) : '',
);
}
/**
* Returns border color classnames depending on whether there are named or custom border colors.
*
* @param array $attributes The block attributes.
*
* @return string The border color classnames to be applied to the block elements.
*/
function get_border_color_classes_for_block_core_search( $attributes ) {
$border_color_classes = array();
$has_custom_border_color = ! empty( $attributes['style']['border']['color'] );
$has_named_border_color = ! empty( $attributes['borderColor'] );
if ( $has_custom_border_color || $has_named_border_color ) {
$border_color_classes[] = 'has-border-color';
}
if ( $has_named_border_color ) {
$border_color_classes[] = sprintf( 'has-%s-border-color', esc_attr( $attributes['borderColor'] ) );
}
return implode( ' ', $border_color_classes );
}
/**
* Returns color classnames depending on whether there are named or custom text and background colors.
*
* @param array $attributes The block attributes.
*
* @return string The color classnames to be applied to the block elements.
*/
function get_color_classes_for_block_core_search( $attributes ) {
$classnames = array();
// Text color.
$has_named_text_color = ! empty( $attributes['textColor'] );
$has_custom_text_color = ! empty( $attributes['style']['color']['text'] );
if ( $has_named_text_color ) {
$classnames[] = sprintf( 'has-text-color has-%s-color', $attributes['textColor'] );
} elseif ( $has_custom_text_color ) {
// If a custom 'textColor' was selected instead of a preset, still add the generic `has-text-color` class.
$classnames[] = 'has-text-color';
}
// Background color.
$has_named_background_color = ! empty( $attributes['backgroundColor'] );
$has_custom_background_color = ! empty( $attributes['style']['color']['background'] );
$has_named_gradient = ! empty( $attributes['gradient'] );
$has_custom_gradient = ! empty( $attributes['style']['color']['gradient'] );
if (
$has_named_background_color ||
$has_custom_background_color ||
$has_named_gradient ||
$has_custom_gradient
) {
$classnames[] = 'has-background';
}
if ( $has_named_background_color ) {
$classnames[] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
}
if ( $has_named_gradient ) {
$classnames[] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] );
}
return implode( ' ', $classnames );
}