Skip to content

Commit

Permalink
Rename and deprecate
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofromtonya committed Dec 22, 2022
1 parent 921135f commit be55e3a
Show file tree
Hide file tree
Showing 46 changed files with 1,515 additions and 1,055 deletions.
24 changes: 12 additions & 12 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;
// Reset the Fonts API.
$wp_fonts->done = $done;
$wp_fonts->queue = $queue;
}
}

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

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 6.0.0
*/
class WP_Fonts_Provider_Local extends WP_Fonts_Provider {

/**
* The provider's unique ID.
*
* @since 6.0.0
*
* @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 6.0.0
*
* @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 6.0.0
*
* @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

1 comment on commit be55e3a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3759574422
📝 Reported issues:

Please sign in to comment.