wppaste
WordPress

WP_Font_Utils::get_font_face_slug( array $settings ): string

Since
6.5.0
Source
wp-includes/fonts/class-wp-font-utils.php:104
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.

Compatibility

WordPress
since 6.5.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$settingsarray
Font face settings.
  • $fontFamilystring

    Font family name.
  • $fontStylestring

    Optional font style, defaults to 'normal'.
  • $fontWeightstring

    Optional font weight, defaults to 400.
  • $fontStretchstring

    Optional font stretch, defaults to '100%'.
  • $unicodeRangestring

    Optional unicode range, defaults to 'U+0-10FFFF'.

Return value

string
Font face slug.

Performance profile

How much work a call to WP_Font_Utils::get_font_face_slug() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Trivial

Touches nothing outside its own arguments.

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
8

Executed per call on PHP 8.5. The body compiles to 63.

Plugin surface
None

Nothing here hands control to plugin code.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksapply_filters()one call below WP_Font_Utils::get_font_face_slug()

Further down the call graph this can also reach option, cache, serialize, query and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 1 distinct outcome

One number would be a lie: the work depends on which branch runs. These are every distinct cost WP_Font_Utils::get_font_face_slug() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
always8none

Across PHP versions

PHPCompiledExecutedBranchesNotes
8.6-dev748111 more instructions than PHP 8.5
8.56380
8.4638017 fewer instructions than PHP 8.3
8.380160
8.280160
8.18016016 more instructions than PHP 7.4
7.464640

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Uses · 2

Used by · 2

Source code

	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

Introduced in 6.5.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 7.1.0 tag, from src/wp-includes/fonts/class-wp-font-utils.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.