wp_style_engine_get_styles( array $block_styles, array $options = array() ): array
- Since
- 6.1.0
- Source
wp-includes/style-engine.php:63
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|nullAn 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: 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 $css A CSS ruleset or declarations block formatted to be placed in an HTML
styleattribute or tag.$declarationsstring[]An associative array of CSS definitions, e.g.array( "$property" => "$value", "$property" => "$value" ).$classnamesstringClassnames 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
- Scaling
- Constant
- Instructions
- 21–54
- Plugin surface
- None
- Called by
- 17
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 54.
Nothing here hands control to plugin code.
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.
| When | Instructions | Calls it makes |
|---|---|---|
empty($parsed_styles) | 21 | wp_parse_args(), ::WP_Style_Engine(), array_filter() |
| always | 28 | wp_parse_args(), ::WP_Style_Engine(), array_unique(), array_filter() |
empty($options) | 36 | wp_parse_args(), ::WP_Style_Engine(), ::WP_Style_Engine(), array_filter() |
!empty($parsed_styles) && empty($options) | 43 | wp_parse_args(), ::WP_Style_Engine(), ::WP_Style_Engine(), array_unique(), array_filter() |
!empty($options) | 47 | wp_parse_args(), ::WP_Style_Engine(), ::WP_Style_Engine(), ::WP_Style_Engine(), array_filter() |
!empty($parsed_styles) && !empty($options) | 54 | wp_parse_args(), ::WP_Style_Engine(), ::WP_Style_Engine(), ::WP_Style_Engine(), array_unique(), array_filter() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 54 | 21–54 | 3 | |
| 8.5 | 54 | 21–54 | 3 | |
| 8.4 | 54 | 21–54 | 3 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 57 | 21–57 | 3 | |
| 8.2 | 57 | 21–57 | 3 | |
| 8.1 | 57 | 21–57 | 3 | |
| 7.4 | 57 | 21–57 | 3 |
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_parse_args()Merges user defined arguments into defaults array.
- WP_Style_Engine::parse_block_styles()Returns classnames and CSS based on the values in a styles object.
- WP_Style_Engine::compile_css()Returns compiled CSS from CSS declarations.
- WP_Style_Engine::store_css_rule()Stores a CSS rule using the provided CSS selector and CSS declarations.
Used by · 17
- WP_Theme_JSON::compute_style_properties()Given a styles array, it extracts the style properties and adds them to the $declarations array following the format:
- get_block_core_avatar_border_attributes()Generates class names and styles to apply the border support styles for the Avatar block.
- get_block_core_post_featured_image_border_attributes()Generates class names and styles to apply the border support styles for the Post Featured Image block.
- render_block_core_calendar()Renders the `core/calendar` block on server.
- render_block_core_icon()Renders the `core/icon` block on server.
- render_block_core_post_featured_image()Renders the `core/post-featured-image` block on the server.
- wp_add_block_state_style_rule()Adds a compiled state style rule to a rule list.
- wp_apply_border_support()Adds CSS classes and inline styles for border styles to the incoming attributes array. This will be applied to the block markup in the front-end.
- wp_apply_colors_support()Adds CSS classes and inline styles for colors to the incoming attributes array.
- wp_apply_dimensions_support()Adds CSS classes for block dimensions to the incoming attributes array.
- wp_apply_shadow_support()Add CSS classes and inline styles for shadow features to the incoming attributes array.
- wp_apply_spacing_support()Adds CSS classes for block spacing to the incoming attributes array.
Show all 17
- wp_apply_typography_support()Adds CSS classes and inline styles for typography features such as font sizes to the incoming attributes array. This will be applied to the block markup in the front-end.
- wp_get_layout_style()Generates the CSS corresponding to the provided layout.
- wp_render_background_support()Renders the background styles to the block wrapper.
- wp_render_dimensions_support()Renders server-side dimensions styles to the block wrapper.
- wp_render_elements_support_styles()Render the elements stylesheet and adds elements class name to block as required.
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.
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.