Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
cef117a
1st Webfonts API implementation - to change.
aristath Aug 6, 2021
9facf39
Register collections instead of individual fonts
aristath Oct 6, 2021
85e0fb9
New architecture scaffolding.
hellofromtonya Oct 11, 2021
5ee49bb
Add get_validated_params method
aristath Oct 11, 2021
a1f34f0
Tweak validation for local fonts
aristath Oct 11, 2021
d5ac8df
fix paths
aristath Oct 11, 2021
6531b66
typo
aristath Oct 11, 2021
f99faf4
Adds tests and fixes for WP_Webfonts_Registry.
hellofromtonya Oct 11, 2021
42bc934
Adds tests amd fixes for WP_Webfonts_Provider_Registry.
hellofromtonya Oct 11, 2021
1804d67
Adds tests and fixes for WP_Webfonts_Controller.
hellofromtonya Oct 11, 2021
a2331ae
Moved schema validator to separate class.
hellofromtonya Oct 11, 2021
ee9ea3b
Made font weight and style optional.
hellofromtonya Oct 11, 2021
0ad4f54
Fix method name
aristath Oct 12, 2021
566fea7
validation: should use kebab since it's 1 step before the CSS output
aristath Oct 12, 2021
ecfa1da
Fix styles generation
aristath Oct 12, 2021
45c6f21
remove blank line (CS)
aristath Oct 12, 2021
78812dd
Add editor styles for webfonts
aristath Oct 13, 2021
75c0bf9
Make function public. We'll need it for the theme.json implementation
aristath Oct 13, 2021
228508d
Revert "Make function public. We'll need it for the theme.json implem…
aristath Oct 13, 2021
880178d
Account for "file:./" URLs defined in a theme.json file.
aristath Oct 13, 2021
780cda6
Update docblocks and remove final for testing.
hellofromtonya Oct 13, 2021
da9ebcf
Replaces _doing_it_wrong() with trigger_error().
hellofromtonya Oct 13, 2021
a73bc1d
Moves validation from providers to Validator.
hellofromtonya Oct 14, 2021
9966b19
Adds src validation to Validator.
hellofromtonya Oct 15, 2021
56ebaa7
Remove invalid_parameters & related method
aristath Oct 18, 2021
0c0354b
Improves readability, usability, and performance.
hellofromtonya Oct 19, 2021
2e541a1
Remove file:./ implementation
aristath Oct 20, 2021
1d92844
update tests
aristath Oct 20, 2021
e5ca831
CS
aristath Oct 20, 2021
33872b8
more tests fixes
aristath Oct 20, 2021
551adee
Make links relative if URL is on-site
aristath Oct 20, 2021
846e860
Update tests for local URLs CSS generation
aristath Oct 20, 2021
dab75cc
Add check to verify the provider is registered.
aristath Oct 21, 2021
d14aa9e
This was already done
aristath Oct 21, 2021
d87a3b8
test unregistered provider
aristath Oct 21, 2021
646c9c0
cs
aristath Oct 21, 2021
fe29ea7
Trigger error when provider is not registered
aristath Oct 21, 2021
01fd844
CS fix
aristath Oct 21, 2021
27116bc
Fix some failing tests
aristath Oct 21, 2021
4be94a3
meh, copy/pasta typo-error
aristath Oct 21, 2021
d2f1474
Fix failing test for unregistered provider
aristath Oct 21, 2021
dc5ed3f
minor improvements to the validator
aristath Oct 25, 2021
291e4a8
fix docs & typos
aristath Oct 26, 2021
4b7b099
another typo fix
aristath Oct 26, 2021
88fb4af
Don't check if provider is registered
aristath Oct 26, 2021
699f96c
This is not necessary, the browser won't get a request for this URL.
aristath Oct 27, 2021
e22ed3e
CS
aristath Oct 27, 2021
4757065
simplify configuration params
aristath Oct 27, 2021
d2b2c91
Add docs to the register method
aristath Oct 27, 2021
09742ca
This was removed
aristath Oct 27, 2021
571c40d
Check if provider is a subclass of WP_Webfonts_Provider
aristath Oct 27, 2021
022467a
more docs
aristath Oct 27, 2021
68f439a
more docs
aristath Oct 27, 2021
b2aef24
rename the webfonts file.
aristath Oct 27, 2021
cf6ac7c
Remove get_by_font_family and related tests
aristath Oct 27, 2021
46c87d5
Move font-family quoting to css-generation method
aristath Oct 27, 2021
5bf5f66
Simplify the controller
aristath Oct 27, 2021
e31dee5
Move require_once to script_loader.php
aristath Oct 27, 2021
9ae7f9a
add missing "since" to classes
aristath Oct 27, 2021
0883e97
Simplify the preconnect-URLs implementation
aristath Oct 27, 2021
2d0274b
minify styles if not debugging scripts
aristath Oct 27, 2021
28445b3
Removes store by font-family during register.
hellofromtonya Oct 27, 2021
aff4233
Abstracts generating resources hints.
aristath Nov 1, 2021
1ec9d69
Fix method parameter name typo.
hellofromtonya Nov 3, 2021
6b08c0a
Fix variable name typo.
hellofromtonya Nov 3, 2021
a9c62fc
Remove unused key variable from `foreach`.
hellofromtonya Nov 3, 2021
b0e0296
Fixes to tests from code review.
hellofromtonya Nov 3, 2021
febc81b
Skip "provider" in CSS generation in the local provider.
aristath Nov 4, 2021
de65387
Update src/wp-includes/webfonts-api/class-wp-webfonts-registry.php
aristath Nov 4, 2021
00c54aa
Allow using a range of font-weights for google-fonts
aristath Nov 4, 2021
0e75bcf
provider no longer printed in the CSS
aristath Nov 4, 2021
ec7c787
Require 'is-external' => true to load external API webfonts
aristath Nov 4, 2021
2139913
fix some (not all) test failures
aristath Nov 4, 2021
4ea1f34
bugfix
aristath Nov 4, 2021
864b5d1
typecast to array
aristath Nov 4, 2021
9dd78c0
Improves the WP_Webfonts_Controller::generate_styles().
hellofromtonya Nov 4, 2021
0f89538
Require valid CSS properties for webfont schema.
hellofromtonya Nov 4, 2021
9a9f91c
Document classname needs for provider registration.
hellofromtonya Nov 4, 2021
d035860
Removes WP_Webfonts_Provider::prepare().
hellofromtonya Nov 4, 2021
3929bf9
Require CSS properties in snake_case for registration.
hellofromtonya Nov 4, 2021
125c29c
Shortens the Controller's getter names.
hellofromtonya Nov 4, 2021
1380de5
Use MINUTE_IN_SECONDS constant instead of hard coded 60.
hellofromtonya Nov 4, 2021
acde215
Type cast font weights to int for range().
hellofromtonya Nov 5, 2021
f88500c
Remove unnecessary empty line.
hellofromtonya Nov 5, 2021
6a0c723
Micro-optimizations, fixes, and comments.
hellofromtonya Nov 5, 2021
14efc74
Removes unnecessary guard clauses.
hellofromtonya Nov 5, 2021
c8ea408
Makes reflection get_webfonts_property() consistent in tests.
hellofromtonya Nov 5, 2021
d898462
Simplify wp_register_webfonts by removing the unnecessary guard clause.
hellofromtonya Nov 5, 2021
cb0c9bc
Adds tests for Google Fonts provider `get_css()`.
hellofromtonya Nov 5, 2021
81bba48
Changes the remote privacy permission logic.
hellofromtonya Nov 5, 2021
81326fd
Code review: comments, typos, micro-optimizations.
hellofromtonya Nov 6, 2021
6024894
Adds remaining class DocBlock comments.
hellofromtonya Nov 7, 2021
aa08030
typo fix
aristath Nov 8, 2021
0ce34f6
should be an instance of WP_Webfonts_Controller, not WP_Webfonts
aristath Nov 18, 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
Prev Previous commit
Next Next commit
Simplify the preconnect-URLs implementation
  • Loading branch information
aristath committed Nov 18, 2021
commit 0883e97fe429b0551c6009540855f0db324fec98
11 changes: 0 additions & 11 deletions src/wp-includes/webfonts-api/class-wp-webfonts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,23 +124,12 @@ private function generate_styles() {
continue;
}

add_action( 'wp_head', array( $this, 'render_links' ) );

$provider->set_webfonts( $registered_webfonts );
$styles .= $provider->get_css();
}
return $styles;
}

/**
* Renders the HTML `<link>` for each provider into `<head>` for enqueued webfonts.
*
* @since 5.9.0
*/
public function render_links() {
echo $this->providers->get_links();
}

/**
* Get the webfonts registry.
*
Expand Down
103 changes: 0 additions & 103 deletions src/wp-includes/webfonts-api/class-wp-webfonts-provider-registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* - registers each provider with the API by:
* 1. creating an instance (object);
* 2. storing it in-memory (by its unique provider ID) for use with the API;
* - handles generating the linked resources `<link>` for all providers.
*
* @since 5.9.0
*/
Expand Down Expand Up @@ -106,106 +105,4 @@ public function register( $classname ) {

return true;
}

/**
* Gets the HTML `<link>` for each provider.
*
* @since 5.9.0
*
* @return string HTML links for each provider.
*/
public function get_links() {
/*
* Store each `<link>` by its provider ID. Why?
* To ensure only one link is created per provider.
*/
static $links = array();

foreach ( $this->get_all_registered() as $provider_id => $provider ) {
// Skip if the provider already added the link.
if ( isset( $links[ $provider_id ] ) ) {
continue;
}

$links[ $provider_id ] = $this->generate_links( $provider );
}

// Combine `<link>` elements and return them as a string.
return implode( '', $links );
}

/**
* Generate the `<link> element(s) for the given provider.
*
* @since 5.9.0
*
* @param WP_Webfonts_Provider $provider Instance of the provider.
* @return string The `<link>` element(s).
*/
private function generate_links( WP_Webfonts_Provider $provider ) {
$link_attributes = $provider->get_link_attributes();

/*
* Bail out if there are no attributes for this provider
* (i.e. no `<link>` is needed).
*/
if ( ! is_array( $link_attributes ) || empty( $link_attributes ) ) {
return '';
}

/*
* This provider needs multiple `<link>` elements.
* Loop through each array and pass its attributes
* to create each of its `<link>` elements.
*/
if ( is_array( current( $link_attributes ) ) ) {
$links = '';
foreach ( $link_attributes as $attributes ) {
$links .= $this->create_link_element( $attributes );
}

return $links;
}

/*
* This provider needs one `<link>` element.
* Pass its attributes to create its `<link>` element.
*/
return $this->create_link_element( $link_attributes );
}

/**
* Creates the `<link>` element and populates with the given attributes.
*
* @since 5.9.0
*
* @param string[] $attributes An array of attributes => values.
* @return string The `<link>` element.
*/
private function create_link_element( array $attributes ) {
$link = '';

foreach ( $attributes as $attribute => $value ) {
// Checks if attribute is a nonempty string. If no, skip it.
if ( ! is_string( $attribute ) || '' === $attribute ) {
continue;
}

if ( 'href' === $attribute ) {
$link .= ' href="' . esc_url( $value ) . '"';
} elseif ( is_bool( $value ) ) {
$link .= $value
? ' ' . esc_attr( $attribute )
: '';
} else {
$link .= ' ' . esc_attr( $attribute ) . '="' . esc_attr( $value ) . '"';
}
}

if ( '' === $link ) {
return '';
}

return '<link rel="preconnect"' . $link . '>' . "\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* - registers each provider with the API by:
* 1. creating an instance (object);
* 2. storing it in-memory (by its unique provider ID) for use with the API;
* - handles generating the linked resources `<link>` for all providers.
*
* @since 5.9.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,34 +24,23 @@ class WP_Webfonts_Google_Provider extends WP_Webfonts_Provider {
protected $id = 'google';

/**
* The `<link>` element's attributes for each linked resource.
* The provider's root URL.
*
* @since 5.9.0
*
* @var array[] {
* An array of linked resources.
*
* @type array() {
* An array of attributes for this linked resource.
*
* @type string $attribute => @type string $attribute_value
* }
* }
* @var string
*/
protected $link_attributes = array(
array(
'href' => 'https://fonts.gstatic.com',
'crossorigin' => true,
),
);
protected $root_url = 'https://fonts.googleapis.com/css2';

/**
* The provider's root URL.
* The object's constructor.
*
* @since 5.9.0
* @var string
*/
protected $root_url = 'https://fonts.googleapis.com/css2';
public function __construct() {

// Add preconnect links.
add_filter( 'wp_resource_hints', array( $this, 'add_preconnect_urls' ), 10, 2 );
}

/**
* Get the CSS for a collection of fonts.
Expand Down Expand Up @@ -160,4 +149,38 @@ private function organize_webfonts() {

return $font_display_groups;
}

/**
* Adds preconnect URLs for webfonts providers.
*
* @since 5.9.0
*
* @param array $urls {
* Array of resources and their attributes, or URLs to print for resource hints.
*
* @type array|string ...$0 {
* Array of resource attributes, or a URL string.
*
* @type string $href URL to include in resource hints. Required.
* @type string $as How the browser should treat the resource
* (`script`, `style`, `image`, `document`, etc).
* @type string $crossorigin Indicates the CORS policy of the specified resource.
* @type float $pr Expected probability that the resource hint will be used.
* @type string $type Type of the resource (`text/html`, `text/css`, etc).
* }
* }
* @param string $relation_type The relation type the URLs are printed for,
* e.g. 'preconnect' or 'prerender'.
*/
public function add_preconnect_urls( $urls, $relation_type ) {
if ( 'preconnect' !== $relation_type ) {
return $urls;
}

$urls[] = array(
'href' => 'https://fonts.gstatic.com',
'crossorigin' => 'anonymous',
);
return $urls;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,6 @@ abstract class WP_Webfonts_Provider {
*/
protected $id;

/**
* The `<link>` element's attributes for each linked resource.
*
* @since 5.9.0
*
* @var array[] {
* An array of linked resources.
*
* @type array() {
* An array of attributes for this linked resource.
*
* @type string $attribute => @type string $attribute_value
* }
* }
*/
protected $link_attributes = array();

/**
* The provider's root URL.
*
Expand Down Expand Up @@ -82,17 +65,6 @@ public function get_root_url() {
return $this->root_url;
}

/**
* Get the `<link>` attributes.
*
* @since 5.9.0
*
* @return array[]
*/
public function get_link_attributes() {
return $this->link_attributes;
}

/**
* Sets the webfonts.
*
Expand Down
48 changes: 0 additions & 48 deletions tests/phpunit/tests/webfonts-api/wpWebfontsProviderRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,52 +80,4 @@ public function test_register_with_core_providers() {
$expected = array( 'google', 'local', 'my-custom-provider' );
$this->assertSame( $expected, array_keys( $providers ) );
}

/**
* @covers WP_Webfonts_Provider_Registry::get_links
*
* @dataProvider data_get_links
*
* @param bool $register_custom When true, registers the custom provider.
* @param string $expected Expected HTML.
*/
public function test_get_links( $register_custom, $expected ) {
$registry = new WP_Webfonts_Provider_Registry();
// Register the core providers.
$registry->init();
// Register a custom provider.
if ( $register_custom ) {
$registry->register( My_Custom_Webfonts_Provider_Mock::class );
}

$actual = $registry->get_links();
$this->assertSame( $expected, $actual );
}

/**
* Data Provider.
*
* return @array
*/
public function data_get_links() {
return array(
'core providers' => array(
'register_custom' => false,
'expected' => <<<LINKS
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>

LINKS
,
),
'core + custom providers' => array(
'register_custom' => true,
'expected' => <<<LINKS
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preconnect" href="https://fonts.my-custom-api.com" crossorigin>

LINKS
,
),
);
}
}