Make WordPress Core

Changeset 58354


Ignore:
Timestamp:
06/06/2024 08:00:08 AM (2 years ago)
Author:
oandregal
Message:

Editor: code quality improvements for theme.json migrate API

Backports ​https://github.com/WordPress/gutenberg/pull/62305

Follow-up to [58328], #61282.

Props ajlende, oandregal, ramonopoly, mukesh27.
Fixes #61282.

Location:
trunk/src/wp-includes
Files:
4 edited

Legend:

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

    r58339 r58354  
    526526                                $decoded_data['isGlobalStylesUserThemeJSON']
    527527                        ) {
     528                                unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
    528529                                $config = $decoded_data;
    529530                        }
    … …  
    531532
    532533                /** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */
    533                 $theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
    534                 $config     = $theme_json->get_data();
    535 
    536                 // Needs to be set for schema migrations of user data.
    537                 $config['isGlobalStylesUserThemeJSON'] = true;
    538 
    539                 static::$user = new WP_Theme_JSON( $config, 'custom' );
     534                $theme_json   = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
     535                static::$user = $theme_json->get_theme_json();
    540536
    541537                return static::$user;
  • trunk/src/wp-includes/class-wp-theme-json-schema.php

    r58339 r58354  
    3636         *
    3737         * @since 5.9.0
    38          * @since 6.6.0 Migrate up to v3.
     38         * @since 6.6.0 Migrate up to v3 and add $origin parameter.
    3939         *
    4040         * @param array $theme_json The structure to migrate.
    41          *
     41         * @param string $origin    Optional. What source of data this object represents.
     42         *                          One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.
    4243         * @return array The structure in the last version.
    4344         */
    44         public static function migrate( $theme_json ) {
     45        public static function migrate( $theme_json, $origin = 'theme' ) {
    4546                if ( ! isset( $theme_json['version'] ) ) {
    4647                        $theme_json = array(
    … …  
    5556                                // Deliberate fall through. Once migrated to v2, also migrate to v3.
    5657                        case 2:
    57                                 $theme_json = self::migrate_v2_to_v3( $theme_json );
     58                                $theme_json = self::migrate_v2_to_v3( $theme_json, $origin );
    5859                }
    5960
    … …  
    101102         * @since 6.6.0
    102103         *
    103          * @param array $old Data to migrate.
    104          *
     104         * @param array $old     Data to migrate.
     105         * @param string $origin What source of data this object represents.
     106         *                       One of 'blocks', 'default', 'theme', or 'custom'.
    105107         * @return array Data with defaultFontSizes set to false.
    106108         */
    107         private static function migrate_v2_to_v3( $old ) {
     109        private static function migrate_v2_to_v3( $old, $origin ) {
    108110                // Copy everything.
    109111                $new = $old;
    … …  
    116118                 * as they should take on the value of the theme origin.
    117119                 */
    118                 if (
    119                         isset( $new['isGlobalStylesUserThemeJSON'] ) &&
    120                         true === $new['isGlobalStylesUserThemeJSON']
    121                 ) {
     120                if ( 'custom' === $origin ) {
    122121                        return $new;
    123122                }
  • trunk/src/wp-includes/class-wp-theme-json.php

    r58339 r58354  
    748748         * @param array  $theme_json A structure that follows the theme.json schema.
    749749         * @param string $origin     Optional. What source of data this object represents.
    750          *                           One of 'default', 'theme', or 'custom'. Default 'theme'.
     750         *                           One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.
    751751         */
    752752        public function __construct( $theme_json = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA ), $origin = 'theme' ) {
    … …  
    755755                }
    756756
    757                 $this->theme_json    = WP_Theme_JSON_Schema::migrate( $theme_json );
     757                $this->theme_json    = WP_Theme_JSON_Schema::migrate( $theme_json, $origin );
    758758                $valid_block_names   = array_keys( static::get_blocks_metadata() );
    759759                $valid_element_names = array_keys( static::ELEMENTS );
    … …  
    32433243         * @since 5.9.0
    32443244         * @since 6.3.2 Preserves global styles block variations when securing styles.
    3245          * @since 6.6.0 Updated to allow variation element styles.
     3245         * @since 6.6.0 Updated to allow variation element styles and $origin parameter.
    32463246         *
    32473247         * @param array $theme_json Structure to sanitize.
     3248         * @param string $origin    Optional. What source of data this object represents.
     3249         *                          One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.
    32483250         * @return array Sanitized structure.
    32493251         */
    3250         public static function remove_insecure_properties( $theme_json ) {
     3252        public static function remove_insecure_properties( $theme_json, $origin = 'theme' ) {
     3253                if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) {
     3254                        $origin = 'theme';
     3255                }
     3256
    32513257                $sanitized = array();
    32523258
    3253                 $theme_json = WP_Theme_JSON_Schema::migrate( $theme_json );
     3259                $theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin );
    32543260
    32553261                $valid_block_names   = array_keys( static::get_blocks_metadata() );
  • trunk/src/wp-includes/kses.php

    r58294 r58354  
    21472147                unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
    21482148
    2149                 $data_to_encode = WP_Theme_JSON::remove_insecure_properties( $decoded_data );
     2149                $data_to_encode = WP_Theme_JSON::remove_insecure_properties( $decoded_data, 'custom' );
    21502150
    21512151                $data_to_encode['isGlobalStylesUserThemeJSON'] = true;
Note: See TracChangeset for help on using the changeset viewer.