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

[Fonts API] Refactor Theme JSON global functions into WP_Fonts_Resolver #50811

Merged

Conversation

anton-vlasenko
Copy link
Contributor

@anton-vlasenko anton-vlasenko commented May 20, 2023

What?

There are 2 global functions within the lib/experimental/fonts-api/register-fonts-from-theme-json.php file that will be needed in Core as part of the API. These functions are large and doing multiple tasks, as such are difficult to debug and understand.

Why?

To improve code quality, readability, and prepare these functionality for future and Core, lib/experimental/fonts-api/register-fonts-from-theme-json.php and its functions need a redesign / refactoring. It's a better fit for a Core-only static class wrapper pattern (i.e. a class with static methods).

How?

  • Rename the file to class-wp-fonts-theme-json-handler.php.
  • Load the file into memory.
  • Add a Core-only static class wrapper called WP_Fonts_Theme_Json_Handler.
  • Add a public static method, coping the code from each global function.
  • Split each method into smaller single-purpose private static methods.
  • Update the tests.
  • Move the add_action( 'init', 'gutenberg_register_fonts_from_theme_json', 21 ); into the fonts-api.php file and update it to use the new static method instead of the global callback function.
  • Update where gutenberg_add_registered_fonts_to_theme_json() is used to now call its static method.
  • Update the DocBlocks as they are inaccurate.

Testing Instructions

Manual Testing

How these functions work and what they do did not change. This means the functionality should work exactly as it did for, i.e. scope of this PR should be a pure refactoring. Manual testing is focused on ensuring no changes happened to the functionality, i.e. works the same as in trunk.

Setting up your local test site:

  1. Make sure Gutenberg is activated: Plugins > Gutenberg.
  2. Install and activate this test plugin.
  3. Activate the TT3, if it is not already.

Testing the theme's fonts get automatically registered and enqueued into the Fonts API:

  1. Open the Site Editor > Styles > Typography UI.
  2. Select Heading.
  3. In the Fonts selector, verify the theme's fonts are present for selection.
  4. Go to the front-end.
  5. In your browser's dev tools in the Inspector (HTML) tab, search for wp-fonts-local. Expectation: It should be in the <head> and contain the @font-face styles for the theme's fonts.

Testing plugin fonts are added to the Theme JSON data layer:

  1. Open the Site Editor > Styles > Typography UI.
  2. Select Heading.
  3. Select a Google Font for all headings:
    • For Fonts: select Playfair Display.
    • For Appearance: select Bold Italic.
  4. Select the "Save" button twice, which saves your user selections.
  5. Go to the front-end and refresh the page.
  6. In your browser's dev tools in the Inspector (HTML) tab:
    • Search for wp-fonts-jetpack-google-fonts. Expectation: The <style> element should be in the <head> and the CSS in it should only contain @font-face styles for Playfair Display.
    • Search for wp-fonts-local. Expectation: It should be in the <head> and contain the @font-face styles for the theme's fonts.

Automated Testing

Run the following:

npm run test:unit:php -- --group fontsapi

All tests should pass.

@github-actions
Copy link

github-actions bot commented May 20, 2023

Flaky tests detected in c146215.
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/5083809805
📝 Reported issues:

@anton-vlasenko anton-vlasenko changed the title Update/refactor register fonts from theme json php [Fonts API] Refactor theme.json to Fonts API handler global functions into static class wrapper May 20, 2023
@anton-vlasenko anton-vlasenko marked this pull request as ready for review May 22, 2023 08:42
@anton-vlasenko anton-vlasenko requested review from hellofromtonya, ironprogrammer and aristath and removed request for aristath May 22, 2023 08:49
@hellofromtonya hellofromtonya removed the request for review from spacedmonkey May 22, 2023 14:00
*
* @since X.X.X
*/
class WP_Fonts_Theme_Json_Handler {
Copy link
Contributor

@hellofromtonya hellofromtonya May 22, 2023

Choose a reason for hiding this comment

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

I'm rethinking this new class. Since I first wrote the issue's scope, a new resolver class WP_Fonts_Resolver was merged into the API. I'm thinking the static functions here can be merged into that resolver class instead. Why? These methods are doing resolver work. Rather than adding another class, I propose consolidating the functionality into the resolver.

To retain @anton-vlasenko's work, I'll open a new PR but cherry-pick Anton's work as a starting point.

Copy link
Contributor

@hellofromtonya hellofromtonya left a comment

Choose a reason for hiding this comment

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

Blocking this PR from merge. A new PR will be opened to consolidate the functionality into the new WP_Fonts_Resolver class.

@hellofromtonya
Copy link
Contributor

hellofromtonya commented May 25, 2023

As a POC (proof of concept), I created PR #50914. It does the following:

  • Moves the code into the existing WP_Fonts_Resolver class instead of a new class.
  • Does more refactoring for readability and reuse.
  • Moves the registered "origin" to the WP_Fonts as a constant to encapsulate that knowledge in one place.
  • Renames one method to better reflect what it does: add_missing_fonts rather than add_registered_fonts.
  • A bit of girlscouting for adding @since, changing "webfont" to "font", etc.

I'll now add each of the changes to this PR to keep it all in the original work @anton-vlasenko created.

Tasks:

  • Rebase this PR on top of trunk to bring in the latest changes including the new WP_Fonts_Resolver.
  • Move the code to WP_Fonts_Resolver.
  • Refactor additional pieces.
  • Rename to ::add_missing_fonts_to_theme_json().
  • Girlscouting.

@hellofromtonya hellofromtonya force-pushed the update/refactor-register-fonts-from-theme-json-php branch from 1f949e0 to 6411d24 Compare May 25, 2023 17:55
Which fonts get added to the Theme JSON data depends on
if in the Site Editor or not.

Site Editor = registered fonts.
Else = enqueued fonts.

Renaming this method better reflects what it does.
Why?

To centralize what the origin value is in one place, rather than
dispersing the internal details outside of the class.
@hellofromtonya hellofromtonya self-requested a review May 25, 2023 19:41
@hellofromtonya
Copy link
Contributor

Test Report

Env:

  • OS: macOS
  • Browser: Chrome, Firefox, Edge
  • LocalHost: Local
  • WP: 6.2.2
  • Plugins: Gutenberg and fonts test plugin
  • Theme: TT3

Results

Testing the theme's fonts get automatically registered and enqueued into the Fonts API

style#wp-fonts-local is in the <head> in the front-end and each iframed editor.

✅ All of the TT3 fonts have @font-face styles in that element.

Testing plugin fonts are added to the Theme JSON data layer

In the Site Editor:
✅ Theme and test plugin's fonts are available for selection.

✅ After selecting the Playfair Display for headings and refreshing on the front-end:

  • style#wp-fonts-jetpack-google-fonts is in the <head> and the CSS in it only contains @font-face styles for Playfair Display.

  • style#wp-fonts-local is in the <head> and the CSS contains all of TT3 fonts @font-face styles.

Conclusion

✅ Works as expected

Copy link
Contributor

@hellofromtonya hellofromtonya left a comment

Choose a reason for hiding this comment

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

@anton-vlasenko the work you did was excellent. Well done 👏

With the new WP_Fonts_Resolver, I moved your work into it, which eliminated the need for another class. Sorry for the confusion.

I've updated the PR accordingly and made a couple more refactorings.

All of the PHPUnit tests are passing ✅

Manual testing works as expected

This PR is ready for merging.

@ironprogrammer
Copy link
Contributor

Test Report

Environment

  • Hardware: MacBook Pro Apple M1 Pro
  • OS: macOS 13.3.1
  • Browser: Safari 16.4
  • Server: nginx/1.23.4
  • PHP: 7.4.33
  • WordPress: 6.3-alpha-55505-src
  • Theme: twentytwentythree v1.1
  • Active Plugins:
    • webfonts-jetpack-test v1.0.0-ironprogrammer
    • gutenberg v15.9.0-rc.1 (PR applied)

Actual Results

With TT3 only:

  • ✅ In Site Editor > Styles > Typography, font pickers for each Element contain the theme's registered fonts.
  • ✅ In the frontend, head>script#wp-fonts-local contains the theme's registered fonts.

With TT3 and the fonts test plugin, setting the global typography for headings and text:

  • ✅ In Site Editor > Styles > Typography, font pickers for each Element contain the theme's registered fonts, plus the fonts registered by the test plugin.
  • ✅ In the frontend, head>script#wp-fonts-local contains the theme's registered fonts.
  • ✅ In the frontend, head>script#wp-fonts-jetpack-google-fonts contains only the user-selected fonts.

Additional Information

Note that as expected, block-level Typography font pickers (in site editor and post editor) also contained all registered fonts, which when user-selected, were subsequently enqueued in the frontend. This is consistent with behavior on trunk, helping validate that functional behavior from this PR remains unchanged.

@hellofromtonya hellofromtonya merged commit b95148f into trunk May 25, 2023
@hellofromtonya hellofromtonya deleted the update/refactor-register-fonts-from-theme-json-php branch May 25, 2023 21:05
@hellofromtonya hellofromtonya changed the title [Fonts API] Refactor theme.json to Fonts API handler global functions into static class wrapper [Fonts API] Refactor Theme JSON global functions into WP_Fonts_Resolver May 25, 2023
@github-actions github-actions bot added this to the Gutenberg 16.0 milestone May 25, 2023
@anton-vlasenko
Copy link
Contributor Author

@anton-vlasenko the work you did was excellent. Well done 👏

Thank you, @hellofromtonya. I'm glad you like it.

BTW, I've noticed a typo in this PR.
WP_Fonts_Resolver::set_tyopgraphy_settings_array_structure should be renamed to WP_Fonts_Resolver::set_typography_settings_array_structure.

Probably, this doesn't deserve a separate PR and could be fixed in one of the following PRs (it's a private method anyway).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Type] Code Quality Issues or PRs that relate to code quality
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Fonts API] Refactor theme.json to Fonts API handler global functions into static class wrapper
4 participants