Skip to content

Commit b3e6507

Browse files
committed
Editor: code quality improvements for theme.json migrate API
Backports WordPress/gutenberg#62305 Follow-up to [58328], #61282. Props ajlende, oandregal, ramonopoly, mukesh27. Fixes #61282. git-svn-id: https://develop.svn.wordpress.org/trunk@58354 602fd350-edb4-49c9-b593-d223f7449a82
1 parent a033cf1 commit b3e6507

4 files changed

+25
-24
lines changed

src/wp-includes/class-wp-theme-json-resolver.php

+3-7
Original file line numberDiff line numberDiff line change
@@ -525,18 +525,14 @@ public static function get_user_data() {
525525
isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) &&
526526
$decoded_data['isGlobalStylesUserThemeJSON']
527527
) {
528+
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );
528529
$config = $decoded_data;
529530
}
530531
}
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;
542538
}

src/wp-includes/class-wp-theme-json-schema.php

+10-11
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,14 @@ class WP_Theme_JSON_Schema {
3535
* Function that migrates a given theme.json structure to the last version.
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(
4748
'version' => WP_Theme_JSON::LATEST_SCHEMA,
@@ -54,7 +55,7 @@ public static function migrate( $theme_json ) {
5455
$theme_json = self::migrate_v1_to_v2( $theme_json );
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

6061
return $theme_json;
@@ -100,11 +101,12 @@ private static function migrate_v1_to_v2( $old ) {
100101
*
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;
110112

@@ -115,10 +117,7 @@ private static function migrate_v2_to_v3( $old ) {
115117
* Remaining changes do not need to be applied to the custom origin,
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
}
124123

src/wp-includes/class-wp-theme-json.php

+11-5
Original file line numberDiff line numberDiff line change
@@ -747,14 +747,14 @@ public static function get_element_class_name( $element ) {
747747
*
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' ) {
753753
if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) {
754754
$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 );
760760
$valid_variations = static::get_valid_block_style_variations();
@@ -3242,15 +3242,21 @@ protected static function filter_slugs( $node, $slugs ) {
32423242
*
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() );
32563262
$valid_element_names = array_keys( static::ELEMENTS );

src/wp-includes/kses.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2146,7 +2146,7 @@ function wp_filter_global_styles_post( $data ) {
21462146
) {
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;
21522152
return wp_slash( wp_json_encode( $data_to_encode ) );

0 commit comments

Comments
 (0)