-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make global styles data filterable #44015
Changes from all commits
4d160b0
7fcbf75
6e7a6c3
e80c7b6
a923703
4cffa5d
60de8c8
a5dddc3
c340a37
23df722
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* API to update a theme.json structure. | ||
* | ||
* @package gutenberg | ||
*/ | ||
|
||
/** | ||
* Class to update with a theme.json structure. | ||
*/ | ||
class WP_Theme_JSON_Data { | ||
|
||
/** | ||
* Container of the data to update. | ||
* | ||
* @var WP_Theme_JSON | ||
*/ | ||
private $theme_json = null; | ||
|
||
/** | ||
* The origin of the data: default, theme, user, etc. | ||
* | ||
* @var string | ||
*/ | ||
private $origin = null; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably default to |
||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param array $data Array following the theme.json specification. | ||
* @param string $origin The origin of the data: default, theme, user. | ||
*/ | ||
public function __construct( $data = array(), $origin = 'theme' ) { | ||
$this->origin = $origin; | ||
$this->theme_json = new WP_Theme_JSON_Gutenberg( $data, $this->origin ); | ||
} | ||
|
||
/** | ||
* Updates the theme.json with the the given data. | ||
* | ||
* @param array $new_data Array following the theme.json specification. | ||
* | ||
* @return WP_Theme_JSON_Data the modified data. | ||
*/ | ||
public function update_with( $new_data ) { | ||
$this->theme_json->merge( new WP_Theme_JSON_Gutenberg( $new_data, $this->origin ) ); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary line break here? |
||
return $this; | ||
} | ||
|
||
/** | ||
* Returns the underlying data. | ||
* | ||
* @return array | ||
*/ | ||
public function get_data() { | ||
return $this->theme_json->get_raw_data(); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,8 +56,50 @@ public static function get_core_data() { | |
|
||
$config = static::read_json_file( __DIR__ . '/theme.json' ); | ||
$config = static::translate( $config ); | ||
static::$core = new WP_Theme_JSON_Gutenberg( $config, 'default' ); | ||
$config = apply_filters( 'global_styles_default', new WP_Theme_JSON_Data( $config, 'default' ) ); | ||
static::$core = new WP_Theme_JSON_Gutenberg( $config->get_data(), 'default' ); | ||
Comment on lines
+59
to
+60
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. With WP filters we need to return the same data type as in the input, so that should be $data = apply_filters( 'global_styles_default', new WP_Theme_JSON_Data( $config, 'default' ) );
static::$core = new WP_Theme_JSON_Gutenberg( $data->get_config(), 'default' ); I see the same pattern in other places. Maybe, we could rename that to |
||
|
||
return static::$core; | ||
} | ||
|
||
/** | ||
* Returns the user's origin config. | ||
* | ||
* @return WP_Theme_JSON_Gutenberg Entity that holds styles for user data. | ||
*/ | ||
public static function get_user_data() { | ||
if ( null !== static::$user ) { | ||
return static::$user; | ||
} | ||
|
||
$config = array(); | ||
$user_cpt = static::get_user_data_from_wp_global_styles( wp_get_theme() ); | ||
|
||
if ( array_key_exists( 'post_content', $user_cpt ) ) { | ||
$decoded_data = json_decode( $user_cpt['post_content'], true ); | ||
|
||
$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' ); | ||
} | ||
|
||
// Very important to verify if the flag isGlobalStylesUserThemeJSON is true. | ||
// If is not true the content was not escaped and is not safe. | ||
if ( | ||
is_array( $decoded_data ) && | ||
isset( $decoded_data['isGlobalStylesUserThemeJSON'] ) && | ||
$decoded_data['isGlobalStylesUserThemeJSON'] | ||
) { | ||
unset( $decoded_data['isGlobalStylesUserThemeJSON'] ); | ||
$config = $decoded_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' ); | ||
|
||
return static::$user; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Apologies for commenting just after this PR merged, but should this line have a
Gutenberg
suffix, or have aclass_exists
check before defining it, so that we don't run into any issues when this is backported to core?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it'd be best to call
class_exists
before requiring this file.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
+1