WP_Style_Engine::parse_block_styles( array $block_styles, array $options ): array
- Since
- 6.1.0
- Source
wp-includes/style-engine/class-wp-style-engine.php:495
Description
Return values are parsed based on the instructions in BLOCK_STYLE_DEFINITIONS_METADATA.
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.
$optionsarray- An array of options. Default empty 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.$selectorstringOptional. When a selector is passed, the value of$cssin 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[] $classnames Array of class names.
$declarationsstring[]An associative array of CSS definitions, e.g.array( "$property" => "$value", "$property" => "$value" ).
Performance profile
How much work a call to WP_Style_Engine::parse_block_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
- Light
- Scaling
- Scales with input
- Instructions
- 6–12
- Plugin surface
- None
- Called by
- 1
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 74.
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 one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Style_Engine::parse_block_styles() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 6–12 | none |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 74 instructions, 6–12 executed per call, 12 branches. The work does not change between versions.
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
- _wp_array_get()Accesses an array in depth based on a path of keys.
- static::is_valid_style_value()
- static::get_classnames()
- static::get_css_declarations()
Used by · 1
- wp_style_engine_get_styles()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.
Source code
public static function parse_block_styles( $block_styles, $options ) { $parsed_styles = array( 'classnames' => array(), 'declarations' => array(), ); if ( empty( $block_styles ) || ! is_array( $block_styles ) ) { return $parsed_styles; } // Collect CSS and classnames. foreach ( static::BLOCK_STYLE_DEFINITIONS_METADATA as $definition_group_key => $definition_group_style ) { if ( empty( $block_styles[ $definition_group_key ] ) ) { continue; } foreach ( $definition_group_style as $style_definition ) { $style_value = _wp_array_get( $block_styles, $style_definition['path'], null ); if ( ! static::is_valid_style_value( $style_value ) ) { continue; } $classnames = static::get_classnames( $style_value, $style_definition ); if ( ! empty( $classnames ) ) { $parsed_styles['classnames'] = array_merge( $parsed_styles['classnames'], $classnames ); } $css_declarations = static::get_css_declarations( $style_value, $style_definition, $options ); if ( ! empty( $css_declarations ) ) { /* * Combine background gradient and background image into a single * comma-separated background-image value, matching the JS style engine. */ if ( isset( $css_declarations['background-image'] ) && isset( $parsed_styles['declarations']['background-image'] ) ) { $css_declarations['background-image'] = $css_declarations['background-image'] . ', ' . $parsed_styles['declarations']['background-image']; } $parsed_styles['declarations'] = array_merge( $parsed_styles['declarations'], $css_declarations ); } } } return $parsed_styles; }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.