wp_render_elements_support_styles( array $parsed_block ): array
- Since
- 6.0.0, 6.1.0, 6.6.0
- Source
wp-includes/block-supports/elements.php:135
Description
In the case of nested blocks we want the parent element styles to be rendered before their descendants.
This solves the issue of an element (e.g.: link color) being styled in both the parent and a descendant: we want the descendant style to take priority, and this is done by loading it after, in DOM order.
Compatibility
- WordPress
- since 6.6.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
$parsed_blockarray- The parsed block.
Return value
array- The same parsed block with elements classname added if appropriate.
Performance profile
How much work a call to wp_render_elements_support_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
- 13–119
- 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 175.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- hookthird-party callbacks
do_action()one call below wp_render_elements_support_styles()
Further down the call graph this can also reach query, option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 8 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_render_elements_support_styles() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
!is_string($parsed_block) | 13–20 | ::WP_Block_Type_Registry(), ->get_registered() |
is_string($parsed_block) | 21–28 | __(), _deprecated_argument(), ::WP_Block_Type_Registry(), ->get_registered() |
!is_string($parsed_block) | 40–43 | ::WP_Block_Type_Registry(), ->get_registered(), wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization() |
is_string($parsed_block) | 48–51 | __(), _deprecated_argument(), ::WP_Block_Type_Registry(), ->get_registered(), wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization() |
!is_string($parsed_block) && !wp_should_add_elements_class_name() | 52–55 | ::WP_Block_Type_Registry(), ->get_registered(), wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization(), wp_should_add_elements_class_name() |
is_string($parsed_block) && !wp_should_add_elements_class_name() | 60–63 | __(), _deprecated_argument(), ::WP_Block_Type_Registry(), ->get_registered(), wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization(), wp_should_add_elements_class_name() |
!is_string($parsed_block) && wp_should_add_elements_class_name() | 103–111 | ::WP_Block_Type_Registry(), ->get_registered(), wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization(), wp_should_add_elements_class_name(), wp_get_elements_class_name(), _wp_array_set() |
is_string($parsed_block) && wp_should_add_elements_class_name() | 111–119 | __(), _deprecated_argument(), ::WP_Block_Type_Registry(), ->get_registered(), wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization(), wp_should_skip_block_supports_serialization(), wp_should_add_elements_class_name(), wp_get_elements_class_name(), _wp_array_set() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 175 | 13–119 | 21 | |
| 8.5 | 175 | 13–119 | 21 | |
| 8.4 | 175 | 13–119 | 21 | |
| 8.3 | 175 | 13–119 | 21 | |
| 8.2 | 175 | 13–119 | 21 | |
| 8.1 | 175 | 13–119 | 21 | 1 fewer instruction than PHP 7.4 |
| 7.4 | 176 | 13–120 | 21 |
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 · 9
- _deprecated_argument()Marks a function argument as deprecated and inform when it has been used.
- __()Retrieves the translation of $text.
- wp_should_skip_block_supports_serialization()Checks whether serialization of the current block's supported properties should occur.
- wp_should_add_elements_class_name()Determines whether an elements class name should be added to the block.
- wp_get_elements_class_name()Gets the elements class names.
- _wp_array_set()Sets an array in depth based on a path of keys.
- 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.
- WP_Block_Type_Registry::get_instance()::get_registered()
- WP_Block_Type_Registry::get_instance()Utility method to retrieve the main instance of the class.
Source code
function wp_render_elements_support_styles( $parsed_block ) { /* * The generation of element styles and classname were moved to the * `render_block_data` filter in 6.6.0 to avoid filtered attributes * breaking the application of the elements CSS class. * * @link https://github.com/WordPress/gutenberg/pull/59535 * * The change in filter means, the argument types for this function * have changed and require deprecating. */ if ( is_string( $parsed_block ) ) { _deprecated_argument( __FUNCTION__, '6.6.0', __( 'Use as a `pre_render_block` filter is deprecated. Use with `render_block_data` instead.' ) ); } $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $parsed_block['blockName'] ); if ( ! $block_type ) { return $parsed_block; } $element_block_styles = $parsed_block['attrs']['style']['elements'] ?? null; if ( ! $element_block_styles ) { return $parsed_block; } $skip_link_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'link' ); $skip_heading_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'heading' ); $skip_button_color_serialization = wp_should_skip_block_supports_serialization( $block_type, 'color', 'button' ); $skips_all_element_color_serialization = $skip_link_color_serialization && $skip_heading_color_serialization && $skip_button_color_serialization; if ( $skips_all_element_color_serialization ) { return $parsed_block; } $options = array( 'button' => array( 'skip' => $skip_button_color_serialization ), 'link' => array( 'skip' => $skip_link_color_serialization ), 'heading' => array( 'skip' => $skip_heading_color_serialization ), ); if ( ! wp_should_add_elements_class_name( $parsed_block, $options ) ) { return $parsed_block; } $class_name = wp_get_elements_class_name(); $updated_class_name = isset( $parsed_block['attrs']['className'] ) ? $parsed_block['attrs']['className'] . " $class_name" : $class_name; _wp_array_set( $parsed_block, array( 'attrs', 'className' ), $updated_class_name ); // Generate element styles based on selector and store in style engine for enqueuing. $element_types = array( 'button' => array( 'selector' => ".$class_name .wp-element-button, .$class_name .wp-block-button__link", 'skip' => $skip_button_color_serialization, ), 'link' => array( 'selector' => ".$class_name a:where(:not(.wp-element-button))", 'hover_selector' => ".$class_name a:where(:not(.wp-element-button)):hover", 'skip' => $skip_link_color_serialization, ), 'heading' => array( 'selector' => ".$class_name h1, .$class_name h2, .$class_name h3, .$class_name h4, .$class_name h5, .$class_name h6", 'skip' => $skip_heading_color_serialization, 'elements' => array( 'h1', 'h2', 'h3', 'h4', 'h5', 'h6' ), ), ); foreach ( $element_types as $element_type => $element_config ) { if ( $element_config['skip'] ) { continue; } $element_style_object = $element_block_styles[ $element_type ] ?? null;Changelog
Introduced in 6.0.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
render_block_data filter instead of pre_render_block.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/block-supports/elements.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.