-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Merged data should consider origin to return early #45969
Merged
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f2b0c16
Bail early for given origin
oandregal 4683653
Remove method from experimental, it is stable
oandregal 1eabb78
Only call set spacing presets when necessary
oandregal 845495d
Add test coverage per origin
oandregal 3a5602a
Add blocks origin test and improve them all
oandregal 60ec9b7
Extract utility functions
oandregal 40bb25c
Improve block styles check
oandregal 795319a
Clean cached data
oandregal 6865e70
Fix lint issues
oandregal 1de4692
Add the spacing sizes generation back in before final return
glendaviesnz 3b0e7bf
Update phpunit/class-wp-theme-json-resolver-test.php
oandregal 75c1202
Fix suggestions
oandregal 7228291
Use data provider in test
oandregal 6d06e33
Inline utility functions
oandregal 07903d3
Fix lint issues
oandregal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -165,47 +165,4 @@ private static function remove_JSON_comments( $array ) { | |
return $array; | ||
} | ||
|
||
/** | ||
* 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. | ||
* | ||
* 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. | ||
* | ||
* 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. | ||
* | ||
* @since 5.8.0 | ||
* @since 5.9.0 Added user data, removed the `$settings` parameter, | ||
* added the `$origin` parameter. | ||
* | ||
* @param string $origin Optional. To what level should we merge data. | ||
* Valid values are 'theme' or 'custom'. Default 'custom'. | ||
* @return WP_Theme_JSON | ||
*/ | ||
public static function get_merged_data( $origin = 'custom' ) { | ||
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. I don't know why this method was part of |
||
if ( is_array( $origin ) ) { | ||
_deprecated_argument( __FUNCTION__, '5.9' ); | ||
} | ||
|
||
$result = new WP_Theme_JSON_Gutenberg(); | ||
$result->merge( static::get_core_data() ); | ||
$result->merge( static::get_block_data() ); | ||
$result->merge( static::get_theme_data() ); | ||
if ( 'custom' === $origin ) { | ||
$result->merge( static::get_user_data() ); | ||
} | ||
// Generate the default spacing sizes presets. | ||
$result->set_spacing_sizes(); | ||
|
||
return $result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
So depending on origin, this function is you more and less information. This is very confusing and even the comment doesnt help explain why this is done at all.
First of all, this is confusing, as the naming seems a little off. Origin means what in this context? Why would you only want to load parts of this data? Why not load the lot?
Instead of param, why not function names, like
get_theme_data_for_blocks,
get_theme_data_for_user,
get_theme_data_for_theme.
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.
That's how the domain works: depending on some context, we need one piece of data or another. Existing use cases are editor vs front-end, or themes with or without theme.json.
Note that the way this is set up is prior to this PR. This PR only fixes the existing behavior. Whether this could have been done differently is out of scope for this PR.
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.
I agree with @spacedmonkey that it would be better to have separate functions (or even classes) for each type of origin:
or
At the same time, I understand @oandregal 's point that the goal of this PR is mainly refactoring/optimization.
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.
That's a conversation separate from this PR. I also want to share my perspective on that: as a collective, we have limited number of hours to produce work, so I tend to favor work that has an impact on users or developer experience. Given this class is private and consumers should use the public methods (see), I'd personally refrain from refactoring for refactor's sake. If anything, we should aim to remove/deprecate these class (and its methods) entirely.
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.
Also, note that there already are separate methods to get the individual origins:
The
get_merged_data
method implements the algorithm that merges the sources together.