Skip to content

Commit

Permalink
Extract global styles webfonts introspector to its own package
Browse files Browse the repository at this point in the history
  • Loading branch information
zaguiini committed Apr 6, 2022
1 parent dda5f5d commit 584632d
Show file tree
Hide file tree
Showing 11 changed files with 251 additions and 0 deletions.
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="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>
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 projects/packages/global-styles-webfonts-introspector/README.md
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.
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 projects/packages/global-styles-webfonts-introspector/package.json
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,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 );
}
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';

0 comments on commit 584632d

Please sign in to comment.