-
Notifications
You must be signed in to change notification settings - Fork 798
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract global styles webfonts introspector to its own package
- Loading branch information
Showing
11 changed files
with
251 additions
and
0 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
projects/packages/global-styles-webfonts-introspector/.gitattributes
This file contains 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,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 |
2 changes: 2 additions & 0 deletions
2
projects/packages/global-styles-webfonts-introspector/.gitignore
This file contains 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,2 @@ | ||
vendor/ | ||
node_modules/ |
24 changes: 24 additions & 0 deletions
24
projects/packages/global-styles-webfonts-introspector/.phpcs.dir.xml
This file contains 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,24 @@ | ||
<?xml version="1.0"?> | ||
<ruleset> | ||
|
||
<rule ref="WordPress.WP.I18n"> | ||
<properties> | ||
<property name="text_domain" type="array"> | ||
<element value="package-name" /> | ||
</property> | ||
</properties> | ||
</rule> | ||
<rule ref="Jetpack.Functions.I18n"> | ||
<properties> | ||
<property name="text_domain" value="package-name" /> | ||
</properties> | ||
</rule> | ||
|
||
<rule ref="WordPress.Utils.I18nTextDomainFixer"> | ||
<properties> | ||
<property name="old_text_domain" type="array" /> | ||
<property name="new_text_domain" value="package-name" /> | ||
</properties> | ||
</rule> | ||
|
||
</ruleset> |
7 changes: 7 additions & 0 deletions
7
projects/packages/global-styles-webfonts-introspector/CHANGELOG.md
This file contains 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,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). | ||
|
20 changes: 20 additions & 0 deletions
20
projects/packages/global-styles-webfonts-introspector/README.md
This file contains 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,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) | ||
|
Empty file.
44 changes: 44 additions & 0 deletions
44
projects/packages/global-styles-webfonts-introspector/composer.json
This file contains 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,44 @@ | ||
{ | ||
"name": "automattic/jetpack-global-styles-webfonts-introspector", | ||
"description": "Looks for webfonts picked in global styles", | ||
"type": "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" | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
projects/packages/global-styles-webfonts-introspector/package.json
This file contains 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,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" | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
projects/packages/global-styles-webfonts-introspector/phpunit.xml.dist
This file contains 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,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> |
84 changes: 84 additions & 0 deletions
84
.../packages/global-styles-webfonts-introspector/src/global-styles-webfonts-introspector.php
This file contains 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,84 @@ | ||
<?php | ||
/** | ||
* Global styles webfonts introspector | ||
* | ||
* @package automattic/global-styles-webfonts-introspector | ||
* @since 0.1.0 | ||
*/ | ||
|
||
namespace Automattic\Jetpack\Fonts; | ||
|
||
/** | ||
* Extract the font family slug from a settings object. | ||
* | ||
* @param object $setting The setting object. | ||
* | ||
* @return string|void | ||
*/ | ||
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; | ||
} | ||
} | ||
|
||
/** | ||
* Introspect global styles for webfonts. | ||
*/ | ||
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... | ||
if ( isset( $global_styles['blocks'] ) ) { | ||
foreach ( $global_styles['blocks'] as $setting ) { | ||
$font_slug = 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 = extract_font_slug_from_setting( $setting ); | ||
|
||
if ( $font_slug ) { | ||
$found_webfonts[ $font_slug ] = 1; | ||
} | ||
} | ||
} | ||
|
||
// Check if a global typography setting was defined. | ||
$font_slug = extract_font_slug_from_setting( $global_styles ); | ||
|
||
if ( $font_slug ) { | ||
$found_webfonts[ $font_slug ] = 1; | ||
} | ||
|
||
return array_keys( $found_webfonts ); | ||
} |
11 changes: 11 additions & 0 deletions
11
projects/packages/global-styles-webfonts-introspector/tests/php/bootstrap.php
This file contains 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,11 @@ | ||
<?php | ||
/** | ||
* Bootstrap. | ||
* | ||
* @package automattic/ | ||
*/ | ||
|
||
/** | ||
* Include the composer autoloader. | ||
*/ | ||
require_once __DIR__ . '/../../vendor/autoload.php'; |