Skip to content

Commit

Permalink
Editor: Improve compatibility for WP_Theme_JSON_Data.
Browse files Browse the repository at this point in the history
This checks that objects returned from any of the `wp_theme_json_data_` filters are `WP_Theme_JSON_Data` objects in order to avoid incompatibilities. Otherwise, reprocess the theme.json data as new `WP_Theme_JSON` objects to ensure the data matches the expectations of code consuming that data.

Follow-up to [58185].

Props joemcgill, adamsilverstein, oandregal, ryelle, ocean90, pbearne.
See #61112.

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


git-svn-id: http://core.svn.wordpress.org/trunk@57892 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
joemcgill committed Jun 19, 2024
1 parent dd55b02 commit b9096d9
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 10 deletions.
69 changes: 60 additions & 9 deletions wp-includes/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,18 @@ public static function get_core_data() {
*
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) );
static::$core = $theme_json->get_theme_json();
$theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) );

/*
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
* compatible class that is not a WP_Theme_JSON_Data object.
*/
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
static::$core = $theme_json->get_theme_json();
} else {
$config = $theme_json->get_data();
static::$core = new WP_Theme_JSON( $config, 'default' );
}

return static::$core;
}
Expand Down Expand Up @@ -263,8 +273,18 @@ public static function get_theme_data( $deprecated = array(), $options = array()
*
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
static::$theme = $theme_json->get_theme_json();
$theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );

/*
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
* compatible class that is not a WP_Theme_JSON_Data object.
*/
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
static::$theme = $theme_json->get_theme_json();
} else {
$config = $theme_json->get_data();
static::$theme = new WP_Theme_JSON( $config );
}

if ( $wp_theme->parent() ) {
// Get parent theme.json.
Expand Down Expand Up @@ -386,8 +406,19 @@ public static function get_block_data() {
*
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'wp_theme_json_data_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) );
static::$blocks = $theme_json->get_theme_json();
$theme_json = apply_filters( 'wp_theme_json_data_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) );

/*
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
* compatible class that is not a WP_Theme_JSON_Data object.
*/
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
static::$blocks = $theme_json->get_theme_json();
} else {
$config = $theme_json->get_data();
static::$blocks = new WP_Theme_JSON( $config, 'blocks' );
}

return static::$blocks;
}

Expand Down Expand Up @@ -523,7 +554,17 @@ public static function get_user_data() {
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
return $theme_json->get_theme_json();

/*
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
* compatible class that is not a WP_Theme_JSON_Data object.
*/
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
return $theme_json->get_theme_json();
} else {
$config = $theme_json->get_data();
return new WP_Theme_JSON( $config, 'custom' );
}
}

/*
Expand All @@ -545,8 +586,18 @@ public static function get_user_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' ) );
static::$user = $theme_json->get_theme_json();
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );

/*
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
* compatible class that is not a WP_Theme_JSON_Data object.
*/
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
static::$user = $theme_json->get_theme_json();
} else {
$config = $theme_json->get_data();
static::$user = new WP_Theme_JSON( $config, 'custom' );
}

return static::$user;
}
Expand Down
2 changes: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.6-beta3-58442';
$wp_version = '6.6-beta3-58443';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit b9096d9

Please sign in to comment.