Skip to content

Commit

Permalink
6.2: update get_merged_data
Browse files Browse the repository at this point in the history
  • Loading branch information
oandregal committed Dec 22, 2022
1 parent fe37493 commit a5bdc76
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 68 deletions.
37 changes: 26 additions & 11 deletions lib/class-wp-theme-json-resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -519,17 +519,23 @@ public static function get_user_data() {
/**
* Returns the data merged from multiple origins.
*
* There are three sources of data (origins) for a site:
* default, theme, and custom. The custom's has higher priority
* than the theme's, and the theme's higher than default's.
* There are four sources of data (origins) for a site:
*
* - default => WordPress
* - blocks => each one of the blocks provides data for itself
* - theme => the active theme
* - custom => data provided by the user
*
* The custom's has higher priority than the theme's, the theme's higher than blocks',
* and block's higher than default's.
*
* Unlike the getters
* {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_core_data/ get_core_data},
* {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_theme_data/ get_theme_data},
* and {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_user_data/ 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.
* (default, blocks, theme, custom), the last origin overrides the previous.
*
* For example, if the user has set a background color
* for the paragraph block, and the theme has done it as well,
Expand All @@ -540,8 +546,9 @@ public static function get_user_data() {
* added the `$origin` parameter.
* @since 6.1.0 Added block data and generation of spacingSizes array.
*
* @param string $origin Optional. To what level should we merge data.
* Valid values are 'theme' or 'custom'. Default 'custom'.
* @param string $origin Optional. To what level should we merge data:'default', 'blocks', 'theme' or 'custom'.
* 'custom' is used as default value as well as fallback value if the origin is unknown.
*
* @return WP_Theme_JSON
*/
public static function get_merged_data( $origin = 'custom' ) {
Expand All @@ -550,16 +557,24 @@ public static function get_merged_data( $origin = 'custom' ) {
}

$result = static::get_core_data();
if ( 'default' === $origin ) {
$result->set_spacing_sizes();
return $result;
}

$result->merge( static::get_block_data() );
$result->merge( static::get_theme_data() );
if ( 'blocks' === $origin ) {
return $result;
}

if ( 'custom' === $origin ) {
$result->merge( static::get_user_data() );
$result->merge( static::get_theme_data() );
if ( 'theme' === $origin ) {
$result->set_spacing_sizes();
return $result;
}

// Generate the default spacingSizes array based on the merged spacingScale settings.
$result->merge( static::get_user_data() );
$result->set_spacing_sizes();

return $result;
}

Expand Down
57 changes: 0 additions & 57 deletions lib/compat/wordpress-6.2/class-wp-theme-json-resolver-6-2.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,6 @@
*/
class WP_Theme_JSON_Resolver_6_2 extends WP_Theme_JSON_Resolver_Base {

/**
* Returns the data merged from multiple origins.
*
* There are four sources of data (origins) for a site:
*
* - default => WordPress
* - blocks => each one of the blocks provides data for itself
* - theme => the active theme
* - custom => data provided by the user
*
* The custom's has higher priority than the theme's, the theme's higher than blocks',
* and block's higher than default's.
*
* Unlike the getters
* {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_core_data/ get_core_data},
* {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_theme_data/ get_theme_data},
* and {@link https://developer.wordpress.org/reference/classes/wp_theme_json_resolver/get_user_data/ 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
* (default, blocks, theme, custom), the last origin overrides the previous.
*
* 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.
*
* @param string $origin Optional. To what level should we merge data:'default', 'blocks', 'theme' or 'custom'.
* 'custom' is used as default value as well as fallback value if the origin is unknown.
*
* @return WP_Theme_JSON
*/
public static function get_merged_data( $origin = 'custom' ) {
if ( is_array( $origin ) ) {
_deprecated_argument( __FUNCTION__, '5.9.0' );
}

$result = static::get_core_data();
if ( 'default' === $origin ) {
$result->set_spacing_sizes();
return $result;
}

$result->merge( static::get_block_data() );
if ( 'blocks' === $origin ) {
return $result;
}

$result->merge( static::get_theme_data() );
if ( 'theme' === $origin ) {
$result->set_spacing_sizes();
return $result;
}

$result->merge( static::get_user_data() );
$result->set_spacing_sizes();
return $result;
}

/**
* Returns the user's origin config.
*
Expand Down

0 comments on commit a5bdc76

Please sign in to comment.