-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add backward compat shim for WP_Theme_JSON_Data::get_theme_json #6626
Add backward compat shim for WP_Theme_JSON_Data::get_theme_json #6626
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the Core Committers: Use this line as a base for the props when committing in SVN:
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
Updated the PR to address merge conflicts |
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.
nicely solved
@@ -172,8 +172,18 @@ public static function get_core_data() { | |||
* | |||
* @param WP_Theme_JSON_Data $theme_json Class to access and update the underlying data. | |||
*/ | |||
$theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) ); | |||
static::$core = $theme_json->get_theme_json(); | |||
$theme_json = apply_filters( 'wp_theme_json_data_default', new WP_Theme_JSON_Data( $config, 'default' ) ); |
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.
Hi, is anyone able to reproduce this error?
I cannot repro the issue reported using latest WordPress trunk
and Gutenberg 14.8.1 (the last release prior to updating WP_Theme_JSON_Data_Gutenberg
). I'd like to understand this better before jumping into implementation.
The only way I can think to reproduce the issue is by not using the provided structure to update the data as a consumer of this filter. This is, instead of doing:
add_filter( 'wp_theme_json_data_*', function( $theme) {
$new_data = array( /* ... */ );
return $theme->update_with( $new_data );
});
Your plugin does something like:
add_filter( 'wp_theme_json_data_*', function( $theme) {
/* your plugin code */
return $your_custom_structure;
});
Note that this is not supported or recommended. The update_with
method is a requirement to use these filters, as stated in the devnote, because it makes sure everything works correctly (data migration, sanitization, etc.). This is what happened to @ryelle, as she reported.
Is there any other use case?
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.
To be clear, I'm not against doing it, but I'm reluctant to given that it only happens if a plugin provides their own structure, which is not supported or recommended.
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.
The issue can be reproduced using the Blocks Everywhere plugin, which does the following, basically:
add_filter( 'wp_theme_json_data_theme', function( $theme) {
$theme = new \WP_Theme_JSON_Data_Gutenberg( /* ... */ );
return $theme;
} );
Which I suppose is incorrect according to the dev note, but it did work before the recent change.
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.
@oandregal I believe this was only reproducible prior to WP_Theme_JSON_Data_Gutenberg::get_theme_json
being added in version 18.3.0, which shipped after the core change. If we don't need to worry about WP 6.6 compat with any Gutenberg versions prior to 18.3.0, then we can probably close this issue as wontfix
.
However, I think this use case is instructive for us as we consider better ways of managing the WP_Theme_JSON_
classes between the GB repo and the WP-dev repo to avoid these types of conflicts.
I've updated the approach here, based on conversation in this thread to ensure that when an object returned from these filters that are not |
This looks good :-) |
* Backward compatibility for extenders returning a WP_Theme_JSON_Data | ||
* compatible class that is not a WP_Theme_JSON_Data object. | ||
*/ | ||
if ( is_a( $theme_json, 'WP_Theme_JSON_Data' ) ) { |
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.
This should use the instanceof
keyword, see https://core.trac.wordpress.org/changeset/56352.
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.
Thanks! I had a sense that we preferred instanceof
but couldn't remember the context. Do we not have a sniff for this?
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.
Addressed in 544d762
Committed in 58443. |
This checks for cases where extenders are returning an object other than
WP_Theme_JSON_Data
from filters and regenerates theWP_Theme_JSON
object if so to avoid issues like this one or #6271 (comment).Trac ticket: https://core.trac.wordpress.org/ticket/61112
This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.