WP_Style_Engine::get_css_declarations( mixed $style_value, array $style_definition, array $options = array() ): string[]
- Since
- 6.1.0
- Source
wp-includes/style-engine/class-wp-style-engine.php:595
Compatibility
- WordPress
- since 6.1.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
$style_valuemixed- A single raw style value from $block_styles array.
$style_definitionarray- A single style definition from BLOCK_STYLE_DEFINITIONS_METADATA.
$optionsarrayoptional- An array of options. Default empty array.Default:
array()$convert_vars_to_classnamesbooldefault: falseWhether to skip converting incoming CSS var patterns, e.g.var:preset|<PRESET_TYPE>|<PRESET_SLUG>, tovar( --wp--preset--* )values.
Return value
string[]- An associative array of CSS definitions, e.g.
array( "$property" => "$value", "$property" => "$value" ).
Performance profile
How much work a call to WP_Style_Engine::get_css_declarations() 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
- Light
- Scaling
- Scales with input
- Instructions
- 16–39
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 91.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 5 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Style_Engine::get_css_declarations() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!isset($style_definition) | 16–24 | none |
isset($style_definition) && is_callable() | 17 | is_callable(), call_user_func() |
isset($style_definition) && !is_callable() | 21–29 | is_callable() |
!isset($style_definition) && is_string($style_value) && $style_value && !empty($style_definition) | 29–34 | ::get_css_var_value(), ::is_valid_style_value() |
isset($style_definition) && !is_callable() && is_string($style_value) && $style_value && !empty($style_definition) | 34–39 | is_callable(), ::get_css_var_value(), ::is_valid_style_value() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 91 | 16–39 | 18 | |
| 8.5 | 91 | 16–39 | 18 | |
| 8.4 | 91 | 16–39 | 18 | 6 fewer instructions than PHP 8.3 |
| 8.3 | 97 | 17–42 | 18 | |
| 8.2 | 97 | 17–42 | 18 | |
| 8.1 | 97 | 17–42 | 18 | |
| 7.4 | 97 | 17–42 | 18 |
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 · 4
- 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.
- static::get_css_var_value()
- static::is_valid_style_value()
Source code
protected static function get_css_declarations( $style_value, $style_definition, $options = array() ) { if ( isset( $style_definition['value_func'] ) && is_callable( $style_definition['value_func'] ) ) { return call_user_func( $style_definition['value_func'], $style_value, $style_definition, $options ); } $css_declarations = array(); $style_property_keys = $style_definition['property_keys']; $should_skip_css_vars = isset( $options['convert_vars_to_classnames'] ) && true === $options['convert_vars_to_classnames']; /* * Build CSS var values from `var:preset|<PRESET_TYPE>|<PRESET_SLUG>` values, e.g, `var(--wp--css--rule-slug )`. * Check if the value is a CSS preset and there's a corresponding css_var pattern in the style definition. */ if ( is_string( $style_value ) && str_contains( $style_value, 'var:' ) ) { if ( ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) { $css_var = static::get_css_var_value( $style_value, $style_definition['css_vars'] ); if ( static::is_valid_style_value( $css_var ) ) { $css_declarations[ $style_property_keys['default'] ] = $css_var; } } return $css_declarations; } /* * Default rule builder. * If the input contains an array, assume box model-like properties * for styles such as margins and padding. */ if ( is_array( $style_value ) ) { // Bail out early if the `'individual'` property is not defined. if ( ! isset( $style_property_keys['individual'] ) ) { return $css_declarations; } foreach ( $style_value as $key => $value ) { if ( is_string( $value ) && str_contains( $value, 'var:' ) && ! $should_skip_css_vars && ! empty( $style_definition['css_vars'] ) ) { $value = static::get_css_var_value( $value, $style_definition['css_vars'] ); } $individual_property = sprintf( $style_property_keys['individual'], _wp_to_kebab_case( $key ) ); if ( $individual_property && static::is_valid_style_value( $value ) ) { $css_declarations[ $individual_property ] = $value; } } return $css_declarations; } $css_declarations[ $style_property_keys['default'] ] = $style_value; return $css_declarations; }Changelog
Introduced in 6.1.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/style-engine/class-wp-style-engine.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.