Skip to content

Commit 6ae489c

Browse files
committed
Editor: Improve compatibility for WP_Theme_JSON_Data.
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. git-svn-id: https://develop.svn.wordpress.org/trunk@58443 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 5c7f94a commit 6ae489c

1 file changed

Lines changed: 60 additions & 9 deletions

File tree

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

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,18 @@ public static function get_core_data() {
172172
*
173173
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
174174
*/
175-
$theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) );
176-
static::$core = $theme_json->get_theme_json();
175+
$theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) );
176+
177+
/*
178+
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
179+
* compatible class that is not a WP_Theme_JSON_Data object.
180+
*/
181+
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
182+
static::$core = $theme_json->get_theme_json();
183+
} else {
184+
$config = $theme_json->get_data();
185+
static::$core = new WP_Theme_JSON( $config, 'default' );
186+
}
177187

178188
return static::$core;
179189
}
@@ -263,8 +273,18 @@ public static function get_theme_data( $deprecated = array(), $options = array()
263273
*
264274
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
265275
*/
266-
$theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
267-
static::$theme = $theme_json->get_theme_json();
276+
$theme_json = apply_filters( 'wp_theme_json_data_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
277+
278+
/*
279+
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
280+
* compatible class that is not a WP_Theme_JSON_Data object.
281+
*/
282+
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
283+
static::$theme = $theme_json->get_theme_json();
284+
} else {
285+
$config = $theme_json->get_data();
286+
static::$theme = new WP_Theme_JSON( $config );
287+
}
268288

269289
if ( $wp_theme->parent() ) {
270290
// Get parent theme.json.
@@ -386,8 +406,19 @@ public static function get_block_data() {
386406
*
387407
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
388408
*/
389-
$theme_json = apply_filters( 'wp_theme_json_data_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) );
390-
static::$blocks = $theme_json->get_theme_json();
409+
$theme_json = apply_filters( 'wp_theme_json_data_blocks', new WP_Theme_JSON_Data( $config, 'blocks' ) );
410+
411+
/*
412+
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
413+
* compatible class that is not a WP_Theme_JSON_Data object.
414+
*/
415+
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
416+
static::$blocks = $theme_json->get_theme_json();
417+
} else {
418+
$config = $theme_json->get_data();
419+
static::$blocks = new WP_Theme_JSON( $config, 'blocks' );
420+
}
421+
391422
return static::$blocks;
392423
}
393424

@@ -523,7 +554,17 @@ public static function get_user_data() {
523554
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data.
524555
*/
525556
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
526-
return $theme_json->get_theme_json();
557+
558+
/*
559+
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
560+
* compatible class that is not a WP_Theme_JSON_Data object.
561+
*/
562+
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
563+
return $theme_json->get_theme_json();
564+
} else {
565+
$config = $theme_json->get_data();
566+
return new WP_Theme_JSON( $config, 'custom' );
567+
}
527568
}
528569

529570
/*
@@ -545,8 +586,18 @@ public static function get_user_data() {
545586
}
546587

547588
/** This filter is documented in wp-includes/class-wp-theme-json-resolver.php */
548-
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
549-
static::$user = $theme_json->get_theme_json();
589+
$theme_json = apply_filters( 'wp_theme_json_data_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
590+
591+
/*
592+
* Backward compatibility for extenders returning a WP_Theme_JSON_Data
593+
* compatible class that is not a WP_Theme_JSON_Data object.
594+
*/
595+
if ( $theme_json instanceof WP_Theme_JSON_Data ) {
596+
static::$user = $theme_json->get_theme_json();
597+
} else {
598+
$config = $theme_json->get_data();
599+
static::$user = new WP_Theme_JSON( $config, 'custom' );
600+
}
550601

551602
return static::$user;
552603
}

0 commit comments

Comments
 (0)