wppaste
WordPress

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
Returns an array of CSS declarations based on valid block style values.

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: false

    Whether to skip converting incoming CSS var patterns, e.g. var:preset|<PRESET_TYPE>|<PRESET_SLUG>, to var( --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

Touches nothing outside its own arguments.

Scaling
Scales with input

The body loops, so the work grows with what you pass in.

Instructions
16–39

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
0

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.

WhenInstructionsCalls it makes
!isset($style_definition)16–24none
isset($style_definition) && is_callable()17is_callable(), call_user_func()
isset($style_definition) && !is_callable()21–29is_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–39is_callable(), ::get_css_var_value(), ::is_valid_style_value()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev9116–3918
8.59116–3918
8.49116–39186 fewer instructions than PHP 8.3
8.39717–4218
8.29717–4218
8.19717–4218
7.49717–4218

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.

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