Skip to content

Commit

Permalink
Register and enqueue improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
hellofromtonya committed Aug 18, 2022
1 parent 1425f32 commit 1c4e7ea
Show file tree
Hide file tree
Showing 19 changed files with 2,077 additions and 688 deletions.
113 changes: 113 additions & 0 deletions lib/experimental/class-wp-webfonts-utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php
/**
* Webfont API's utility helpers.
*
* @package WordPress
* @subpackage WebFonts
* @since 6.1.0
*/

if ( class_exists( 'WP_Webfonts_Utils' ) ) {
return;
}

/**
* Utility helpers for the Webfonts API.
*
* @since 6.1.0
*/
class WP_Webfonts_Utils {

/**
* Converts the given font family into a handle.
*
* @since 6.1.0
*
* @param string $font_family Font family to convert into a handle.
* @return string|null The font family handle on success. Else, null.
*/
public static function convert_font_family_into_handle( $font_family ) {
if ( ! is_string( $font_family ) || empty( $font_family ) ) {
return null;
}

return sanitize_title( $font_family );
}

/**
* Converts the given variation and its font-family into a handle.
*
* @since 6.1.0
*
* @param string $font_family The font family's handle for this variation.
* @param array $variation An array of variation properties.
* @return string|null The variation handle.
*/
public static function convert_variation_into_handle( $font_family, array $variation ) {
$handle = '';
foreach ( array( 'font-weight', 'font-style' ) as $property ) {
if ( ! array_key_exists( $property, $variation ) || ! static::is_defined( $variation[ $property ] ) ) {
continue;
}

$handle .= ' ' . $variation[ $property ];
}

if ( '' === $handle ) {
trigger_error( 'Variant handle could not be determined as font-weight and/or font-style are require' );
return null;
}

return sanitize_title( $font_family . $handle );
}

/**
* Gets the font family from the variation.
*
* @since 6.1.0
*
* @param array $variation An array of variation properties to search.
* @return string|null The font family if defined. Else, null.
*/
public static function get_font_family_from_variation( array $variation ) {
return static::search_for_font_family( $variation );
}

/**
* Checks if the given input is defined, i.e. meaning is a non-empty string.
*
* @since 6.1.0
*
* @param string $input The input to check.
* @return bool True when non-empty string. Else false.
*/
public static function is_defined( $input ) {
return ( is_string( $input ) && ! empty( $input ) );
}

/**
* Searches the variation array to extract the font family.
*
* @since 6.1.0
*
* @param array $haystack An array of variation properties to search.
* @return string|null The font family when found. Else, null.
*/
private static function search_for_font_family( array $haystack ) {
if ( array_key_exists( 'fontFamily', $haystack ) ) {
$key = 'fontFamily';
} elseif ( array_key_exists( 'font-family', $haystack ) ) {
$key = 'font-family';
} else {
trigger_error( 'Font family not found.' );
return null;
}

if ( static::is_defined( $haystack[ $key ] ) ) {
return $haystack[ $key ];
}

trigger_error( 'Font family not defined in the variation.' );
return null;
}
}
87 changes: 54 additions & 33 deletions lib/experimental/class-wp-webfonts.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,36 +148,54 @@ public function remove_variation( $font_family_handle, $variation_handle ) {
* @param string $variation_handle Optional. The variation's handle. When none is provided, the
* handle will be dynamically generated.
* Default empty string.
* @return string|bool Variation handle on success. Else false.
* @return string|null Variation handle on success. Else null.
*/
public function add_variation( $font_family_handle, array $variation, $variation_handle = '' ) {
if ( ! WP_Webfonts_Utils::is_defined( $font_family_handle ) ) {
trigger_error( 'Font family handle must be a non-empty string.' );
return null;
}

if ( '' !== $variation_handle && ! WP_Webfonts_Utils::is_defined( $variation_handle ) ) {
trigger_error( 'Variant handle must be a non-empty string.' );
return null;
}

// Register the font family when it does not yet exist.
if ( ! isset( $this->registered[ $font_family_handle ] ) ) {
if ( ! $this->add( $font_family_handle, false ) ) {
return false;
return null;
}
}

$variation = $this->validate_variation( $font_family_handle, $variation );

// Variation validation failed.
if ( ! $variation ) {
return false;
return null;
}

if ( '' === $variation_handle ) {
$variation_handle = static::get_variation_handle( $font_family_handle, $variation );
$variation_handle = WP_Webfonts_Utils::convert_variation_into_handle( $font_family_handle, $variation );
if ( is_null( $variation_handle ) ) {
return null;
}
}

// Bail out if the variant is already registered.
if ( $this->is_variation_registered( $font_family_handle, $variation_handle ) ) {
return $variation_handle;
}

if ( $variation['src'] ) {
if ( array_key_exists( 'src', $variation ) ) {
$result = $this->add( $variation_handle, $variation['src'] );
} else {
$result = $this->add( $variation_handle );
$result = $this->add( $variation_handle, false );
}

// Bail out if the registration failed.
if ( ! $result ) {
return false;
return null;
}

$this->add_data( $variation_handle, 'font-properties', $variation );
Expand All @@ -190,6 +208,23 @@ public function add_variation( $font_family_handle, array $variation, $variation
return $variation_handle;
}

/**
* Checks if the variation is registered.
*
* @since 6.1.0
*
* @param string $font_family_handle The font family's handle for this variation.
* @param string $variant_handle Variation's handle.
* @return bool
*/
private function is_variation_registered( $font_family_handle, $variant_handle ) {
if ( ! isset( $this->registered[ $font_family_handle ] ) ) {
return array();
}

return in_array( $variant_handle, $this->registered[ $font_family_handle ]->deps );
}

/**
* Adds a variation as a dependency to the given font family.
*
Expand Down Expand Up @@ -248,7 +283,7 @@ private function validate_variation( $font_family_handle, $variation ) {

// Check the font-weight.
if ( ! is_string( $variation['font-weight'] ) && ! is_int( $variation['font-weight'] ) ) {
trigger_error( __( 'Webfont font weight must be a properly formatted string or integer.', 'gutenberg' ) );
trigger_error( __( 'Webfont font-weight must be a properly formatted string or integer.', 'gutenberg' ) );
return false;
}

Expand Down Expand Up @@ -425,38 +460,24 @@ private function get_enqueued_fonts_for_provider( $provider_id ) {
/**
* Get the font slug.
*
* @since 6.1.0
* @since X.X.X
* @deprecated Use WP_Webfonts_Utils::convert_font_family_into_handle()
*
* @param array|string $to_convert The value to convert into a slug. Expected as the web font's array
* or a font-family as a string.
* @return string|false The font slug on success, or false if the font-family cannot be determined.
*/
public static function get_font_slug( $to_convert ) {
if ( is_array( $to_convert ) ) {
if ( isset( $to_convert['font-family'] ) ) {
$to_convert = $to_convert['font-family'];
} elseif ( isset( $to_convert['fontFamily'] ) ) {
$to_convert = $to_convert['fontFamily'];
} else {
_doing_it_wrong( __METHOD__, __( 'Could not determine the font family name.', 'gutenberg' ), '6.0.0' );
return false;
}
}
_deprecated_function(
__METHOD__,
'X.X.X', // Gutenberg version, not Core as this method will not be backported to Core.
'Use WP_Webfonts_Utils::get_font_family_from_variation() to get the font family from an array and WP_Webfonts_Utils::convert_font_family_into_handle() to get the handle'
);

return sanitize_title( $to_convert );
}
$font_family = is_array( $to_convert )
? WP_Webfonts_Utils::get_font_family_from_variation( $to_convert )
: $to_convert;

/**
* Gets a handle for the given variation.
*
* @since 6.1.0
*
* @param string $font_family The font family's handle for this variation.
* @param array $variation An array of variation properties.
* @return string The variation handle.
*/
public static function get_variation_handle( $font_family, array $variation ) {
$handle = sprintf( '%s-%s', $font_family, implode( ' ', array( $variation['font-weight'], $variation['font-style'] ) ) );
return sanitize_title( $handle );
return WP_Webfonts_Utils::convert_font_family_into_handle( $font_family );
}
}
14 changes: 10 additions & 4 deletions lib/experimental/register-webfonts-from-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,16 @@ function gutenberg_register_webfonts_from_theme_json() {
}
}

$font_family_handle = array_key_exists( 'fontFamily', $font_face )
? WP_Webfonts::get_font_slug( $font_face['fontFamily'] )
: $font_family['slug'];
$handles[] = $font_family_handle;
$font_family = WP_Webfonts_Utils::get_font_family_from_variation( $font_face );
if ( empty( $font_family_handle ) ) {
$font_family = $font_family['slug'];
}
$font_family_handle = WP_Webfonts_Utils::convert_font_family_into_handle( $font_family );
if ( is_null( $font_family_handle ) ) {
_doing_it_wrong( __FUNCTION__, __( 'Font family not defined in the variation or "slug".', 'gutenberg' ), '6.1.0' );
}

$handles[] = $font_family_handle;
if ( ! array_key_exists( $font_family_handle, $webfonts ) ) {
$webfonts[ $font_family_handle ] = array();
}
Expand Down
Loading

0 comments on commit 1c4e7ea

Please sign in to comment.