-
Notifications
You must be signed in to change notification settings - Fork 843
Google Fonts: enqueue only picked font families in Gutenberg #23810
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
Changes from all commits
30563a2
0f4668d
279c275
8b31d4a
2896b93
9bab79d
b660425
b9c92a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| # Files not needed to be distributed in the package. | ||
| .gitattributes export-ignore | ||
| .github/ export-ignore | ||
| package.json export-ignore | ||
|
|
||
| # Files to include in the mirror repo, but excluded via gitignore | ||
| # Remember to end all directories with `/**` to properly tag every file. | ||
| # /src/js/example.min.js production-include | ||
|
|
||
| # Files to exclude from the mirror repo, but included in the monorepo. | ||
| # Remember to end all directories with `/**` to properly tag every file. | ||
| .gitignore production-exclude | ||
| changelog/** production-exclude | ||
| phpunit.xml.dist production-exclude | ||
| .phpcs.dir.xml production-exclude | ||
| tests/** production-exclude |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| vendor/ | ||
| node_modules/ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| <?xml version="1.0"?> | ||
| <ruleset> | ||
|
|
||
| <rule ref="WordPress.WP.I18n"> | ||
| <properties> | ||
| <property name="text_domain" type="array"> | ||
| <element value="jetpack-global-styles-webfonts-introspector" /> | ||
| </property> | ||
| </properties> | ||
| </rule> | ||
| <rule ref="Jetpack.Functions.I18n"> | ||
| <properties> | ||
| <property name="text_domain" value="jetpack-global-styles-webfonts-introspector" /> | ||
| </properties> | ||
| </rule> | ||
|
|
||
| <rule ref="WordPress.Utils.I18nTextDomainFixer"> | ||
| <properties> | ||
| <property name="old_text_domain" type="array" /> | ||
| <property name="new_text_domain" value="jetpack-global-styles-webfonts-introspector" /> | ||
| </properties> | ||
| </rule> | ||
|
|
||
| </ruleset> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| # Changelog | ||
|
|
||
| All notable changes to this project will be documented in this file. | ||
|
|
||
| The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) | ||
| and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # global-styles-webfonts-introspector | ||
|
|
||
| Looks for webfonts picked in global styles | ||
|
|
||
| ## How to install global-styles-webfonts-introspector | ||
|
|
||
| ### Installation From Git Repo | ||
|
|
||
| ## Contribute | ||
|
|
||
| ## Get Help | ||
|
|
||
| ## Security | ||
|
|
||
| Need to report a security vulnerability? Go to [https://automattic.com/security/](https://automattic.com/security/) or directly to our security bug bounty site [https://hackerone.com/automattic](https://hackerone.com/automattic). | ||
|
|
||
| ## License | ||
|
|
||
| global-styles-webfonts-introspector is licensed under [GNU General Public License v2 (or later)](./LICENSE.txt) | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: added | ||
|
|
||
| Add Google fonts global styles introspection package. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| { | ||
| "name": "automattic/jetpack-global-styles-webfonts-introspector", | ||
| "description": "Looks for webfonts picked in global styles", | ||
| "type": "jetpack-library", | ||
| "license": "GPL-2.0-or-later", | ||
| "require": {}, | ||
| "require-dev": { | ||
| "yoast/phpunit-polyfills": "1.0.3", | ||
| "automattic/jetpack-changelogger": "^3.0" | ||
| }, | ||
| "autoload": { | ||
| "classmap": [ | ||
| "src/" | ||
| ] | ||
| }, | ||
| "scripts": { | ||
| "phpunit": [ | ||
| "./vendor/phpunit/phpunit/phpunit --colors=always" | ||
| ], | ||
| "test-coverage": [ | ||
| "php -dpcov.directory=. ./vendor/bin/phpunit --coverage-clover \"$COVERAGE_DIR/clover.xml\"" | ||
| ], | ||
| "test-php": [ | ||
| "@composer phpunit" | ||
| ] | ||
| }, | ||
| "repositories": [ | ||
| { | ||
| "type": "path", | ||
| "url": "../../packages/*", | ||
| "options": { | ||
| "monorepo": true | ||
| } | ||
| } | ||
| ], | ||
| "minimum-stability": "dev", | ||
| "prefer-stable": true, | ||
| "extra": { | ||
| "branch-alias": { | ||
| "dev-master": "0.1.x-dev" | ||
| }, | ||
| "textdomain": "jetpack-global-styles-webfonts-introspector" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| { | ||
| "private": true, | ||
| "name": "@automattic/jetpack-global-styles-webfonts-introspector", | ||
| "version": "0.1.0-alpha", | ||
| "description": "Looks for webfonts picked in global styles", | ||
| "homepage": "https://jetpack.com", | ||
| "bugs": { | ||
| "url": "https://github.com/Automattic/jetpack/issues" | ||
| }, | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "https://github.com/Automattic/jetpack.git" | ||
| }, | ||
| "license": "GPL-2.0-or-later", | ||
| "author": "Automattic", | ||
| "scripts": { | ||
| "build": "echo 'Not implemented.'", | ||
| "build-js": "echo 'Not implemented.'", | ||
| "build-production": "echo 'Not implemented.'", | ||
| "build-production-js": "echo 'Not implemented.'", | ||
| "clean": "true" | ||
| }, | ||
| "devDependencies": {}, | ||
| "engines": { | ||
| "node": "^14.18.3 || ^16.13.2", | ||
| "pnpm": "^6.32.3", | ||
| "yarn": "use pnpm instead - see docs/yarn-upgrade.md" | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| <phpunit bootstrap="tests/php/bootstrap.php" backupGlobals="false" colors="true" convertDeprecationsToExceptions="true"> | ||
| <testsuites> | ||
| <testsuite name="main"> | ||
| <directory prefix="test" suffix=".php">tests/php</directory> | ||
| </testsuite> | ||
| </testsuites> | ||
| <filter> | ||
| <whitelist processUncoveredFilesFromWhitelist="false"> | ||
| <!-- Better to only include "src" than to add "." and then exclude "tests", "vendor", and so on, as PHPUnit still scans the excluded directories. --> | ||
| <!-- Add additional lines for any files or directories outside of src/ that need coverage. --> | ||
| <directory suffix=".php">src</directory> | ||
| </whitelist> | ||
| </filter> | ||
| </phpunit> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,90 @@ | ||
| <?php | ||
| /** | ||
| * Global styles webfonts introspector | ||
| * | ||
| * @package automattic/global-styles-webfonts-introspector | ||
| * @since 0.1.0 | ||
| */ | ||
|
|
||
| namespace Automattic\Jetpack\Fonts; | ||
|
|
||
| /** | ||
| * Global styles webfonts introspector. | ||
| */ | ||
| class Global_Styles_Webfonts_Introspector { | ||
| /** | ||
| * Extract the font family slug from a settings object. | ||
| * | ||
| * @param object $setting The setting object. | ||
| * | ||
| * @return string|void | ||
| */ | ||
| private static function extract_font_slug_from_setting( $setting ) { | ||
| if ( isset( $setting['typography'] ) && isset( $setting['typography']['fontFamily'] ) ) { | ||
| $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; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you describe what this line would return and under what circumstances this condition would be triggered? I'm guessing one of the presets we evaluate ( blocks, HTML, or global typography ) return a normal slug. Also completely unrelated to this PR, but it seems so odd to have so many different ways of representing the font family slug 🙃
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Right! That's because you can specify a CSS variable to link it inside But then, you can also solely specify the slug, and that's why we're returning... If none of the formats match, just return what's in there and we'll try our luck. It might be a system font, might be just the slug... Who knows. |
||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Introspect global styles for webfonts. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add an @return inline comment here for consistency? |
||
| */ | ||
| public static function introspect_global_styles_webfonts() { | ||
| if ( ! function_exists( 'gutenberg_get_global_styles' ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $global_styles = gutenberg_get_global_styles(); | ||
|
|
||
| $found_webfonts = array(); | ||
|
|
||
| // Look for fonts in block presets... | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's more explicit, for sure! I'm curious if this is necessary since it kind of goes without saying that global styles can specify the typography since we're looking for them in the code?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Sure -- for those examples, I see what you're saying. My intention is to ultimately provide clarity for others by adding context about business use cases, especially since this code will live in Jetpack for the time being ( where folks might not be as familiar with the global styles feature ). Does that make sense? Maybe we can think about different, more meaningful wording.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Gotcha. Sure, we can think of something. |
||
| 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 ] = 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 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 ] = 1; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // 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 ] = 1; | ||
| } | ||
|
|
||
| return array_keys( $found_webfonts ); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <?php | ||
| /** | ||
| * Bootstrap. | ||
| * | ||
| * @package automattic/ | ||
| */ | ||
|
|
||
| /** | ||
| * Include the composer autoloader. | ||
| */ | ||
| require_once __DIR__ . '/../../vendor/autoload.php'; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| Significance: minor | ||
| Type: enhancement | ||
|
|
||
| Introspect Google fonts instead of enqueueing them all |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.