WP_Theme_JSON::get_blocks_metadata
Returns the metadata for each block.
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': {}
}
}
Method of the class: WP_Theme_JSON{}
No Hooks.
Returns
Array. Block metadata.
Usage
$result = WP_Theme_JSON::get_blocks_metadata();
Changelog
| Since 5.8.0 | Introduced. |
| Since 5.9.0 | Added duotone key with CSS selector. |
| Since 6.1.0 | Added features key with block support feature level selectors. |
| Since 6.3.0 | Refactored and stabilized selectors API. |
| Since 6.6.0 | Updated to include block style variations from the block styles registry. |
WP_Theme_JSON::get_blocks_metadata() WP Theme JSON::get blocks metadata code WP 6.9.1
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;
}