WP_Theme_JSON::get_block_nodes()private staticWP 6.1.0

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, $selectors, $options );
$theme_json(array) (required)
The theme.json converted to an array.
$selectors(array)
Optional list of selectors per block.
Default: array()
$options(array)

An array of options for now used for internal purposes only (may change without notice).

Default: array()

  • include_block_style_variations(true|false)
    Include nodes for block style variations.
    Default: false

  • include_node_paths_only(true|false)
    Return only block nodes node paths.
    Default: false

Changelog

Since 6.1.0 Introduced.
Since 6.3.0 Refactored and stabilized selectors API.
Since 6.6.0 Added optional selectors and options for generating block nodes.
Since 6.7.0 Added $include_node_paths_only option.

WP_Theme_JSON::get_block_nodes() code WP 6.7.1

private static function get_block_nodes( $theme_json, $selectors = array(), $options = array() ) {
	$nodes = array();

	if ( ! isset( $theme_json['styles']['blocks'] ) ) {
		return $nodes;
	}

	$include_variations      = $options['include_block_style_variations'] ?? false;
	$include_node_paths_only = $options['include_node_paths_only'] ?? false;

	// If only node paths are to be returned, skip selector assignment.
	if ( ! $include_node_paths_only ) {
		$selectors = empty( $selectors ) ? static::get_blocks_metadata() : $selectors;
	}

	foreach ( $theme_json['styles']['blocks'] as $name => $node ) {
		$node_path = array( 'styles', 'blocks', $name );
		if ( $include_node_paths_only ) {
			$nodes[] = array(
				'path' => $node_path,
			);
		} else {
			$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 ]['selectors'] ) ) {
				$feature_selectors = $selectors[ $name ]['selectors'];
			}

			$variation_selectors = array();
			if ( $include_variations && isset( $node['variations'] ) ) {
				foreach ( $node['variations'] as $variation => $node ) {
					$variation_selectors[] = array(
						'path'     => array( 'styles', 'blocks', $name, 'variations', $variation ),
						'selector' => $selectors[ $name ]['styleVariations'][ $variation ],
					);
				}
			}

			$nodes[] = array(
				'name'       => $name,
				'path'       => $node_path,
				'selector'   => $selector,
				'selectors'  => $feature_selectors,
				'duotone'    => $duotone_selector,
				'features'   => $feature_selectors,
				'variations' => $variation_selectors,
				'css'        => $selector,
			);
		}

		if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'] ) ) {
			foreach ( $theme_json['styles']['blocks'][ $name ]['elements'] as $element => $node ) {
				$node_path = array( 'styles', 'blocks', $name, 'elements', $element );
				if ( $include_node_paths_only ) {
					$nodes[] = array(
						'path' => $node_path,
					);
					continue;
				}

				$nodes[] = array(
					'path'     => $node_path,
					'selector' => $selectors[ $name ]['elements'][ $element ],
				);

				// Handle any pseudo selectors for the element.
				if ( isset( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] ) ) {
					foreach ( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] as $pseudo_selector ) {
						if ( isset( $theme_json['styles']['blocks'][ $name ]['elements'][ $element ][ $pseudo_selector ] ) ) {
							$node_path = array( 'styles', 'blocks', $name, 'elements', $element );
							if ( $include_node_paths_only ) {
								$nodes[] = array(
									'path' => $node_path,
								);
								continue;
							}

							$nodes[] = array(
								'path'     => $node_path,
								'selector' => static::append_to_selector( $selectors[ $name ]['elements'][ $element ], $pseudo_selector ),
							);
						}
					}
				}
			}
		}
	}

	return $nodes;
}