Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
66 commits
Select commit Hold shift + click to select a range
0e31766
copy files from styles, we'll use these as a base for webfonts
aristath Aug 6, 2021
e46c345
wip
aristath Aug 10, 2021
9f4f26c
remove get_local_stylesheet_path method
aristath Aug 11, 2021
d7ca2a9
remove get_remote_files_from_url method
aristath Aug 11, 2021
be34433
remove get_styles method
aristath Aug 11, 2021
381ae55
Remove the get_remote_url_contents method
aristath Aug 11, 2021
9d730d7
remove get_local_files_from_css method.
aristath Aug 11, 2021
38079bd
more tweaks
aristath Aug 11, 2021
613a9e4
WIP - remove class
aristath Aug 11, 2021
6d71325
forgot to rename function
aristath Aug 11, 2021
42acd3a
Fixes & tweaks
aristath Aug 11, 2021
7391700
Default to screen
aristath Aug 11, 2021
e1ced36
CS fix
aristath Aug 11, 2021
76c6e51
minor fixes
aristath Aug 12, 2021
a193417
get remote webfonts on shutdown.
aristath Sep 6, 2021
15b09be
invalidate caches monthly
aristath Sep 6, 2021
b7302ff
Only proceed with local download if a CSS asset.
aristath Sep 6, 2021
52039ad
no need for trailingslashit here
aristath Sep 6, 2021
7babdc2
remove the wp_maybe_get_local_webfont_url for now
aristath Sep 6, 2021
c008185
Add wp_webfont_generate_styles function
aristath Sep 7, 2021
d6c2ece
src should be key/value pairs
aristath Sep 7, 2021
b081344
Revert "src should be key/value pairs"
aristath Sep 7, 2021
f2ad38b
improve the src param
aristath Sep 7, 2021
9b9ec63
variations can be an array
aristath Sep 7, 2021
f1b5abb
improve extension detection & cleanup
aristath Sep 7, 2021
f0de7b1
rename function
aristath Sep 8, 2021
1587c48
allow preloading webfonts
aristath Sep 8, 2021
e6d83c5
inline docs
aristath Sep 8, 2021
05ef54d
preload by default
aristath Sep 9, 2021
19a6668
properly parse params
aristath Sep 9, 2021
55bff14
optimize browser support in the src param
aristath Sep 9, 2021
0783913
improve src implementation
aristath Sep 9, 2021
93b8a09
preload the 1st available file
aristath Sep 9, 2021
abe9053
remove svg support
aristath Sep 9, 2021
4e23427
take care of tests
aristath Sep 9, 2021
fd86012
add support for data urls
aristath Sep 9, 2021
3e921b4
basic validation
aristath Sep 9, 2021
249e0fc
more validation
aristath Sep 9, 2021
bb7db34
more tests fixes
aristath Sep 9, 2021
b00e36b
bugfix/typo
aristath Sep 9, 2021
9d5fe85
fix enqueue function
aristath Sep 9, 2021
f43cc70
allow shorter syntax when using local files
aristath Sep 9, 2021
6a1fa7b
tweak previous commit
aristath Sep 9, 2021
d492ef0
inline comments/docs
aristath Sep 9, 2021
3df7f18
tweak for data URLs
aristath Sep 9, 2021
2c0f0be
variations is actually font-variation-settings
aristath Sep 10, 2021
5bc97c6
default preload to false
aristath Sep 28, 2021
7c47e74
Alternative implementation for webfont providers
aristath Sep 29, 2021
601151a
remove preload and add api params
aristath Sep 29, 2021
1b16b7d
cache for a month
aristath Sep 29, 2021
11a7766
Add preconnect links
aristath Sep 29, 2021
d0572a3
require new files
aristath Sep 29, 2021
857b23f
syntax error
aristath Sep 29, 2021
3cadeb4
bugfix for gfonts
aristath Sep 29, 2021
bca733b
bugfix for preconnect URLs
aristath Sep 29, 2021
24b1286
Allow only using $params.
aristath Sep 29, 2021
dceaae4
fix inline docs
aristath Sep 29, 2021
3bd1c71
fix for using only a param arg
aristath Sep 29, 2021
dd51bd2
bugfix for additional props
aristath Sep 30, 2021
34a1bf4
typo
aristath Sep 30, 2021
50ac1ca
refactor
aristath Sep 30, 2021
91b928a
typo
aristath Sep 30, 2021
5bb6a34
docblock fixes
aristath Sep 30, 2021
548c789
remove extra comma
aristath Sep 30, 2021
a06fe06
phpcs fix
aristath Sep 30, 2021
453d35e
make sure response is 200
aristath Oct 6, 2021
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
149 changes: 149 additions & 0 deletions src/wp-includes/class-wp-fonts-provider-google.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
<?php
/**
* Webfonts API provider for Google fonts.
*
* @package WordPress
* @subpackage WebFonts
* @since 5.9.0
*/

/**
* Webfonts API provider for Google Fonts.
*/
final class WP_Fonts_Provider_Google extends WP_Fonts_Provider {

/**
* The provider's unique ID.
*
* @access protected
* @since 5.9.0
* @var string
*/
protected $id = 'google';

/**
* An array of URLs to preconnect to.
*
* @access protected
* @since 5.9.0
* @var array
*/
protected $preconnect_urls = array(
array(
'href' => 'https://fonts.gstatic.com',
'crossorigin' => true,
),
array(
'href' => 'https://fonts.googleapis.com',
'crossorigin' => false,
),
);

/**
* The provider's root URL.
*
* @access protected
* @since 5.9.0
* @var string
*/
protected $root_url = 'https://fonts.googleapis.com/css2';

/**
* An array of API parameters which will not be added to the @font-face.
*
* @access protected
* @since 5.9.0
* @var array
*/
protected $api_params = array(
'subset',
'text',
'effect',
);

/**
* Build the API URL from the query args.
*
* @access protected
* @since 5.9.0
* @return string
*/
protected function build_api_url() {
$query_args = array(
'family' => $this->params['font-family'],
'display' => $this->params['font-display'],
);

if ( 'italic' === $this->params['font-style'] ) {
$query_args['family'] .= ':ital,wght@1,' . $this->params['font-weight'];
} else {
$query_args['family'] .= ':wght@' . $this->params['font-weight'];
}

if ( ! empty( $this->params['subset'] ) ) {
$query_args['subset'] = implode( ',', (array) $this->params['subset'] );
}

if ( ! empty( $this->params['text'] ) ) {
$query_args['text'] = $this->params['text'];
}

if ( ! empty( $this->params['effect'] ) ) {
$query_args['effect'] = implode( '|', (array) $this->params['effect'] );
}

return add_query_arg( $query_args, $this->root_url );
}

/**
* Get the CSS for the font.
*
* @access public
* @since 5.9.0
* @return string
*/
public function get_css() {
$remote_url = $this->build_api_url();
$transient_name = 'google_fonts_' . md5( $remote_url );
$css = get_site_transient( $transient_name );

// Get remote response and cache the CSS if it hasn't been cached already.
if ( false === $css ) {
// Get the remote URL contents.
$response = wp_remote_get(
$remote_url,
array(
// Use a modern user-agent, to get woff2 files.
'user-agent' => 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0',
)
);

// Early return if the request failed.
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
set_site_transient( $transient_name, '', 60 );
return '';
}

// Get the response body.
$css = wp_remote_retrieve_body( $response );

// Cache the CSS for a month.
set_site_transient( $transient_name, $css, MONTH_IN_SECONDS );
}

// If there are additional props not included in the CSS provided by the API, add them to the final CSS.
$additional_props = array_diff(
array_keys( $this->params ),
array( 'font-family', 'font-style', 'font-weight', 'font-display', 'src', 'unicode-range' )
);
foreach ( $additional_props as $prop ) {
$css = str_replace(
'@font-face {',
'@font-face {' . $prop . ':' . $this->params[ $prop ] . ';',
$css
);
}

return $css;
}
}
72 changes: 72 additions & 0 deletions src/wp-includes/class-wp-fonts-provider-local.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php
/**
* Webfonts API provider for locally-hosted fonts.
*
* @package WordPress
* @subpackage WebFonts
* @since 5.9.0
*/

/**
* Webfonts API provider for locally-hosted fonts.
*/
final class WP_Fonts_Provider_Local extends WP_Fonts_Provider {

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

/**
* Get the CSS for the font.
*
* @access public
* @since 5.9.0
* @return string
*/
public function get_css() {
if ( empty( $this->params['font-family'] ) ) {
return '';
}

$css = '@font-face{';
foreach ( $this->params as $key => $value ) {

// Skip the "preload" parameter.
if ( 'preload' === $key ) {
continue;
}

// Compile the "src" parameter.
if ( 'src' === $key ) {
$src = "local({$this->params['font-family']})";
foreach ( $value as $item ) {
$src .= ( 'data' === $item['format'] )
? ", url({$item['url']})"
: ", url('{$item['url']}') format('{$item['format']}')";
}
$value = $src;
}

// If font-variation-settings is an array, convert it to a string.
if ( 'font-variation-settings' === $key && is_array( $value ) ) {
$variations = array();
foreach ( $value as $key => $val ) {
$variations[] = "$key $val";
}
$value = implode( ', ', $variations );
}

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

return $css;
}
}
Loading