Skip to content

Commit

Permalink
Add get_webfonts_by_provider() method to handle regrouping.
Browse files Browse the repository at this point in the history
The `WP_Webfonts::generate_styles()` should not be aware of how to
reorganize the webfonts from grouped by font-family to grouped by
provider. Why? This is a separate task from generating styles for
the webfonts.

This commit creates a private method called `get_webfonts_by_provider()`
to encapsulate the know-how for this reorganizational work.

It also includes a micro-optimization that eliminates an extra
`foreach` loop and the building of an unnecessary `$webfonts`
array of arrays.
  • Loading branch information
hellofromtonya authored and zaguiini committed Apr 2, 2022
1 parent 4266deb commit 03a4db8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
67 changes: 40 additions & 27 deletions lib/experimental/class-wp-webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,8 @@ public function register_provider( $provider, $class ) {
*/
public function generate_and_enqueue_styles() {
// Generate the styles.
$styles = $this->generate_styles( $this->get_enqueued_webfonts() );
$webfonts = $this->get_webfonts_by_provider( $this->get_enqueued_webfonts() );
$styles = $this->generate_styles( $webfonts );

// Bail out if there are no styles to enqueue.
if ( '' === $styles ) {
Expand All @@ -331,7 +332,8 @@ public function generate_and_enqueue_styles() {
*/
public function generate_and_enqueue_editor_styles() {
// Generate the styles.
$styles = $this->generate_styles( $this->get_all_webfonts() );
$webfonts = $this->get_webfonts_by_provider( $this->get_all_webfonts() );
$styles = $this->generate_styles( $webfonts );

// Bail out if there are no styles to enqueue.
if ( '' === $styles ) {
Expand All @@ -347,35 +349,13 @@ public function generate_and_enqueue_editor_styles() {
*
* @since 6.0.0
*
* @param array[] $font_families Font families and each of their webfonts.
* @param array[] $webfonts_by_provider Webfonts organized by provider.
* @return string $styles Generated styles.
*/
public function generate_styles( $font_families ) {
private function generate_styles( array $webfonts_by_provider ) {
$styles = '';
$providers = $this->get_providers();

$webfonts = array();

// Grab only the font face declarations from $font_families.
foreach ( $font_families as $font_family ) {
foreach ( $font_family as $font_face ) {
$webfonts[] = $font_face;
}
}

// Group webfonts by provider.
$webfonts_by_provider = array();
foreach ( $webfonts as $slug => $webfont ) {
$provider = $webfont['provider'];
if ( ! isset( $providers[ $provider ] ) ) {
/* translators: %s is the provider name. */
trigger_log( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider ) );
continue;
}
$webfonts_by_provider[ $provider ] = isset( $webfonts_by_provider[ $provider ] ) ? $webfonts_by_provider[ $provider ] : array();
$webfonts_by_provider[ $provider ][ $slug ] = $webfont;
}

/*
* Loop through each of the providers to get the CSS for their respective webfonts
* to incrementally generate the collective styles for all of them.
Expand All @@ -385,7 +365,7 @@ public function generate_styles( $font_families ) {
// Bail out if the provider class does not exist.
if ( ! class_exists( $provider_class ) ) {
/* translators: %s is the provider name. */
trigger_log( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider_id ) );
trigger_error( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider_id ) );
continue;
}

Expand All @@ -409,4 +389,37 @@ public function generate_styles( $font_families ) {

return $styles;
}


/**
* Reorganizes webfonts grouped by font-family into grouped by provider.
*
* @param array[] $font_families Font families and each of their webfonts.
* @return array[] Webfonts organized by providers.
*/
private function get_webfonts_by_provider( array $font_families ) {
$providers = $this->get_providers();
$webfonts_by_provider = array();

foreach ( $font_families as $webfonts ) {
foreach ( $webfonts as $webfont ) {
$provider = $webfont['provider'];

// Skip if the provider is not registered.
if ( ! isset( $providers[ $provider ] ) ) {
/* translators: %s is the provider name. */
trigger_error( sprintf( __( 'Webfont provider "%s" is not registered.', 'gutenberg' ), $provider ) );
continue;
}

// Initialize a new provider collection.
if ( ! isset( $webfonts_by_provider[ $provider ] ) ) {
$webfonts_by_provider[ $provider ] = array();
}
$webfonts_by_provider[ $provider ][] = $webfont;
}
}

return $webfonts_by_provider;
}
}
2 changes: 1 addition & 1 deletion phpunit/class-wp-webfonts-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ public function test_validate_webfont() {
/**
* Test generate_and_enqueue_styles outputs only enqueued webfonts.
*
* @covers WP_Webfonts::generate_styles
* @covers WP_Webfonts::generate_and_enqueue_styles
*/
public function test_generate_styles() {
wp_register_webfonts(
Expand Down

0 comments on commit 03a4db8

Please sign in to comment.