WP_Theme_JSON::get_blocks_metadata()protected staticWP 5.8.0

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': {}
  }
}

Метод класса: WP_Theme_JSON{}

No Hooks.

Return

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.

Код WP_Theme_JSON::get_blocks_metadata() WP 6.1.1

protected static function get_blocks_metadata() {
	$registry = WP_Block_Type_Registry::get_instance();
	$blocks   = $registry->get_all_registered();

	// Is there metadata for all currently registered blocks?
	$blocks = array_diff_key( $blocks, static::$blocks_metadata );
	if ( empty( $blocks ) ) {
		return static::$blocks_metadata;
	}

	foreach ( $blocks as $block_name => $block_type ) {
		if (
			isset( $block_type->supports['__experimentalSelector'] ) &&
			is_string( $block_type->supports['__experimentalSelector'] )
		) {
			static::$blocks_metadata[ $block_name ]['selector'] = $block_type->supports['__experimentalSelector'];
		} else {
			static::$blocks_metadata[ $block_name ]['selector'] = '.wp-block-' . str_replace( '/', '-', str_replace( 'core/', '', $block_name ) );
		}

		if (
			isset( $block_type->supports['color']['__experimentalDuotone'] ) &&
			is_string( $block_type->supports['color']['__experimentalDuotone'] )
		) {
			static::$blocks_metadata[ $block_name ]['duotone'] = $block_type->supports['color']['__experimentalDuotone'];
		}

		// Generate block support feature level selectors if opted into
		// for the current block.
		$features = array();
		foreach ( static::BLOCK_SUPPORT_FEATURE_LEVEL_SELECTORS as $key => $feature ) {
			if (
				isset( $block_type->supports[ $key ]['__experimentalSelector'] ) &&
				$block_type->supports[ $key ]['__experimentalSelector']
			) {
				$features[ $feature ] = static::scope_selector(
					static::$blocks_metadata[ $block_name ]['selector'],
					$block_type->supports[ $key ]['__experimentalSelector']
				);
			}
		}

		if ( ! empty( $features ) ) {
			static::$blocks_metadata[ $block_name ]['features'] = $features;
		}

		// Assign defaults, then overwrite those that the block sets by itself.
		// If the block selector is compounded, will append the element to each
		// individual block selector.
		$block_selectors = explode( ',', static::$blocks_metadata[ $block_name ]['selector'] );
		foreach ( static::ELEMENTS as $el_name => $el_selector ) {
			$element_selector = array();
			foreach ( $block_selectors as $selector ) {
				if ( $selector === $el_selector ) {
					$element_selector = array( $el_selector );
					break;
				}
				$element_selector[] = static::append_to_selector( $el_selector, $selector . ' ', 'left' );
			}
			static::$blocks_metadata[ $block_name ]['elements'][ $el_name ] = implode( ',', $element_selector );
		}
	}

	return static::$blocks_metadata;
}