diff --git a/lib/compat/wordpress-6.0/class-wp-webfonts.php b/lib/compat/wordpress-6.0/class-wp-webfonts.php index 9b5114b7452bb..5a28b02b50a25 100644 --- a/lib/compat/wordpress-6.0/class-wp-webfonts.php +++ b/lib/compat/wordpress-6.0/class-wp-webfonts.php @@ -19,6 +19,15 @@ class WP_Webfonts { */ private static $webfonts = array(); + /** + * An array of used webfonts. + * + * @static + * @access private + * @var array + */ + private static $used_webfonts = array(); + /** * An array of registered providers. * @@ -51,12 +60,77 @@ public function init() { $this->stylesheet_handle = 'webfonts'; $hook = 'wp_enqueue_scripts'; } + + add_action( 'save_post_wp_template', array( $this, 'save_used_webfonts_for_template' ), 10, 2 ); + 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 save_used_webfonts_for_template( $post_id, $post ) { + $blocks = _flatten_blocks( parse_blocks( $post->post_content ) ); + + foreach ( $blocks as $block ) { + $this->register_used_font_family_from_block( $block ); + } + + $this->register_used_font_families_on_global_styles(); + + update_option( 'gutenberg_used_webfonts', self::$used_webfonts ); + } + + private function register_used_font_family( $setting ) { + if ( isset( $setting['typography'] ) && isset( $setting['typography']['fontFamily'] ) ) { + $font_family_custom = $setting['typography']['fontFamily']; + + if ( strpos( $font_family_custom, 'var:preset|font-family|' ) !== false ) { + $index_to_splice = strrpos( $font_family_custom, '|' ) + 1; + $font_family_slug = _wp_to_kebab_case( substr( $font_family_custom, $index_to_splice ) ); + + self::$used_webfonts[ $font_family_slug ] = 1; + } + } + } + + public function register_used_font_families_on_global_styles() { + $global_styles = gutenberg_get_global_styles(); + + foreach ( $global_styles['blocks'] as $id => $global_block_setting ) { + $this->register_used_font_family( $global_block_setting ); + } + + // Register used webfonts from elements. + foreach ( $global_styles['elements'] as $id => $global_element_setting ) { + $this->register_used_font_family( $global_element_setting ); + } + + // Register used webfonts from global styles. + $this->register_used_font_family( $global_styles ); + } + + public function register_used_font_family_from_block( $block ) { + if ( isset( $block['attrs']['fontFamily'] ) ) { + self::$used_webfonts[ $block['attrs']['fontFamily'] ] = 1; + } + } + + private function filter_unused_webfonts() { + $registered_webfonts = $this->get_fonts(); + $used_webfonts = get_option( 'gutenberg_used_webfonts', array() ); + + foreach ( $registered_webfonts as $id => $webfont ) { + $font_name = _wp_to_kebab_case( $webfont['font-family'] ); + + if ( ! isset( $used_webfonts[ $font_name ] ) ) { + unset( $registered_webfonts[ $id ] ); + } + } + + self::$webfonts = $registered_webfonts; + } + /** * Get the list of fonts. * @@ -202,6 +276,8 @@ public function register_provider( $provider, $class ) { * Generate and enqueue webfonts styles. */ public function generate_and_enqueue_styles() { + $this->filter_unused_webfonts(); + // Generate the styles. $styles = $this->generate_styles();