W3cubDocs

/WordPress

WP_Theme_JSON::get_block_nodes( array $theme_json ): array

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

Parameters

$theme_jsonarrayrequired
The theme.json converted to an array.

Return

array The block nodes in theme.json.

Source

 * It also transforms camelCase names into kebab-case
 * and substitutes '/' by '-'.
 *
 * This is thought to be useful to generate
 * CSS Custom Properties from a tree,
 * although there's nothing in the implementation
 * of this function that requires that format.
 *
 * For example, assuming the given prefix is '--wp'
 * and the token is '--', for this input tree:
 *
 *     {
 *       'some/property': 'value',
 *       'nestedProperty': {
 *         'sub-property': 'value'
 *       }
 *     }
 *
 * it'll return this output:
 *
 *     {
 *       '--wp--some-property': 'value',
 *       '--wp--nested-property--sub-property': 'value'
 *     }
 *
 * @since 5.8.0
 *
 * @param array  $tree   Input tree to process.
 * @param string $prefix Optional. Prefix to prepend to each variable. Default empty string.
 * @param string $token  Optional. Token to use between levels. Default '--'.
 * @return array The flattened tree.
 */
protected static function flatten_tree( $tree, $prefix = '', $token = '--' ) {
	$result = array();
	foreach ( $tree as $property => $value ) {
		$new_key = $prefix . str_replace(
			'/',
			'-',
			strtolower( _wp_to_kebab_case( $property ) )
		);

		if ( is_array( $value ) ) {
			$new_prefix        = $new_key . $token;
			$flattened_subtree = static::flatten_tree( $value, $new_prefix, $token );
			foreach ( $flattened_subtree as $subtree_key => $subtree_value ) {
				$result[ $subtree_key ] = $subtree_value;
			}
		} else {
			$result[ $new_key ] = $value;
		}
	}
	return $result;
}

/**
 * Given a styles array, it extracts the style properties
 * and adds them to the $declarations array following the format:
 *
 *     array(
 *       'name'  => 'property_name',
 *       'value' => 'property_value',
 *     )
 *
 * @since 5.8.0
 * @since 5.9.0 Added the `$settings` and `$properties` parameters.
 * @since 6.1.0 Added `$theme_json`, `$selector`, and `$use_root_padding` parameters.
 * @since 6.5.0 Output a `min-height: unset` rule when `aspect-ratio` is set.
 * @since 6.6.0 Pass current theme JSON settings to wp_get_typography_font_size_value(), and process background properties.
 * @since 6.7.0 `ref` resolution of background properties, and assigning custom default values.
 *
 * @param array   $styles Styles to process.
 * @param array   $settings Theme settings.
 * @param array   $properties Properties metadata.
 * @param array   $theme_json Theme JSON array.
 * @param string  $selector The style block selector.
 * @param boolean $use_root_padding Whether to add custom properties at root level.

Changelog

Version Description
6.3.0 Refactored and stabilized selectors API.
6.1.0 Introduced.

© 2003–2024 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/classes/wp_theme_json/get_block_nodes