Introduce font-face styles generator and printer.

Introducing Font Face, a server-side `@font-face` styles generator and printer. 

tl;dr:
* Introduces Font Face.
* Deprecates `_wp_theme_json_webfonts_handler()`.

**Introduce Font Face**

From an array of fonts (i.e. each font-family and its font variations to be processed), it:

1. Validates each `font-face` declaration, i.e. the CSS property and value pairing. If validation fails, processing stops with no font-face styles printed.
3. Generates the `@font-face` CSS for each font-family.
4. Prints the CSS within a `<style id="wp-fonts-local">` element.

The entry point into Font Face is through a new global function called `wp_print_font_faces()`, which is automatically called:

* when the `'wp_head'` hook runs (for the front-end).
* when the `'admin_print_styles'` hook runs (for the back-end).
* when `_wp_get_iframed_editor_assets()` runs to inject the `@font-face` styles into the iframed editor.

Once called, it gets the fonts from Theme_JSON merged data layer, which includes theme defined fonts and user activated fonts (once the Font Library #59166 is introduced into Core).

For classic sites, themes and plugins can directly call `wp_print_font_faces()` and pass their fonts array to it for processing.

**Deprecates `_wp_theme_json_webfonts_handler()`.**

As Font Face is a direct replacement, the stopgap code in `_wp_theme_json_webfonts_handler()` (introduced in 6.0.0 via [53282]) is deprecated and unused in Core.

**Props note:**
There's a long multiple year history baked into Font Face, which dates back to the early versions of a web font API (see #46370 and [https://github.com/WordPress/gutenberg/issues/41479 roadmap]. The props list includes those who contributed from those early versions up to this commit.

**References:**
* #46370 original (Web)Fonts API proposal for registering and enqueuing web fonts.
* [https://github.com/WordPress/gutenberg/issues/41479 Gutenberg tracking issue] which includes the evolution from Webfonts API to Fonts API to Font Face.
* [53282] / #55567 Added the stopgap code `_wp_theme_json_webfonts_handler()` in 6.0.
* [https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face @font-face on mdn web docs]
* #59166 Font Library: Font manager for WordPress

Follow-up to [53282].

Props aristath, jonoaldersonwp, hellofromTonya, andraganescu, annezazu, antonvlasenko, arena, askdesign, azaozz, bph, bradley2083, colorful-tones, costdev, davidbaumwald, desrosj, dingo_d, djcowan, domainsupport, dryanpress, elmastudio, flixos90, francina, garrett-eclipse, gigitux, grantmkin, grapplerulrich, gziolo, ironprogrammer, jb510, jeffpaul, jeremyyip, jffng, joostdevalk, jorgefilipecosta, juanmaguitar, mamaduka, matveb, mburridge, mitogh, ndiego, ntsekouras, oandregal, ocean90, oglekler, paaljoachim, pagelab, peterwilsoncc, poena, priethor, scruffian, SergeyBiryukov, shiloey, simison, skorasaurus, soean, westonruter, wildworks, zaguiini.
Fixes #59165.

git-svn-id: https://develop.svn.wordpress.org/trunk@56500 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Tonya Mork
2023-08-31 21:47:40 +00:00
parent 54a177910a
commit c29b095e76
41 changed files with 2302 additions and 612 deletions
@@ -0,0 +1,94 @@
Copyright 2014-2017 Indian Type Foundry (info@indiantypefoundry.com). Copyright 2019 Google LLC.
Copyright 2014-2018 Adobe (http://www.adobe.com/), with Reserved Font Name 'Source'. All Rights Reserved. Source is a trademark of Adobe in the United States and/or other countries. Copyright 2019 Google LLC.
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
@@ -0,0 +1,93 @@
Copyright 2020 The Open Sans Project Authors (https://github.com/googlefonts/opensans)
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at:
http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
@@ -0,0 +1,93 @@
Copyright 2014 - 2021 Adobe Systems Incorporated (http://www.adobe.com/), with Reserved Font Name 'Source'. All Rights Reserved. Source is a trademark of Adobe Systems Incorporated in the United States and/or other countries.
This Font Software is licensed under the SIL Open Font License, Version 1.1.
This license is copied below, and is also available with a FAQ at: http://scripts.sil.org/OFL
-----------------------------------------------------------
SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
-----------------------------------------------------------
PREAMBLE
The goals of the Open Font License (OFL) are to stimulate worldwide
development of collaborative font projects, to support the font creation
efforts of academic and linguistic communities, and to provide a free and
open framework in which fonts may be shared and improved in partnership
with others.
The OFL allows the licensed fonts to be used, studied, modified and
redistributed freely as long as they are not sold by themselves. The
fonts, including any derivative works, can be bundled, embedded,
redistributed and/or sold with any software provided that any reserved
names are not used by derivative works. The fonts and derivatives,
however, cannot be released under any other type of license. The
requirement for fonts to remain under this license does not apply
to any document created using the fonts or their derivatives.
DEFINITIONS
"Font Software" refers to the set of files released by the Copyright
Holder(s) under this license and clearly marked as such. This may
include source files, build scripts and documentation.
"Reserved Font Name" refers to any names specified as such after the
copyright statement(s).
"Original Version" refers to the collection of Font Software components as
distributed by the Copyright Holder(s).
"Modified Version" refers to any derivative made by adding to, deleting,
or substituting -- in part or in whole -- any of the components of the
Original Version, by changing formats or by porting the Font Software to a
new environment.
"Author" refers to any designer, engineer, programmer, technical
writer or other person who contributed to the Font Software.
PERMISSION & CONDITIONS
Permission is hereby granted, free of charge, to any person obtaining
a copy of the Font Software, to use, study, copy, merge, embed, modify,
redistribute, and sell modified and unmodified copies of the Font
Software, subject to the following conditions:
1) Neither the Font Software nor any of its individual components,
in Original or Modified Versions, may be sold by itself.
2) Original or Modified Versions of the Font Software may be bundled,
redistributed and/or sold with any software, provided that each copy
contains the above copyright notice and this license. These can be
included either as stand-alone text files, human-readable headers or
in the appropriate machine-readable metadata fields within text or
binary files as long as those fields can be easily viewed by the user.
3) No Modified Version of the Font Software may use the Reserved Font
Name(s) unless explicit written permission is granted by the corresponding
Copyright Holder. This restriction only applies to the primary font name as
presented to the users.
4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
Software shall not be used to promote, endorse or advertise any
Modified Version, except to acknowledge the contribution(s) of the
Copyright Holder(s) and the Author(s) or with their explicit written
permission.
5) The Font Software, modified or unmodified, in part or in whole,
must be distributed entirely under this license, and must not be
distributed under any other license. The requirement for fonts to
remain under this license does not apply to any document created
using the Font Software.
TERMINATION
This license becomes null and void if any of the above conditions are
not met.
DISCLAIMER
THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM
OTHER DEALINGS IN THE FONT SOFTWARE.
@@ -1,7 +1,7 @@
/*
Theme Name: Webfonts theme
Theme Name: Block Theme with defined Typography Fonts
Theme URI: https://wordpress.org/
Description: For testing purposes only.
Version: 1.0.0
Text Domain: webfonts-theme
Text Domain: fonts-block-theme
*/
@@ -0,0 +1,40 @@
{
"version": 2,
"title": "Variation: duplicate fonts",
"settings": {
"typography": {
"fontFamilies": [
{
"fontFamily": "\"DM Sans\", sans-serif",
"name": "DM Sans",
"slug": "dm-sans",
"fontFace": [
{
"fontFamily": "DM Sans",
"fontStretch": "normal",
"fontStyle": "normal",
"fontWeight": "400",
"src": [
"file:./assets/fonts/dm-sans/DMSans-Regular.woff2"
]
},
{
"fontFamily": "DM Sans",
"fontStretch": "normal",
"fontStyle": "normal",
"fontWeight": "700",
"src": [
"file:./assets/fonts/dm-sans/DMSans-Bold.woff2"
]
}
]
}
]
}
},
"styles": {
"typography": {
"fontFamily": "var(--wp--preset--font-family--dm-sans)"
}
}
}
@@ -0,0 +1,40 @@
{
"version": 2,
"title": "Variation: new font family",
"settings": {
"typography": {
"fontFamilies": [
{
"fontFamily": "\"Open Sans\", serif",
"name": "Open Sans",
"slug": "open-sans",
"fontFace": [
{
"fontFamily": "Open Sans",
"fontStretch": "normal",
"fontStyle": "normal",
"fontWeight": "400",
"src": [
"file:./assets/fonts/open-sans/OpenSans-VariableFont_wdth,wght.tff"
]
},
{
"fontFamily": "Open Sans",
"fontStretch": "normal",
"fontStyle": "italic",
"fontWeight": "400",
"src": [
"file:./assets/fonts/open-sans/OpenSans-Italic-VariableFont_wdth,wght.tff"
]
}
]
}
]
}
},
"styles": {
"typography": {
"fontFamily": "var(--wp--preset--font-family--open-sans)"
}
}
}
@@ -0,0 +1,40 @@
{
"version": 2,
"title": "Variation: new font variations",
"settings": {
"typography": {
"fontFamilies": [
{
"fontFamily": "\"DM Sans\", sans-serif",
"name": "DM Sans",
"slug": "dm-sans",
"fontFace": [
{
"fontFamily": "DM Sans",
"fontStretch": "normal",
"fontStyle": "normal",
"fontWeight": "500",
"src": [
"file:./assets/fonts/dm-sans/DMSans-Medium.woff2"
]
},
{
"fontFamily": "DM Sans",
"fontStretch": "normal",
"fontStyle": "italic",
"fontWeight": "500",
"src": [
"file:./assets/fonts/dm-sans/DMSans-Medium-Italic.woff2"
]
}
]
}
]
}
},
"styles": {
"typography": {
"fontFamily": "var(--wp--preset--font-family--dm-sans)"
}
}
}
@@ -0,0 +1,9 @@
{
"version": 2,
"title": "Variation - no fonts",
"styles": {
"typography": {
"fontFamily": "var(--wp--preset--font-family--dm-sans)"
}
}
}
@@ -0,0 +1,112 @@
{
"version": 2,
"settings": {
"appearanceTools": true,
"color": {
"palette": [
{
"slug": "light",
"name": "Light",
"color": "#f5f7f9"
},
{
"slug": "dark",
"name": "Dark",
"color": "#000"
}
]
},
"typography": {
"dropCap": false,
"fluid": true,
"fontFamilies": [
{
"fontFace": [
{
"fontFamily": "DM Sans",
"fontStretch": "normal",
"fontStyle": "normal",
"fontWeight": "400",
"src": [
"file:./assets/fonts/dm-sans/DMSans-Regular.woff2"
]
},
{
"fontFamily": "DM Sans",
"fontStretch": "normal",
"fontStyle": "italic",
"fontWeight": "400",
"src": [
"file:./assets/fonts/dm-sans/DMSans-Regular-Italic.woff2"
]
},
{
"fontFamily": "DM Sans",
"fontStretch": "normal",
"fontStyle": "normal",
"fontWeight": "700",
"src": [
"file:./assets/fonts/dm-sans/DMSans-Bold.woff2"
]
},
{
"fontFamily": "DM Sans",
"fontStretch": "normal",
"fontStyle": "italic",
"fontWeight": "700",
"src": [
"file:./assets/fonts/dm-sans/DMSans-Bold-Italic.woff2"
]
}
],
"fontFamily": "\"DM Sans\", sans-serif",
"name": "DM Sans",
"slug": "dm-sans"
},
{
"fontFamily": "-apple-system,BlinkMacSystemFont,\"Segoe UI\",Roboto,Oxygen-Sans,Ubuntu,Cantarell,\"Helvetica Neue\",sans-serif",
"name": "System Font",
"slug": "system-font"
},
{
"fontFace": [
{
"fontFamily": "Source Serif Pro",
"fontStretch": "normal",
"fontStyle": "normal",
"fontWeight": "200 900",
"src": [
"file:./assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2"
]
},
{
"fontFamily": "Source Serif Pro",
"fontStretch": "normal",
"fontStyle": "italic",
"fontWeight": "200 900",
"src": [
"file:./assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2"
]
}
],
"fontFamily": "\"Source Serif Pro\", serif",
"name": "Source Serif Pro",
"slug": "source-serif-pro"
}
]
},
"useRootPaddingAwareAlignments": true
},
"styles": {
"typography": {
"fontFamily": "var(--wp--preset--font-family--system-font)"
}
},
"templateParts": [
{
"name": "small-header",
"title": "Small Header",
"area": "header"
}
]
}
@@ -1 +0,0 @@
<?php
@@ -1,4 +0,0 @@
<?php
/**
* Block theme.
*/
@@ -1,103 +0,0 @@
{
"version": 2,
"customTemplates": [
{
"name": "blank",
"title": "Blank",
"postTypes": [
"page",
"post"
]
}
],
"settings": {
"appearanceTools": true,
"color": {
"duotone": [],
"gradients": [],
"palette": []
},
"custom": {},
"spacing": {
"units": [
"%",
"px",
"em",
"rem",
"vh",
"vw"
]
},
"typography": {
"dropCap": false,
"fontFamilies": [
{
"fontFamily": "\"Source Serif Pro\", serif",
"name": "Source Serif Pro",
"slug": "source-serif-pro",
"fontFace": [
{
"fontFamily": "Source Serif Pro",
"fontWeight": "200 900",
"fontStyle": "normal",
"fontStretch": "normal",
"src": [ "file:./assets/fonts/SourceSerif4Variable-Roman.ttf.woff2" ]
},
{
"fontFamily": "Source Serif Pro",
"fontWeight": "200 900",
"fontStyle": "italic",
"fontStretch": "normal",
"src": [ "file:./assets/fonts/SourceSerif4Variable-Italic.ttf.woff2" ]
}
]
}
],
"fontSizes": [
{
"size": "1rem",
"slug": "small"
},
{
"size": "1.125rem",
"slug": "medium"
},
{
"size": "1.75rem",
"slug": "large"
},
{
"size": "clamp(1.75rem, 3vw, 2.25rem)",
"slug": "x-large"
}
]
},
"layout": {
"contentSize": "650px",
"wideSize": "1000px"
}
},
"styles": {
"blocks": {},
"color": {
"background": "var(--wp--preset--color--background)",
"text": "var(--wp--preset--color--foreground)"
},
"elements": {},
"spacing": {
"blockGap": "1.5rem"
},
"typography": {
"fontFamily": "var(--wp--preset--font-family--system-font)",
"lineHeight": "var(--wp--custom--typography--line-height--normal)",
"fontSize": "var(--wp--preset--font-size--medium)"
}
},
"templateParts": [
{
"name": "header",
"title": "Header",
"area": "header"
}
]
}
@@ -0,0 +1,122 @@
<?php
/**
* Test case for the Fonts tests.
*
* @package WordPress
* @subpackage Fonts
*/
require_once __DIR__ . '/wp-font-face-tests-dataset.php';
/**
* Abstracts the common tasks for the Font Face tests.
*/
abstract class WP_Font_Face_UnitTestCase extends WP_UnitTestCase {
use WP_Font_Face_Tests_Datasets;
/**
* Current error reporting level (before a test changes it).
*
* @var null|int
*/
protected $error_reporting_level = null;
/**
* Reflection data store for non-public property access.
*
* @var ReflectionProperty[]
*/
protected $property = array();
/**
* Indicates the test class uses `switch_theme()` and requires
* set_up and tear_down fixtures to set and reset hooks and memory.
*
* If a test class switches themes, set this property to `true`.
*
* @var bool
*/
protected static $requires_switch_theme_fixtures = false;
/**
* Theme root directory.
*
* @var string
*/
protected static $theme_root;
/**
* Original theme directory.
*
* @var string
*/
protected $orig_theme_dir;
/**
* Administrator ID.
*
* @var int
*/
protected static $administrator_id = 0;
public static function set_up_before_class() {
parent::set_up_before_class();
if ( self::$requires_switch_theme_fixtures ) {
self::$theme_root = realpath( DIR_TESTDATA . '/themedir1' );
}
}
public static function tear_down_after_class() {
// Reset static flags.
self::$requires_switch_theme_fixtures = false;
parent::tear_down_after_class();
}
public function set_up() {
parent::set_up();
if ( self::$requires_switch_theme_fixtures ) {
$this->orig_theme_dir = $GLOBALS['wp_theme_directories'];
// /themes is necessary as theme.php functions assume /themes is the root if there is only one root.
$GLOBALS['wp_theme_directories'] = array( WP_CONTENT_DIR . '/themes', self::$theme_root );
// Set up the new root.
add_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
add_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
// Clear caches.
wp_clean_themes_cache();
unset( $GLOBALS['wp_themes'] );
}
}
public function tear_down() {
$this->property = array();
// Reset the error reporting when modified within a test.
if ( is_int( $this->error_reporting_level ) ) {
error_reporting( $this->error_reporting_level );
$this->error_reporting_level = null;
}
// Restore themes.
if ( self::$requires_switch_theme_fixtures ) {
$GLOBALS['wp_theme_directories'] = $this->orig_theme_dir;
remove_filter( 'theme_root', array( $this, 'filter_set_theme_root' ) );
remove_filter( 'stylesheet_root', array( $this, 'filter_set_theme_root' ) );
remove_filter( 'template_root', array( $this, 'filter_set_theme_root' ) );
wp_clean_themes_cache();
wp_clean_theme_json_cache();
unset( $GLOBALS['wp_themes'] );
}
parent::tear_down();
}
public function filter_set_theme_root() {
return self::$theme_root;
}
}
@@ -0,0 +1,274 @@
<?php
/**
* Datasets for unit and integration tests.
*
* @package WordPress
* @subpackage Fonts
*/
/**
* Trait for reusing datasets within the Fonts tests.
*/
trait WP_Font_Face_Tests_Datasets {
/**
* Data provider.
*
* @return array
*/
public function data_should_print_given_fonts() {
return array(
'single truetype format font' => array(
'fonts' => array(
'Inter' =>
array(
array(
'src' =>
array(
'https://example.org/assets/fonts/inter/Inter-VariableFont_slnt,wght.ttf',
),
'font-family' => 'Inter',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '200',
),
),
),
'expected' => <<<CSS
@font-face{font-family:Inter;font-style:normal;font-weight:200;font-display:fallback;src:url('https://example.org/assets/fonts/inter/Inter-VariableFont_slnt,wght.ttf') format('truetype');font-stretch:normal;}
CSS
,
),
'multiple truetype format fonts' => array(
'fonts' => array(
'Inter' =>
array(
array(
'src' =>
array(
'https://example.org/assets/fonts/inter/Inter-VariableFont_slnt,wght.ttf',
),
'font-family' => 'Inter',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '200',
),
array(
'src' =>
array(
'https://example.org/assets/fonts/inter/Inter-VariableFont_slnt-Italic,wght.ttf',
),
'font-family' => 'Inter',
'font-stretch' => 'normal',
'font-style' => 'italic',
'font-weight' => '900',
),
),
),
'expected' => <<<CSS
@font-face{font-family:Inter;font-style:normal;font-weight:200;font-display:fallback;src:url('https://example.org/assets/fonts/inter/Inter-VariableFont_slnt,wght.ttf') format('truetype');font-stretch:normal;}
@font-face{font-family:Inter;font-style:italic;font-weight:900;font-display:fallback;src:url('https://example.org/assets/fonts/inter/Inter-VariableFont_slnt-Italic,wght.ttf') format('truetype');font-stretch:normal;}
CSS
,
),
'single woff2 format font' => array(
'fonts' => array(
'DM Sans' =>
array(
array(
'src' =>
array(
'https://example.org/assets/fonts/dm-sans/DMSans-Regular.woff2',
),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '400',
),
),
),
'expected' => <<<CSS
@font-face{font-family:"DM Sans";font-style:normal;font-weight:400;font-display:fallback;src:url('https://example.org/assets/fonts/dm-sans/DMSans-Regular.woff2') format('woff2');font-stretch:normal;}
CSS
,
),
'multiple woff2 format fonts' => array(
'fonts' => array(
'DM Sans' =>
array(
array(
'src' =>
array(
'https://example.org/assets/fonts/dm-sans/DMSans-Regular.woff2',
),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '400',
),
array(
'src' =>
array(
'https://example.org/assets/fonts/dm-sans/DMSans-Regular-Italic.woff2',
),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'italic',
'font-weight' => '400',
),
array(
'src' =>
array(
'https://example.org/assets/fonts/dm-sans/DMSans-Bold.woff2',
),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '700',
),
array(
'src' =>
array(
'https://example.org/assets/fonts/dm-sans/DMSans-Bold-Italic.woff2',
),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'italic',
'font-weight' => '700',
),
),
'IBM Plex Mono' =>
array(
array(
'src' =>
array(
'https://example.org/assets/fonts/ibm-plex-mono/IBMPlexMono-Light.woff2',
),
'font-family' => 'IBM Plex Mono',
'font-display' => 'block',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '300',
),
array(
'src' =>
array(
'https://example.org/assets/fonts/ibm-plex-mono/IBMPlexMono-Regular.woff2',
),
'font-family' => 'IBM Plex Mono',
'font-display' => 'block',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '400',
),
array(
'src' =>
array(
'https://example.org/assets/fonts/ibm-plex-mono/IBMPlexMono-Italic.woff2',
),
'font-family' => 'IBM Plex Mono',
'font-display' => 'block',
'font-stretch' => 'normal',
'font-style' => 'italic',
'font-weight' => '400',
),
array(
'src' =>
array(
'https://example.org/assets/fonts/ibm-plex-mono/IBMPlexMono-Bold.woff2',
),
'font-family' => 'IBM Plex Mono',
'font-display' => 'block',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '700',
),
),
),
'expected' => <<<CSS
@font-face{font-family:"DM Sans";font-style:normal;font-weight:400;font-display:fallback;src:url('https://example.org/assets/fonts/dm-sans/DMSans-Regular.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"DM Sans";font-style:italic;font-weight:400;font-display:fallback;src:url('https://example.org/assets/fonts/dm-sans/DMSans-Regular-Italic.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"DM Sans";font-style:normal;font-weight:700;font-display:fallback;src:url('https://example.org/assets/fonts/dm-sans/DMSans-Bold.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"DM Sans";font-style:italic;font-weight:700;font-display:fallback;src:url('https://example.org/assets/fonts/dm-sans/DMSans-Bold-Italic.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"IBM Plex Mono";font-style:normal;font-weight:300;font-display:block;src:url('https://example.org/assets/fonts/ibm-plex-mono/IBMPlexMono-Light.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"IBM Plex Mono";font-style:normal;font-weight:400;font-display:block;src:url('https://example.org/assets/fonts/ibm-plex-mono/IBMPlexMono-Regular.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"IBM Plex Mono";font-style:italic;font-weight:400;font-display:block;src:url('https://example.org/assets/fonts/ibm-plex-mono/IBMPlexMono-Italic.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"IBM Plex Mono";font-style:normal;font-weight:700;font-display:block;src:url('https://example.org/assets/fonts/ibm-plex-mono/IBMPlexMono-Bold.woff2') format('woff2');font-stretch:normal;}
CSS
,
),
);
}
public function get_expected_fonts_for_fonts_block_theme( $key = '' ) {
static $data = null;
if ( null === $data ) {
$uri = get_stylesheet_directory_uri() . '/assets/fonts/';
$data = array(
'fonts' => array(
'DM Sans' => array(
array(
'src' => array( $uri . 'dm-sans/DMSans-Regular.woff2' ),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '400',
),
array(
'src' => array( $uri . 'dm-sans/DMSans-Regular-Italic.woff2' ),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'italic',
'font-weight' => '400',
),
array(
'src' => array( $uri . 'dm-sans/DMSans-Bold.woff2' ),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '700',
),
array(
'src' => array( $uri . 'dm-sans/DMSans-Bold-Italic.woff2' ),
'font-family' => 'DM Sans',
'font-stretch' => 'normal',
'font-style' => 'italic',
'font-weight' => '700',
),
),
'Source Serif Pro' => array(
array(
'src' => array( $uri . 'source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2' ),
'font-family' => 'Source Serif Pro',
'font-stretch' => 'normal',
'font-style' => 'normal',
'font-weight' => '200 900',
),
array(
'src' => array( $uri . 'source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2' ),
'font-family' => 'Source Serif Pro',
'font-stretch' => 'normal',
'font-style' => 'italic',
'font-weight' => '200 900',
),
),
),
'font_face_styles' => <<<CSS
@font-face{font-family:"DM Sans";font-style:normal;font-weight:400;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Regular.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"DM Sans";font-style:italic;font-weight:400;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Regular-Italic.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"DM Sans";font-style:normal;font-weight:700;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Bold.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"DM Sans";font-style:italic;font-weight:700;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Bold-Italic.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"Source Serif Pro";font-style:normal;font-weight:200 900;font-display:fallback;src:url('{$uri}source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2') format('woff2');font-stretch:normal;}
@font-face{font-family:"Source Serif Pro";font-style:italic;font-weight:200 900;font-display:fallback;src:url('{$uri}source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2') format('woff2');font-stretch:normal;}
CSS
,
);
}
if ( isset( $data[ $key ] ) ) {
return $data[ $key ];
}
return $data;
}
}
@@ -0,0 +1,40 @@
<?php
/**
* Test case for WP_Font_Face::generate_and_print().
*
* @package WordPress
* @subpackage Fonts
*
* @since 6.4.0
*
* @group fonts
* @group fontface
*
* @covers WP_Font_Face::generate_and_print
*/
class Tests_Fonts_WPFontFace_GenerateAndPrint extends WP_UnitTestCase {
use WP_Font_Face_Tests_Datasets;
public function test_should_not_generate_and_print_when_no_fonts() {
$font_face = new WP_Font_Face();
$fonts = array();
$this->expectOutputString( '' );
$font_face->generate_and_print( $fonts );
}
/**
* @dataProvider data_should_print_given_fonts
*
* @param array $fonts Prepared fonts.
* @param string $expected Expected CSS.
*/
public function test_should_generate_and_print_given_fonts( array $fonts, $expected ) {
$font_face = new WP_Font_Face();
$style_element = "<style id='wp-fonts-local' type='text/css'>\n%s\n</style>\n";
$expected_output = sprintf( $style_element, $expected );
$this->expectOutputString( $expected_output );
$font_face->generate_and_print( $fonts );
}
}
@@ -0,0 +1,99 @@
<?php
/**
* Test case for WP_Font_Face_Resolver::get_fonts_from_theme_json().
*
* @package WordPress
* @subpackage Fonts
*
* @since 6.4.0
*
* @group fonts
* @group fontface
*
* @covers WP_Font_Face_Resolver::get_fonts_from_theme_json
*/
class Tests_Fonts_WPFontFaceResolver_GetFontsFromThemeJson extends WP_Font_Face_UnitTestCase {
const FONTS_THEME = 'fonts-block-theme';
public static function set_up_before_class() {
self::$requires_switch_theme_fixtures = true;
parent::set_up_before_class();
}
public function test_should_return_empty_array_when_no_fonts_defined_in_theme() {
switch_theme( 'block-theme' );
$fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json();
$this->assertIsArray( $fonts, 'Should return an array data type' );
$this->assertEmpty( $fonts, 'Should return an empty array' );
}
public function test_should_return_all_fonts_from_theme() {
switch_theme( static::FONTS_THEME );
$actual = WP_Font_Face_Resolver::get_fonts_from_theme_json();
$expected = $this->get_expected_fonts_for_fonts_block_theme( 'fonts' );
$this->assertSame( $expected, $actual );
}
/**
* @dataProvider data_should_replace_src_file_placeholder
*
* @param string $font_name Font's name.
* @param string $font_index Font's index in the $fonts array.
* @param string $expected Expected src.
*/
public function test_should_replace_src_file_placeholder( $font_name, $font_index, $expected ) {
switch_theme( static::FONTS_THEME );
$fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json();
$actual = $fonts[ $font_name ][ $font_index ]['src'][0];
$expected = get_stylesheet_directory_uri() . $expected;
$this->assertStringNotContainsString( 'file:./', $actual, 'Font src should not contain the "file:./" placeholder' );
$this->assertSame( $expected, $actual, 'Font src should be an URL to its file' );
}
/**
* Data provider.
*
* @return array
*/
public function data_should_replace_src_file_placeholder() {
return array(
// Theme's theme.json.
'DM Sans: 400 normal' => array(
'font_name' => 'DM Sans',
'font_index' => 0,
'expected' => '/assets/fonts/dm-sans/DMSans-Regular.woff2',
),
'DM Sans: 400 italic' => array(
'font_name' => 'DM Sans',
'font_index' => 1,
'expected' => '/assets/fonts/dm-sans/DMSans-Regular-Italic.woff2',
),
'DM Sans: 700 normal' => array(
'font_name' => 'DM Sans',
'font_index' => 2,
'expected' => '/assets/fonts/dm-sans/DMSans-Bold.woff2',
),
'DM Sans: 700 italic' => array(
'font_name' => 'DM Sans',
'font_index' => 3,
'expected' => '/assets/fonts/dm-sans/DMSans-Bold-Italic.woff2',
),
'Source Serif Pro: 200-900 normal' => array(
'font_name' => 'Source Serif Pro',
'font_index' => 0,
'expected' => '/assets/fonts/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2',
),
'Source Serif Pro: 200-900 italic' => array(
'font_name' => 'Source Serif Pro',
'font_index' => 1,
'expected' => '/assets/fonts/source-serif-pro/SourceSerif4Variable-Italic.ttf.woff2',
),
);
}
}
@@ -0,0 +1,88 @@
<?php
/**
* Test case for wp_print_font_faces().
*
* @package WordPress
* @subpackage Fonts
*
* @since 6.4.0
*/
require_once __DIR__ . '/base.php';
/**
* @group fonts
* @group fontface
*
* @covers wp_print_font_faces
*
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*/
class Tests_Fonts_WpPrintFontFaces extends WP_Font_Face_UnitTestCase {
const FONTS_THEME = 'fonts-block-theme';
public static function set_up_before_class() {
self::$requires_switch_theme_fixtures = true;
parent::set_up_before_class();
}
public function test_should_not_print_when_no_fonts() {
switch_theme( 'block-theme' );
$this->expectOutputString( '' );
wp_print_font_faces();
}
/**
* @dataProvider data_should_print_given_fonts
*
* @param array $fonts Fonts to process.
* @param string $expected Expected CSS.
*/
public function test_should_print_given_fonts( array $fonts, $expected ) {
$expected_output = $this->get_expected_styles_output( $expected );
$this->expectOutputString( $expected_output );
wp_print_font_faces( $fonts );
}
public function test_should_escape_tags() {
$fonts = array(
'Source Serif Pro' => array(
array(
'src' => array( 'http://example.com/assets/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2' ),
'font-family' => 'Source Serif Pro',
'font-style' => 'normal',
'font-weight' => '200 900',
'font-stretch' => '</style><script>console.log("Hello")</script><style>',
),
),
);
$expected_output = <<<CSS
<style id='wp-fonts-local' type='text/css'>
@font-face{font-family:"Source Serif Pro";font-style:normal;font-weight:200 900;font-display:fallback;src:url('http://example.com/assets/source-serif-pro/SourceSerif4Variable-Roman.ttf.woff2') format('woff2');font-stretch:;}
</style>
CSS;
$this->expectOutputString( $expected_output );
wp_print_font_faces( $fonts );
}
public function test_should_print_fonts_in_merged_data() {
switch_theme( static::FONTS_THEME );
$expected = $this->get_expected_fonts_for_fonts_block_theme( 'font_face_styles' );
$expected_output = $this->get_expected_styles_output( $expected );
$this->expectOutputString( $expected_output );
wp_print_font_faces();
}
private function get_expected_styles_output( $styles ) {
$style_element = "<style id='wp-fonts-local' type='text/css'>\n%s\n</style>\n";
return sprintf( $style_element, $styles );
}
}
+1 -1
View File
@@ -185,7 +185,7 @@ class Tests_Theme_ThemeDir extends WP_UnitTestCase {
'Block Theme [0.4.0]',
'Block Theme [1.0.0] in subdirectory',
'Block Theme Deprecated Path',
'Webfonts theme',
'Block Theme with defined Typography Fonts',
'Empty `fontFace` in theme.json - no webfonts defined',
'A theme with the Update URI header',
);