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

Read only once the theme.json files from the styles/ folder. #6843

Conversation

oandregal
Copy link
Member

@oandregal oandregal commented Jun 17, 2024

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

What

Read only once the theme.json files stored in the styles/ folder.

Why

I've realized that, unlike the theme.json files defined by core & theme, we don't cache these — hence we end up reading them a few times from the filesystem.

How

By using the existing read_json_file function instead of using wp_json_file_decode directly.

How to test

  1. Verify the theme style variations are working as expected.

Go to "Site Editor > Styles" and apply one of them. Verify the changes are reflected in the front end. Do the same from the global styles sidebar in the site editor.

  1. Verify the block style variations defined via theme.json are working as expected.

Create a partial.json file within the styles/ folder with the following contents:

{
        "$schema": "https://schemas.wp.org/trunk/theme.json",
        "version": 2,
        "title": "Partial",
        "blockTypes": [ "core/group" ],
        "styles": {
                "color": {
                        "background": "aliceblue"
                }
        }
}

Go to any editor, add a group block, and verify there is a "Partial" style variation (Block Settings > Styles). Apply the variation and save the changes. Verify the contents are the expected (background color is aliceblue) — also in the front-end.

Commit

Commit message, to make it easy for committers:

Read only once the theme.json files stored in the `styles/` folder.

Props oandregal, aaronrobertshaw, joemcgill, ramonopoly, isabel_brison.
See #61451.

Copy link

github-actions bot commented Jun 17, 2024

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 props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props oandregal, aaronrobertshaw, joemcgill, ramonopoly, isabel_brison.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@oandregal oandregal self-assigned this Jun 17, 2024
@oandregal oandregal changed the title Cache reading theme.json files from styles/ folder. Read the theme.json files from the styles/ folder only once. Jun 17, 2024
@oandregal oandregal changed the title Read the theme.json files from the styles/ folder only once. Read only once the theme.json files from the styles/ folder. Jun 17, 2024
Copy link

Test using WordPress Playground

The 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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

Copy link
Member

@joemcgill joemcgill left a comment

Choose a reason for hiding this comment

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

Definitely a good idea to avoid rereading these same files. I wonder if we can also avoid the potential of processing the same decoded files multiple times as well?

@aaronrobertshaw
Copy link

I wonder if we can also avoid the potential of processing the same decoded files multiple times as well?

This PR was an initial step given issues encountered in an original attempt to also cache the additional processing of those files in: WordPress/gutenberg#62610

ramonjd added a commit to WordPress/gutenberg that referenced this pull request Jun 18, 2024
Cache reading theme.json files from styles/ folder.
@ramonjd
Copy link
Member

ramonjd commented Jun 18, 2024

@ramonjd
Copy link
Member

ramonjd commented Jun 18, 2024

I wonder if we can also avoid the potential of processing the same decoded files multiple times as well?

Very good question! I'll keep that in mind when testing WordPress/gutenberg#62638, maybe there's an way to test in the plugin first.

Copy link

@aaronrobertshaw aaronrobertshaw left a comment

Choose a reason for hiding this comment

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

LGTM 👍

✅ Theme style variations continue to work
✅ Block style variations are registered from partials correctly

Screen.Recording.2024-06-18.at.2.39.11.PM.mp4

Might be worth adding a note about the caching to the @since comment?

@aaronrobertshaw
Copy link

Might be worth adding a note about the caching to the @SInCE comment?

I've taken then liberty to push this minor tweak.

@tellthemachines
Copy link
Contributor

Just gave this a test run and switching styles and variations works as expected!

@tellthemachines
Copy link
Contributor

committed in r58428.

@oandregal oandregal deleted the update/cache-reading-files-from-filesystem branch June 18, 2024 07:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
5 participants