WP_Font_Utils::get_font_face_slug()public staticWP 6.5.0

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.

Метод класса: WP_Font_Utils{}

Внутренняя функция — эта функция рассчитана на использование самим ядром. Не рекомендуется использовать эту функцию в своем коде.

Хуков нет.

Возвращает

Строку. Font face slug.

Использование

$result = WP_Font_Utils::get_font_face_slug( $settings );
$settings(массив) (обязательный)

Font face settings.

  • fontFamily(строка)
    Font family name.

  • fontStyle(строка)
    Optional font style.
    По умолчанию: 'normal'

  • fontWeight(строка)
    Optional font weight.
    По умолчанию: 400

  • fontStretch(строка)
    Optional font stretch.
    По умолчанию: '100%'

  • unicodeRange(строка)
    Optional unicode range.
    По умолчанию: 'U+0-10FFFF'

Список изменений

С версии 6.5.0 Введена.

Код WP_Font_Utils::get_font_face_slug() WP 6.7.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 ) );
}