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.

Built from https://develop.svn.wordpress.org/trunk@58354


git-svn-id: http://core.svn.wordpress.org/trunk@57806 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
oandregal 2024-06-06 08:02:16 +00:00
parent 3b8d75611b
commit 43dd91cdc6
5 changed files with 26 additions and 25 deletions

View File

@ -525,18 +525,14 @@ class WP_Theme_JSON_Resolver {
isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
$decoded_data['isGlobalStylesUserThemeJSON']
) {
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
$config = $decoded_data;
}
}
/** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
$config = $theme_json->get_data();
// Needs to be set for schema migrations of user data.
$config['isGlobalStylesUserThemeJSON'] = true;
static::$user = new WP_Theme_JSON( $config, 'custom' );
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
static::$user = $theme_json->get_theme_json();
return static::$user;
}

View File

@ -35,13 +35,14 @@ class WP_Theme_JSON_Schema {
* Function that migrates a given theme.json structure to the last version.
*
* @since 5.9.0
* @since 6.6.0 Migrate up to v3.
* @since 6.6.0 Migrate up to v3 and add $origin parameter.
*
* @param array $theme_json The structure to migrate.
*
* @param string $origin Optional. What source of data this object represents.
* One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.
* @return array The structure in the last version.
*/
public static function migrate( $theme_json ) {
public static function migrate( $theme_json, $origin = 'theme' ) {
if ( ! isset( $theme_json['version'] ) ) {
$theme_json = array(
'version' => WP_Theme_JSON::LATEST_SCHEMA,
@ -54,7 +55,7 @@ class WP_Theme_JSON_Schema {
$theme_json = self::migrate_v1_to_v2( $theme_json );
// Deliberate fall through. Once migrated to v2, also migrate to v3.
case 2:
$theme_json = self::migrate_v2_to_v3( $theme_json );
$theme_json = self::migrate_v2_to_v3( $theme_json, $origin );
}
return $theme_json;
@ -100,11 +101,12 @@ class WP_Theme_JSON_Schema {
*
* @since 6.6.0
*
* @param array $old Data to migrate.
*
* @param array $old Data to migrate.
* @param string $origin What source of data this object represents.
* One of 'blocks', 'default', 'theme', or 'custom'.
* @return array Data with defaultFontSizes set to false.
*/
private static function migrate_v2_to_v3( $old ) {
private static function migrate_v2_to_v3( $old, $origin ) {
// Copy everything.
$new = $old;
@ -115,10 +117,7 @@ class WP_Theme_JSON_Schema {
* Remaining changes do not need to be applied to the custom origin,
* as they should take on the value of the theme origin.
*/
if (
isset( $new['isGlobalStylesUserThemeJSON'] ) &&
true === $new['isGlobalStylesUserThemeJSON']
) {
if ( 'custom' === $origin ) {
return $new;
}

View File

@ -747,14 +747,14 @@ class WP_Theme_JSON {
*
* @param array $theme_json A structure that follows the theme.json schema.
* @param string $origin Optional. What source of data this object represents.
* One of 'default', 'theme', or 'custom'. Default 'theme'.
* One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.
*/
public function __construct( $theme_json = array( 'version' => WP_Theme_JSON::LATEST_SCHEMA ), $origin = 'theme' ) {
if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) {
$origin = 'theme';
}
$this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json );
$this->theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin );
$valid_block_names = array_keys( static::get_blocks_metadata() );
$valid_element_names = array_keys( static::ELEMENTS );
$valid_variations = static::get_valid_block_style_variations();
@ -3242,15 +3242,21 @@ class WP_Theme_JSON {
*
* @since 5.9.0
* @since 6.3.2 Preserves global styles block variations when securing styles.
* @since 6.6.0 Updated to allow variation element styles.
* @since 6.6.0 Updated to allow variation element styles and $origin parameter.
*
* @param array $theme_json Structure to sanitize.
* @param string $origin Optional. What source of data this object represents.
* One of 'blocks', 'default', 'theme', or 'custom'. Default 'theme'.
* @return array Sanitized structure.
*/
public static function remove_insecure_properties( $theme_json ) {
public static function remove_insecure_properties( $theme_json, $origin = 'theme' ) {
if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) {
$origin = 'theme';
}
$sanitized = array();
$theme_json = WP_Theme_JSON_Schema::migrate( $theme_json );
$theme_json = WP_Theme_JSON_Schema::migrate( $theme_json, $origin );
$valid_block_names = array_keys( static::get_blocks_metadata() );
$valid_element_names = array_keys( static::ELEMENTS );

View File

@ -2146,7 +2146,7 @@ function wp_filter_global_styles_post( $data ) {
) {
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
$data_to_encode = WP_Theme_JSON::remove_insecure_properties( $decoded_data );
$data_to_encode = WP_Theme_JSON::remove_insecure_properties( $decoded_data, 'custom' );
$data_to_encode['isGlobalStylesUserThemeJSON'] = true;
return wp_slash( wp_json_encode( $data_to_encode ) );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.6-beta1-58353';
$wp_version = '6.6-beta1-58354';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.