[Fonts API] Remove global from wp_print_fonts() #50225
Merged
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.
Partially fixes #50075.
Sets up decision making for which fonts to print for PR #50151.
What?
Removes the global check. Instead checks if any fonts are registered; if no, bails out.
Why?
The original design (being replaced by this PR) comes from
wp_print_styles()
.The global check exists to avoid instantiating
WP_Fonts
when invokingwp_print_fonts()
. In other words, ifwp_print_fonts()
is called before the API is used, it would bail out. This technique saves the overhead withinwp_fonts()
, i.e. instantiatingWP_Fonts
and registering the local provider. However, this overhead is tiny and, in itself, not worthy of having a global variable for this purpose.The function lacks a proper bail out check: if there are no fonts registered, then there's nothing to print. The global check assumed fonts are registered; however, this is a false assumption.
How?
Removes the global check.
Replaces it with
wp_fonts()->get_registered_font_families()
. If it returns empty, then there are no fonts registered to print. Even if handles are passed into thewp_print_fonts()
, those handles must be registered.Checking if no fonts are registered:
WP_Fonts::do_items()
stack.Testing Instructions
Visually Test:
Set up on your test site:
Before applying this PR:
wp-fonts-local
<style>
element.After applying this PR:
wp-fonts-local
<style>
element.To test the early bail out if no fonts are registered:
wp-content/mu-plugins/test.php
:An empty array should be displayed in your browser.
Running the automated tests
All tests should pass when running the PHPUnit tests: