Skip to content

Commit

Permalink
Use webfonts introspector when looking for fonts used in global styles
Browse files Browse the repository at this point in the history
  • Loading branch information
zaguiini committed Apr 7, 2022
1 parent 3899efa commit 29ee55d
Showing 1 changed file with 6 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function __construct() {
* so we only need to scan for webfonts when browsing as a guest.
*/
if ( ! is_admin() ) {
add_action( 'wp_loaded', array( $this, 'scan_global_styles_for_google_fonts' ) );
add_action( 'wp_loaded', array( $this, 'enqueue_global_styles_google_fonts' ) );
add_filter( 'pre_render_block', array( $this, 'scan_block_for_google_fonts' ), 10, 2 );
}
}
Expand Down Expand Up @@ -138,69 +138,13 @@ private function maybe_enqueue_font_family( $font_family_slug ) {
}

/**
* Scan global styles for Google fonts.
* Enqueue Google fonts used in global styles.
*/
public function scan_global_styles_for_google_fonts() {
$global_styles = gutenberg_get_global_styles();
public function enqueue_global_styles_google_fonts() {
$webfonts_found = \Automattic\Jetpack\Fonts\introspect_global_styles_webfonts();

// Look for fonts in block presets...
if ( isset( $global_styles['blocks'] ) ) {
foreach ( $global_styles['blocks'] as $setting ) {
$font_slug = $this->extract_font_slug_from_setting( $setting );

if ( $font_slug ) {
$this->maybe_enqueue_font_family( $font_slug );
}
}
}

// Look for fonts in HTML element presets...
if ( isset( $global_styles['elements'] ) ) {
foreach ( $global_styles['elements'] as $setting ) {
$font_slug = $this->extract_font_slug_from_setting( $setting );

if ( $font_slug ) {
$this->maybe_enqueue_font_family( $font_slug );
}
}
}

// Check if a global typography setting was defined.
$font_slug = $this->extract_font_slug_from_setting( $global_styles );

if ( $font_slug ) {
$this->maybe_enqueue_font_family( $font_slug );
}
}

/**
* Extract the font family slug from a settings object.
*
* @param object $setting The setting object.
*
* @return string|void
*/
private function extract_font_slug_from_setting( $setting ) {
if ( isset( $setting['typography'] ) && isset( $setting['typography']['fontFamily'] ) ) {
$font_family = $setting['typography']['fontFamily'];

// Full string: var(--wp--preset--font-family--slug).
// We do not care about the origin of the font, only its slug.
preg_match( '/font-family--(?P<slug>.+)\)$/', $font_family, $matches );

if ( isset( $matches['slug'] ) ) {
return $matches['slug'];
}

// Full string: var:preset|font-family|slug
// We do not care about the origin of the font, only its slug.
preg_match( '/font-family\|(?P<slug>.+)$/', $font_family, $matches );

if ( isset( $matches['slug'] ) ) {
return $matches['slug'];
}

return $font_family;
foreach ( $webfonts_found as $font_family_slug ) {
$this->maybe_enqueue_font_family( $font_family_slug );
}
}
}

0 comments on commit 29ee55d

Please sign in to comment.