diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index 9ce42192d72a9..b408c50f5c393 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -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. * @@ -71,6 +80,7 @@ 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' ) ); @@ -78,12 +88,39 @@ public function init() { 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' ); @@ -97,11 +134,21 @@ 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; } @@ -109,7 +156,7 @@ public function register_used_webfonts_by_template( $template ) { $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 ] ); } /** @@ -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. *