WP_Theme_JSON::get_feature_declarations_for_node()protectedWP 6.3.0

Generates style declarations for a node's features e.g., color, border, typography etc. that have custom selectors in their related block's metadata.

Method of the class: WP_Theme_JSON{}

No Hooks.

Return

Array. The style declarations for the node's features with custom selectors.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_feature_declarations_for_node( $metadata, $node );
$metadata(object) (required)
The related block metadata containing selectors.
$node(object) (required) (passed by reference — &)
A merged theme.json node for block or variation.

Changelog

Since 6.3.0 Introduced.

WP_Theme_JSON::get_feature_declarations_for_node() code WP 6.6.2

protected function get_feature_declarations_for_node( $metadata, &$node ) {
	$declarations = array();

	if ( ! isset( $metadata['selectors'] ) ) {
		return $declarations;
	}

	$settings = isset( $this->theme_json['settings'] )
		? $this->theme_json['settings']
		: array();

	foreach ( $metadata['selectors'] as $feature => $feature_selectors ) {
		/*
		 * Skip if this is the block's root selector or the block doesn't
		 * have any styles for the feature.
		 */
		if ( 'root' === $feature || empty( $node[ $feature ] ) ) {
			continue;
		}

		if ( is_array( $feature_selectors ) ) {
			foreach ( $feature_selectors as $subfeature => $subfeature_selector ) {
				if ( 'root' === $subfeature || empty( $node[ $feature ][ $subfeature ] ) ) {
					continue;
				}

				/*
				 * Create temporary node containing only the subfeature data
				 * to leverage existing `compute_style_properties` function.
				 */
				$subfeature_node = array(
					$feature => array(
						$subfeature => $node[ $feature ][ $subfeature ],
					),
				);

				// Generate style declarations.
				$new_declarations = static::compute_style_properties( $subfeature_node, $settings, null, $this->theme_json );

				// Merge subfeature declarations into feature declarations.
				if ( isset( $declarations[ $subfeature_selector ] ) ) {
					foreach ( $new_declarations as $new_declaration ) {
						$declarations[ $subfeature_selector ][] = $new_declaration;
					}
				} else {
					$declarations[ $subfeature_selector ] = $new_declarations;
				}

				/*
				 * Remove the subfeature from the block's node now its
				 * styles will be included under its own selector not the
				 * block's.
				 */
				unset( $node[ $feature ][ $subfeature ] );
			}
		}

		/*
		 * Now subfeatures have been processed and removed we can process
		 * feature root selector or simple string selector.
		 */
		if (
			is_string( $feature_selectors ) ||
			( isset( $feature_selectors['root'] ) && $feature_selectors['root'] )
		) {
			$feature_selector = is_string( $feature_selectors ) ? $feature_selectors : $feature_selectors['root'];

			/*
			 * Create temporary node containing only the feature data
			 * to leverage existing `compute_style_properties` function.
			 */
			$feature_node = array( $feature => $node[ $feature ] );

			// Generate the style declarations.
			$new_declarations = static::compute_style_properties( $feature_node, $settings, null, $this->theme_json );

			/*
			 * Merge new declarations with any that already exist for
			 * the feature selector. This may occur when multiple block
			 * support features use the same custom selector.
			 */
			if ( isset( $declarations[ $feature_selector ] ) ) {
				foreach ( $new_declarations as $new_declaration ) {
					$declarations[ $feature_selector ][] = $new_declaration;
				}
			} else {
				$declarations[ $feature_selector ] = $new_declarations;
			}

			/*
			 * Remove the feature from the block's node now its styles
			 * will be included under its own selector not the block's.
			 */
			unset( $node[ $feature ] );
		}
	}

	return $declarations;
}