WP_Font_Utils::get_font_face_slug( array $settings ): string
- Since
- 6.5.0
- Source
wp-includes/fonts/class-wp-font-utils.php:104
open sans;normal;400;100%;U+0-10FFFFDescription
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.
$fontFamilystringFont family name.$fontStylestringOptional font style, defaults to 'normal'.$fontWeightstringOptional font weight, defaults to 400.$fontStretchstringOptional font stretch, defaults to '100%'.$unicodeRangestringOptional 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
- Scaling
- Constant
- Instructions
- 8
- Plugin surface
- None
- Called by
- 2
Touches nothing outside its own arguments.
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5. The body compiles to 63.
Nothing here hands control to plugin code.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_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.
| When | Instructions | Calls it makes |
|---|---|---|
| always | 8 | none |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 74 | 8 | 1 | 11 more instructions than PHP 8.5 |
| 8.5 | 63 | 8 | 0 | |
| 8.4 | 63 | 8 | 0 | 17 fewer instructions than PHP 8.3 |
| 8.3 | 80 | 16 | 0 | |
| 8.2 | 80 | 16 | 0 | |
| 8.1 | 80 | 16 | 0 | 16 more instructions than PHP 7.4 |
| 7.4 | 64 | 64 | 0 |
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
- wp_parse_args()Merges user defined arguments into defaults array.
- sanitize_text_field()Sanitizes a string from user input or from the database.
Used by · 2
- WP_REST_Font_Faces_Controller::create_item()Creates a font face for the parent font family.
- WP_REST_Font_Faces_Controller::prepare_item_for_database()Prepares a single font face post for creation.
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.
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.