Changeset 54257 for trunk/src/wp-includes/blocks/search.php
- Timestamp:
- 09/20/2022 03:14:54 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/search.php
r53879 r54257 14 14 */ 15 15 function render_block_core_search( $attributes ) { 16 static $instance_id = 0;17 18 16 // Older versions of the Search block defaulted the label and buttonText 19 17 // attributes to `__( 'Search' )` meaning that many posts contain `<!-- … … 28 26 ); 29 27 30 $input_id = 'wp-block-search__input-' . ++$instance_id; 31 $classnames = classnames_for_block_core_search( $attributes ); 32 $show_label = ( ! empty( $attributes['showLabel'] ) ) ? true : false; 33 $use_icon_button = ( ! empty( $attributes['buttonUseIcon'] ) ) ? true : false; 34 $show_input = ( ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'] ) ? false : true; 35 $show_button = ( ! empty( $attributes['buttonPosition'] ) && 'no-button' === $attributes['buttonPosition'] ) ? false : true; 36 $input_markup = ''; 37 $button_markup = ''; 38 $inline_styles = styles_for_block_core_search( $attributes ); 39 $color_classes = get_color_classes_for_block_core_search( $attributes ); 40 $is_button_inside = ! empty( $attributes['buttonPosition'] ) && 28 $input_id = wp_unique_id( 'wp-block-search__input-' ); 29 $classnames = classnames_for_block_core_search( $attributes ); 30 $show_label = ( ! empty( $attributes['showLabel'] ) ) ? true : false; 31 $use_icon_button = ( ! empty( $attributes['buttonUseIcon'] ) ) ? true : false; 32 $show_input = ( ! empty( $attributes['buttonPosition'] ) && 'button-only' === $attributes['buttonPosition'] ) ? false : true; 33 $show_button = ( ! empty( $attributes['buttonPosition'] ) && 'no-button' === $attributes['buttonPosition'] ) ? false : true; 34 $query_params = ( ! empty( $attributes['query'] ) ) ? $attributes['query'] : array(); 35 $input_markup = ''; 36 $button_markup = ''; 37 $query_params_markup = ''; 38 $inline_styles = styles_for_block_core_search( $attributes ); 39 $color_classes = get_color_classes_for_block_core_search( $attributes ); 40 $typography_classes = get_typography_classes_for_block_core_search( $attributes ); 41 $is_button_inside = ! empty( $attributes['buttonPosition'] ) && 41 42 'button-inside' === $attributes['buttonPosition']; 42 43 // Border color classes need to be applied to the elements that have a border color. … … 51 52 ); 52 53 if ( $show_label && ! empty( $attributes['label'] ) ) { 54 $label_classes = array( 'wp-block-search__label' ); 55 if ( ! empty( $typography_classes ) ) { 56 $label_classes[] = $typography_classes; 57 } 53 58 $label_markup = sprintf( 54 '<label for="%1$s" class="wp-block-search__label">%2$s</label>', 55 $input_id, 59 '<label for="%1$s" class="%2$s" %3$s>%4$s</label>', 60 esc_attr( $input_id ), 61 esc_attr( implode( ' ', $label_classes ) ), 62 $inline_styles['label'], 56 63 $label_inner_html 57 64 ); … … 59 66 60 67 if ( $show_input ) { 61 $input_classes = ! $is_button_inside ? $border_color_classes : ''; 62 $input_markup = sprintf( 68 $input_classes = array( 'wp-block-search__input' ); 69 if ( $is_button_inside ) { 70 $input_classes[] = $border_color_classes; 71 } 72 if ( ! empty( $typography_classes ) ) { 73 $input_classes[] = $typography_classes; 74 } 75 $input_markup = sprintf( 63 76 '<input type="search" id="%s" class="wp-block-search__input %s" name="s" value="%s" placeholder="%s" %s required />', 64 77 $input_id, 65 esc_attr( $input_classes),66 esc_attr( get_search_query()),78 esc_attr( implode( ' ', $input_classes ) ), 79 get_search_query(), 67 80 esc_attr( $attributes['placeholder'] ), 68 81 $inline_styles['input'] … … 70 83 } 71 84 85 if ( count( $query_params ) > 0 ) { 86 foreach ( $query_params as $param => $value ) { 87 $query_params_markup .= sprintf( 88 '<input type="hidden" name="%s" value="%s" />', 89 esc_attr( $param ), 90 esc_attr( $value ) 91 ); 92 } 93 } 94 72 95 if ( $show_button ) { 96 $button_classes = array( 'wp-block-search__button' ); 73 97 $button_internal_markup = ''; 74 $button_classes = $color_classes; 75 $aria_label = ''; 76 77 if ( ! $is_button_inside ) { 78 $button_classes .= ' ' . $border_color_classes; 98 if ( ! empty( $color_classes ) ) { 99 $button_classes[] = $color_classes; 100 } 101 if ( ! empty( $typography_classes ) ) { 102 $button_classes[] = $typography_classes; 103 } 104 $aria_label = ''; 105 106 if ( ! $is_button_inside && ! empty( $border_color_classes ) ) { 107 $button_classes[] = $border_color_classes; 79 108 } 80 109 if ( ! $use_icon_button ) { … … 83 112 } 84 113 } else { 85 $aria_label = sprintf( 'aria-label="%s"', esc_attr( wp_strip_all_tags( $attributes['buttonText'] ) ) );86 $button_classes .= 'has-icon';114 $aria_label = sprintf( 'aria-label="%s"', esc_attr( wp_strip_all_tags( $attributes['buttonText'] ) ) ); 115 $button_classes[] = 'has-icon'; 87 116 88 117 $button_internal_markup = 89 '<svg id="search-icon"class="search-icon" viewBox="0 0 24 24" width="24" height="24">118 '<svg class="search-icon" viewBox="0 0 24 24" width="24" height="24"> 90 119 <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> 91 120 </svg>'; 92 121 } 93 122 94 $button_markup = sprintf( 95 '<button type="submit" class="wp-block-search__button %s" %s %s>%s</button>', 96 esc_attr( $button_classes ), 123 // Include the button element class. 124 $button_classes[] = wp_theme_get_element_class_name( 'button' ); 125 $button_markup = sprintf( 126 '<button type="submit" class="%s" %s %s>%s</button>', 127 esc_attr( implode( ' ', $button_classes ) ), 97 128 $inline_styles['button'], 98 129 $aria_label, … … 106 137 esc_attr( $field_markup_classes ), 107 138 $inline_styles['wrapper'], 108 $input_markup . $button_markup 109 ); 110 $wrapper_attributes = get_block_wrapper_attributes( array( 'class' => $classnames ) ); 139 $input_markup . $query_params_markup . $button_markup 140 ); 141 $wrapper_attributes = get_block_wrapper_attributes( 142 array( 'class' => $classnames ) 143 ); 111 144 112 145 return sprintf( … … 173 206 174 207 /** 208 * This generates a CSS rule for the given border property and side if provided. 209 * Based on whether the Search block is configured to display the button inside 210 * or not, the generated rule is injected into the appropriate collection of 211 * styles for later application in the block's markup. 212 * 213 * @param array $attributes The block attributes. 214 * @param string $property Border property to generate rule for e.g. width or color. 215 * @param string $side Optional side border. The dictates the value retrieved and final CSS property. 216 * @param array $wrapper_styles Current collection of wrapper styles. 217 * @param array $button_styles Current collection of button styles. 218 * @param array $input_styles Current collection of input styles. 219 * 220 * @return void 221 */ 222 function apply_block_core_search_border_style( $attributes, $property, $side, &$wrapper_styles, &$button_styles, &$input_styles ) { 223 $is_button_inside = 'button-inside' === _wp_array_get( $attributes, array( 'buttonPosition' ), false ); 224 225 $path = array( 'style', 'border', $property ); 226 227 if ( $side ) { 228 array_splice( $path, 2, 0, $side ); 229 } 230 231 $value = _wp_array_get( $attributes, $path, false ); 232 233 if ( empty( $value ) ) { 234 return; 235 } 236 237 if ( 'color' === $property && $side ) { 238 $has_color_preset = str_contains( $value, 'var:preset|color|' ); 239 if ( $has_color_preset ) { 240 $named_color_value = substr( $value, strrpos( $value, '|' ) + 1 ); 241 $value = sprintf( 'var(--wp--preset--color--%s)', $named_color_value ); 242 } 243 } 244 245 $property_suffix = $side ? sprintf( '%s-%s', $side, $property ) : $property; 246 247 if ( $is_button_inside ) { 248 $wrapper_styles[] = sprintf( 'border-%s: %s;', $property_suffix, esc_attr( $value ) ); 249 } else { 250 $button_styles[] = sprintf( 'border-%s: %s;', $property_suffix, esc_attr( $value ) ); 251 $input_styles[] = sprintf( 'border-%s: %s;', $property_suffix, esc_attr( $value ) ); 252 } 253 } 254 255 /** 256 * This adds CSS rules for a given border property e.g. width or color. It 257 * injects rules into the provided wrapper, button and input style arrays for 258 * uniform "flat" borders or those with individual sides configured. 259 * 260 * @param array $attributes The block attributes. 261 * @param string $property Border property to generate rule for e.g. width or color. 262 * @param array $wrapper_styles Current collection of wrapper styles. 263 * @param array $button_styles Current collection of button styles. 264 * @param array $input_styles Current collection of input styles. 265 * 266 * @return void 267 */ 268 function apply_block_core_search_border_styles( $attributes, $property, &$wrapper_styles, &$button_styles, &$input_styles ) { 269 apply_block_core_search_border_style( $attributes, $property, null, $wrapper_styles, $button_styles, $input_styles ); 270 apply_block_core_search_border_style( $attributes, $property, 'top', $wrapper_styles, $button_styles, $input_styles ); 271 apply_block_core_search_border_style( $attributes, $property, 'right', $wrapper_styles, $button_styles, $input_styles ); 272 apply_block_core_search_border_style( $attributes, $property, 'bottom', $wrapper_styles, $button_styles, $input_styles ); 273 apply_block_core_search_border_style( $attributes, $property, 'left', $wrapper_styles, $button_styles, $input_styles ); 274 } 275 276 /** 175 277 * Builds an array of inline styles for the search block. 176 278 * … … 187 289 $button_styles = array(); 188 290 $input_styles = array(); 291 $label_styles = array(); 189 292 $is_button_inside = ! empty( $attributes['buttonPosition'] ) && 190 293 'button-inside' === $attributes['buttonPosition']; 294 $show_label = ( isset( $attributes['showLabel'] ) ) && false !== $attributes['showLabel']; 191 295 192 296 // Add width styles. … … 202 306 } 203 307 204 // Add border width styles. 205 $has_border_width = ! empty( $attributes['style']['border']['width'] ); 206 207 if ( $has_border_width ) { 208 $border_width = $attributes['style']['border']['width']; 209 210 if ( $is_button_inside ) { 211 $wrapper_styles[] = sprintf( 'border-width: %s;', esc_attr( $border_width ) ); 212 } else { 213 $button_styles[] = sprintf( 'border-width: %s;', esc_attr( $border_width ) ); 214 $input_styles[] = sprintf( 'border-width: %s;', esc_attr( $border_width ) ); 215 } 216 } 308 // Add border width and color styles. 309 apply_block_core_search_border_styles( $attributes, 'width', $wrapper_styles, $button_styles, $input_styles ); 310 apply_block_core_search_border_styles( $attributes, 'color', $wrapper_styles, $button_styles, $input_styles ); 311 apply_block_core_search_border_styles( $attributes, 'style', $wrapper_styles, $button_styles, $input_styles ); 217 312 218 313 // Add border radius styles. … … 270 365 } 271 366 272 // Add border color styles.273 $has_border_color = ! empty( $attributes['style']['border']['color'] );274 275 if ( $has_border_color ) {276 $border_color = $attributes['style']['border']['color'];277 278 // Apply wrapper border color if button placed inside.279 if ( $is_button_inside ) {280 $wrapper_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) );281 } else {282 $button_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) );283 $input_styles[] = sprintf( 'border-color: %s;', esc_attr( $border_color ) );284 }285 }286 287 367 // Add color styles. 288 368 $has_text_color = ! empty( $attributes['style']['color']['text'] ); … … 301 381 } 302 382 383 // Get typography styles to be shared across inner elements. 384 $typography_styles = get_typography_styles_for_block_core_search( $attributes ); 385 if ( ! empty( $typography_styles ) ) { 386 $label_styles [] = $typography_styles; 387 $button_styles[] = $typography_styles; 388 $input_styles [] = $typography_styles; 389 } 390 391 // Typography text-decoration is only applied to the label and button. 392 if ( ! empty( $attributes['style']['typography']['textDecoration'] ) ) { 393 $text_decoration_value = sprintf( 'text-decoration: %s;', esc_attr( $attributes['style']['typography']['textDecoration'] ) ); 394 $button_styles[] = $text_decoration_value; 395 // Input opts out of text decoration. 396 if ( $show_label ) { 397 $label_styles[] = $text_decoration_value; 398 } 399 } 400 303 401 return array( 304 402 'input' => ! empty( $input_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $input_styles ) ) ) : '', 305 403 'button' => ! empty( $button_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $button_styles ) ) ) : '', 306 404 'wrapper' => ! empty( $wrapper_styles ) ? sprintf( ' style="%s"', safecss_filter_attr( implode( ' ', $wrapper_styles ) ) ) : '', 307 ); 405 'label' => ! empty( $label_styles ) ? sprintf( ' style="%s"', esc_attr( safecss_filter_attr( implode( ' ', $label_styles ) ) ) ) : '', 406 ); 407 } 408 409 /** 410 * Returns typography classnames depending on whether there are named font sizes/families . 411 * 412 * @param array $attributes The block attributes. 413 * 414 * @return string The typography color classnames to be applied to the block elements. 415 */ 416 function get_typography_classes_for_block_core_search( $attributes ) { 417 $typography_classes = array(); 418 $has_named_font_family = ! empty( $attributes['fontFamily'] ); 419 $has_named_font_size = ! empty( $attributes['fontSize'] ); 420 421 if ( $has_named_font_size ) { 422 $typography_classes[] = sprintf( 'has-%s-font-size', esc_attr( $attributes['fontSize'] ) ); 423 } 424 425 if ( $has_named_font_family ) { 426 $typography_classes[] = sprintf( 'has-%s-font-family', esc_attr( $attributes['fontFamily'] ) ); 427 } 428 429 return implode( ' ', $typography_classes ); 430 } 431 432 /** 433 * Returns typography styles to be included in an HTML style tag. 434 * This excludes text-decoration, which is applied only to the label and button elements of the search block. 435 * 436 * @param array $attributes The block attributes. 437 * 438 * @return string A string of typography CSS declarations. 439 */ 440 function get_typography_styles_for_block_core_search( $attributes ) { 441 $typography_styles = array(); 442 443 // Add typography styles. 444 if ( ! empty( $attributes['style']['typography']['fontSize'] ) ) { 445 $typography_styles[] = sprintf( 'font-size: %s;', esc_attr( $attributes['style']['typography']['fontSize'] ) ); 446 } 447 448 if ( ! empty( $attributes['style']['typography']['fontFamily'] ) ) { 449 $typography_styles[] = sprintf( 'font-family: %s;', esc_attr( $attributes['style']['typography']['fontFamily'] ) ); 450 } 451 452 if ( ! empty( $attributes['style']['typography']['letterSpacing'] ) ) { 453 $typography_styles[] = sprintf( 'letter-spacing: %s;', esc_attr( $attributes['style']['typography']['letterSpacing'] ) ); 454 } 455 456 if ( ! empty( $attributes['style']['typography']['fontWeight'] ) ) { 457 $typography_styles[] = sprintf( 'font-weight: %s;', esc_attr( $attributes['style']['typography']['fontWeight'] ) ); 458 } 459 460 if ( ! empty( $attributes['style']['typography']['fontStyle'] ) ) { 461 $typography_styles[] = sprintf( 'font-style: %s;', esc_attr( $attributes['style']['typography']['fontStyle'] ) ); 462 } 463 464 if ( ! empty( $attributes['style']['typography']['lineHeight'] ) ) { 465 $typography_styles[] = sprintf( 'line-height: %s;', esc_attr( $attributes['style']['typography']['lineHeight'] ) ); 466 } 467 468 if ( ! empty( $attributes['style']['typography']['textTransform'] ) ) { 469 $typography_styles[] = sprintf( 'text-transform: %s;', esc_attr( $attributes['style']['typography']['textTransform'] ) ); 470 } 471 472 return implode( '', $typography_styles ); 308 473 } 309 474 … … 316 481 */ 317 482 function get_border_color_classes_for_block_core_search( $attributes ) { 483 $border_color_classes = array(); 318 484 $has_custom_border_color = ! empty( $attributes['style']['border']['color'] ); 319 $border_color_classes = ! empty( $attributes['borderColor'] ) ? sprintf( 'has-border-color has-%s-border-color', $attributes['borderColor'] ) : ''; 320 // If there's a border color style and no `borderColor` text string, we still want to add the generic `has-border-color` class name to the element. 321 if ( $has_custom_border_color && empty( $attributes['borderColor'] ) ) { 322 $border_color_classes = 'has-border-color'; 323 } 324 return $border_color_classes; 485 $has_named_border_color = ! empty( $attributes['borderColor'] ); 486 487 if ( $has_custom_border_color || $has_named_border_color ) { 488 $border_color_classes[] = 'has-border-color'; 489 } 490 491 if ( $has_named_border_color ) { 492 $border_color_classes[] = sprintf( 'has-%s-border-color', esc_attr( $attributes['borderColor'] ) ); 493 } 494 495 return implode( ' ', $border_color_classes ); 325 496 } 326 497
Note: See TracChangeset
for help on using the changeset viewer.