wppaste
WordPress

styles_for_block_core_search( array $attributes ): array

Since
5.8.0
Source
wp-includes/blocks/search.php:367
Builds an array of inline styles for the search block.

Description

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".

Compatibility

WordPress
since 5.8.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$attributesarray
The block attributes.

Return value

array
Style HTML attribute.

Performance profile

How much work a call to styles_for_block_core_search() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
92–183

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 340.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below styles_for_block_core_search()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 10 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost styles_for_block_core_search() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$value && !isset($attributes) && empty($value) && empty($input_styles) && empty($button_styles) && empty($wrapper_styles) && empty($label_styles)92–128apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), get_typography_styles_for_block_core_search(), esc_attr()
$value && !isset($attributes) && empty($value)102–138apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), get_typography_styles_for_block_core_search(), esc_attr(), safecss_filter_attr(), esc_attr()
$value && !isset($attributes) && !empty($value) && empty($input_styles) && empty($button_styles) && empty($wrapper_styles) && empty($label_styles)105–143apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), get_typography_styles_for_block_core_search(), esc_attr(), esc_attr()
$value && !isset($attributes) && empty($value)112–148apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), get_typography_styles_for_block_core_search(), esc_attr(), safecss_filter_attr(), esc_attr(), safecss_filter_attr(), esc_attr()
$value && !isset($attributes) && !empty($value)115–153apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), get_typography_styles_for_block_core_search(), esc_attr(), esc_attr(), safecss_filter_attr(), esc_attr()
$value && !isset($attributes) && empty($value)122–158apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), get_typography_styles_for_block_core_search(), esc_attr(), safecss_filter_attr(), esc_attr(), safecss_filter_attr(), esc_attr(), safecss_filter_attr(), esc_attr()
$value && !isset($attributes) && !empty($value)125–163apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), get_typography_styles_for_block_core_search(), esc_attr(), esc_attr(), safecss_filter_attr(), esc_attr(), safecss_filter_attr(), esc_attr()
$value && !isset($attributes) && empty($value) && !empty($input_styles) && !empty($button_styles) && !empty($wrapper_styles) && !empty($label_styles)132–168apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), get_typography_styles_for_block_core_search(), esc_attr(), safecss_filter_attr(), esc_attr(), safecss_filter_attr(), esc_attr(), safecss_filter_attr(), esc_attr(), safecss_filter_attr(), esc_attr()
$value && !isset($attributes) && !empty($value)135–173apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), get_typography_styles_for_block_core_search(), esc_attr(), esc_attr(), safecss_filter_attr(), esc_attr(), safecss_filter_attr(), esc_attr(), safecss_filter_attr(), esc_attr()
$value && !isset($attributes) && !empty($value) && !empty($input_styles) && !empty($button_styles) && !empty($wrapper_styles) && !empty($label_styles)145–183apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), apply_block_core_search_border_styles(), get_typography_styles_for_block_core_search(), esc_attr(), esc_attr(), safecss_filter_attr(), esc_attr(), safecss_filter_attr(), esc_attr(), safecss_filter_attr(), esc_attr(), safecss_filter_attr(), esc_attr()

This body has more branch combinations than are worth enumerating, so the table covers the outcomes found first rather than every one that exists.

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev34092–18333
8.534092–18333
8.434092–1833349 fewer instructions than PHP 8.3
8.338992–21133
8.238992–21133
8.138992–211331 fewer instruction than PHP 7.4
7.439092–21133

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 6

Used by · 1

Source code

function styles_for_block_core_search( $attributes ) {	$wrapper_styles   = array();	$button_styles    = array();	$input_styles     = array();	$label_styles     = array();	$is_button_inside = ! empty( $attributes['buttonPosition'] ) &&		'button-inside' === $attributes['buttonPosition'];	$show_label       = ( isset( $attributes['showLabel'] ) ) && false !== $attributes['showLabel']; 	// Add width styles.	$has_width = ! empty( $attributes['width'] ) && ! empty( $attributes['widthUnit'] ); 	if ( $has_width ) {		$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 ) {				// Get border-radius CSS variable from preset value if provided.				if ( is_string( $value ) && str_contains( $value, 'var:preset|border-radius|' ) ) {					$index_to_splice = strrpos( $value, '|' ) + 1;					$slug            = _wp_to_kebab_case( substr( $value, $index_to_splice ) );					$value           = "var(--wp--preset--border-radius--$slug)";				} 				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 || str_contains( $value, 'var(--wp--preset--border-radius--' ) ) ) {						$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;			// Get border-radius CSS variable from preset value if provided.			if ( is_string( $border_radius ) && str_contains( $border_radius, 'var:preset|border-radius|' ) ) {				$index_to_splice = strrpos( $border_radius, '|' ) + 1;				$slug            = _wp_to_kebab_case( substr( $border_radius, $index_to_splice ) );				$border_radius   = "var(--wp--preset--border-radius--$slug)";			} 			$border_style    = sprintf( 'border-radius: %s;', esc_attr( $border_radius ) );			$input_styles[]  = $border_style;			$button_styles[] = $border_style;

Changelog

Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/blocks/search.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.