Make WordPress Core

Opened 7 weeks ago

Last modified 7 weeks ago

#61937 new defect (bug)

WP_Theme_JSON_Schema::migrate removes data when version is missing

Reported by: soean's profile Soean Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version:
Component: Themes Keywords:
Focuses: Cc:

Description

I want to override a theme.json, therefore I can use the wp_theme_json_data_theme filter.

I tried it with this code:

<?php
add_filter(
        'wp_theme_json_data_theme',
        function ( \WP_Theme_JSON_Data $theme_json_data ): \WP_Theme_JSON_Data {

                return $theme_json_data->update_with(
                        [
                                // 'version'  => 3, // only works with the version parameter
                                'settings' => [
                                        'color' => [
                                                'palette' => [
                                                        'slug'  => 'some-color',
                                                        'color' => '#01579b',
                                                        'name'  => 'Some Color',
                                                ],
                                        ],
                                ],
                        ]
                );
        }
);

This only works when I add the version, because the WP_Theme_JSON_Data constructor calls WP_Theme_JSON_Schema::migrate().

I found out, that I need to add a version to my array, otherwise this function overrides my data. See

We can fix it, if we only set the version parameter and don't override the array:

<?php
if ( ! isset( $theme_json['version'] ) ) {
        $theme_json['version'] = WP_Theme_JSON::LATEST_SCHEMA;
}
...

Change History (2)

#1 @Soean
7 weeks ago

If we want a version, because otherwise we can't migrate old values, maybe we should call a _doing_it_wrong() if the value is missing.

#2 @sabernhardt
7 weeks ago

  • Component changed from General to Themes
Note: See TracTickets for help on using tickets.