wppaste
WordPress

WP_Theme_JSON::get_blocks_metadata(): array

Since
5.8.0, 5.9.0, 6.1.0, 6.3.0, 6.6.0
Source
wp-includes/class-wp-theme-json.php:1148
Returns the metadata for each block.

Description

Example:

{
 'core/paragraph': {
 'selector': 'p',
 'elements': {
 'link' => 'link selector',
 'etc' => 'element selector'
 }
 },
 'core/heading': {
 'selector': 'h1',
 'elements': {}
 },
 'core/image': {
 'selector': '.wp-block-image',
 'duotone': 'img',
 'elements': {}
 }
}

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.

Return value

array
Block metadata.

Performance profile

How much work a call to WP_Theme_JSON::get_blocks_metadata() 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 how much data it finds.

Instructions
21–26

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

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 one call costs · 2 distinct outcomes

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

WhenInstructionsCalls it makes
!empty($blocks)21–22::WP_Block_Type_Registry(), ->get_all_registered(), ::WP_Block_Styles_Registry(), array_diff_key()
empty($blocks)25–26::WP_Block_Type_Registry(), ->get_all_registered(), ::WP_Block_Styles_Registry(), array_diff_key(), ->get_all_registered()

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev17121–2621
8.517121–2621
8.417121–2621
8.317121–2621
8.217121–2621
8.117121–26211 fewer instruction than PHP 7.4
7.417221–2621

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

Used by · 1

Source code

	protected static function get_blocks_metadata() {		$registry       = WP_Block_Type_Registry::get_instance();		$blocks         = $registry->get_all_registered();		$style_registry = WP_Block_Styles_Registry::get_instance(); 		// Is there metadata for all currently registered blocks?		$blocks = array_diff_key( $blocks, static::$blocks_metadata );		if ( empty( $blocks ) ) {			/*			 * New block styles may have been registered within WP_Block_Styles_Registry.			 * Update block metadata for any new block style variations.			 */			$registered_styles = $style_registry->get_all_registered();			foreach ( static::$blocks_metadata as $block_name => $block_metadata ) {				if ( ! empty( $registered_styles[ $block_name ] ) ) {					$style_selectors = $block_metadata['styleVariations'] ?? array(); 					foreach ( $registered_styles[ $block_name ] as $block_style ) {						if ( ! isset( $style_selectors[ $block_style['name'] ] ) ) {							$style_selectors[ $block_style['name'] ] = static::get_block_style_variation_selector( $block_style['name'], $block_metadata['selector'] );						}					} 					static::$blocks_metadata[ $block_name ]['styleVariations'] = $style_selectors;				}			}			return static::$blocks_metadata;		} 		foreach ( $blocks as $block_name => $block_type ) {			$root_selector = wp_get_block_css_selector( $block_type ); 			static::$blocks_metadata[ $block_name ]['selector']  = $root_selector;			static::$blocks_metadata[ $block_name ]['selectors'] = static::get_block_selectors( $block_type, $root_selector ); 			$elements = static::get_block_element_selectors( $root_selector );			if ( ! empty( $elements ) ) {				static::$blocks_metadata[ $block_name ]['elements'] = $elements;			} 			// The block may or may not have a duotone selector.			$duotone_selector = wp_get_block_css_selector( $block_type, 'filter.duotone' ); 			// Keep backwards compatibility for support.color.__experimentalDuotone.			if ( null === $duotone_selector ) {				$duotone_support = isset( $block_type->supports['color']['__experimentalDuotone'] )					? $block_type->supports['color']['__experimentalDuotone']					: null; 				if ( $duotone_support ) {					$root_selector    = wp_get_block_css_selector( $block_type );					$duotone_selector = static::scope_selector( $root_selector, $duotone_support );				}			} 			if ( null !== $duotone_selector ) {				static::$blocks_metadata[ $block_name ]['duotone'] = $duotone_selector;			} 			// If the block has style variations, append their selectors to the block metadata.			$style_selectors = array();			if ( ! empty( $block_type->styles ) ) {				foreach ( $block_type->styles as $style ) {					$style_selectors[ $style['name'] ] = static::get_block_style_variation_selector( $style['name'], static::$blocks_metadata[ $block_name ]['selector'] );				}			} 			// Block style variations can be registered through the WP_Block_Styles_Registry as well as block.json.			$registered_styles = $style_registry->get_registered_styles_for_block( $block_name );			foreach ( $registered_styles as $style ) {				$style_selectors[ $style['name'] ] = static::get_block_style_variation_selector( $style['name'], static::$blocks_metadata[ $block_name ]['selector'] );			} 			if ( ! empty( $style_selectors ) ) {				static::$blocks_metadata[ $block_name ]['styleVariations'] = $style_selectors;			}		} 		return static::$blocks_metadata;	}

Changelog

Introduced in 5.8.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
Updated to include block style variations from the block styles registry.from the docblock
6.3.0
Refactored and stabilized selectors API.from the docblock
6.1.0
Added features key with block support feature level selectors.from the docblock
5.9.0
Added duotone key with CSS selector.from the docblock
5.8.0
Introduced.from the docblock

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/class-wp-theme-json.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.