Skip to content
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

[Webfonts] Rename to Fonts API #46749

Merged
merged 14 commits into from
Jan 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/class-wp-theme-json-resolver-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ public static function get_theme_data( $deprecated = array(), $options = array()
$theme_json_data = array();
}
// BEGIN OF EXPERIMENTAL CODE. Not to backport to core.
$theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $theme_json_data );
$theme_json_data = gutenberg_add_registered_fonts_to_theme_json( $theme_json_data );
// END OF EXPERIMENTAL CODE.

/**
Expand All @@ -267,7 +267,7 @@ public static function get_theme_data( $deprecated = array(), $options = array()
$parent_theme_json_data = static::read_json_file( $parent_theme_json_file );
$parent_theme_json_data = static::translate( $parent_theme_json_data, $wp_theme->parent()->get( 'TextDomain' ) );
// BEGIN OF EXPERIMENTAL CODE. Not to backport to core.
$parent_theme_json_data = gutenberg_add_registered_webfonts_to_theme_json( $parent_theme_json_data );
$parent_theme_json_data = gutenberg_add_registered_fonts_to_theme_json( $parent_theme_json_data );
// END OF EXPERIMENTAL CODE.
$parent_theme = new WP_Theme_JSON_Gutenberg( $parent_theme_json_data );

Expand Down
22 changes: 11 additions & 11 deletions lib/compat/wordpress-6.2/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,26 @@ function gutenberg_resolve_assets_override() {
$scripts = ob_get_clean();

/*
* Generate web font @font-face styles for the site editor iframe.
* Generate font @font-face styles for the site editor iframe.
* Use the registered font families for printing.
*/
if ( class_exists( 'WP_Web_Fonts' ) ) {
$wp_webfonts = wp_webfonts();
$registered = $wp_webfonts->get_registered_font_families();
if ( class_exists( 'WP_Fonts' ) ) {
$wp_fonts = wp_fonts();
$registered = $wp_fonts->get_registered_font_families();
if ( ! empty( $registered ) ) {
$queue = $wp_webfonts->queue;
$done = $wp_webfonts->done;
$queue = $wp_fonts->queue;
$done = $wp_fonts->done;

$wp_webfonts->done = array();
$wp_webfonts->queue = $registered;
$wp_fonts->done = array();
$wp_fonts->queue = $registered;

ob_start();
$wp_webfonts->do_items();
$wp_fonts->do_items();
$styles .= ob_get_clean();

// Reset the Web Fonts API.
$wp_webfonts->done = $done;
$wp_webfonts->queue = $queue;
$wp_fonts->done = $done;
$wp_fonts->queue = $queue;
}
}

Expand Down
240 changes: 240 additions & 0 deletions lib/experimental/fonts-api/class-wp-fonts-provider-local.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
<?php
/**
* Webfonts API: Provider for locally-hosted fonts.
*
* @package WordPress
* @subpackage Fonts API
* @since X.X.X
*/

if ( class_exists( 'WP_Fonts_Provider_Local' ) ) {
return;
}

/**
* A core bundled provider for generating `@font-face` styles
* from locally-hosted font files.
*
* This provider builds an optimized `src` (for browser support)
* and then generates the `@font-face` styles.
*
* All know-how (business logic) for how to interact with and
* generate styles from locally-hosted font files is contained
* in this provider.
*
* @since X.X.X
*/
class WP_Fonts_Provider_Local extends WP_Fonts_Provider {

/**
* The provider's unique ID.
*
* @since X.X.X
*
* @var string
*/
protected $id = 'local';

/**
* Constructor.
*
* @since 6.1.0
*/
public function __construct() {
if (
function_exists( 'is_admin' ) && ! is_admin()
&&
function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'style' )
) {
$this->style_tag_atts = array( 'type' => 'text/css' );
}
}

/**
* Gets the `@font-face` CSS styles for locally-hosted font files.
*
* This method does the following processing tasks:
* 1. Orchestrates an optimized `src` (with format) for browser support.
* 2. Generates the `@font-face` for all its fonts.
*
* @since X.X.X
*
* @return string The `@font-face` CSS.
*/
public function get_css() {
$css = '';

foreach ( $this->fonts as $font ) {
// Order the font's `src` items to optimize for browser support.
$font = $this->order_src( $font );

// Build the @font-face CSS for this font.
$css .= '@font-face{' . $this->build_font_face_css( $font ) . '}';
}

return $css;
}

/**
* Order `src` items to optimize for browser support.
*
* @since X.X.X
*
* @param array $font Font to process.
* @return array
*/
private function order_src( array $font ) {
if ( ! is_array( $font['src'] ) ) {
$font['src'] = (array) $font['src'];
}

$src = array();
$src_ordered = array();

foreach ( $font['src'] as $url ) {
// Add data URIs first.
if ( str_starts_with( trim( $url ), 'data:' ) ) {
$src_ordered[] = array(
'url' => $url,
'format' => 'data',
);
continue;
}
$format = pathinfo( $url, PATHINFO_EXTENSION );
$src[ $format ] = $url;
}

// Add woff2.
if ( ! empty( $src['woff2'] ) ) {
$src_ordered[] = array(
'url' => $src['woff2'],
'format' => 'woff2',
);
}

// Add woff.
if ( ! empty( $src['woff'] ) ) {
$src_ordered[] = array(
'url' => $src['woff'],
'format' => 'woff',
);
}

// Add ttf.
if ( ! empty( $src['ttf'] ) ) {
$src_ordered[] = array(
'url' => $src['ttf'],
'format' => 'truetype',
);
}

// Add eot.
if ( ! empty( $src['eot'] ) ) {
$src_ordered[] = array(
'url' => $src['eot'],
'format' => 'embedded-opentype',
);
}

// Add otf.
if ( ! empty( $src['otf'] ) ) {
$src_ordered[] = array(
'url' => $src['otf'],
'format' => 'opentype',
);
}
$font['src'] = $src_ordered;

return $font;
}

/**
* Builds the font-family's CSS.
*
* @since X.X.X
*
* @param array $font Font to process.
* @return string This font-family's CSS.
*/
private function build_font_face_css( array $font ) {
$css = '';

// Wrap font-family in quotes if it contains spaces
// and is not already wrapped in quotes.
if (
str_contains( $font['font-family'], ' ' ) &&
! str_contains( $font['font-family'], '"' ) &&
! str_contains( $font['font-family'], "'" )
) {
$font['font-family'] = '"' . $font['font-family'] . '"';
}

foreach ( $font as $key => $value ) {

// Skip "provider", since it's for internal API use,
// and not a valid CSS property.
if ( 'provider' === $key ) {
continue;
}

// Compile the "src" parameter.
if ( 'src' === $key ) {
$value = $this->compile_src( $font['font-family'], $value );
}

// If font-variation-settings is an array, convert it to a string.
if ( 'font-variation-settings' === $key && is_array( $value ) ) {
$value = $this->compile_variations( $value );
}

if ( ! empty( $value ) ) {
$css .= "$key:$value;";
}
}

return $css;
}

/**
* Compiles the `src` into valid CSS.
*
* @since X.X.X
*
* @param string $font_family Font family.
* @param array $value Value to process.
* @return string The CSS.
*/
private function compile_src( $font_family, array $value ) {
$src = "local($font_family)";

foreach ( $value as $item ) {

if ( str_starts_with( $item['url'], get_site_url() ) ) {
$item['url'] = wp_make_link_relative( $item['url'] );
}

$src .= ( 'data' === $item['format'] )
? ", url({$item['url']})"
: ", url('{$item['url']}') format('{$item['format']}')";
}
return $src;
}

/**
* Compiles the font variation settings.
*
* @since X.X.X
*
* @param array $font_variation_settings Array of font variation settings.
* @return string The CSS.
*/
private function compile_variations( array $font_variation_settings ) {
$variations = '';

foreach ( $font_variation_settings as $key => $value ) {
$variations .= "$key $value";
}

return $variations;
}
}
Loading