-
Notifications
You must be signed in to change notification settings - Fork 846
Google Fonts: Enqueue fonts used in block and Global Styles #23958
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5363704
Google Fonts Provider: enqueue used fonts
creativecoder b27808d
changelog
creativecoder 3d77dc2
Merge branch 'master' into update/enqueue-used-google-fonts
creativecoder d3a1947
Refactor to account for gutenberg#39988 not being merged, yet.
creativecoder 686ef9f
Update versions
creativecoder 0892fba
lock files
sdixon194 5c4a10c
composer.lock
sdixon194 d9e7c1c
undo pnpm lock change
sdixon194 fafe801
Merge remote-tracking branch 'origin/master' into update/enqueue-used…
sdixon194 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
projects/packages/google-fonts-provider/changelog/update-enqueue-used-google-fonts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: added | ||
|
|
||
| Add functions for enqueueing fonts used in block and global style settings |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
projects/packages/google-fonts-provider/src/introspectors/class-blocks.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| <?php | ||
| /** | ||
| * Google Fonts package Blocks fonts introspector class file. | ||
| * | ||
| * @package automattic/jetpack-google-fonts-provider | ||
| */ | ||
|
|
||
| namespace Automattic\Jetpack\Fonts\Introspectors; | ||
|
|
||
| use Automattic\Jetpack\Fonts\Utils; | ||
|
|
||
| /** | ||
| * Blocks fonts introspector. | ||
| */ | ||
| class Blocks { | ||
| /** | ||
| * Enqueue fonts used for block typography settings. | ||
| * | ||
| * @filter pre_render_block | ||
| * | ||
| * @param string|null $content The pre-rendered content. Default null. | ||
| * @param array $parsed_block The block being rendered. | ||
| */ | ||
| public static function enqueue_block_fonts( $content, $parsed_block ) { | ||
| if ( ! is_admin() && isset( $parsed_block['attrs']['fontFamily'] ) ) { | ||
|
|
||
| $block_font_family = $parsed_block['attrs']['fontFamily']; | ||
| $font_is_registered = Utils::is_font_family_registered( $block_font_family ); | ||
|
|
||
| if ( $font_is_registered ) { | ||
| wp_enqueue_webfont( $block_font_family ); | ||
| } | ||
| } | ||
|
|
||
| return $content; | ||
| } | ||
| } |
115 changes: 115 additions & 0 deletions
115
projects/packages/google-fonts-provider/src/introspectors/class-global-styles.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,115 @@ | ||
| <?php | ||
| /** | ||
| * Google Fonts package Global Styles fonts introspector class file. | ||
| * | ||
| * @package automattic/jetpack-google-fonts-provider | ||
| */ | ||
|
|
||
| namespace Automattic\Jetpack\Fonts\Introspectors; | ||
|
|
||
| use Automattic\Jetpack\Fonts\Utils; | ||
|
|
||
| /** | ||
| * Global Styles fonts introspector. | ||
| */ | ||
| class Global_Styles { | ||
| /** | ||
| * Enqueue fonts used in global styles settings. | ||
| * | ||
| * @return void | ||
| */ | ||
| public static function enqueue_global_styles_fonts() { | ||
| if ( is_admin() || ! function_exists( 'wp_enqueue_webfont' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $global_styles_fonts = self::collect_fonts_from_global_styles(); | ||
|
|
||
| foreach ( $global_styles_fonts as $font ) { | ||
| $font_is_registered = Utils::is_font_family_registered( $font ); | ||
|
|
||
| if ( $font_is_registered ) { | ||
| wp_enqueue_webfont( $font ); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Collect fonts set in Global Styles settings. | ||
| * | ||
| * @return array Font faces from Global Styles settings. | ||
| */ | ||
| public static function collect_fonts_from_global_styles() { | ||
| if ( ! function_exists( 'gutenberg_get_global_styles' ) ) { | ||
| return array(); | ||
| } | ||
|
|
||
| $global_styles = gutenberg_get_global_styles(); | ||
|
|
||
| $found_webfonts = array(); | ||
|
|
||
| // Look for fonts in block presets... | ||
| if ( isset( $global_styles['blocks'] ) ) { | ||
| foreach ( $global_styles['blocks'] as $setting ) { | ||
| $font_slug = self::extract_font_slug_from_setting( $setting ); | ||
|
|
||
| if ( $font_slug ) { | ||
| $found_webfonts[] = $font_slug; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Look for fonts in HTML element presets... | ||
| if ( isset( $global_styles['elements'] ) ) { | ||
| foreach ( $global_styles['elements'] as $setting ) { | ||
| $font_slug = self::extract_font_slug_from_setting( $setting ); | ||
|
|
||
| if ( $font_slug ) { | ||
| $found_webfonts[] = $font_slug; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Check if a global typography setting was defined. | ||
| $font_slug = self::extract_font_slug_from_setting( $global_styles ); | ||
|
|
||
| if ( $font_slug ) { | ||
| $found_webfonts[] = $font_slug; | ||
| } | ||
|
|
||
| return $found_webfonts; | ||
| } | ||
|
|
||
| /** | ||
| * Extract the font family slug from a settings array. | ||
| * | ||
| * @param array $setting The settings object. | ||
| * | ||
| * @return string|null | ||
| */ | ||
| protected static function extract_font_slug_from_setting( $setting ) { | ||
| if ( ! isset( $setting['typography']['fontFamily'] ) ) { | ||
| return null; | ||
| } | ||
|
|
||
| $font_family = $setting['typography']['fontFamily']; | ||
|
|
||
| // Full string: var(--wp--preset--font-family--slug). | ||
| // We do not care about the origin of the font, only its slug. | ||
| preg_match( '/font-family--(?P<slug>.+)\)$/', $font_family, $matches ); | ||
|
|
||
| if ( isset( $matches['slug'] ) ) { | ||
| return $matches['slug']; | ||
| } | ||
|
|
||
| // Full string: var:preset|font-family|slug | ||
| // We do not care about the origin of the font, only its slug. | ||
| preg_match( '/font-family\|(?P<slug>.+)$/', $font_family, $matches ); | ||
|
|
||
| if ( isset( $matches['slug'] ) ) { | ||
| return $matches['slug']; | ||
| } | ||
|
|
||
| return $font_family; | ||
| } | ||
| } | ||
102 changes: 102 additions & 0 deletions
102
projects/packages/google-fonts-provider/tests/php/test-blocks-font-introspector.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| <?php | ||
| /** | ||
| * Tests the Blocks fonts introspector. | ||
| * | ||
| * @package automattic/jetpack-google-fonts-provider | ||
| */ | ||
|
|
||
| use Automattic\Jetpack\Fonts\Introspectors\Blocks; | ||
| use Brain\Monkey; | ||
| use Brain\Monkey\Functions; | ||
| use PHPUnit\Framework\TestCase; | ||
|
|
||
| /** | ||
| * Blocks fonts introspector test suite. | ||
| */ | ||
| class Test_Blocks_Font_Introspector extends TestCase { | ||
| /** | ||
| * WP_Webfont instance. | ||
| * | ||
| * @var WP_Webfont | ||
| */ | ||
| protected $webfonts; | ||
|
|
||
| /** | ||
| * Test setup. | ||
| * | ||
| * @before | ||
| */ | ||
| public function set_up() { | ||
| Monkey\setUp(); | ||
|
|
||
| $this->webfonts = \Mockery::mock( 'WP_Webfonts' ); | ||
|
|
||
| Functions\stubs( | ||
| array( | ||
| 'is_admin' => false, | ||
| 'wp_webfonts' => $this->webfonts, | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that a font family in block attributes is enqueued. | ||
| */ | ||
| public function test_enqueues_block_font_when_set() { | ||
| $content = 'foo'; | ||
| $parsed_block = array( | ||
| 'attrs' => array( | ||
| 'fontFamily' => 'Roboto', | ||
| ), | ||
| ); | ||
|
|
||
| $this->webfonts | ||
| ->shouldReceive( 'get_registered_webfonts' ) | ||
| ->andReturn( array( 'roboto' => array() ) ); | ||
|
|
||
| Functions\expect( 'wp_enqueue_webfont' ) | ||
| ->once() | ||
| ->with( 'Roboto' ); | ||
|
|
||
| $this->assertEquals( $content, Blocks::enqueue_block_fonts( $content, $parsed_block ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Test that a block without font settings still returns the filtered content. | ||
| */ | ||
| public function test_does_not_enqueue_block_font_when_not_set() { | ||
| $content = 'foo'; | ||
| $parsed_block = array(); | ||
|
|
||
| Functions\expect( 'wp_enqueue_webfont' ) | ||
| ->never(); | ||
|
|
||
| $this->assertEquals( $content, Blocks::enqueue_block_fonts( $content, $parsed_block ) ); | ||
| } | ||
|
|
||
| /** | ||
| * Test teardown. | ||
| * | ||
| * @after | ||
| */ | ||
| public function tear_down() { | ||
| Monkey\tearDown(); | ||
| } | ||
| } | ||
|
|
||
| // phpcs:disable | ||
|
|
||
| /** | ||
| * Use stub so that method_exists checks will pass. | ||
| * | ||
| * This will not be needed if/when WP_Webfonts provides a check for | ||
| * is_font_family_registered(). | ||
| * | ||
| * @link https://github.com/WordPress/gutenberg/pull/39988 | ||
| * @link https://github.com/WordPress/gutenberg/blob/e94fffae0684aa5a6dc370ce3eba262cb77071d9/lib/experimental/class-wp-webfonts.php#L217 | ||
| */ | ||
| class WP_Webfonts { | ||
| public static function get_font_slug( $font ) { | ||
| return strtolower( $font ); | ||
| } | ||
| } |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Non-blocking: This might be a silly question, but would you mind explaining theis_admin()condition here? I thought that we wanted to enqueue all fonts in the site editor? I'm assuming that it's already being done elsewhere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Answering now for posterity (sorry I missed this initially!): this is because the all the fonts are already enqueued in wp-admin views (at least in the editor) by Gutenberg, so there's no need to run this function in that context.