From b9096d9c7fcf8390048ad24aff630c94f746fdbc Mon Sep 17 00:00:00 2001 From: Joe McGill Date: Wed, 19 Jun 2024 16:41:13 +0000 Subject: [PATCH] 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. Built from https://develop.svn.wordpress.org/trunk@58443 git-svn-id: http://core.svn.wordpress.org/trunk@57892 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-theme-json-resolver.php | 69 +++++++++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/wp-includes/class-wp-theme-json-resolver.php b/wp-includes/class-wp-theme-json-resolver.php index 6f249f1321..a67ea80db4 100644 --- a/wp-includes/class-wp-theme-json-resolver.php +++ b/wp-includes/class-wp-theme-json-resolver.php @@ -172,8 +172,18 @@ class WP_Theme_JSON_Resolver { * * @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; } @@ -263,8 +273,18 @@ class WP_Theme_JSON_Resolver { * * @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. @@ -386,8 +406,19 @@ class WP_Theme_JSON_Resolver { * * @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; } @@ -523,7 +554,17 @@ class WP_Theme_JSON_Resolver { * @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' ); + } } /* @@ -545,8 +586,18 @@ class WP_Theme_JSON_Resolver { } /** 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; } diff --git a/wp-includes/version.php b/wp-includes/version.php index da32c816bf..593ff9a884 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.