WP_Theme_JSON::get_block_nodes( array $theme_json, array $selectors = array(), array $options = array() ): array

In this article

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only by core. It is listed here for completeness.

An internal method to get the block nodes from a theme.json file.

Parameters

$theme_jsonarrayrequired
The theme.json converted to an array.
$selectorsarrayoptional
Optional list of selectors per block.

Default:array()

$optionsarrayoptional
An array of options for now used for internal purposes only (may change without notice).
  • include_block_style_variations bool
    Include nodes for block style variations. Default false.
  • include_node_paths_only bool
    Return only block nodes node paths. Default false.

Default:array()

Return

array The block nodes in theme.json.

Source

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 ) {
			$variation_paths = array();
			if ( $include_variations && isset( $node['variations'] ) ) {
				foreach ( $node['variations'] as $variation => $variation_node ) {
					$variation_paths[] = array(
						'path' => array( 'styles', 'blocks', $name, 'variations', $variation ),
					);
				}
			}
			$node = array(
				'path' => $node_path,
			);
			if ( ! empty( $variation_paths ) ) {
				$node['variations'] = $variation_paths;
			}
			$nodes[] = $node;
		} 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 );

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

	return $nodes;
}

Changelog

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

User Contributed Notes

You must log in before being able to contribute a note or feedback.