Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
get_merged_data: use empty theme.json object as base instead of core
The PR #3441
introduced a bug by which the get_merged_data method is not cleared
in certain situations.

By doing (pseudo-code):

```php
function get_merged_data( $origin ) {
    $result = static::get_core_data();

    if ( 'block' === $origin ) { $result->merge( static::get_block_data() ); }
    if ( 'theme' === $origin ) { $result->merge( static::get_theme_data() ); }
    if ( 'custom' === $origin ) { $result->merge( static::get_custom_data() ); }

    return $result;
}
```

the base object ($result) is modifying the $core object directly,
and $core ends up storing the consolidated values instead of only the $core ones.

This is problematic because $core data is cached. Take, for example,
the following scenario:

```php
$data = get_merged_data( 'custom' );
$data = get_merged_data( 'theme' );
```

The expected output for $data is that it should not have data coming
from the 'custom' origin, however, it does.

The fix is reverting the change and use an empty object as base (pseudo-code):

```php
function get_merged_data( $origin ) {
    $result = new WP_Theme_JSON();

    $result->merge( static::get_core_data() );
    if ( 'block' === $origin ) { $result->merge( static::get_block_data() ); }
    if ( 'theme' === $origin ) { $result->merge( static::get_theme_data() ); }
    if ( 'custom' === $origin ) { $result->merge( static::get_custom_data() ); }

    return $result;
}
```
  • Loading branch information
oandregal committed Feb 28, 2023
commit a59f4403b92ab9fc73e7d3e0f45e3be9104b0dcc
3 changes: 2 additions & 1 deletion src/wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,8 @@ public static function get_merged_data( $origin = 'custom' ) {
_deprecated_argument( __FUNCTION__, '5.9.0' );
}

$result = static::get_core_data();
$result = new WP_Theme_JSON();
$result->merge( static::get_core_data() );
if ( 'default' === $origin ) {
$result->set_spacing_sizes();
return $result;
Expand Down