| 1 |
<?php |
| 2 |
/** |
| 3 |
* Font API's utility helpers. |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
* @subpackage Fonts API |
| 7 |
* @since X.X.X |
| 8 |
*/ |
| 9 |
|
| 10 |
if ( class_exists( 'WP_Fonts_Utils' ) ) { |
| 11 |
return; |
| 12 |
} |
| 13 |
|
| 14 |
/** |
| 15 |
* Utility helpers for the Fonts API. |
| 16 |
* |
| 17 |
* @since X.X.X |
| 18 |
*/ |
| 19 |
class WP_Fonts_Utils { |
| 20 |
|
| 21 |
/** |
| 22 |
* Converts the given font family into a handle. |
| 23 |
* |
| 24 |
* @since X.X.X |
| 25 |
* |
| 26 |
* @param string $font_family Font family to convert into a handle. |
| 27 |
* @return string|null The font family handle on success. Else, null. |
| 28 |
*/ |
| 29 |
public static function convert_font_family_into_handle( $font_family ) { |
| 30 |
if ( ! self::is_defined( $font_family ) ) { |
| 31 |
return null; |
| 32 |
} |
| 33 |
|
| 34 |
// If the font-family is a comma-separated list (example: "Inter, sans-serif" ), use just the first font. |
| 35 |
if ( str_contains( $font_family, ',' ) ) { |
| 36 |
$font_family = explode( ',', $font_family )[0]; |
| 37 |
} |
| 38 |
|
| 39 |
return sanitize_title( $font_family ); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Converts the given variation and its font-family into a handle. |
| 44 |
* |
| 45 |
* @since X.X.X |
| 46 |
* |
| 47 |
* @param string $font_family The font family's handle for this variation. |
| 48 |
* @param array $variation An array of variation properties. |
| 49 |
* @return string|null The variation handle. |
| 50 |
*/ |
| 51 |
public static function convert_variation_into_handle( $font_family, array $variation ) { |
| 52 |
$handle = ''; |
| 53 |
foreach ( array( 'font-weight', 'font-style' ) as $property ) { |
| 54 |
if ( ! array_key_exists( $property, $variation ) || ! static::is_defined( $variation[ $property ] ) ) { |
| 55 |
continue; |
| 56 |
} |
| 57 |
|
| 58 |
$handle .= ' ' . $variation[ $property ]; |
| 59 |
} |
| 60 |
|
| 61 |
if ( '' === $handle ) { |
| 62 |
trigger_error( 'Variant handle could not be determined as font-weight and/or font-style are require' ); |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
return sanitize_title( $font_family . $handle ); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Gets the font family from the variation. |
| 71 |
* |
| 72 |
* @since X.X.X |
| 73 |
* |
| 74 |
* @param array $variation An array of variation properties to search. |
| 75 |
* @return string|null The font family if defined. Else, null. |
| 76 |
*/ |
| 77 |
public static function get_font_family_from_variation( array $variation ) { |
| 78 |
return static::search_for_font_family( $variation ); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Checks if the given input is defined, i.e. meaning is a non-empty string. |
| 83 |
* |
| 84 |
* @since X.X.X |
| 85 |
* |
| 86 |
* @param string $input The input to check. |
| 87 |
* @return bool True when non-empty string. Else false. |
| 88 |
*/ |
| 89 |
public static function is_defined( $input ) { |
| 90 |
return ( is_string( $input ) && ! empty( $input ) ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Searches the variation array to extract the font family. |
| 95 |
* |
| 96 |
* @since X.X.X |
| 97 |
* |
| 98 |
* @param array $haystack An array of variation properties to search. |
| 99 |
* @return string|null The font family when found. Else, null. |
| 100 |
*/ |
| 101 |
private static function search_for_font_family( array $haystack ) { |
| 102 |
if ( array_key_exists( 'fontFamily', $haystack ) ) { |
| 103 |
$key = 'fontFamily'; |
| 104 |
} elseif ( array_key_exists( 'font-family', $haystack ) ) { |
| 105 |
$key = 'font-family'; |
| 106 |
} else { |
| 107 |
trigger_error( 'Font family not found.' ); |
| 108 |
return null; |
| 109 |
} |
| 110 |
|
| 111 |
if ( static::is_defined( $haystack[ $key ] ) ) { |
| 112 |
// If the font-family is a comma-separated list (example: "Inter, sans-serif" ), use just the first font. |
| 113 |
if ( strpos( $haystack[ $key ], ',' ) !== false ) { |
| 114 |
return explode( ',', $haystack[ $key ] )[0]; |
| 115 |
} |
| 116 |
return $haystack[ $key ]; |
| 117 |
} |
| 118 |
|
| 119 |
trigger_error( 'Font family not defined in the variation.' ); |
| 120 |
return null; |
| 121 |
} |
| 122 |
} |
| 123 |
|