PluginProbe
Gutenberg / 20.0.1
Gutenberg v20.0.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 / search.php

search.php in Gutenberg 20.0.1, at build/block-library/blocks/search.php

605 lines 22.7 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/search` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Dynamically renders the `core/search` block.
10 *
11 * @since 6.3.0 Using block.json `viewScript` to register script, and update `view_script_handles()` only when needed.
12 *
13 * @param array $attributes The block attributes.
14 * @param string $content The saved content.
15 * @param WP_Block $block The parsed block.
16 *
17 * @return string The search block markup.
18 */
19 function gutenberg_render_block_core_search( $attributes ) {
20 // Older versions of the Search block defaulted the label and buttonText
21 // attributes to `__( 'Search' )` meaning that many posts contain `<!--
22 // wp:search /-->`. Support these by defaulting an undefined label and
23 // buttonText to `__( 'Search' )`.
24 $attributes = wp_parse_args(
25 $attributes,
26 array(
27 'label' => __( 'Search' ),
28 'buttonText' => __( 'Search' ),
29 )
30 );
31
32 $input_id = wp_unique_id( 'wp-block-search__input-' );
33 $classnames = gutenberg_classnames_for_block_core_search( $attributes );
34 $show_label = ! empty( $attributes['showLabel'] );
35 $use_icon_button = ! empty( $attributes['buttonUseIcon'] );
36 $show_button = ( ! empty( $attributes['buttonPosition'] ) && 'no-button' === $attributes['buttonPosition'] ) ? false : true;
37 $button_position = $show_button ? $attributes['buttonPosition'] : null;
38 $query_params = ( ! empty( $attributes['query'] ) ) ? $attributes['query'] : array();
39 $button = '';
40 $query_params_markup = '';
41 $inline_styles = gutenberg_styles_for_block_core_search( $attributes );
42 $color_classes = gutenberg_get_color_classes_for_block_core_search( $attributes );
43 $typography_classes = gutenberg_get_typography_classes_for_block_core_search( $attributes );
44 $is_button_inside = ! empty( $attributes['buttonPosition'] ) &&
45 'button-inside' === $attributes['buttonPosition'];
46 // Border color classes need to be applied to the elements that have a border color.
47 $border_color_classes = gutenberg_get_border_color_classes_for_block_core_search( $attributes );
48 // This variable is a constant and its value is always false at this moment.
49 // It is defined this way because some values depend on it, in case it changes in the future.
50 $open_by_default = false;
51
52 $label_inner_html = empty( $attributes['label'] ) ? __( 'Search' ) : wp_kses_post( $attributes['label'] );
53 $label = new WP_HTML_Tag_Processor( sprintf( '<label %1$s>%2$s</label>', $inline_styles['label'], $label_inner_html ) );
54 if ( $label->next_tag() ) {
55 $label->set_attribute( 'for', $input_id );
56 $label->add_class( 'wp-block-search__label' );
57 if ( $show_label && ! empty( $attributes['label'] ) ) {
58 if ( ! empty( $typography_classes ) ) {
59 $label->add_class( $typography_classes );
60 }
61 } else {
62 $label->add_class( 'screen-reader-text' );
63 }
64 }
65
66 $input = new WP_HTML_Tag_Processor( sprintf( '<input type="search" name="s" required %s/>', $inline_styles['input'] ) );
67 $input_classes = array( 'wp-block-search__input' );
68 if ( ! $is_button_inside && ! empty( $border_color_classes ) ) {
69 $input_classes[] = $border_color_classes;
70 }
71 if ( ! empty( $typography_classes ) ) {
72 $input_classes[] = $typography_classes;
73 }
74 if ( $input->next_tag() ) {
75 $input->add_class( implode( ' ', $input_classes ) );
76 $input->set_attribute( 'id', $input_id );
77 $input->set_attribute( 'value', get_search_query() );
78 $input->set_attribute( 'placeholder', $attributes['placeholder'] );
79
80 // If it's interactive, enqueue the script module and add the directives.
81 $is_expandable_searchfield = 'button-only' === $button_position;
82 if ( $is_expandable_searchfield ) {
83 wp_enqueue_script_module( '@wordpress/block-library/search/view' );
84
85 $input->set_attribute( 'data-wp-bind--aria-hidden', '!context.isSearchInputVisible' );
86 $input->set_attribute( 'data-wp-bind--tabindex', 'state.tabindex' );
87
88 // Adding these attributes manually is needed until the Interactivity API
89 // SSR logic is added to core.
90 $input->set_attribute( 'aria-hidden', 'true' );
91 $input->set_attribute( 'tabindex', '-1' );
92 }
93 }
94
95 if ( count( $query_params ) > 0 ) {
96 foreach ( $query_params as $param => $value ) {
97 $query_params_markup .= sprintf(
98 '<input type="hidden" name="%s" value="%s" />',
99 esc_attr( $param ),
100 esc_attr( $value )
101 );
102 }
103 }
104
105 if ( $show_button ) {
106 $button_classes = array( 'wp-block-search__button' );
107 $button_internal_markup = '';
108 if ( ! empty( $color_classes ) ) {
109 $button_classes[] = $color_classes;
110 }
111 if ( ! empty( $typography_classes ) ) {
112 $button_classes[] = $typography_classes;
113 }
114
115 if ( ! $is_button_inside && ! empty( $border_color_classes ) ) {
116 $button_classes[] = $border_color_classes;
117 }
118 if ( ! $use_icon_button ) {
119 if ( ! empty( $attributes['buttonText'] ) ) {
120 $button_internal_markup = wp_kses_post( $attributes['buttonText'] );
121 }
122 } else {
123 $button_classes[] = 'has-icon';
124 $button_internal_markup =
125 '<svg class="search-icon" viewBox="0 0 24 24" width="24" height="24">
126 <path d="M13 5c-3.3 0-6 2.7-6 6 0 1.4.5 2.7 1.3 3.7l-3.8 3.8 1.1 1.1 3.8-3.8c1 .8 2.3 1.3 3.7 1.3 3.3 0 6-2.7 6-6S16.3 5 13 5zm0 10.5c-2.5 0-4.5-2-4.5-4.5s2-4.5 4.5-4.5 4.5 2 4.5 4.5-2 4.5-4.5 4.5z"></path>
127 </svg>';
128 }
129
130 // Include the button element class.
131 $button_classes[] = wp_theme_get_element_class_name( 'button' );
132 $button = new WP_HTML_Tag_Processor( sprintf( '<button type="submit" %s>%s</button>', $inline_styles['button'], $button_internal_markup ) );
133
134 if ( $button->next_tag() ) {
135 $button->add_class( implode( ' ', $button_classes ) );
136 if ( 'button-only' === $attributes['buttonPosition'] ) {
137 $button->set_attribute( 'data-wp-bind--aria-label', 'state.ariaLabel' );
138 $button->set_attribute( 'data-wp-bind--aria-controls', 'state.ariaControls' );
139 $button->set_attribute( 'data-wp-bind--aria-expanded', 'context.isSearchInputVisible' );
140 $button->set_attribute( 'data-wp-bind--type', 'state.type' );
141 $button->set_attribute( 'data-wp-on--click', 'actions.openSearchInput' );
142
143 // Adding these attributes manually is needed until the Interactivity
144 // API SSR logic is added to core.
145 $button->set_attribute( 'aria-label', __( 'Expand search field' ) );
146 $button->set_attribute( 'aria-controls', 'wp-block-search__input-' . $input_id );
147 $button->set_attribute( 'aria-expanded', 'false' );
148 $button->set_attribute( 'type', 'button' );
149 } else {
150 $button->set_attribute( 'aria-label', wp_strip_all_tags( $attributes['buttonText'] ) );
151 }
152 }
153 }
154
155 $field_markup_classes = $is_button_inside ? $border_color_classes : '';
156 $field_markup = sprintf(
157 '<div class="wp-block-search__inside-wrapper %s" %s>%s</div>',
158 esc_attr( $field_markup_classes ),
159 $inline_styles['wrapper'],
160 $input . $query_params_markup . $button
161 );
162 $wrapper_attributes = get_block_wrapper_attributes(
163 array( 'class' => $classnames )
164 );
165 $form_directives = '';
166
167 // If it's interactive, add the directives.
168 if ( $is_expandable_searchfield ) {
169 $aria_label_expanded = __( 'Submit Search' );
170 $aria_label_collapsed = __( 'Expand search field' );
171 $form_context = wp_interactivity_data_wp_context(
172 array(
173 'isSearchInputVisible' => $open_by_default,
174 'inputId' => $input_id,
175 'ariaLabelExpanded' => $aria_label_expanded,
176 'ariaLabelCollapsed' => $aria_label_collapsed,
177 )
178 );
179 $form_directives = '
180 data-wp-interactive="core/search"
181 ' . $form_context . '
182 data-wp-class--wp-block-search__searchfield-hidden="!context.isSearchInputVisible"
183 data-wp-on-async--keydown="actions.handleSearchKeydown"
184 data-wp-on-async--focusout="actions.handleSearchFocusout"
185 ';
186 }
187
188 return sprintf(
189 '<form role="search" method="get" action="%1s" %2s %3s>%4s</form>',
190 esc_url( home_url( '/' ) ),
191 $wrapper_attributes,
192 $form_directives,
193 $label . $field_markup
194 );
195 }
196
197 /**
198 * Registers the `core/search` block on the server.
199 *
200 * @since 5.2.0
201 */
202 function gutenberg_register_block_core_search() {
203 register_block_type_from_metadata(
204 __DIR__ . '/search',
205 array(
206 'render_callback' => 'gutenberg_render_block_core_search',
207 )
208 );
209 }
210 add_action( 'init', 'gutenberg_register_block_core_search', 20 );
211
212 /**
213 * Builds the correct top level classnames for the 'core/search' block.
214 *
215 * @since 5.6.0
216 *
217 * @param array $attributes The block attributes.
218 *
219 * @return string The classnames used in the block.
220 */
221 function gutenberg_classnames_for_block_core_search( $attributes ) {
222 $classnames = array();
223
224 if ( ! empty( $attributes['buttonPosition'] ) ) {
225 if ( 'button-inside' === $attributes['buttonPosition'] ) {
226 $classnames[] = 'wp-block-search__button-inside';
227 }
228
229 if ( 'button-outside' === $attributes['buttonPosition'] ) {
230 $classnames[] = 'wp-block-search__button-outside';
231 }
232
233 if ( 'no-button' === $attributes['buttonPosition'] ) {
234 $classnames[] = 'wp-block-search__no-button';
235 }
236
237 if ( 'button-only' === $attributes['buttonPosition'] ) {
238 $classnames[] = 'wp-block-search__button-only wp-block-search__searchfield-hidden';
239 }
240 }
241
242 if ( isset( $attributes['buttonUseIcon'] ) ) {
243 if ( ! empty( $attributes['buttonPosition'] ) && 'no-button' !== $attributes['buttonPosition'] ) {
244 if ( $attributes['buttonUseIcon'] ) {
245 $classnames[] = 'wp-block-search__icon-button';
246 } else {
247 $classnames[] = 'wp-block-search__text-button';
248 }
249 }
250 }
251
252 return implode( ' ', $classnames );
253 }
254
255 /**
256 * This generates a CSS rule for the given border property and side if provided.
257 * Based on whether the Search block is configured to display the button inside
258 * or not, the generated rule is injected into the appropriate collection of
259 * styles for later application in the block's markup.
260 *
261 * @since 6.1.0
262 *
263 * @param array $attributes The block attributes.
264 * @param string $property Border property to generate rule for e.g. width or color.
265 * @param string $side Optional side border. The dictates the value retrieved and final CSS property.
266 * @param array $wrapper_styles Current collection of wrapper styles.
267 * @param array $button_styles Current collection of button styles.
268 * @param array $input_styles Current collection of input styles.
269 */
270 function gutenberg_apply_block_core_search_border_style( $attributes, $property, $side, &$wrapper_styles, &$button_styles, &$input_styles ) {
271 $is_button_inside = isset( $attributes['buttonPosition'] ) && 'button-inside' === $attributes['buttonPosition'];
272
273 $path = array( 'style', 'border', $property );
274
275 if ( $side ) {
276 array_splice( $path, 2, 0, $side );
277 }
278
279 $value = _wp_array_get( $attributes, $path, false );
280
281 if ( empty( $value ) ) {
282 return;
283 }
284
285 if ( 'color' === $property && $side ) {
286 $has_color_preset = str_contains( $value, 'var:preset|color|' );
287 if ( $has_color_preset ) {
288 $named_color_value = substr( $value, strrpos( $value, '|' ) + 1 );
289 $value = sprintf( 'var(--wp--preset--color--%s)', $named_color_value );
290 }
291 }
292
293 $property_suffix = $side ? sprintf( '%s-%s', $side, $property ) : $property;
294
295 if ( $is_button_inside ) {
296 $wrapper_styles[] = sprintf( 'border-%s: %s;', $property_suffix, esc_attr( $value ) );
297 } else {
298 $button_styles[] = sprintf( 'border-%s: %s;', $property_suffix, esc_attr( $value ) );
299 $input_styles[] = sprintf( 'border-%s: %s;', $property_suffix, esc_attr( $value ) );
300 }
301 }
302
303 /**
304 * This adds CSS rules for a given border property e.g. width or color. It
305 * injects rules into the provided wrapper, button and input style arrays for
306 * uniform "flat" borders or those with individual sides configured.
307 *
308 * @since 6.1.0
309 *
310 * @param array $attributes The block attributes.
311 * @param string $property Border property to generate rule for e.g. width or color.
312 * @param array $wrapper_styles Current collection of wrapper styles.
313 * @param array $button_styles Current collection of button styles.
314 * @param array $input_styles Current collection of input styles.
315 */
316 function gutenberg_apply_block_core_search_border_styles( $attributes, $property, &$wrapper_styles, &$button_styles, &$input_styles ) {
317 gutenberg_apply_block_core_search_border_style( $attributes, $property, null, $wrapper_styles, $button_styles, $input_styles );
318 gutenberg_apply_block_core_search_border_style( $attributes, $property, 'top', $wrapper_styles, $button_styles, $input_styles );
319 gutenberg_apply_block_core_search_border_style( $attributes, $property, 'right', $wrapper_styles, $button_styles, $input_styles );
320 gutenberg_apply_block_core_search_border_style( $attributes, $property, 'bottom', $wrapper_styles, $button_styles, $input_styles );
321 gutenberg_apply_block_core_search_border_style( $attributes, $property, 'left', $wrapper_styles, $button_styles, $input_styles );
322 }
323
324 /**
325 * Builds an array of inline styles for the search block.
326 *
327 * The result will contain one entry for shared styles such as those for the
328 * inner input or button and a second for the inner wrapper should the block
329 * be positioning the button "inside".
330 *
331 * @since 5.8.0
332 *
333 * @param array $attributes The block attributes.
334 *
335 * @return array Style HTML attribute.
336 */
337 function gutenberg_styles_for_block_core_search( $attributes ) {
338 $wrapper_styles = array();
339 $button_styles = array();
340 $input_styles = array();
341 $label_styles = array();
342 $is_button_inside = ! empty( $attributes['buttonPosition'] ) &&
343 'button-inside' === $attributes['buttonPosition'];
344 $show_label = ( isset( $attributes['showLabel'] ) ) && false !== $attributes['showLabel'];
345
346 // Add width styles.
347 $has_width = ! empty( $attributes['width'] ) && ! empty( $attributes['widthUnit'] );
348
349 if ( $has_width ) {
350 $wrapper_styles[] = sprintf(
351 'width: %d%s;',
352 esc_attr( $attributes['width'] ),
353 esc_attr( $attributes['widthUnit'] )
354 );
355 }
356
357 // Add border width and color styles.
358 gutenberg_apply_block_core_search_border_styles( $attributes, 'width', $wrapper_styles, $button_styles, $input_styles );
359 gutenberg_apply_block_core_search_border_styles( $attributes, 'color', $wrapper_styles, $button_styles, $input_styles );
360 gutenberg_apply_block_core_search_border_styles( $attributes, 'style', $wrapper_styles, $button_styles, $input_styles );
361
362 // Add border radius styles.
363 $has_border_radius = ! empty( $attributes['style']['border']['radius'] );
364
365 if ( $has_border_radius ) {
366 $default_padding = '4px';
367 $border_radius = $attributes['style']['border']['radius'];
368
369 if ( is_array( $border_radius ) ) {
370 // Apply styles for individual corner border radii.
371 foreach ( $border_radius as $key => $value ) {
372 if ( null !== $value ) {
373 // Convert camelCase key to kebab-case.
374 $name = strtolower( preg_replace( '/(?<!^)[A-Z]/', '-$0', $key ) );
375
376 // Add shared styles for individual border radii for input & button.
377 $border_style = sprintf(
378 'border-%s-radius: %s;',
379 esc_attr( $name ),
380 esc_attr( $value )
381 );
382 $input_styles[] = $border_style;
383 $button_styles[] = $border_style;
384
385 // Add adjusted border radius styles for the wrapper element
386 // if button is positioned inside.
387 if ( $is_button_inside && intval( $value ) !== 0 ) {
388 $wrapper_styles[] = sprintf(
389 'border-%s-radius: calc(%s + %s);',
390 esc_attr( $name ),
391 esc_attr( $value ),
392 $default_padding
393 );
394 }
395 }
396 }
397 } else {
398 // Numeric check is for backwards compatibility purposes.
399 $border_radius = is_numeric( $border_radius ) ? $border_radius . 'px' : $border_radius;
400 $border_style = sprintf( 'border-radius: %s;', esc_attr( $border_radius ) );
401 $input_styles[] = $border_style;
402 $button_styles[] = $border_style;
403
404 if ( $is_button_inside && intval( $border_radius ) !== 0 ) {
405 // Adjust wrapper border radii to maintain visual consistency
406 // with inner elements when button is positioned inside.
407 $wrapper_styles[] = sprintf(
408 'border-radius: calc(%s + %s);',
409 esc_attr( $border_radius ),
410 $default_padding
411 );
412 }
413 }
414 }
415
416 // Add color styles.
417 $has_text_color = ! empty( $attributes['style']['color']['text'] );
418 if ( $has_text_color ) {
419 $button_styles[] = sprintf( 'color: %s;', $attributes['style']['color']['text'] );
420 }
421
422 $has_background_color = ! empty( $attributes['style']['color']['background'] );
423 if ( $has_background_color ) {
424 $button_styles[] = sprintf( 'background-color: %s;', $attributes['style']['color']['background'] );
425 }
426
427 $has_custom_gradient = ! empty( $attributes['style']['color']['gradient'] );
428 if ( $has_custom_gradient ) {
429 $button_styles[] = sprintf( 'background: %s;', $attributes['style']['color']['gradient'] );
430 }
431
432 // Get typography styles to be shared across inner elements.
433 $typography_styles = esc_attr( get_typography_gutenberg_styles_for_block_core_search( $attributes ) );
434 if ( ! empty( $typography_styles ) ) {
435 $label_styles [] = $typography_styles;
436 $button_styles[] = $typography_styles;
437 $input_styles [] = $typography_styles;
438 }
439
440 // Typography text-decoration is only applied to the label and button.
441 if ( ! empty( $attributes['style']['typography']['textDecoration'] ) ) {
442 $text_decoration_value = sprintf( 'text-decoration: %s;', esc_attr( $attributes['style']['typography']['textDecoration'] ) );
443 $button_styles[] = $text_decoration_value;
444 // Input opts out of text decoration.
445 if ( $show_label ) {
446 $label_styles[] = $text_decoration_value;
447 }
448 }
449
450 return array(
451 'input' => ! empty( $input_styles ) ? sprintf( ' style="%s"', esc_attr( safecss_filter_attr( implode( ' ', $input_styles ) ) ) ) : '',
452 'button' => ! empty( $button_styles ) ? sprintf( ' style="%s"', esc_attr( safecss_filter_attr( implode( ' ', $button_styles ) ) ) ) : '',
453 'wrapper' => ! empty( $wrapper_styles ) ? sprintf( ' style="%s"', esc_attr( safecss_filter_attr( implode( ' ', $wrapper_styles ) ) ) ) : '',
454 'label' => ! empty( $label_styles ) ? sprintf( ' style="%s"', esc_attr( safecss_filter_attr( implode( ' ', $label_styles ) ) ) ) : '',
455 );
456 }
457
458 /**
459 * Returns typography classnames depending on whether there are named font sizes/families.
460 *
461 * @since 6.1.0
462 *
463 * @param array $attributes The block attributes.
464 *
465 * @return string The typography color classnames to be applied to the block elements.
466 */
467 function gutenberg_get_typography_classes_for_block_core_search( $attributes ) {
468 $typography_classes = array();
469 $has_named_font_family = ! empty( $attributes['fontFamily'] );
470 $has_named_font_size = ! empty( $attributes['fontSize'] );
471
472 if ( $has_named_font_size ) {
473 $typography_classes[] = sprintf( 'has-%s-font-size', esc_attr( $attributes['fontSize'] ) );
474 }
475
476 if ( $has_named_font_family ) {
477 $typography_classes[] = sprintf( 'has-%s-font-family', esc_attr( $attributes['fontFamily'] ) );
478 }
479
480 return implode( ' ', $typography_classes );
481 }
482
483 /**
484 * Returns typography styles to be included in an HTML style tag.
485 * This excludes text-decoration, which is applied only to the label and button elements of the search block.
486 *
487 * @since 6.1.0
488 *
489 * @param array $attributes The block attributes.
490 *
491 * @return string A string of typography CSS declarations.
492 */
493 function get_typography_gutenberg_styles_for_block_core_search( $attributes ) {
494 $typography_styles = array();
495
496 // Add typography styles.
497 if ( ! empty( $attributes['style']['typography']['fontSize'] ) ) {
498 $typography_styles[] = sprintf(
499 'font-size: %s;',
500 gutenberg_get_typography_font_size_value(
501 array(
502 'size' => $attributes['style']['typography']['fontSize'],
503 )
504 )
505 );
506
507 }
508
509 if ( ! empty( $attributes['style']['typography']['fontFamily'] ) ) {
510 $typography_styles[] = sprintf( 'font-family: %s;', $attributes['style']['typography']['fontFamily'] );
511 }
512
513 if ( ! empty( $attributes['style']['typography']['letterSpacing'] ) ) {
514 $typography_styles[] = sprintf( 'letter-spacing: %s;', $attributes['style']['typography']['letterSpacing'] );
515 }
516
517 if ( ! empty( $attributes['style']['typography']['fontWeight'] ) ) {
518 $typography_styles[] = sprintf( 'font-weight: %s;', $attributes['style']['typography']['fontWeight'] );
519 }
520
521 if ( ! empty( $attributes['style']['typography']['fontStyle'] ) ) {
522 $typography_styles[] = sprintf( 'font-style: %s;', $attributes['style']['typography']['fontStyle'] );
523 }
524
525 if ( ! empty( $attributes['style']['typography']['lineHeight'] ) ) {
526 $typography_styles[] = sprintf( 'line-height: %s;', $attributes['style']['typography']['lineHeight'] );
527 }
528
529 if ( ! empty( $attributes['style']['typography']['textTransform'] ) ) {
530 $typography_styles[] = sprintf( 'text-transform: %s;', $attributes['style']['typography']['textTransform'] );
531 }
532
533 return implode( '', $typography_styles );
534 }
535
536 /**
537 * Returns border color classnames depending on whether there are named or custom border colors.
538 *
539 * @since 5.9.0
540 *
541 * @param array $attributes The block attributes.
542 *
543 * @return string The border color classnames to be applied to the block elements.
544 */
545 function gutenberg_get_border_color_classes_for_block_core_search( $attributes ) {
546 $border_color_classes = array();
547 $has_custom_border_color = ! empty( $attributes['style']['border']['color'] );
548 $has_named_border_color = ! empty( $attributes['borderColor'] );
549
550 if ( $has_custom_border_color || $has_named_border_color ) {
551 $border_color_classes[] = 'has-border-color';
552 }
553
554 if ( $has_named_border_color ) {
555 $border_color_classes[] = sprintf( 'has-%s-border-color', esc_attr( $attributes['borderColor'] ) );
556 }
557
558 return implode( ' ', $border_color_classes );
559 }
560
561 /**
562 * Returns color classnames depending on whether there are named or custom text and background colors.
563 *
564 * @since 5.9.0
565 *
566 * @param array $attributes The block attributes.
567 *
568 * @return string The color classnames to be applied to the block elements.
569 */
570 function gutenberg_get_color_classes_for_block_core_search( $attributes ) {
571 $classnames = array();
572
573 // Text color.
574 $has_named_text_color = ! empty( $attributes['textColor'] );
575 $has_custom_text_color = ! empty( $attributes['style']['color']['text'] );
576 if ( $has_named_text_color ) {
577 $classnames[] = sprintf( 'has-text-color has-%s-color', $attributes['textColor'] );
578 } elseif ( $has_custom_text_color ) {
579 // If a custom 'textColor' was selected instead of a preset, still add the generic `has-text-color` class.
580 $classnames[] = 'has-text-color';
581 }
582
583 // Background color.
584 $has_named_background_color = ! empty( $attributes['backgroundColor'] );
585 $has_custom_background_color = ! empty( $attributes['style']['color']['background'] );
586 $has_named_gradient = ! empty( $attributes['gradient'] );
587 $has_custom_gradient = ! empty( $attributes['style']['color']['gradient'] );
588 if (
589 $has_named_background_color ||
590 $has_custom_background_color ||
591 $has_named_gradient ||
592 $has_custom_gradient
593 ) {
594 $classnames[] = 'has-background';
595 }
596 if ( $has_named_background_color ) {
597 $classnames[] = sprintf( 'has-%s-background-color', $attributes['backgroundColor'] );
598 }
599 if ( $has_named_gradient ) {
600 $classnames[] = sprintf( 'has-%s-gradient-background', $attributes['gradient'] );
601 }
602
603 return implode( ' ', $classnames );
604 }
605