-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
921135f
commit be55e3a
Showing
46 changed files
with
1,515 additions
and
1,055 deletions.
There are no files selected for viewing
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
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,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; | ||
} | ||
} |
Oops, something went wrong.
be55e3a
There was a problem hiding this comment.
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:
specs/editor/various/multi-block-selection.test.js