Skip to content

Conversation

@sgomes
Copy link
Contributor

@sgomes sgomes commented Jan 13, 2026

What?

Currently, the Duotone metadata initialization runs on wp_loaded. This PR changes it to instead run the first time the metadata is needed.

Why?

Computing the Duotone metadata requires running WP_Theme_JSON_Resolver_Gutenberg::get_merged_data(), which performs a number of expensive operations across several large JSON tree structures that get loaded in from the filesystem. While WP_Theme_JSON_Resolver_Gutenberg::get_merged_data() relies on caching to reduce the amount of work in subsequent calls, it still takes a while to run the first time it's called for a pageview. Consequently, we want it to run as late as possible, so that we can exit before it does, if it's not needed.

This PR defers the Duotone metadata computations so that they happen on-demand. While this isn't enough to avoid the expensive work in most cases, it does mean that in some special cases (such as in some PHP early exit scenarios) they can be completely avoided. It also lays the groundwork for potential future improvements that avoid them in further scenarios.

How?

This PR adds two getters with the responsibility of initializing the internal class variables, and moves all class code to using the getters instead of accessing the variables directly.

Note that despite these changes, it's still possible to call the setter methods to explicitly initialize the variables, which means that any existing external code that happens to call WP_Duotone_Gutenberg::set_global_styles_presets or WP_Duotone_Gutenberg::set_global_style_block_names will continue to work correctly.

Note to reviewers: Since we can't rely on PHP 8.4's property hooks yet (which would be my preferred implementation), I decided to add a simple getter with the same name as the class variable, while prepending the class variable with __ to discourage direct use. I'm happy to consider any other approach, especially if I overlooked a more idiomatic technique.

Testing Instructions

Smoke-test the Duotone feature and ensure that it continues to work correctly, by adding a Duotone filter to an image block and ensuring the page loads correctly, with the filter correctly applied.

@sgomes sgomes requested a review from spacedmonkey as a code owner January 13, 2026 15:50
@github-actions
Copy link

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.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: sgomes <sergiomdgomes@git.wordpress.org>

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

@sgomes sgomes added [Feature] Blocks Overall functionality of blocks [Type] Performance Related to performance efforts labels Jan 13, 2026
@sgomes
Copy link
Contributor Author

sgomes commented Jan 13, 2026

While WP_Duotone_Gutenberg doesn't exist in Core, if this approach is found desirable we may want to port it to WP_Duotone.

I'm happy to either add a backport entry or add the No Core Sync Required label to the PR, depending on what you think is the right thing to do.

@github-actions
Copy link

Flaky tests detected in 7ab0d6c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/20963136256
📝 Reported issues:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Blocks Overall functionality of blocks [Type] Performance Related to performance efforts

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants