WP_Theme_JSON::get_style_nodes( array $theme_json, array $selectors = array() ): array

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

Description

[ [ ‘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 ], ]

Parameters

$theme_jsonarrayrequired
The tree to extract style nodes from.
$selectorsarrayoptional
List of selectors per block.

Default:array()

Return

array An array of style nodes metadata.

Source

			'selector' => $selector,
		);
	}

	return $nodes;
}

/**
 * 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
 *       ],
 *     ]
 *
 * @since 5.8.0
 *
 * @param array $theme_json The tree to extract style nodes from.
 * @param array $selectors  List of selectors per block.
 * @return array An array of style nodes metadata.
 */
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.
			if ( isset( static::VALID_ELEMENT_PSEUDO_SELECTORS[ $element ] ) ) {
				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 ),
						);
					}
				}

Changelog

VersionDescription
5.8.0Introduced.

User Contributed Notes

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