Skip to content
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

Iterate on the public API of WP_Theme_JSON_Resolver #28855

Merged
merged 7 commits into from
Feb 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 28 additions & 43 deletions lib/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ private static function translate_presets( &$theme_json_structure, $domain = 'de
*
* @return WP_Theme_JSON Entity that holds core data.
*/
private static function get_core_origin() {
public static function get_core_data() {
if ( null !== self::$core ) {
return self::$core;
}
Expand Down Expand Up @@ -279,7 +279,7 @@ private static function get_core_origin() {
*
* @return WP_Theme_JSON Entity that holds theme data.
*/
public function get_theme_data( $theme_support_data = array() ) {
public static function get_theme_data( $theme_support_data = array() ) {
if ( null === self::$theme ) {
$theme_json_data = self::get_from_file( locate_template( 'experimental-theme.json' ) );
self::translate_presets( $theme_json_data, wp_get_theme()->get( 'TextDomain' ) );
Expand Down Expand Up @@ -351,7 +351,7 @@ private static function get_user_data_from_custom_post_type( $should_create_cpt
*
* @return WP_Theme_JSON Entity that holds user data.
*/
private static function get_user_origin() {
public static function get_user_data() {
if ( null !== self::$user ) {
return self::$user;
}
Expand Down Expand Up @@ -384,57 +384,42 @@ private static function get_user_origin() {
}

/**
* There are three sources of data for a site:
* core, theme, and user.
* There are three sources of data (origins) for a site:
* core, theme, and user. The user's has higher priority
* than the theme's, and the theme's higher than core's.
*
* The main function of the resolver is to
* merge all this data following this algorithm:
* theme overrides core, and user overrides
* data coming from either theme or core.
* Unlike the getters {@link get_core_data},
* {@link get_theme_data}, and {@link get_user_data},
* this method returns data after it has been merged
* with the previous origins. This means that if the same piece of data
* is declared in different origins (user, theme, and core),
* the last origin overrides the previous.
*
* user data > theme data > core data
* For example, if the user has set a background color
* for the paragraph block, and the theme has done it as well,
* the user preference wins.
*
* The main use case for the resolver is to return
* the merged data up to the user level.However,
* there are situations in which we need the
* data merged up to a different level (theme)
* or no merged at all.
*
* @param array $theme_support_data Existing block editor settings.
* Empty array by default.
* @param string $origin The source of data the consumer wants.
* Valid values are 'core', 'theme', 'user'.
* @param array $theme_support_data Existing block editor settings.
* Empty array by default.
* @param string $origin To what level should we merge data.
* Valid values are 'theme' or 'user'.
* Default is 'user'.
* @param boolean $merged Whether the data should be merged
* with the previous origins (the default).
*
* @return WP_Theme_JSON
*/
public function get_origin( $theme_support_data = array(), $origin = 'user', $merged = true ) {
if ( ( 'user' === $origin ) && $merged ) {
$result = new WP_Theme_JSON();
$result->merge( self::get_core_origin() );
$result->merge( $this->get_theme_data( $theme_support_data ) );
$result->merge( self::get_user_origin() );
return $result;
}

if ( ( 'theme' === $origin ) && $merged ) {
public static function get_merged_data( $theme_support_data = array(), $origin = 'user' ) {
if ( 'theme' === $origin ) {
$result = new WP_Theme_JSON();
$result->merge( self::get_core_origin() );
$result->merge( $this->get_theme_data( $theme_support_data ) );
$result->merge( self::get_core_data() );
$result->merge( self::get_theme_data( $theme_support_data ) );
return $result;
}

if ( 'user' === $origin ) {
return self::get_user_origin();
}

if ( 'theme' === $origin ) {
return $this->get_theme_data( $theme_support_data );
}

return self::get_core_origin();
$result = new WP_Theme_JSON();
$result->merge( self::get_core_data() );
$result->merge( self::get_theme_data( $theme_support_data ) );
$result->merge( self::get_user_data() );
return $result;
}

/**
Expand Down
3 changes: 1 addition & 2 deletions lib/full-site-editing/page-templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ function gutenberg_load_block_page_templates( $templates, $theme, $post, $post_t
return $templates;
}

$resolver = new WP_Theme_JSON_Resolver();
$data = $resolver->get_theme_data()->get_custom_templates();
$data = WP_Theme_JSON_Resolver::get_theme_data()->get_custom_templates();
$custom_templates = array();
if ( isset( $data ) ) {
foreach ( $data as $key => $template ) {
Expand Down
10 changes: 4 additions & 6 deletions lib/global-styles.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,7 @@ function gutenberg_experimental_global_styles_enqueue_assets() {
$settings = gutenberg_get_common_block_editor_settings();
$theme_support_data = gutenberg_experimental_global_styles_get_theme_support_settings( $settings );

$resolver = new WP_Theme_JSON_Resolver();
$all = $resolver->get_origin( $theme_support_data );
$all = WP_Theme_JSON_Resolver::get_merged_data( $theme_support_data );

$stylesheet = gutenberg_experimental_global_styles_get_stylesheet( $all );
if ( empty( $stylesheet ) ) {
Expand Down Expand Up @@ -201,16 +200,15 @@ function gutenberg_experimental_global_styles_settings( $settings ) {
unset( $settings['fontSizes'] );
unset( $settings['gradients'] );

$resolver = new WP_Theme_JSON_Resolver();
$origin = 'theme';
$origin = 'theme';
if (
WP_Theme_JSON_Resolver::theme_has_support() &&
gutenberg_is_fse_theme()
) {
// Only lookup for the user data if we need it.
$origin = 'user';
}
$tree = $resolver->get_origin( $theme_support_data, $origin );
$tree = WP_Theme_JSON_Resolver::get_merged_data( $theme_support_data, $origin );

// STEP 1: ADD FEATURES
//
Expand All @@ -234,7 +232,7 @@ function_exists( 'gutenberg_is_edit_site_page' ) &&
gutenberg_is_fse_theme()
) {
$user_cpt_id = WP_Theme_JSON_Resolver::get_user_custom_post_type_id();
$base_styles = $resolver->get_origin( $theme_support_data, 'theme' )->get_raw_data();
$base_styles = WP_Theme_JSON_Resolver::get_merged_data( $theme_support_data, 'theme' )->get_raw_data();

$settings['__experimentalGlobalStylesUserEntityId'] = $user_cpt_id;
$settings['__experimentalGlobalStylesBaseStyles'] = $base_styles;
Expand Down
2 changes: 1 addition & 1 deletion phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ function test_get_stylesheet() {
),
'misc' => 'value',
),
'core/group' => array(
'core/group' => array(
'custom' => array(
'base-font' => 16,
'line-height' => array(
Expand Down