wppaste
WordPress

wp_add_global_styles_for_blocks()

Since
6.1.0, 6.7.0
Source
wp-includes/global-styles-and-settings.php:248
Adds global style rules to the inline style for each block.

Compatibility

WordPress
since 6.7.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.

Performance profile

How much work a call to wp_add_global_styles_for_blocks() 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
Heavy

Reads stored settings via get_option(), cached per request but not free on a cold cache.

Scaling
Scales with input

The body loops, so the work grows with how much data it finds.

Instructions
21–49

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

Plugin surface
None

Nothing here hands control to plugin code.

Called by
1

1 place in core call this, so the cost is paid more often than your own code shows.

What it touches

  • transienttransientget_transient()called directly
  • hookthird-party callbacksapply_filters()one call below wp_add_global_styles_for_blocks()
  • cacheobject cachewp_cache_get()one call below wp_add_global_styles_for_blocks()
  • optionoption read or writeget_option()one call below wp_add_global_styles_for_blocks()

Further down the call graph this can also reach serialize and query. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_add_global_styles_for_blocks() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
$value21–22::WP_Theme_JSON_Resolver(), ::WP_Theme_JSON_Resolver(), ->get_styles_block_nodes(), wp_is_development_mode()
$value25–26::WP_Theme_JSON_Resolver(), ::WP_Theme_JSON_Resolver(), ->get_styles_block_nodes(), wp_is_development_mode(), set_transient()
!$value40–45::WP_Theme_JSON_Resolver(), ::WP_Theme_JSON_Resolver(), ->get_styles_block_nodes(), wp_is_development_mode(), ->get_raw_data(), wp_json_encode(), md5(), get_transient()
!$value44–49::WP_Theme_JSON_Resolver(), ::WP_Theme_JSON_Resolver(), ->get_styles_block_nodes(), wp_is_development_mode(), ->get_raw_data(), wp_json_encode(), md5(), get_transient(), set_transient()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev14221–4917
8.514221–4917
8.414221–491718 fewer instructions than PHP 8.3
8.316021–4917
8.216021–4917
8.116021–49171 fewer instruction than PHP 7.4
7.416121–4917

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 · 10

Used by · 1

Source code

function wp_add_global_styles_for_blocks() {	global $wp_styles; 	$tree        = WP_Theme_JSON_Resolver::get_merged_data();	$tree        = WP_Theme_JSON_Resolver::resolve_theme_file_uris( $tree );	$block_nodes = $tree->get_styles_block_nodes(); 	$can_use_cached = ! wp_is_development_mode( 'theme' );	$update_cache   = false; 	if ( $can_use_cached ) {		// Hash the merged WP_Theme_JSON data to bust cache on settings or styles change.		$cache_hash = md5( wp_json_encode( $tree->get_raw_data() ) );		$cache_key  = 'wp_styles_for_blocks';		$cached     = get_transient( $cache_key ); 		// Reset the cached data if there is no value or if the hash has changed.		if ( ! is_array( $cached ) || $cached['hash'] !== $cache_hash ) {			$cached = array(				'hash'   => $cache_hash,				'blocks' => array(),			); 			// Update the cache if the hash has changed.			$update_cache = true;		}	} 	foreach ( $block_nodes as $metadata ) { 		if ( $can_use_cached ) {			// Generate a unique cache key based on the full metadata to ensure pseudo-selectors and other variations get unique keys.			$cache_node_key = md5( wp_json_encode( $metadata ) ); 			if ( isset( $cached['blocks'][ $cache_node_key ] ) ) {				$block_css = $cached['blocks'][ $cache_node_key ];			} else {				$block_css                           = $tree->get_styles_for_block( $metadata );				$cached['blocks'][ $cache_node_key ] = $block_css; 				// Update the cache if the cache contents have changed.				$update_cache = true;			}		} else {			$block_css = $tree->get_styles_for_block( $metadata );		} 		if ( ! wp_should_load_block_assets_on_demand() ) {			wp_add_inline_style( 'global-styles', $block_css );			continue;		} 		$stylesheet_handle = 'global-styles'; 		/*		 * When `wp_should_load_block_assets_on_demand()` is true, block styles are		 * enqueued for each block on the page in class WP_Block's render function.		 * This means there will be a handle in the styles queue for each of those blocks.		 * Block-specific global styles should be attached to the global-styles handle, but		 * only for blocks on the page, thus we check if the block's handle is in the queue		 * before adding the inline style.		 * This conditional loading only applies to core blocks.		 * TODO: Explore how this could be expanded to third-party blocks as well.		 */		if ( isset( $metadata['name'] ) ) {			if ( str_starts_with( $metadata['name'], 'core/' ) ) {				$block_name   = str_replace( 'core/', '', $metadata['name'] );				$block_handle = 'wp-block-' . $block_name;				if ( in_array( $block_handle, $wp_styles->queue, true ) ) {					wp_add_inline_style( $stylesheet_handle, $block_css );				}			} else {				wp_add_inline_style( $stylesheet_handle, $block_css );			}		} 		// The likes of block element styles from theme.json do not have  $metadata['name'] set.		if ( ! isset( $metadata['name'] ) && ! empty( $metadata['path'] ) ) {			$block_name = wp_get_block_name_from_theme_json_path( $metadata['path'] );			if ( $block_name ) {

Changelog

Introduced in 6.1.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.7.0
Resolve relative paths in block styles.from the docblock
6.1.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/global-styles-and-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.