WP_Font_Utils::get_font_face_slug( array $settings ): string

This function’s access is marked private. This means it is not intended for use by plugin or theme developers, only in other core functions. It is listed here for completeness.

Generates a slug from font face properties, e.g. open sans;normal;400;100%;U+0-10FFFF

Description

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.

Parameters

$settingsarrayrequired
Font face settings.
  • fontFamily string
    Font family name.
  • fontStyle string
    Optional font style, defaults to 'normal'.
  • fontWeight string
    Optional font weight, defaults to 400.
  • fontStretch string
    Optional font stretch, defaults to '100%'.
  • unicodeRange string
    Optional unicode range, defaults to 'U+0-10FFFF'.

Return

string Font face slug.

Source

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 ) );
}

Changelog

VersionDescription
6.5.0Introduced.

User Contributed Notes

You must log in before being able to contribute a note or feedback.