Skip to content

Commit

Permalink
Load globally used fonts and regenerate cache after changes
Browse files Browse the repository at this point in the history
  • Loading branch information
zaguiini committed Mar 2, 2022
1 parent 59afbcc commit b3ce575
Showing 1 changed file with 49 additions and 24 deletions.
73 changes: 49 additions & 24 deletions lib/compat/wordpress-6.0/class-wp-webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ class WP_Webfonts {
*/
private static $webfont_cache_option = 'gutenberg_used_webfonts';

/**
* The name of the globally used webfont cache option name.
*
* @static
* @access private
* @var string
*/
private static $global_webfont_cache_option = 'gutenberg_globally_used_webfonts';

/**
* An array of registered providers.
*
Expand Down Expand Up @@ -71,19 +80,47 @@ public function init() {
}

add_action( 'init', array( $this, 'register_current_template_filter' ) );
add_action( 'init', array( $this, 'get_globally_used_fonts' ) );

add_action( 'switch_theme', array( $this, 'invalidate_used_webfonts_cache' ) );
add_action( 'save_post_wp_template', array( $this, 'invalidate_used_webfonts_cache' ) );
add_action( 'save_post_wp_template_part', array( $this, 'invalidate_used_webfonts_cache' ) );

add_filter( 'the_content', array( $this, 'register_webfonts_used_in_content' ) );

add_filter( 'rest_request_after_callbacks', array( $this, 'invalidate_globally_used_webfonts_cache' ), 10, 3 );

add_action( $hook, array( $this, 'generate_and_enqueue_styles' ) );

// Enqueue webfonts in the block editor.
add_action( 'admin_init', array( $this, 'generate_and_enqueue_editor_styles' ) );
}

public function invalidate_globally_used_webfonts_cache( $response, $handler, $request ) {
// TODO: identify Global Styles request and invalidate the global fonts cache.
// Or, maybe, build the cache here... Who knows.
return $response;
}

public function get_globally_used_fonts() {
$globally_used_webfonts = get_option( self::$global_webfont_cache_option );

if ( $globally_used_webfonts ) {
self::$used_webfonts = array_merge( self::$used_webfonts, $globally_used_webfonts );
return;
}

$globally_used_webfonts = $this->get_globally_used_webfonts();
update_option( self::$global_webfont_cache_option, $globally_used_webfonts );

self::$used_webfonts = array_merge( self::$used_webfonts, $globally_used_webfonts );
}

/**
* Hook into every possible template so we can get the full template object on page load.
*
* @return void
*/
public function register_current_template_filter() {
$templates = get_block_templates( array(), 'wp_template' );

Expand All @@ -97,19 +134,29 @@ function() use ( $template ) {
}
}

/**
* Look up used webfonts cache for the template that triggered the hook
* registered on `register_current_template_filter`.
*
* If the webfonts array is not found there, parse the template, extract the webfonts
* and register it in the option.
*
* @param WP_Template $template The template that is about to be rendered.
* @return void
*/
public function register_used_webfonts_by_template( $template ) {
$used_webfonts_cache = get_option( self::$webfont_cache_option, array() );

if ( isset( $used_webfonts_cache[ $template->slug ] ) ) {
self::$used_webfonts = $used_webfonts_cache[ $template->slug ];
self::$used_webfonts = array_merge( self::$used_webfonts, $used_webfonts_cache[ $template->slug ] );
return;
}

$used_webfonts = $this->get_fonts_from_template( $template->content );
$used_webfonts_cache[ $template->slug ] = $used_webfonts;

update_option( self::$webfont_cache_option, $used_webfonts_cache );
self::$used_webfonts = $used_webfonts_cache[ $template->slug ];
self::$used_webfonts = array_merge( self::$used_webfonts, $used_webfonts_cache[ $template->slug ] );
}

/**
Expand Down Expand Up @@ -149,28 +196,6 @@ private function get_fonts_from_content( $content ) {
return $used_webfonts;
}

/**
* Set list of used fonts in the current page.
*
* @return void
*/
public function load_used_webfonts() {
self::$used_webfonts = $this->get_globally_used_webfonts();
$used_webfonts = get_option( self::$webfont_cache_option, array() );

foreach ( $used_webfonts as $template => $webfonts ) {
add_filter(
$template . '_template',
function() use ( $webfonts ) {
self::$used_webfonts = array_merge(
self::$used_webfonts,
$webfonts
);
}
);
}
}

/**
* Get globally used webfonts.
*
Expand Down

0 comments on commit b3ce575

Please sign in to comment.