wppaste
WordPress

wp_style_engine_get_styles( array $block_styles, array $options = array() ): array

Since
6.1.0
Source
wp-includes/style-engine.php:63
Global public interface method to generate styles from a single style object, e.g. the value of a block's attributes.style object or the top level styles in theme.json.

Description

Example usage:

$styles = wp_style_engine_get_styles(
 array(
 'color' => array( 'text' => '#cccccc' ),
 )
);

Returns:

array(
 'css' => 'color: #cccccc',
 'declarations' => array( 'color' => '#cccccc' ),
 'classnames' => 'has-color',
)

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

$block_stylesarray
The style object.
$optionsarrayoptional
An array of options. Default empty array.Default: array()
  • $contextstring|null

    An identifier describing the origin of the style object, e.g. 'block-supports' or 'global-styles'. Default null. When set, the style engine will attempt to store the CSS rules, where a selector is also passed.
  • $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.
  • $selectorstring

    Optional. When a selector is passed, the value of $css in the return value will comprise a full CSS rule $selector { ...$css_declarations }, otherwise, the value will be a concatenated string of CSS declarations.

Return value

array
@type string $css A CSS ruleset or declarations block formatted to be placed in an HTML style attribute or tag.
  • $declarationsstring[]

    An associative array of CSS definitions, e.g. array( "$property" => "$value", "$property" => "$value" ).
  • $classnamesstring

    Classnames separated by a space.

Performance profile

How much work a call to wp_style_engine_get_styles() 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
21–54

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
17

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

What one call costs · 6 distinct outcomes

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

WhenInstructionsCalls it makes
empty($parsed_styles)21wp_parse_args(), ::WP_Style_Engine(), array_filter()
always28wp_parse_args(), ::WP_Style_Engine(), array_unique(), array_filter()
empty($options)36wp_parse_args(), ::WP_Style_Engine(), ::WP_Style_Engine(), array_filter()
!empty($parsed_styles) && empty($options)43wp_parse_args(), ::WP_Style_Engine(), ::WP_Style_Engine(), array_unique(), array_filter()
!empty($options)47wp_parse_args(), ::WP_Style_Engine(), ::WP_Style_Engine(), ::WP_Style_Engine(), array_filter()
!empty($parsed_styles) && !empty($options)54wp_parse_args(), ::WP_Style_Engine(), ::WP_Style_Engine(), ::WP_Style_Engine(), array_unique(), array_filter()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev5421–543
8.55421–543
8.45421–5433 fewer instructions than PHP 8.3
8.35721–573
8.25721–573
8.15721–573
7.45721–573

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

Used by · 17

Show all 17

Source code

function wp_style_engine_get_styles( $block_styles, $options = array() ) {	$options = wp_parse_args(		$options,		array(			'selector'                   => null,			'context'                    => null,			'convert_vars_to_classnames' => false,		)	); 	$parsed_styles = WP_Style_Engine::parse_block_styles( $block_styles, $options ); 	// Output.	$styles_output = array(); 	if ( ! empty( $parsed_styles['declarations'] ) ) {		$styles_output['css']          = WP_Style_Engine::compile_css( $parsed_styles['declarations'], $options['selector'] );		$styles_output['declarations'] = $parsed_styles['declarations'];		if ( ! empty( $options['context'] ) ) {			WP_Style_Engine::store_css_rule( $options['context'], $options['selector'], $parsed_styles['declarations'] );		}	} 	if ( ! empty( $parsed_styles['classnames'] ) ) {		$styles_output['classnames'] = implode( ' ', array_unique( $parsed_styles['classnames'] ) );	} 	return array_filter( $styles_output );}

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