WP_Theme_JSON::flatten_tree( array $tree, string $prefix = '', string $token = '--' ): array
- Since
- 5.8.0
- Source
wp-includes/class-wp-theme-json.php:2960
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'
}Compatibility
- WordPress
- since 5.8.0
- PHP
- 7.4–8.6-dev
- 6.7.7
- 6.8.8
- 6.9.7
- 7.0.4
- 7.1.0
Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.
Parameters
$treearray- Input tree to process.
$prefixstringoptional- Prefix to prepend to each variable. Default empty string.Default:
'' $tokenstringoptional- Token to use between levels. Default '--'.Default:
'--'
Return value
array- The flattened tree.
Performance profile
How much work a call to WP_Theme_JSON::flatten_tree() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.
- Cost class
- Light
- Scaling
- Scales with input
- Instructions
- 7–8
- Plugin surface
- None
- Called by
- 0
Touches nothing outside its own arguments.
The body loops, so the work grows with what you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 40.
Nothing here hands control to plugin code.
Nothing in core calls this; the cost is only what you spend yourself.
What one call costs · 1 distinct outcome
One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Theme_JSON::flatten_tree() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 7–8 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 40 | 7–8 | 5 | |
| 8.5 | 40 | 7–8 | 5 | |
| 8.4 | 40 | 7–8 | 5 | 3 fewer instructions than PHP 8.3 |
| 8.3 | 43 | 7–8 | 5 | |
| 8.2 | 43 | 7–8 | 5 | |
| 8.1 | 43 | 7–8 | 5 | |
| 7.4 | 43 | 7–8 | 5 |
An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.
Uses · 2
- _wp_to_kebab_case()This function is trying to replicate what lodash's kebabCase (JS library) does in the client.
- static::flatten_tree()
Source code
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; }Changelog
Introduced in 5.8.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/class-wp-theme-json.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it. - Corrections
- Something wrong on this page? Report it and it gets fixed in the next regeneration.