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.
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. WhileWP_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_presetsorWP_Duotone_Gutenberg::set_global_style_block_nameswill continue to work correctly.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.