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] Relocate which fonts to print from script-loader into wp_print_fonts() #50140

Closed
hellofromtonya opened this issue Apr 27, 2023 · 0 comments · Fixed by #50151
Closed
Assignees
Labels
[Type] Code Quality Issues or PRs that relate to code quality [Type] Enhancement A suggestion for improvement.

Comments

@hellofromtonya
Copy link
Contributor

When printing fonts for the iframed editor assets, relocate the know-how of which fonts to print into the Fonts API wp_print_fonts() function.

Why?

For 6.3 compat, a recent change from PR #49655 invokes only wp_print_fonts(). This makes sense, as code outside of the API should not have to know the implementation details. That's the job of the API.

The code implemented in 6.2 compat needs to be relocated:

	/*
	 * Generate font @font-face styles for the iframe.
	 *
	 * For the Site Editor, use the registered fonts.
	 * For the Block Editor, use the enqueued fonts.
	 */
	if ( class_exists( 'WP_Fonts' ) ) {
		$wp_fonts       = wp_fonts();
		$registered     = $wp_fonts->get_registered_font_families();
		$is_site_editor = 'site-editor.php' === $pagenow;

		if ( ! empty( $registered ) ) {
			$done           = $wp_fonts->done;
			$wp_fonts->done = array();
			if ( $is_site_editor ) {
				$queue           = $wp_fonts->queue;
				$wp_fonts->queue = $registered;
			}

			ob_start();
			$wp_fonts->do_items();
			$styles .= ob_get_clean();

			// Reset the Fonts API.
			$wp_fonts->done = $done;
			if ( $is_site_editor ) {
				$wp_fonts->queue = $queue;
			}
		}
	}

Note: The above code is from #49646, which fixes a bug of which fonts to be printed.

In this way, the API's wp_print_fonts() is responsible for knowing which fonts to print:

  • When in the Site Editor, print all of the registered fonts. Why? For user previewing and typography selection.
  • Else, print only the enqueued fonts, i.e. the global fonts (which are the theme defined fonts and all user-selected fonts from Site Editor > Styles > Typography UI - global styles).

Impacts

Fixing #49645 is currently blocked as noted here by #40353. Care is needed on how to relocate the code.

Suggested Plan

I'd suggest the following to avoid the above impacts:

  1. Relocate the current code from the lib/compat/wordpress-6.2/script-loader.php into wp_print_fonts() and instead invoke wp_print_fonts() in the script loader.
  2. For 6.3 compat, consider a proper fix of #469645 but potentially do a WP version check.
  3. Then when fixing #469645, remove the WP version check.
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 [Type] Enhancement A suggestion for improvement.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants