[wp-trac] [WordPress Trac] #61937: WP_Theme_JSON_Schema::migrate removes data when version is missing

WordPress Trac noreply at wordpress.org
Tue Aug 27 15:00:00 UTC 2024


#61937: WP_Theme_JSON_Schema::migrate removes data when version is missing
--------------------------+-----------------------------
 Reporter:  Soean         |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 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
 <?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
 - https://github.com/WordPress/wordpress-develop/blob/6.6.1/src/wp-
 includes/class-wp-theme-json-schema.php#L46-L50
 -
 https://developer.wordpress.org/reference/classes/wp_theme_json_schema/migrate/
 {{{#!php
 <?php
 if ( ! isset( $theme_json['version'] ) ) {
         $theme_json = array(
                 'version' => WP_Theme_JSON::LATEST_SCHEMA,
         );
 }
 ...
 }}}


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

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

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/61937>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list