_wp_add_block_level_preset_styles( string|null $pre_render, array $block ): null
- Since
- 6.2.0, 6.3.0
- Source
wp-includes/block-supports/settings.php:77
Compatibility
- WordPress
- since 6.3.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
$pre_renderstring|null- The pre-rendered content. Default null.
$blockarray- The block being rendered.
Return value
null
Performance profile
How much work a call to _wp_add_block_level_preset_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
- 17–79
- 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 102.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 3 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost _wp_add_block_level_preset_styles() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 17–24 | ::WP_Block_Type_Registry(), ->get_registered(), block_has_support() |
block_has_support() && !empty($block_settings) && empty($styles) | 74–76 | ::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), _wp_get_presets_class_name(), ::WP_Block_Type_Registry(), ->get_all_registered(), ::WP_Theme_JSON(), version(), ::WP_Theme_JSON(), root_selector(), root_selector() |
block_has_support() && !empty($block_settings) && !empty($styles) | 77–79 | ::WP_Block_Type_Registry(), ->get_registered(), block_has_support(), _wp_get_presets_class_name(), ::WP_Block_Type_Registry(), ->get_all_registered(), ::WP_Theme_JSON(), version(), ::WP_Theme_JSON(), root_selector(), root_selector(), wp_enqueue_block_support_styles() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 102 instructions, 17–79 executed per call, 10 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 · 9
- block_has_support()Checks whether the current block type supports the feature requested.
- _wp_get_presets_class_name()Get the class name used on block level presets.
- wp_get_block_css_selector()Determines the CSS selector for the block type and property provided, returning it if available.
- wp_enqueue_block_support_styles()Hooks inline styles in the proper place, depending on the active theme.
- WP_Block_Type_Registry::get_instance()::get_registered()
- WP_Block_Type_Registry::get_instance()Utility method to retrieve the main instance of the class.
- WP_Theme_JSON::scope_selector()Function that scopes a selector with another one. This works a bit like SCSS nesting except the `&` operator isn't supported.
- WP_Theme_JSON::remove_insecure_properties()Removes insecure data from theme.json.
- WP_Theme_JSON::__construct()Constructor.
Source code
function _wp_add_block_level_preset_styles( $pre_render, $block ) { // Return early if the block has not support for descendent block styles. $block_type = WP_Block_Type_Registry::get_instance()->get_registered( $block['blockName'] ); if ( ! block_has_support( $block_type, '__experimentalSettings', false ) ) { return null; } // return early if no settings are found on the block attributes. $block_settings = $block['attrs']['settings'] ?? null; if ( empty( $block_settings ) ) { return null; } $class_name = '.' . _wp_get_presets_class_name( $block ); // the root selector for preset variables needs to target every possible block selector // in order for the general setting to override any bock specific setting of a parent block or // the site root. $variables_root_selector = '*,[class*="wp-block"]'; $registry = WP_Block_Type_Registry::get_instance(); $blocks = $registry->get_all_registered(); foreach ( $blocks as $block_type ) { /* * We only want to append selectors for blocks using custom selectors * i.e. not `wp-block-<name>`. */ $has_custom_selector = ( isset( $block_type->supports['__experimentalSelector'] ) && is_string( $block_type->supports['__experimentalSelector'] ) ) || ( isset( $block_type->selectors['root'] ) && is_string( $block_type->selectors['root'] ) ); if ( $has_custom_selector ) { $variables_root_selector .= ',' . wp_get_block_css_selector( $block_type ); } } $variables_root_selector = WP_Theme_JSON::scope_selector( $class_name, $variables_root_selector ); // Remove any potentially unsafe styles. $theme_json_shape = WP_Theme_JSON::remove_insecure_properties( array( 'version' => WP_Theme_JSON::LATEST_SCHEMA, 'settings' => $block_settings, ) ); $theme_json_object = new WP_Theme_JSON( $theme_json_shape ); $styles = ''; // include preset css variables declaration on the stylesheet. $styles .= $theme_json_object->get_stylesheet( array( 'variables' ), null, array( 'root_selector' => $variables_root_selector, 'scope' => $class_name, ) ); // include preset css classes on the stylesheet. $styles .= $theme_json_object->get_stylesheet( array( 'presets' ), null, array( 'root_selector' => $class_name . ',' . $class_name . ' *', 'scope' => $class_name, ) ); if ( ! empty( $styles ) ) { wp_enqueue_block_support_styles( $styles ); } return null;}Changelog
Introduced in 6.2.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/settings.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.