Skip to content

Commit

Permalink
Prepare WP_Theme_JSON_Data class for backporting (#44109)
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal authored Sep 13, 2022
1 parent 2488a79 commit b46a119
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* Class to update with a theme.json structure.
*/
class WP_Theme_JSON_Data {
class WP_Theme_JSON_Data_Gutenberg {

/**
* Container of the data to update.
Expand All @@ -22,7 +22,7 @@ class WP_Theme_JSON_Data {
*
* @var string
*/
private $origin = null;
private $origin = '';

/**
* Constructor.
Expand All @@ -40,11 +40,10 @@ public function __construct( $data = array(), $origin = 'theme' ) {
*
* @param array $new_data Array following the theme.json specification.
*
* @return WP_Theme_JSON_Data the modified data.
* @return WP_Theme_JSON_Data_Gutenberg The own instance with access to the modified data.
*/
public function update_with( $new_data ) {
$this->theme_json->merge( new WP_Theme_JSON_Gutenberg( $new_data, $this->origin ) );

return $this;
}

Expand Down
34 changes: 26 additions & 8 deletions lib/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,16 @@ public static function get_core_data() {
return static::$core;
}

$config = static::read_json_file( __DIR__ . '/theme.json' );
$config = static::translate( $config );
$config = apply_filters( 'global_styles_default', new WP_Theme_JSON_Data( $config, 'default' ) );
static::$core = new WP_Theme_JSON_Gutenberg( $config->get_data(), 'default' );
$config = static::read_json_file( __DIR__ . '/theme.json' );
$config = static::translate( $config );
/**
* Filters the default data provided by WordPress for global styles & settings.
*
* @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'global_styles_default', new WP_Theme_JSON_Data_Gutenberg( $config, 'default' ) );
$config = $theme_json->get_data();
static::$core = new WP_Theme_JSON_Gutenberg( $config, 'default' );

return static::$core;
}
Expand All @@ -81,8 +87,14 @@ public static function get_user_data() {
$json_decoding_error = json_last_error();
if ( JSON_ERROR_NONE !== $json_decoding_error ) {
trigger_error( 'Error when decoding a theme.json schema for user data. ' . json_last_error_msg() );
$config = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
return new WP_Theme_JSON_Gutenberg( $config->get_data(), 'custom' );
/**
* Filters the data provided by the user for global styles & settings.
*
* @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) );
$config = $theme_json->get_data();
return new WP_Theme_JSON_Gutenberg( $config, 'custom' );
}

// Very important to verify if the flag isGlobalStylesUserThemeJSON is true.
Expand All @@ -97,8 +109,14 @@ public static function get_user_data() {
}
}

$config = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data( $config, 'custom' ) );
static::$user = new WP_Theme_JSON_Gutenberg( $config->get_data(), 'custom' );
/**
* Filters the data provided by the user for global styles & settings.
*
* @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'global_styles_user', new WP_Theme_JSON_Data_Gutenberg( $config, 'custom' ) );
$config = $theme_json->get_data();
static::$user = new WP_Theme_JSON_Gutenberg( $config, 'custom' );

return static::$user;
}
Expand Down
20 changes: 16 additions & 4 deletions lib/experimental/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,14 @@ public static function get_theme_data( $deprecated = array(), $settings = array(
$theme_json_data = static::translate( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
$theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_data );

$theme_json_data = apply_filters( 'global_styles_theme', new WP_Theme_JSON_Data( $theme_json_data, 'theme' ) );
static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data->get_data() );
/**
* Filters the data provided by the theme for global styles & settings.
*
* @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'global_styles_theme', new WP_Theme_JSON_Data_Gutenberg( $theme_json_data, 'theme' ) );
$theme_json_data = $theme_json->get_data();
static::$theme = new WP_Theme_JSON_Gutenberg( $theme_json_data );

if ( wp_get_theme()->parent() ) {
// Get parent theme.json.
Expand Down Expand Up @@ -130,11 +136,17 @@ public static function get_block_data() {
}
}

$config = apply_filters( 'global_styles_blocks', new WP_Theme_JSON_Data( $config, 'core' ) );
/**
* Filters the data provided by the blocks for global styles & settings.
*
* @param WP_Theme_JSON_Data_Gutenberg Class to access and update the underlying data.
*/
$theme_json = apply_filters( 'global_styles_blocks', new WP_Theme_JSON_Data_Gutenberg( $config, 'core' ) );
$config = $theme_json->get_data();

// Core here means it's the lower level part of the styles chain.
// It can be a core or a third-party block.
return new WP_Theme_JSON_Gutenberg( $config->get_data(), 'core' );
return new WP_Theme_JSON_Gutenberg( $config, 'core' );
}

/**
Expand Down
2 changes: 1 addition & 1 deletion lib/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ function gutenberg_is_experiment_enabled( $name ) {
require __DIR__ . '/compat/wordpress-6.1/block-editor-settings.php';
require __DIR__ . '/compat/wordpress-6.1/persisted-preferences.php';
require __DIR__ . '/compat/wordpress-6.1/get-global-styles-and-settings.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-data.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-data-gutenberg.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-6-1.php';
require __DIR__ . '/compat/wordpress-6.1/class-wp-theme-json-resolver-6-1.php';
require __DIR__ . '/compat/wordpress-6.1/block-template-utils.php';
Expand Down

0 comments on commit b46a119

Please sign in to comment.