Skip to content

Commit

Permalink
Separate between registering and enqueueing webfonts
Browse files Browse the repository at this point in the history
  • Loading branch information
zaguiini committed Mar 17, 2022
1 parent b23ba2e commit feb1de1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 12 deletions.
65 changes: 54 additions & 11 deletions lib/compat/wordpress-6.0/class-wp-webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ class WP_Webfonts {
* @access private
* @var array
*/
private $webfonts = array();
private $registered_webfonts = array();

/**
* An array of enqueued webfonts.
*
* @access private
* @var array
*/
private $enqueued_webfonts = array();

/**
* An array of registered providers.
Expand Down Expand Up @@ -56,12 +64,30 @@ public function init() {
}

/**
* Get the list of fonts.
* Get the list of registered fonts.
*
* @return array
*/
public function get_registered_webfonts() {
return $this->registered_webfonts;
}

/**
* Get the list of enqueued fonts.
*
* @return array
*/
public function get_fonts() {
return $this->webfonts;
public function get_enqueued_webfonts() {
return $this->enqueued_webfonts;
}

/**
* Get the list of all fonts.
*
* @return array
*/
public function get_all_webfonts() {
return array_merge( $this->get_registered_webfonts(), $this->get_enqueued_webfonts() );
}

/**
Expand All @@ -83,14 +109,31 @@ public function register_font( $font ) {
if ( $font ) {
$slug = $this->get_font_slug( $font );

if ( ! isset( $this->webfonts[ $slug ] ) ) {
$this->webfonts[ $slug ] = array();
if ( ! isset( $this->registered_webfonts[ $slug ] ) ) {
$this->registered_webfonts[ $slug ] = array();
}

$this->webfonts[ $slug ][] = $font;
$this->registered_webfonts[ $slug ][] = $font;
}
}

public function enqueue_font( $font_family ) {
$slug = $this->get_font_slug( $font_family );

if ( isset( $this->enqueued_webfonts[ $slug ] ) ) {
_doing_it_wrong( __FUNCTION__, sprintf( __( 'The "%s" font family is already enqueued.' ), $slug ), '6.0.0' );
return false;
}

if ( ! isset( $this->registered_webfonts[ $slug ] ) ) {
_doing_it_wrong( __FUNCTION__, sprintf( __( 'The "%s" font family is not registered.' ), $slug ), '6.0.0' );
return false;
}

$this->enqueued_webfonts[ $slug ] = $this->registered_webfonts[ $slug ];
unset( $this->registered_webfonts[ $slug ] );
}

/**
* Get the font slug.
*
Expand Down Expand Up @@ -210,7 +253,7 @@ public function register_provider( $provider, $class ) {
*/
public function generate_and_enqueue_styles() {
// Generate the styles.
$styles = $this->generate_styles();
$styles = $this->generate_styles( $this->get_enqueued_webfonts() );

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

// Bail out if there are no styles to enqueue.
if ( '' === $styles ) {
Expand All @@ -247,14 +290,14 @@ public function generate_and_enqueue_editor_styles() {
*
* @return string $styles Generated styles.
*/
public function generate_styles() {
public function generate_styles( $font_families ) {
$styles = '';
$providers = $this->get_providers();

$webfonts = array();

// Grab only the font face declarations from $font_families.
foreach ( $this->get_fonts() as $font_family ) {
foreach ( $font_families as $font_family ) {
foreach ( $font_family as $font_face ) {
$webfonts[] = $font_face;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ function gutenberg_register_webfonts_from_theme_json() {
* @return array The global styles with missing fonts data.
*/
function gutenberg_add_registered_webfonts_to_theme_json( $data ) {
$font_families_registered = wp_webfonts()->get_fonts();
$font_families_registered = wp_webfonts()->get_all_webfonts();

// Make sure the path to settings.typography.fontFamilies.theme exists
// before adding missing fonts.
Expand Down
4 changes: 4 additions & 0 deletions lib/compat/wordpress-6.0/webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ function wp_register_webfont( array $webfont ) {
wp_webfonts()->register_font( $webfont );
}

function wp_enqueue_webfont( $font_family ) {
wp_webfonts()->enqueue_font( $font_family );
}

/**
* Registers a custom font service provider.
*
Expand Down

0 comments on commit feb1de1

Please sign in to comment.