wp_render_block_style_variation_support_styles( array $parsed_block ): array
- Since
- 6.6.0
- Source
wp-includes/block-supports/block-style-variations.php:81
Description
In the case of nested blocks with variations applied, we want the parent variation's styles to be rendered before their descendants. This solves the issue of a block type being styled in both the parent and descendant: we want the descendant style to take priority, and this is done by loading it after, in the DOM order. This is why the variation stylesheet generation is in a different filter.
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 parsed block with block style variation classname added.
Performance profile
How much work a call to wp_render_block_style_variation_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
- 11–144
- 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 147.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 10 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_render_block_style_variation_support_styles() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 11–12 | wp_get_block_style_variation_name_from_class() |
empty($variation_data) | 22–35 | wp_get_block_style_variation_name_from_class(), ::WP_Theme_JSON_Resolver(), ->get_raw_data() |
!empty($variation_data) && is_admin() && empty($variation_styles) | 102–122 | wp_get_block_style_variation_name_from_class(), ::WP_Theme_JSON_Resolver(), ->get_raw_data(), wp_resolve_block_style_variation_ref_values(), wp_unique_id(), is_admin(), ::WP_Block_Styles_Registry(), name(), ->register(), include_block_style_variations(), ->unregister(), is_admin() |
!empty($variation_data) && empty($variation_styles) | 106–126 | wp_get_block_style_variation_name_from_class(), ::WP_Theme_JSON_Resolver(), ->get_raw_data(), wp_resolve_block_style_variation_ref_values(), wp_unique_id(), is_admin(), ::WP_Block_Styles_Registry(), name(), ->register(), include_block_style_variations(), ->unregister(), is_admin(), add_filter() |
!empty($variation_data) && empty($variation_styles) | 106–126 | wp_get_block_style_variation_name_from_class(), ::WP_Theme_JSON_Resolver(), ->get_raw_data(), wp_resolve_block_style_variation_ref_values(), wp_unique_id(), is_admin(), remove_filter(), ::WP_Block_Styles_Registry(), name(), ->register(), include_block_style_variations(), ->unregister(), is_admin() |
!empty($variation_data) && !is_admin() && empty($variation_styles) | 110–130 | wp_get_block_style_variation_name_from_class(), ::WP_Theme_JSON_Resolver(), ->get_raw_data(), wp_resolve_block_style_variation_ref_values(), wp_unique_id(), is_admin(), remove_filter(), ::WP_Block_Styles_Registry(), name(), ->register(), include_block_style_variations(), ->unregister(), is_admin(), add_filter() |
!empty($variation_data) && is_admin() && !empty($variation_styles) | 116–136 | wp_get_block_style_variation_name_from_class(), ::WP_Theme_JSON_Resolver(), ->get_raw_data(), wp_resolve_block_style_variation_ref_values(), wp_unique_id(), is_admin(), ::WP_Block_Styles_Registry(), name(), ->register(), include_block_style_variations(), ->unregister(), is_admin(), wp_register_style(), wp_add_inline_style(), _wp_array_set() |
!empty($variation_data) && !empty($variation_styles) | 120–140 | wp_get_block_style_variation_name_from_class(), ::WP_Theme_JSON_Resolver(), ->get_raw_data(), wp_resolve_block_style_variation_ref_values(), wp_unique_id(), is_admin(), ::WP_Block_Styles_Registry(), name(), ->register(), include_block_style_variations(), ->unregister(), is_admin(), add_filter(), wp_register_style(), wp_add_inline_style(), _wp_array_set() |
!empty($variation_data) && !empty($variation_styles) | 120–140 | wp_get_block_style_variation_name_from_class(), ::WP_Theme_JSON_Resolver(), ->get_raw_data(), wp_resolve_block_style_variation_ref_values(), wp_unique_id(), is_admin(), remove_filter(), ::WP_Block_Styles_Registry(), name(), ->register(), include_block_style_variations(), ->unregister(), is_admin(), wp_register_style(), wp_add_inline_style(), _wp_array_set() |
!empty($variation_data) && !is_admin() && !empty($variation_styles) | 124–144 | wp_get_block_style_variation_name_from_class(), ::WP_Theme_JSON_Resolver(), ->get_raw_data(), wp_resolve_block_style_variation_ref_values(), wp_unique_id(), is_admin(), remove_filter(), ::WP_Block_Styles_Registry(), name(), ->register(), include_block_style_variations(), ->unregister(), is_admin(), add_filter(), wp_register_style(), wp_add_inline_style(), _wp_array_set() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 147 instructions, 11–144 executed per call, 13 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 · 12
- wp_get_block_style_variation_name_from_class()Determines the block style variation names within a CSS class string.
- wp_resolve_block_style_variation_ref_values()Recursively resolves any `ref` values within a block style variation's data.
- wp_unique_id()Gets unique ID.
- _wp_array_set()Sets an array in depth based on a path of keys.
- is_admin()Determines whether the current request is for an administrative interface page.
- remove_filter()Removes a callback function from a filter hook.
- add_filter()Adds a callback function to a filter hook.
- wp_register_style()Registers a CSS stylesheet.
- wp_add_inline_style()Adds extra CSS styles to a registered stylesheet.
- WP_Theme_JSON_Resolver::get_merged_data()Returns the data merged from multiple origins.
- WP_Block_Styles_Registry::get_instance()Utility method to retrieve the main instance of the class.
- WP_Theme_JSON::__construct()Constructor.
Source code
function wp_render_block_style_variation_support_styles( $parsed_block ) { $classes = $parsed_block['attrs']['className'] ?? null; $variations = wp_get_block_style_variation_name_from_class( $classes ); if ( ! $variations ) { return $parsed_block; } $tree = WP_Theme_JSON_Resolver::get_merged_data(); $theme_json = $tree->get_raw_data(); // Only the first block style variation with data is supported. $variation_data = array(); foreach ( $variations as $variation ) { $variation_data = $theme_json['styles']['blocks'][ $parsed_block['blockName'] ]['variations'][ $variation ] ?? array(); if ( ! empty( $variation_data ) ) { break; } } if ( empty( $variation_data ) ) { return $parsed_block; } /* * Recursively resolve any ref values with the appropriate value within the * theme_json data. */ wp_resolve_block_style_variation_ref_values( $variation_data, $theme_json ); $variation_instance = wp_unique_id( $variation . '--' ); $class_name = "is-style-$variation_instance"; $updated_class_name = $parsed_block['attrs']['className'] . " $class_name"; /* * Even though block style variations are effectively theme.json partials, * they can't be processed completely as though they are. * * Block styles support custom selectors to direct specific types of styles * to inner elements. For example, borders on Image block's get applied to * the inner `img` element rather than the wrapping `figure`. * * The following relocates the "root" block style variation styles to * under an appropriate blocks property to leverage the preexisting style * generation for simple block style variations. This way they get the * custom selectors they need. * * The inner elements and block styles for the variation itself are * still included at the top level but scoped by the variation's selector * when the stylesheet is generated. */ $elements_data = $variation_data['elements'] ?? array(); $blocks_data = $variation_data['blocks'] ?? array(); unset( $variation_data['elements'] ); unset( $variation_data['blocks'] ); _wp_array_set( $blocks_data, array( $parsed_block['blockName'], 'variations', $variation_instance ), $variation_data ); $config = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA, 'settings' => array( 'spacing' => array( 'blockGap' => true, ), ), 'styles' => array( 'elements' => $elements_data, 'blocks' => $blocks_data, ), ); // Ensure variation state styles know about any custom viewport breakpoints. if ( isset( $theme_json['settings']['viewport'] ) ) { $config['settings']['viewport'] = $theme_json['settings']['viewport']; }Changelog
Introduced in 6.6.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/block-supports/block-style-variations.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.