Skip to content

Commit

Permalink
Migrate theme.json based on origin (#62305)
Browse files Browse the repository at this point in the history
Co-authored-by: ajlende <ajlende@git.wordpress.org>
Co-authored-by: oandregal <oandregal@git.wordpress.org>
  • Loading branch information
3 people authored Jun 5, 2024
1 parent 06470bb commit 68beef3
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 23 deletions.
3 changes: 3 additions & 0 deletions backport-changelog/6.6/6737.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
https://github.com/WordPress/wordpress-develop/pull/6737

* https://github.com/WordPress/gutenberg/pull/62305
16 changes: 11 additions & 5 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -734,14 +734,14 @@ public static function get_element_class_name( $element ) {
*
* @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_Gutenberg::LATEST_SCHEMA ), $origin = 'theme' ) {
if ( ! in_array( $origin, static::VALID_ORIGINS, true ) ) {
$origin = 'theme';
}

$this->theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json );
$this->theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json, $origin );
$registry = WP_Block_Type_Registry::get_instance();
$valid_block_names = array_keys( $registry->get_all_registered() );
$valid_element_names = array_keys( static::ELEMENTS );
Expand Down Expand Up @@ -3278,15 +3278,21 @@ protected static function filter_slugs( $node, $slugs ) {
* Removes insecure data from theme.json.
*
* @since 5.9.0
* @since 6.6.0 Added support for block style variation element styles.
* @since 6.6.0 Added support for block style 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_Gutenberg::migrate( $theme_json );
$theme_json = WP_Theme_JSON_Schema_Gutenberg::migrate( $theme_json, $origin );

$valid_block_names = array_keys( static::get_blocks_metadata() );
$valid_element_names = array_keys( static::ELEMENTS );
Expand Down
10 changes: 3 additions & 7 deletions lib/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -534,18 +534,14 @@ public static function get_user_data() {
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_Gutenberg( $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_Gutenberg( $config, 'custom' );
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) );
static::$user = $theme_json->get_theme_json();

return static::$user;
}
Expand Down
20 changes: 10 additions & 10 deletions lib/class-wp-theme-json-schema-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ class WP_Theme_JSON_Schema_Gutenberg {
* @since 5.9.0
* @since 6.6.0 Migrate up to v3.
*
* @param array $theme_json The structure to migrate.
* @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,
Expand All @@ -57,7 +59,7 @@ public static function migrate( $theme_json ) {
$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;
Expand Down Expand Up @@ -103,11 +105,12 @@ private static function migrate_v1_to_v2( $old ) {
*
* @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;

Expand All @@ -118,10 +121,7 @@ private static function migrate_v2_to_v3( $old ) {
* 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;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/experimental/kses.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function gutenberg_filter_global_styles_post( $data ) {
) {
unset( $decoded_data['isGlobalStylesUserThemeJSON'] );

$data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data );
$data_to_encode = WP_Theme_JSON_Gutenberg::remove_insecure_properties( $decoded_data, 'custom' );

$data_to_encode['isGlobalStylesUserThemeJSON'] = true;
return wp_slash( wp_json_encode( $data_to_encode ) );
Expand Down

0 comments on commit 68beef3

Please sign in to comment.