wppaste
WordPress

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
Render the elements stylesheet and adds elements class name to block as required.

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

Touches nothing outside its own arguments.

Scaling
Scales with input

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

Instructions
13–119

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

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 it touches

  • hookthird-party callbacksdo_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.

WhenInstructionsCalls 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

PHPCompiledExecutedBranchesNotes
8.6-dev17513–11921
8.517513–11921
8.417513–11921
8.317513–11921
8.217513–11921
8.117513–119211 fewer instruction than PHP 7.4
7.417613–12021

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

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.

  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.

6.6.0
Element block support class and styles are generated via the render_block_data filter instead of pre_render_block.from the docblock
6.1.0
Implemented the style engine to generate CSS and classnames.from the docblock
6.0.0
Introduced.from the docblock

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