styles_for_block_core_search( array $attributes ): array
- Since
- 5.8.0
- Source
wp-includes/blocks/search.php:367
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
- Scaling
- Constant
- Instructions
- 92–183
- Plugin surface
- None
- Called by
- 1
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 340.
Nothing here hands control to plugin code.
1 place in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
$value && !isset($attributes) && empty($value) && empty($input_styles) && empty($button_styles) && empty($wrapper_styles) && empty($label_styles) | 92–128 | apply_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–138 | apply_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–143 | apply_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–148 | apply_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–153 | apply_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–158 | apply_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–163 | apply_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–168 | apply_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–173 | apply_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–183 | apply_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
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 340 | 92–183 | 33 | |
| 8.5 | 340 | 92–183 | 33 | |
| 8.4 | 340 | 92–183 | 33 | 49 fewer instructions than PHP 8.3 |
| 8.3 | 389 | 92–211 | 33 | |
| 8.2 | 389 | 92–211 | 33 | |
| 8.1 | 389 | 92–211 | 33 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 390 | 92–211 | 33 |
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
- esc_attr()Escaping for HTML attributes.
- apply_block_core_search_border_styles()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.
- str_contains()Polyfill for `str_contains()` function added in PHP 8.0.
- _wp_to_kebab_case()This function is trying to replicate what lodash's kebabCase (JS library) does in the client.
- get_typography_styles_for_block_core_search()Returns typography styles to be included in an HTML style tag.
- safecss_filter_attr()Filters an inline style attribute and removes disallowed rules.
Used by · 1
- render_block_core_search()Dynamically renders the `core/search` block.
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.
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.