WP_Theme_JSON::get_style_nodes()protected staticWP 5.8.0

Builds metadata for the style nodes, which returns in the form of:

[ [ 'path' => [ 'path', 'to', 'some', 'node' ], 'selector' => 'CSS selector for some node', 'duotone' => 'CSS selector for duotone for some node' ], [ 'path' => ['path', 'to', 'other', 'node' ], 'selector' => 'CSS selector for other node', 'duotone' => null ],

]

Method of the class: WP_Theme_JSON{}

Hooks from the method

Return

Array. An array of style nodes metadata.

Usage

$result = WP_Theme_JSON::get_style_nodes( $theme_json, $selectors );
$theme_json(array) (required)
The tree to extract style nodes from.
$selectors(array)
List of selectors per block.
Default: array()

Changelog

Since 5.8.0 Introduced.

WP_Theme_JSON::get_style_nodes() code WP 6.1.1

protected static function get_style_nodes( $theme_json, $selectors = array() ) {
	$nodes = array();
	if ( ! isset( $theme_json['styles'] ) ) {
		return $nodes;
	}

	// Top-level.
	$nodes[] = array(
		'path'     => array( 'styles' ),
		'selector' => static::ROOT_BLOCK_SELECTOR,
	);

	if ( isset( $theme_json['styles']['elements'] ) ) {
		foreach ( self::ELEMENTS as $element => $selector ) {
			if ( ! isset( $theme_json['styles']['elements'][ $element ] ) ) {
				continue;
			}
			$nodes[] = array(
				'path'     => array( 'styles', 'elements', $element ),
				'selector' => static::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']['elements'][ $element ][ $pseudo_selector ] ) ) {
						$nodes[] = array(
							'path'     => array( 'styles', 'elements', $element ),
							'selector' => static::append_to_selector( static::ELEMENTS[ $element ], $pseudo_selector ),
						);
					}
				}
			}
		}
	}

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

	$block_nodes = static::get_block_nodes( $theme_json );
	foreach ( $block_nodes as $block_node ) {
		$nodes[] = $block_node;
	}

	/**
	 * Filters the list of style nodes with metadata.
	 *
	 * This allows for things like loading block CSS independently.
	 *
	 * @since 6.1.0
	 *
	 * @param array $nodes Style nodes with metadata.
	 */
	return apply_filters( 'wp_theme_json_get_style_nodes', $nodes );
}