فهرست منبع

Eliminated Blockbase font provider. Filtered Blockbase-handled fonts from Jetpack Provider.

Jason Crist 3 سال پیش
والد
کامیت
37915b2eea

+ 0 - 1
blockbase/functions.php

@@ -1,5 +1,4 @@
 <?php
-
 if ( ! function_exists( 'blockbase_support' ) ) :
 	function blockbase_support() {
 		// Alignwide and alignfull classes in the block editor.

+ 0 - 138
blockbase/inc/fonts/blockbase-fonts-provider.php

@@ -1,138 +0,0 @@
-<?php
-
-/**
- * Blockbase Font Provider
- */
-class Blockbase_Fonts_Provider extends \WP_Webfonts_Provider {
-
-	/**
-	 * Font provider ID.
-	 *
-	 * @var string
-	 */
-	protected $id = 'blockbase-fonts';
-
-	protected function get_style_css( $family ) {
-		$contents = file_get_contents( get_template_directory() . '/assets/fonts/' . $family . '/font-face.css' );
-		return str_replace( 'src: url(./', 'src: url(' . get_template_directory_uri() . '/assets/fonts/' . $family . '/', $contents );
-	}
-
-	/**
-	 * Gets the `@font-face` CSS styles for enqueued Blockbase Fonts.
-	 *
-	 * @return string The `@font-face` CSS.
-	 */
-	public function get_css() {
-		$css = '';
-
-		foreach ( $this->webfonts as $webfont ) {
-			$font_slug = \WP_Webfonts::get_font_slug( $webfont['font-family'] );
-			$css      .= $this->get_style_css( $font_slug );
-		}
-
-		return $css;
-	}
-
-}
-
-function provider_enqueue_global_styles_fonts() {
-	if ( is_admin() || ! function_exists( 'wp_enqueue_webfont' ) ) {
-		return;
-	}
-
-	$global_styles_fonts = collect_fonts_from_global_styles();
-
-	foreach ( $global_styles_fonts as $font ) {
-		$font_is_registered = is_font_family_registered( $font );
-		if ( $font_is_registered ) {
-			wp_enqueue_webfont( $font );
-		}
-	}
-}
-
-function provider_enqueue_block_fonts( $content, $parsed_block ) {
-	if ( ! is_admin() && isset( $parsed_block['attrs']['fontFamily'] ) ) {
-
-		$font               = $parsed_block['attrs']['fontFamily'];
-		$font_is_registered = is_font_family_registered( $font );
-
-		if ( $font_is_registered ) {
-			wp_enqueue_webfont( $font );
-		}
-	}
-
-	return $content;
-}
-
-/**
- * Check if a font family is registered (verifying that it can be enqueued).
- *
- * This function will not be needed if/when WP_Webfonts provides this functionality.
- *
- * @link https://github.com/WordPress/gutenberg/pull/39988
- * @link https://github.com/WordPress/gutenberg/blob/e94fffae0684aa5a6dc370ce3eba262cb77071d9/lib/experimental/class-wp-webfonts.php#L217
- *
- * @param string $font_family_name Name of font family.
- * @return boolean|void Whether the font family is registered, or void if WP_Webfonts is not available.
- */
-function is_font_family_registered( $font_slug ) {
-	if ( ! function_exists( 'wp_webfonts' ) ) {
-		return;
-	}
-
-	$wp_webfonts = wp_webfonts();
-
-	return isset( $wp_webfonts->get_registered_webfonts()[ $font_slug ] );
-}
-
-function register_blockbase_fonts_provider() {
-
-	if ( ! function_exists( 'wp_register_webfont_provider' ) ) {
-		return;
-	}
-
-	$settings = gutenberg_get_global_settings();
-
-	// Bail out early if there are no settings for webfonts.
-	if ( empty( $settings['typography'] ) || empty( $settings['typography']['fontFamilies'] ) ) {
-		return;
-	}
-
-	wp_register_webfont_provider( 'blockbase-fonts', 'Blockbase_Fonts_Provider' );
-
-	foreach ( $settings['typography']['fontFamilies'] as $font_families ) {
-		foreach ( $font_families as $font ) {
-
-			// NOTE: Implemented correctly this would then operate on a `fontFace` collection.
-			// The format of the fontface in Blockbase's theme.json is abnormal so that CORE doesn't try to process
-			// them as local files (and allowing Blockbase to manage that itself).  Therefore 'provider' is defined
-			// incorrectly.  This should be fixable in WP 6.1
-
-			// Skip if the provider isn't blockbase-fonts
-			if ( ! array_key_exists( 'provider', $font ) || ! 'blockbase-fonts' === $font['provider'] ) {
-				continue;
-			}
-
-			$font['fontFamily'] = $font['name'];
-			unset( $font['name'] );
-
-			// Convert keys to kebab-case.
-			foreach ( $font as $property => $value ) {
-				$kebab_case          = _wp_to_kebab_case( $property );
-				$font[ $kebab_case ] = $value;
-				if ( $kebab_case !== $property ) {
-					unset( $font[ $property ] );
-				}
-			}
-
-			wp_webfonts()->register_webfont( $font );
-		}
-	}
-
-}
-add_action( 'init', 'register_blockbase_fonts_provider', 100 );
-add_action( 'init', 'provider_enqueue_global_styles_fonts', 101 );
-
-// NOTE: As far as I can tell you can't assign a font-family at the individual block level
-// which is what this logic is for.  This may someday be necessary but I don't believe it is now.
-// add_filter( 'pre_render_block', 'provider_enqueue_block_fonts', 10, 2 );

+ 2 - 9
blockbase/inc/fonts/custom-font-migration.php

@@ -31,9 +31,6 @@ function migrate_blockbase_custom_fonts() {
 		if ( strpos( $font_family['slug'], 'body' ) !== false && array_key_exists( 'fontSlug', $font_family ) ) {
 			$body_font_slug = $font_family['fontSlug'];
 		}
-		if ( array_key_exists( 'provider', $font_family ) && 'blockbase-fonts' === $font_family['provider'] ) {
-			$blockbase_provider_fonts_count++;
-		}
 	}
 
 	if ( ! $body_font_slug && ! $heading_font_slug && $blockbase_provider_fonts_count > 0 ) {
@@ -84,12 +81,8 @@ function migrate_blockbase_custom_fonts() {
 		);
 	}
 
-	if ( 0 === $blockbase_provider_fonts_count ) {
-		// Set new typography settings (copy from Blockbase theme.json file)
-		$parent_theme_json_data                     = json_decode( file_get_contents( get_template_directory() . '/theme.json' ), true );
-		$parent_theme                               = new WP_Theme_JSON( $parent_theme_json_data );
-		$parent_font_families                       = $parent_theme->get_data()['settings']['typography']['fontFamilies'];
-		$new_settings['typography']['fontFamilies'] = $parent_font_families;
+	if ( $heading_font_slug || $body_font_slug ) {
+		unset( $new_settings['typography']['fontFamilies'] );
 	}
 
 	update_global_styles( $new_settings, $new_styles );

+ 37 - 32
blockbase/inc/fonts/custom-fonts.php

@@ -6,6 +6,10 @@ require get_template_directory() . '/inc/customizer/wp-customize-fonts.php';
 // Font Migration
 require get_template_directory() . '/inc/fonts/custom-font-migration.php';
 
+add_action( 'init', 'enqueue_global_styles_fonts', 100 );
+add_action( 'admin_init', 'enqueue_fse_font_styles' );
+add_filter( 'jetpack_google_fonts_list', 'blockbase_filter_jetpack_google_fonts_list' );
+
 /**
  * Get the CSS containing font_face values for a given slug
  *
@@ -112,30 +116,19 @@ function extract_font_slug_from_setting( $setting ) {
  * @return array Collection of all font slugs defined in the theme.json file
  */
 function collect_fonts_from_blockbase() {
-	$font_family_slugs = array();
-
-	// It would be nice to be able to get the global settings of the PARENT theme.
-	// As it stands we can only get the values of the THEME but those are parent/child combined
-	// The only sure way to get the values is directly from the file.
-	if ( function_exists( 'gutenberg_get_global_settings' ) ) {
-		$font_families = gutenberg_get_global_settings( array( 'typography', 'fontFamilies' ) );
-	} else {
-		$font_families = wp_get_global_settings( array( 'typography', 'fontFamilies' ) );
-	}
-	if ( isset( $font_families['custom'] ) && is_array( $font_families['custom'] ) ) {
-		$font_families = $font_families['custom'];
-	} else {
-		$font_families = $font_families['theme'];
-	}
+	$fonts                  = array();
+	$parent_theme_json_data = json_decode( file_get_contents( get_template_directory() . '/theme.json' ), true );
+	$parent_theme           = new WP_Theme_JSON( $parent_theme_json_data );
+	$font_families          = $parent_theme->get_data()['settings']['typography']['fontFamilies'];
 
 	foreach ( $font_families as $font ) {
 		// Only pick it up if we're claiming it as ours to manage
 		if ( array_key_exists( 'provider', $font ) && 'blockbase-fonts' === $font['provider'] ) {
-			$font_family_slugs[] = $font['slug'];
+			$fonts[] = $font;
 		}
 	}
 
-	return $font_family_slugs;
+	return $fonts;
 }
 
 /**
@@ -144,17 +137,20 @@ function collect_fonts_from_blockbase() {
  * @return void
  */
 function enqueue_global_styles_fonts() {
-	$fonts;
-	$font_css = '';
+	$font_slugs = array();
+	$font_css   = '';
 
 	if ( is_admin() ) {
-		$fonts = collect_fonts_from_blockbase();
+		$font_families = collect_fonts_from_blockbase();
+		foreach ( $font_families as $font_family ) {
+			$font_slugs[] = $font_family['slug'];
+		}
 	} else {
-		$fonts = collect_fonts_from_global_styles();
+		$font_slugs = collect_fonts_from_global_styles();
 	}
 
-	foreach ( $fonts as $font ) {
-		$font_css .= get_style_css( $font );
+	foreach ( $font_slugs as $font_slug ) {
+		$font_css .= get_style_css( $font_slug );
 	}
 
 	// Bail out if there are no styles to enqueue.
@@ -168,7 +164,6 @@ function enqueue_global_styles_fonts() {
 
 	// Add the styles to the stylesheet.
 	wp_add_inline_style( 'blockbase_font_faces', $font_css );
-
 }
 
 /**
@@ -179,18 +174,28 @@ function enqueue_fse_font_styles( $fonts ) {
 	$font_css = '';
 
 	foreach ( $fonts as $font ) {
-		$font_css .= get_style_css( $font );
+		$font_css .= get_style_css( $font['slug'] );
 	}
 
 	wp_enqueue_style( 'wp-block-library' );
 	wp_add_inline_style( 'wp-block-library', $font_css );
 }
 
-// If we have a Webfonts Provider then leverage a Custom Fonts Provider
-// Otherwise we'll enqueu the necessary font faces manually.
-if ( class_exists( '\WP_Webfonts_Provider' ) ) {
-	require get_template_directory() . '/inc/fonts/blockbase-fonts-provider.php';
-} else {
-	add_action( 'init', 'enqueue_global_styles_fonts' );
-	add_action( 'admin_init', 'enqueue_fse_font_styles' );
+/**
+ * Jetpack may attempt to register fonts for the Google Font Provider.
+ * Filter out any fonts that Blockbase is already handling.
+ */
+function blockbase_filter_jetpack_google_fonts_list( $list_to_filter ) {
+	$filtered_list           = array();
+	$blockbase_fonts         = collect_fonts_from_blockbase();
+	$blockbase_font_families = array();
+	foreach ( $blockbase_fonts as $font ) {
+		$blockbase_font_families[] = $font['name'];
+	}
+	foreach ( $list_to_filter as $font_family ) {
+		if ( ! in_array( $font_family, $blockbase_font_families, true ) ) {
+			$filtered_list[] = $font_family;
+		}
+	}
+	return $filtered_list;
 }