WP_Font_Utils::get_font_face_slug
Generates a slug from font face properties, e.g. open sans;normal;400;100%;U+0-10FFFF
Used for comparison with other font faces in the same family, to prevent duplicates that would both match according the CSS font matching spec. Uses only simple case-insensitive matching for fontFamily and unicodeRange, so does not handle overlapping font-family lists or unicode ranges.
Method of the class: WP_Font_Utils{}
Internal function — this function is designed to be used by the kernel itself. It is not recommended to use this function in your code.
No Hooks.
Returns
String. Font face slug.
Usage
$result = WP_Font_Utils::get_font_face_slug( $settings );
- $settings(array) (required)
Font face settings.
-
fontFamily(string)
Font family name. -
fontStyle(string)
Optional font style.
Default: 'normal' -
fontWeight(string)
Optional font weight.
Default: 400 -
fontStretch(string)
Optional font stretch.
Default: '100%' - unicodeRange(string)
Optional unicode range.
Default: 'U+0-10FFFF'
-
Changelog
| Since 6.5.0 | Introduced. |
WP_Font_Utils::get_font_face_slug() WP Font Utils::get font face slug code WP 6.9.1
public static function get_font_face_slug( $settings ) {
$defaults = array(
'fontFamily' => '',
'fontStyle' => 'normal',
'fontWeight' => '400',
'fontStretch' => '100%',
'unicodeRange' => 'U+0-10FFFF',
);
$settings = wp_parse_args( $settings, $defaults );
if ( function_exists( 'mb_strtolower' ) ) {
$font_family = mb_strtolower( $settings['fontFamily'] );
} else {
$font_family = strtolower( $settings['fontFamily'] );
}
$font_style = strtolower( $settings['fontStyle'] );
$font_weight = strtolower( $settings['fontWeight'] );
$font_stretch = strtolower( $settings['fontStretch'] );
$unicode_range = strtoupper( $settings['unicodeRange'] );
// Convert weight keywords to numeric strings.
$font_weight = str_replace( array( 'normal', 'bold' ), array( '400', '700' ), $font_weight );
// Convert stretch keywords to numeric strings.
$font_stretch_map = array(
'ultra-condensed' => '50%',
'extra-condensed' => '62.5%',
'condensed' => '75%',
'semi-condensed' => '87.5%',
'normal' => '100%',
'semi-expanded' => '112.5%',
'expanded' => '125%',
'extra-expanded' => '150%',
'ultra-expanded' => '200%',
);
$font_stretch = str_replace( array_keys( $font_stretch_map ), array_values( $font_stretch_map ), $font_stretch );
$slug_elements = array( $font_family, $font_style, $font_weight, $font_stretch, $unicode_range );
$slug_elements = array_map(
function ( $elem ) {
// Remove quotes to normalize font-family names, and ';' to use as a separator.
$elem = trim( str_replace( array( '"', "'", ';' ), '', $elem ) );
// Normalize comma separated lists by removing whitespace in between items,
// but keep whitespace within items (e.g. "Open Sans" and "OpenSans" are different fonts).
// CSS spec for whitespace includes: U+000A LINE FEED, U+0009 CHARACTER TABULATION, or U+0020 SPACE,
// which by default are all matched by \s in PHP.
return preg_replace( '/,\s+/', ',', $elem );
},
$slug_elements
);
return sanitize_text_field( implode( ';', $slug_elements ) );
}