WP_Theme_JSON::get_block_nodes()
An internal method to get the block nodes from a theme.json file.
Method of the class: WP_Theme_JSON{}
No Hooks.
Return
Array
. The block nodes in theme.json.
Usage
$result = WP_Theme_JSON::get_block_nodes( $theme_json );
- $theme_json(array) (required)
- The theme.json converted to an array.
Changelog
Since 6.1.0 | Introduced. |
WP_Theme_JSON::get_block_nodes() WP Theme JSON::get block nodes code WP 6.1.1
private static function get_block_nodes( $theme_json ) { $selectors = static::get_blocks_metadata(); $nodes = array(); if ( ! isset( $theme_json['styles'] ) ) { return $nodes; } // Blocks. if ( ! isset( $theme_json['styles']['blocks'] ) ) { return $nodes; } foreach ( $theme_json['styles']['blocks'] as $name => $node ) { $selector = null; if ( isset( $selectors[ $name ]['selector'] ) ) { $selector = $selectors[ $name ]['selector']; } $duotone_selector = null; if ( isset( $selectors[ $name ]['duotone'] ) ) { $duotone_selector = $selectors[ $name ]['duotone']; } $feature_selectors = null; if ( isset( $selectors[ $name ]['features'] ) ) { $feature_selectors = $selectors[ $name ]['features']; } $nodes[] = array( 'name' => $name, 'path' => array( 'styles', 'blocks', $name ), 'selector' => $selector, 'duotone' => $duotone_selector, 'features' => $feature_selectors, ); if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'] ) ) { foreach ( $theme_json['styles']['blocks'][ $name ]['elements'] as $element => $node ) { $nodes[] = array( 'path' => array( 'styles', 'blocks', $name, 'elements', $element ), 'selector' => $selectors[ $name ]['elements'][ $element ], ); // Handle any pseudo selectors for the element. // TODO: Replace array_key_exists() with isset() check once WordPress drops // support for PHP 5.6. See https://core.trac.wordpress.org/ticket/57067. if ( array_key_exists( $element, static::VALID_ELEMENT_PSEUDO_SELECTORS ) ) { foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] as $pseudo_selector ) { if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'][ $element ][ $pseudo_selector ] ) ) { $nodes[] = array( 'path' => array( 'styles', 'blocks', $name, 'elements', $element ), 'selector' => static::append_to_selector( $selectors[ $name ]['elements'][ $element ], $pseudo_selector ), ); } } } } } } return $nodes; }