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

Make Editor and Frontend reflect Block Styles in theme.json #3408

Closed

Conversation

ockham
Copy link
Contributor

@ockham ockham commented Oct 5, 2022

Based on @c4rl0sbr4v0's #3401, and @oandregal's suggestion.

Description

Currently, individual block styling defined in a theme's theme.json isn't being applied on the frontend (see https://core.trac.wordpress.org/ticket/56736 or the testing instructions below).

While working on WordPress/gutenberg#44434 (which is somewhat related), we've identified WP_Theme_JSON_Resolver's caching of theme data as the problem. That caching happens early and sanitizes block-specific styling found in theme.json against the list of registered blocks. However, this can happen before all blocks are registered. As a consequence, some styling is scrapped.

This can be fixed by removing the caching from WP_Theme_JSON_Resolver::get_theme_data(), and instead freshly computing theme data each time that method is called, which will take into account all blocks registered at that point.

In order to prevent performance from being impacted all too much, we add caching to the JSON file reading and processing in WP_Theme_JSON_Resolver::read_json_file() instead (per this suggestion).

Testing instructions

To test, activate the TT2 theme. In a new post, insert some button blocks and view on the frontend. They should be rectangular, with a dark green background (rather than round and black).

Before (broken) After (fixed)
image image

Trac ticket: https://core.trac.wordpress.org/ticket/56736


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.

@ockham ockham marked this pull request as ready for review October 5, 2022 13:34
@ockham ockham self-assigned this Oct 5, 2022
@ockham
Copy link
Contributor Author

ockham commented Oct 5, 2022

cc/ @andrewserong @oandregal @ndiego @c4rl0sbr4v0 for review

@ockham ockham changed the title Fix/wo theme json resolver caching Make Editor and Frontend reflect Block Styles in theme.json Oct 5, 2022
@ockham
Copy link
Contributor Author

ockham commented Oct 5, 2022

As a follow-up, we could consider caching WP_Theme_JSON_Resolver::translate() for some more performance gains. (We might wanna use a checksum generated from its $theme_json arg as cache key.)

Alternatively, we could explore introducing a new, cached, method that calls both read_json_file and translate after each other, since those seem to almost always occur in sequence.

Copy link
Contributor

@cbravobernal cbravobernal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM and works as expected 🚀

ockham and others added 3 commits October 5, 2022 16:14
Co-authored-by: David B <dream-encode@users.noreply.github.com>
Co-authored-by: David B <dream-encode@users.noreply.github.com>
Copy link

@andrewserong andrewserong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is testing well for me, too, and it looks like it nicely implements the suggestion from @oandregal. Good idea using an array and storing the parsed file by $file_path key 👍

✅ TwentyTwentyTwo button styling is now output correctly
✅ Saving changes to global styles still works as expected in the site editor, and overrides values set set at the theme's theme.json
✅ Visually confirmed the code now reduces the scope of caching for get_theme_data to just read_json_file, via moving the caching to that function

Alternatively, we could explore introducing a new, cached, method that calls both read_json_file and translate after each other, since those seem to almost always occur in sequence.

I like that idea. But in principle I think this fix is a really good one to go with — it reduces the caching to what we know we can safely cache, and opens the path to incrementally adding caching in future follow-ups, where it'll be possible to tease apart the reasoning as to which pieces require caching, and what the potential implications might be.

LGTM! ✨

}
} else {
return array();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're missing one path here: if the decoded file is not an array.

@oandregal
Copy link
Member

oandregal commented Oct 6, 2022

We also need to remove the caching in get_core_data and get_user_data. I'm looking at pushing changes to this PR. I have them locally, have to coordinate with Bernie for permissions.

Edit: the changes are pushed and this is ready.

Copy link
Member

@aristath aristath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@dream-encode
Copy link
Contributor

Merged into core in https://core.trac.wordpress.org/changeset/54399.

@ockham
Copy link
Contributor Author

ockham commented Oct 7, 2022

Unfortunately, it seems like this introduced a major performance issue after all: WordPress/gutenberg#44772 (comment) 😕

@oandregal
Copy link
Member

oandregal commented Oct 7, 2022

As I mentioned WordPress/gutenberg#44772 (comment) it'd be good to have some before/after numbers (ms) as to know whether this is something we need to act for 6.1 or can be tackled after. Ari mentioned he's into that (my setup is malfunctioning).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

6 participants