wppaste
WordPress

WP_Font_Utils

Since
6.5.0
Source
wp-includes/fonts/class-wp-font-utils.php:20
A class of utilities for working with the Font Library.

Description

These utilities may change or be removed in the future and are intended for internal use only.

Compatibility

WordPress
since 6.5.0
  • 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).

Methods · 6

Source code

class WP_Font_Utils {	/**	 * Adds surrounding quotes to font family names that contain special characters.	 *	 * It follows the recommendations from the CSS Fonts Module Level 4.	 * @link https://www.w3.org/TR/css-fonts-4/#font-family-prop	 *	 * @since 6.5.0	 *	 * @param string $item A font family name.	 * @return string The font family name with surrounding quotes, if necessary.	 */	private static function maybe_add_quotes( $item ) {		// Matches strings that are not exclusively alphabetic characters or hyphens, and do not exactly follow the pattern generic(alphabetic characters or hyphens).		$regex = '/^(?!generic\([a-zA-Z\-]+\)$)(?!^[a-zA-Z\-]+$).+/';		$item  = trim( $item );		if ( preg_match( $regex, $item ) ) {			$item = trim( $item, "\"'" );			return '"' . $item . '"';		}		return $item;	} 	/**	 * Sanitizes and formats font family names.	 *	 * - Applies `sanitize_text_field`.	 * - Adds surrounding quotes to names containing any characters that are not alphabetic or dashes.	 *	 * It follows the recommendations from the CSS Fonts Module Level 4.	 * @link https://www.w3.org/TR/css-fonts-4/#font-family-prop	 *	 * @since 6.5.0	 * @access private	 *	 * @see sanitize_text_field()	 *	 * @param string $font_family Font family name(s), comma-separated.	 * @return string Sanitized and formatted font family name(s).	 */	public static function sanitize_font_family( $font_family ) {		if ( ! $font_family ) {			return '';		} 		$output          = sanitize_text_field( $font_family );		$formatted_items = array();		if ( str_contains( $output, ',' ) ) {			$items = explode( ',', $output );			foreach ( $items as $item ) {				$formatted_item = self::maybe_add_quotes( $item );				if ( ! empty( $formatted_item ) ) {					$formatted_items[] = $formatted_item;				}			}			return implode( ', ', $formatted_items );		}		return self::maybe_add_quotes( $output );	} 	/**	 * 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.	 *	 * @since 6.5.0	 * @access private	 *	 * @link https://drafts.csswg.org/css-fonts/#font-style-matching	 *	 * @param array $settings {	 *     Font face settings.	 *	 *     @type string $fontFamily   Font family name.	 *     @type string $fontStyle    Optional font style, defaults to 'normal'.	 *     @type string $fontWeight   Optional font weight, defaults to 400.	 *     @type string $fontStretch  Optional font stretch, defaults to '100%'.

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.