Make WordPress Core


Ignore:
Timestamp:
06/04/2024 11:53:37 AM (6 months ago)
Author:
ellatrix
Message:

Editor: Add theme.json v3 migrations.

See https://github.com/WordPress/wordpress-develop/pull/6616.
See also the original Gutenberg PRs:

Fixes #61282.

Props ajlende, talldanwp, ramonopoly, ellatrix.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-theme-json-schema.php

    r56753 r58328  
    3636     *
    3737     * @since 5.9.0
     38     * @since 6.6.0 Migrate up to v3.
    3839     *
    3940     * @param array $theme_json The structure to migrate.
     
    4849        }
    4950
    50         if ( 1 === $theme_json['version'] ) {
    51             $theme_json = self::migrate_v1_to_v2( $theme_json );
     51        // Migrate each version in order starting with the current version.
     52        switch ( $theme_json['version'] ) {
     53            case 1:
     54                $theme_json = self::migrate_v1_to_v2( $theme_json );
     55                // no break
     56            case 2:
     57                $theme_json = self::migrate_v2_to_v3( $theme_json );
     58                // no break
    5259        }
    5360
     
    8188        // Set the new version.
    8289        $new['version'] = 2;
     90
     91        return $new;
     92    }
     93
     94    /**
     95     * Migrates from v2 to v3.
     96     *
     97     * - Sets settings.typography.defaultFontSizes to false.
     98     *
     99     * @since 6.6.0
     100     *
     101     * @param array $old Data to migrate.
     102     *
     103     * @return array Data with defaultFontSizes set to false.
     104     */
     105    private static function migrate_v2_to_v3( $old ) {
     106        // Copy everything.
     107        $new = $old;
     108
     109        // Set the new version.
     110        $new['version'] = 3;
     111
     112        /*
     113         * Remaining changes do not need to be applied to the custom origin,
     114         * as they should take on the value of the theme origin.
     115         */
     116        if (
     117            isset( $new['isGlobalStylesUserThemeJSON'] ) &&
     118            true === $new['isGlobalStylesUserThemeJSON']
     119        ) {
     120            return $new;
     121        }
     122
     123        /*
     124         * Even though defaultFontSizes and defaultSpacingSizes are new
     125         * settings, we need to migrate them as they each control
     126         * PRESETS_METADATA prevent_override values which were previously
     127         * hardcoded to false. This only needs to happen when the theme provides
     128         * fontSizes or spacingSizes as they could match the default ones and
     129         * affect the generated CSS.
     130         */
     131        if ( isset( $old['settings']['typography']['fontSizes'] ) ) {
     132            if ( ! isset( $new['settings'] ) ) {
     133                $new['settings'] = array();
     134            }
     135            if ( ! isset( $new['settings']['typography'] ) ) {
     136                $new['settings']['typography'] = array();
     137            }
     138            $new['settings']['typography']['defaultFontSizes'] = false;
     139        }
     140
     141        /*
     142         * Similarly to defaultFontSizes, we need to migrate defaultSpacingSizes
     143         * as it controls the PRESETS_METADATA prevent_override which was
     144         * previously hardcoded to false. This only needs to happen when the
     145         * theme provided spacing sizes via spacingSizes or spacingScale.
     146         */
     147        if (
     148            isset( $old['settings']['spacing']['spacingSizes'] ) ||
     149            isset( $old['settings']['spacing']['spacingScale'] )
     150        ) {
     151            if ( ! isset( $new['settings'] ) ) {
     152                $new['settings'] = array();
     153            }
     154            if ( ! isset( $new['settings']['spacing'] ) ) {
     155                $new['settings']['spacing'] = array();
     156            }
     157            $new['settings']['spacing']['defaultSpacingSizes'] = false;
     158        }
     159
     160        /*
     161         * In v3 spacingSizes is merged with the generated spacingScale sizes
     162         * instead of completely replacing them. The v3 behavior is what was
     163         * documented for the v2 schema, but the code never actually did work
     164         * that way. Instead of surprising users with a behavior change two
     165         * years after the fact at the same time as a v3 update is introduced,
     166         * we'll continue using the "bugged" behavior for v2 themes. And treat
     167         * the "bug fix" as a breaking change for v3.
     168         */
     169        if ( isset( $old['settings']['spacing']['spacingSizes'] ) ) {
     170            unset( $new['settings']['spacing']['spacingScale'] );
     171        }
    83172
    84173        return $new;
Note: See TracChangeset for help on using the changeset viewer.