WP_Theme_JSON::flatten_tree( array $tree, string $prefix = , string $token = ‘–‘ ): array

In this article

Given a tree, it creates a flattened one by merging the keys and binding the leaf values to the new keys.

Description

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'
}

Parameters

$treearrayrequired
Input tree to process.
$prefixstringoptional
Prefix to prepend to each variable.

Default:''

$tokenstringoptional
Token to use between levels. Default '--'.

Default:'--'

Return

array The flattened tree.

Source

 * @param array    $preset_metadata One of the PRESETS_METADATA values.
 * @param string[] $origins         List of origins to process.
 * @return array Array of presets where the key and value are both the slug.
 */
protected static function get_settings_slugs( $settings, $preset_metadata, $origins = null ) {
	if ( null === $origins ) {
		$origins = static::VALID_ORIGINS;
	}

	$preset_per_origin = _wp_array_get( $settings, $preset_metadata['path'], array() );

	$result = array();
	foreach ( $origins as $origin ) {
		if ( ! isset( $preset_per_origin[ $origin ] ) ) {
			continue;
		}
		foreach ( $preset_per_origin[ $origin ] as $preset ) {
			$slug = _wp_to_kebab_case( $preset['slug'] );

			// Use the array as a set so we don't get duplicates.
			$result[ $slug ] = $slug;

Changelog

VersionDescription
5.8.0Introduced.

User Contributed Notes

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