edit pages

This commit is contained in:
razusrest
2024-04-18 17:45:44 +05:45
parent f4e3b7c750
commit 46f1b89d3d
3923 changed files with 1838473 additions and 156 deletions

View File

@ -0,0 +1,295 @@
<?php
/**
* Font Collection class.
*
* This file contains the Font Collection class definition.
*
* @package WordPress
* @subpackage Fonts
* @since 6.5.0
*/
/**
* Font Collection class.
*
* @since 6.5.0
*
* @see wp_register_font_collection()
*/
final class WP_Font_Collection {
/**
* The unique slug for the font collection.
*
* @since 6.5.0
* @var string
*/
public $slug;
/**
* Font collection data.
*
* @since 6.5.0
* @var array|WP_Error|null
*/
private $data;
/**
* Font collection JSON file path or URL.
*
* @since 6.5.0
* @var string|null
*/
private $src;
/**
* WP_Font_Collection constructor.
*
* @since 6.5.0
*
* @param string $slug Font collection slug. May only contain alphanumeric characters, dashes,
* and underscores. See sanitize_title().
* @param array $args Font collection data. See wp_register_font_collection() for information on accepted arguments.
*/
public function __construct( string $slug, array $args ) {
$this->slug = sanitize_title( $slug );
if ( $this->slug !== $slug ) {
_doing_it_wrong(
__METHOD__,
/* translators: %s: Font collection slug. */
sprintf( __( 'Font collection slug "%s" is not valid. Slugs must use only alphanumeric characters, dashes, and underscores.' ), $slug ),
'6.5.0'
);
}
$required_properties = array( 'name', 'font_families' );
if ( isset( $args['font_families'] ) && is_string( $args['font_families'] ) ) {
// JSON data is lazy loaded by ::get_data().
$this->src = $args['font_families'];
unset( $args['font_families'] );
$required_properties = array( 'name' );
}
$this->data = $this->sanitize_and_validate_data( $args, $required_properties );
}
/**
* Retrieves the font collection data.
*
* @since 6.5.0
*
* @return array|WP_Error An array containing the font collection data, or a WP_Error on failure.
*/
public function get_data() {
if ( is_wp_error( $this->data ) ) {
return $this->data;
}
// If the collection uses JSON data, load it and cache the data/error.
if ( isset( $this->src ) ) {
$this->data = $this->load_from_json( $this->src );
}
if ( is_wp_error( $this->data ) ) {
return $this->data;
}
// Set defaults for optional properties.
$defaults = array(
'description' => '',
'categories' => array(),
);
return wp_parse_args( $this->data, $defaults );
}
/**
* Loads font collection data from a JSON file or URL.
*
* @since 6.5.0
*
* @param string $file_or_url File path or URL to a JSON file containing the font collection data.
* @return array|WP_Error An array containing the font collection data on success,
* else an instance of WP_Error on failure.
*/
private function load_from_json( $file_or_url ) {
$url = wp_http_validate_url( $file_or_url );
$file = file_exists( $file_or_url ) ? wp_normalize_path( realpath( $file_or_url ) ) : false;
if ( ! $url && ! $file ) {
// translators: %s: File path or URL to font collection JSON file.
$message = __( 'Font collection JSON file is invalid or does not exist.' );
_doing_it_wrong( __METHOD__, $message, '6.5.0' );
return new WP_Error( 'font_collection_json_missing', $message );
}
$data = $url ? $this->load_from_url( $url ) : $this->load_from_file( $file );
if ( is_wp_error( $data ) ) {
return $data;
}
$data = array(
'name' => $this->data['name'],
'font_families' => $data['font_families'],
);
if ( isset( $this->data['description'] ) ) {
$data['description'] = $this->data['description'];
}
if ( isset( $this->data['categories'] ) ) {
$data['categories'] = $this->data['categories'];
}
return $data;
}
/**
* Loads the font collection data from a JSON file path.
*
* @since 6.5.0
*
* @param string $file File path to a JSON file containing the font collection data.
* @return array|WP_Error An array containing the font collection data on success,
* else an instance of WP_Error on failure.
*/
private function load_from_file( $file ) {
$data = wp_json_file_decode( $file, array( 'associative' => true ) );
if ( empty( $data ) ) {
return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection JSON file contents.' ) );
}
return $this->sanitize_and_validate_data( $data, array( 'font_families' ) );
}
/**
* Loads the font collection data from a JSON file URL.
*
* @since 6.5.0
*
* @param string $url URL to a JSON file containing the font collection data.
* @return array|WP_Error An array containing the font collection data on success,
* else an instance of WP_Error on failure.
*/
private function load_from_url( $url ) {
// Limit key to 167 characters to avoid failure in the case of a long URL.
$transient_key = substr( 'wp_font_collection_url_' . $url, 0, 167 );
$data = get_site_transient( $transient_key );
if ( false === $data ) {
$response = wp_safe_remote_get( $url );
if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
return new WP_Error(
'font_collection_request_error',
sprintf(
// translators: %s: Font collection URL.
__( 'Error fetching the font collection data from "%s".' ),
$url
)
);
}
$data = json_decode( wp_remote_retrieve_body( $response ), true );
if ( empty( $data ) ) {
return new WP_Error( 'font_collection_decode_error', __( 'Error decoding the font collection data from the HTTP response JSON.' ) );
}
// Make sure the data is valid before storing it in a transient.
$data = $this->sanitize_and_validate_data( $data, array( 'font_families' ) );
if ( is_wp_error( $data ) ) {
return $data;
}
set_site_transient( $transient_key, $data, DAY_IN_SECONDS );
}
return $data;
}
/**
* Sanitizes and validates the font collection data.
*
* @since 6.5.0
*
* @param array $data Font collection data to sanitize and validate.
* @param array $required_properties Required properties that must exist in the passed data.
* @return array|WP_Error Sanitized data if valid, otherwise a WP_Error instance.
*/
private function sanitize_and_validate_data( $data, $required_properties = array() ) {
$schema = self::get_sanitization_schema();
$data = WP_Font_Utils::sanitize_from_schema( $data, $schema );
foreach ( $required_properties as $property ) {
if ( empty( $data[ $property ] ) ) {
$message = sprintf(
// translators: 1: Font collection slug, 2: Missing property name, e.g. "font_families".
__( 'Font collection "%1$s" has missing or empty property: "%2$s".' ),
$this->slug,
$property
);
_doing_it_wrong( __METHOD__, $message, '6.5.0' );
return new WP_Error( 'font_collection_missing_property', $message );
}
}
return $data;
}
/**
* Retrieves the font collection sanitization schema.
*
* @since 6.5.0
*
* @return array Font collection sanitization schema.
*/
private static function get_sanitization_schema() {
return array(
'name' => 'sanitize_text_field',
'description' => 'sanitize_text_field',
'font_families' => array(
array(
'font_family_settings' => array(
'name' => 'sanitize_text_field',
'slug' => static function ( $value ) {
return _wp_to_kebab_case( sanitize_title( $value ) );
},
'fontFamily' => 'WP_Font_Utils::sanitize_font_family',
'preview' => 'sanitize_url',
'fontFace' => array(
array(
'fontFamily' => 'sanitize_text_field',
'fontStyle' => 'sanitize_text_field',
'fontWeight' => 'sanitize_text_field',
'src' => static function ( $value ) {
return is_array( $value )
? array_map( 'sanitize_text_field', $value )
: sanitize_text_field( $value );
},
'preview' => 'sanitize_url',
'fontDisplay' => 'sanitize_text_field',
'fontStretch' => 'sanitize_text_field',
'ascentOverride' => 'sanitize_text_field',
'descentOverride' => 'sanitize_text_field',
'fontVariant' => 'sanitize_text_field',
'fontFeatureSettings' => 'sanitize_text_field',
'fontVariationSettings' => 'sanitize_text_field',
'lineGapOverride' => 'sanitize_text_field',
'sizeAdjust' => 'sanitize_text_field',
'unicodeRange' => 'sanitize_text_field',
),
),
),
'categories' => array( 'sanitize_title' ),
),
),
'categories' => array(
array(
'name' => 'sanitize_text_field',
'slug' => 'sanitize_title',
),
),
);
}
}

View File

@ -0,0 +1,173 @@
<?php
/**
* WP_Font_Face_Resolver class.
*
* @package WordPress
* @subpackage Fonts
* @since 6.4.0
*/
/**
* The Font Face Resolver abstracts the processing of different data sources
* (such as theme.json) for processing within the Font Face.
*
* This class is for internal core usage and is not supposed to be used by
* extenders (plugins and/or themes).
*
* @access private
*/
class WP_Font_Face_Resolver {
/**
* Gets fonts defined in theme.json.
*
* @since 6.4.0
*
* @return array Returns the font-families, each with their font-face variations.
*/
public static function get_fonts_from_theme_json() {
$settings = wp_get_global_settings();
// Bail out early if there are no font settings.
if ( empty( $settings['typography']['fontFamilies'] ) ) {
return array();
}
return static::parse_settings( $settings );
}
/**
* Parse theme.json settings to extract font definitions with variations grouped by font-family.
*
* @since 6.4.0
*
* @param array $settings Font settings to parse.
* @return array Returns an array of fonts, grouped by font-family.
*/
private static function parse_settings( array $settings ) {
$fonts = array();
foreach ( $settings['typography']['fontFamilies'] as $font_families ) {
foreach ( $font_families as $definition ) {
// Skip if "fontFace" is not defined, meaning there are no variations.
if ( empty( $definition['fontFace'] ) ) {
continue;
}
// Skip if "fontFamily" is not defined.
if ( empty( $definition['fontFamily'] ) ) {
continue;
}
$font_family_name = static::maybe_parse_name_from_comma_separated_list( $definition['fontFamily'] );
// Skip if no font family is defined.
if ( empty( $font_family_name ) ) {
continue;
}
$fonts[] = static::convert_font_face_properties( $definition['fontFace'], $font_family_name );
}
}
return $fonts;
}
/**
* Parse font-family name from comma-separated lists.
*
* If the given `fontFamily` is a comma-separated lists (example: "Inter, sans-serif" ),
* parse and return the fist font from the list.
*
* @since 6.4.0
*
* @param string $font_family Font family `fontFamily' to parse.
* @return string Font-family name.
*/
private static function maybe_parse_name_from_comma_separated_list( $font_family ) {
if ( str_contains( $font_family, ',' ) ) {
$font_family = explode( ',', $font_family )[0];
}
return trim( $font_family, "\"'" );
}
/**
* Converts font-face properties from theme.json format.
*
* @since 6.4.0
*
* @param array $font_face_definition The font-face definitions to convert.
* @param string $font_family_property The value to store in the font-face font-family property.
* @return array Converted font-face properties.
*/
private static function convert_font_face_properties( array $font_face_definition, $font_family_property ) {
$converted_font_faces = array();
foreach ( $font_face_definition as $font_face ) {
// Add the font-family property to the font-face.
$font_face['font-family'] = $font_family_property;
// Converts the "file:./" src placeholder into a theme font file URI.
if ( ! empty( $font_face['src'] ) ) {
$font_face['src'] = static::to_theme_file_uri( (array) $font_face['src'] );
}
// Convert camelCase properties into kebab-case.
$font_face = static::to_kebab_case( $font_face );
$converted_font_faces[] = $font_face;
}
return $converted_font_faces;
}
/**
* Converts each 'file:./' placeholder into a URI to the font file in the theme.
*
* The 'file:./' is specified in the theme's `theme.json` as a placeholder to be
* replaced with the URI to the font file's location in the theme. When a "src"
* beings with this placeholder, it is replaced, converting the src into a URI.
*
* @since 6.4.0
*
* @param array $src An array of font file sources to process.
* @return array An array of font file src URI(s).
*/
private static function to_theme_file_uri( array $src ) {
$placeholder = 'file:./';
foreach ( $src as $src_key => $src_url ) {
// Skip if the src doesn't start with the placeholder, as there's nothing to replace.
if ( ! str_starts_with( $src_url, $placeholder ) ) {
continue;
}
$src_file = str_replace( $placeholder, '', $src_url );
$src[ $src_key ] = get_theme_file_uri( $src_file );
}
return $src;
}
/**
* Converts all first dimension keys into kebab-case.
*
* @since 6.4.0
*
* @param array $data The array to process.
* @return array Data with first dimension keys converted into kebab-case.
*/
private static function to_kebab_case( array $data ) {
foreach ( $data as $key => $value ) {
$kebab_case = _wp_to_kebab_case( $key );
$data[ $kebab_case ] = $value;
if ( $kebab_case !== $key ) {
unset( $data[ $key ] );
}
}
return $data;
}
}

View File

@ -0,0 +1,430 @@
<?php
/**
* WP_Font_Face class.
*
* @package WordPress
* @subpackage Fonts
* @since 6.4.0
*/
/**
* Font Face generates and prints `@font-face` styles for given fonts.
*
* @since 6.4.0
*/
class WP_Font_Face {
/**
* The font-face property defaults.
*
* @since 6.4.0
*
* @var string[]
*/
private $font_face_property_defaults = array(
'font-family' => '',
'font-style' => 'normal',
'font-weight' => '400',
'font-display' => 'fallback',
);
/**
* Valid font-face property names.
*
* @since 6.4.0
*
* @var string[]
*/
private $valid_font_face_properties = array(
'ascent-override',
'descent-override',
'font-display',
'font-family',
'font-stretch',
'font-style',
'font-weight',
'font-variant',
'font-feature-settings',
'font-variation-settings',
'line-gap-override',
'size-adjust',
'src',
'unicode-range',
);
/**
* Valid font-display values.
*
* @since 6.4.0
*
* @var string[]
*/
private $valid_font_display = array( 'auto', 'block', 'fallback', 'swap', 'optional' );
/**
* Array of font-face style tag's attribute(s)
* where the key is the attribute name and the
* value is its value.
*
* @since 6.4.0
*
* @var string[]
*/
private $style_tag_attrs = array();
/**
* Creates and initializes an instance of WP_Font_Face.
*
* @since 6.4.0
*/
public function __construct() {
if (
function_exists( 'is_admin' ) && ! is_admin()
&&
function_exists( 'current_theme_supports' ) && ! current_theme_supports( 'html5', 'style' )
) {
$this->style_tag_attrs = array( 'type' => 'text/css' );
}
}
/**
* Generates and prints the `@font-face` styles for the given fonts.
*
* @since 6.4.0
*
* @param array[][] $fonts Optional. The font-families and their font variations.
* See {@see wp_print_font_faces()} for the supported fields.
* Default empty array.
*/
public function generate_and_print( array $fonts ) {
$fonts = $this->validate_fonts( $fonts );
// Bail out if there are no fonts are given to process.
if ( empty( $fonts ) ) {
return;
}
$css = $this->get_css( $fonts );
/*
* The font-face CSS is contained within <style> tags and can only be interpreted
* as CSS in the browser. Using wp_strip_all_tags() is sufficient escaping
* to avoid malicious attempts to close </style> and open a <script>.
*/
$css = wp_strip_all_tags( $css );
// Bail out if there is no CSS to print.
if ( empty( $css ) ) {
return;
}
printf( $this->get_style_element(), $css );
}
/**
* Validates each of the font-face properties.
*
* @since 6.4.0
*
* @param array $fonts The fonts to valid.
* @return array Prepared font-faces organized by provider and font-family.
*/
private function validate_fonts( array $fonts ) {
$validated_fonts = array();
foreach ( $fonts as $font_faces ) {
foreach ( $font_faces as $font_face ) {
$font_face = $this->validate_font_face_declarations( $font_face );
// Skip if failed validation.
if ( false === $font_face ) {
continue;
}
$validated_fonts[] = $font_face;
}
}
return $validated_fonts;
}
/**
* Validates each font-face declaration (property and value pairing).
*
* @since 6.4.0
*
* @param array $font_face Font face property and value pairings to validate.
* @return array|false Validated font-face on success, or false on failure.
*/
private function validate_font_face_declarations( array $font_face ) {
$font_face = wp_parse_args( $font_face, $this->font_face_property_defaults );
// Check the font-family.
if ( empty( $font_face['font-family'] ) || ! is_string( $font_face['font-family'] ) ) {
// @todo replace with `wp_trigger_error()`.
_doing_it_wrong(
__METHOD__,
__( 'Font font-family must be a non-empty string.' ),
'6.4.0'
);
return false;
}
// Make sure that local fonts have 'src' defined.
if ( empty( $font_face['src'] ) || ( ! is_string( $font_face['src'] ) && ! is_array( $font_face['src'] ) ) ) {
// @todo replace with `wp_trigger_error()`.
_doing_it_wrong(
__METHOD__,
__( 'Font src must be a non-empty string or an array of strings.' ),
'6.4.0'
);
return false;
}
// Validate the 'src' property.
foreach ( (array) $font_face['src'] as $src ) {
if ( empty( $src ) || ! is_string( $src ) ) {
// @todo replace with `wp_trigger_error()`.
_doing_it_wrong(
__METHOD__,
__( 'Each font src must be a non-empty string.' ),
'6.4.0'
);
return false;
}
}
// Check the font-weight.
if ( ! is_string( $font_face['font-weight'] ) && ! is_int( $font_face['font-weight'] ) ) {
// @todo replace with `wp_trigger_error()`.
_doing_it_wrong(
__METHOD__,
__( 'Font font-weight must be a properly formatted string or integer.' ),
'6.4.0'
);
return false;
}
// Check the font-display.
if ( ! in_array( $font_face['font-display'], $this->valid_font_display, true ) ) {
$font_face['font-display'] = $this->font_face_property_defaults['font-display'];
}
// Remove invalid properties.
foreach ( $font_face as $property => $value ) {
if ( ! in_array( $property, $this->valid_font_face_properties, true ) ) {
unset( $font_face[ $property ] );
}
}
return $font_face;
}
/**
* Gets the style element for wrapping the `@font-face` CSS.
*
* @since 6.4.0
*
* @return string The style element.
*/
private function get_style_element() {
$attributes = $this->generate_style_element_attributes();
return "<style id='wp-fonts-local'{$attributes}>\n%s\n</style>\n";
}
/**
* Gets the defined <style> element's attributes.
*
* @since 6.4.0
*
* @return string A string of attribute=value when defined, else, empty string.
*/
private function generate_style_element_attributes() {
$attributes = '';
foreach ( $this->style_tag_attrs as $name => $value ) {
$attributes .= " {$name}='{$value}'";
}
return $attributes;
}
/**
* Gets the `@font-face` CSS styles for locally-hosted font files.
*
* This method does the following processing tasks:
* 1. Orchestrates an optimized `src` (with format) for browser support.
* 2. Generates the `@font-face` for all its fonts.
*
* @since 6.4.0
*
* @param array $font_faces The font-faces to generate @font-face CSS styles.
* @return string The `@font-face` CSS styles.
*/
private function get_css( $font_faces ) {
$css = '';
foreach ( $font_faces as $font_face ) {
// Order the font's `src` items to optimize for browser support.
$font_face = $this->order_src( $font_face );
// Build the @font-face CSS for this font.
$css .= '@font-face{' . $this->build_font_face_css( $font_face ) . '}' . "\n";
}
// Don't print the last newline character.
return rtrim( $css, "\n" );
}
/**
* Orders `src` items to optimize for browser support.
*
* @since 6.4.0
*
* @param array $font_face Font face to process.
* @return array Font-face with ordered src items.
*/
private function order_src( array $font_face ) {
if ( ! is_array( $font_face['src'] ) ) {
$font_face['src'] = (array) $font_face['src'];
}
$src = array();
$src_ordered = array();
foreach ( $font_face['src'] as $url ) {
// Add data URIs first.
if ( str_starts_with( trim( $url ), 'data:' ) ) {
$src_ordered[] = array(
'url' => $url,
'format' => 'data',
);
continue;
}
$format = pathinfo( $url, PATHINFO_EXTENSION );
$src[ $format ] = $url;
}
// Add woff2.
if ( ! empty( $src['woff2'] ) ) {
$src_ordered[] = array(
'url' => $src['woff2'],
'format' => 'woff2',
);
}
// Add woff.
if ( ! empty( $src['woff'] ) ) {
$src_ordered[] = array(
'url' => $src['woff'],
'format' => 'woff',
);
}
// Add ttf.
if ( ! empty( $src['ttf'] ) ) {
$src_ordered[] = array(
'url' => $src['ttf'],
'format' => 'truetype',
);
}
// Add eot.
if ( ! empty( $src['eot'] ) ) {
$src_ordered[] = array(
'url' => $src['eot'],
'format' => 'embedded-opentype',
);
}
// Add otf.
if ( ! empty( $src['otf'] ) ) {
$src_ordered[] = array(
'url' => $src['otf'],
'format' => 'opentype',
);
}
$font_face['src'] = $src_ordered;
return $font_face;
}
/**
* Builds the font-family's CSS.
*
* @since 6.4.0
*
* @param array $font_face Font face to process.
* @return string This font-family's CSS.
*/
private function build_font_face_css( array $font_face ) {
$css = '';
/*
* Wrap font-family in quotes if it contains spaces
* and is not already wrapped in quotes.
*/
if (
str_contains( $font_face['font-family'], ' ' ) &&
! str_contains( $font_face['font-family'], '"' ) &&
! str_contains( $font_face['font-family'], "'" )
) {
$font_face['font-family'] = '"' . $font_face['font-family'] . '"';
}
foreach ( $font_face as $key => $value ) {
// Compile the "src" parameter.
if ( 'src' === $key ) {
$value = $this->compile_src( $value );
}
// If font-variation-settings is an array, convert it to a string.
if ( 'font-variation-settings' === $key && is_array( $value ) ) {
$value = $this->compile_variations( $value );
}
if ( ! empty( $value ) ) {
$css .= "$key:$value;";
}
}
return $css;
}
/**
* Compiles the `src` into valid CSS.
*
* @since 6.4.0
*
* @param array $value Value to process.
* @return string The CSS.
*/
private function compile_src( array $value ) {
$src = '';
foreach ( $value as $item ) {
$src .= ( 'data' === $item['format'] )
? ", url({$item['url']})"
: ", url('{$item['url']}') format('{$item['format']}')";
}
$src = ltrim( $src, ', ' );
return $src;
}
/**
* Compiles the font variation settings.
*
* @since 6.4.0
*
* @param array $font_variation_settings Array of font variation settings.
* @return string The CSS.
*/
private function compile_variations( array $font_variation_settings ) {
$variations = '';
foreach ( $font_variation_settings as $key => $value ) {
$variations .= "$key $value";
}
return $variations;
}
}

View File

@ -0,0 +1,142 @@
<?php
/**
* Font Library class.
*
* This file contains the Font Library class definition.
*
* @package WordPress
* @subpackage Fonts
* @since 6.5.0
*/
/**
* Font Library class.
*
* @since 6.5.0
*/
class WP_Font_Library {
/**
* Font collections.
*
* @since 6.5.0
* @var array
*/
private $collections = array();
/**
* Container for the main instance of the class.
*
* @since 6.5.0
* @var WP_Font_Library|null
*/
private static $instance = null;
/**
* Register a new font collection.
*
* @since 6.5.0
*
* @param string $slug Font collection slug. May only contain alphanumeric characters, dashes,
* and underscores. See sanitize_title().
* @param array $args Font collection data. See wp_register_font_collection() for information on accepted arguments.
* @return WP_Font_Collection|WP_Error A font collection if it was registered successfully,
* or WP_Error object on failure.
*/
public function register_font_collection( string $slug, array $args ) {
$new_collection = new WP_Font_Collection( $slug, $args );
if ( $this->is_collection_registered( $new_collection->slug ) ) {
$error_message = sprintf(
/* translators: %s: Font collection slug. */
__( 'Font collection with slug: "%s" is already registered.' ),
$new_collection->slug
);
_doing_it_wrong(
__METHOD__,
$error_message,
'6.5.0'
);
return new WP_Error( 'font_collection_registration_error', $error_message );
}
$this->collections[ $new_collection->slug ] = $new_collection;
return $new_collection;
}
/**
* Unregisters a previously registered font collection.
*
* @since 6.5.0
*
* @param string $slug Font collection slug.
* @return bool True if the font collection was unregistered successfully and false otherwise.
*/
public function unregister_font_collection( string $slug ) {
if ( ! $this->is_collection_registered( $slug ) ) {
_doing_it_wrong(
__METHOD__,
/* translators: %s: Font collection slug. */
sprintf( __( 'Font collection "%s" not found.' ), $slug ),
'6.5.0'
);
return false;
}
unset( $this->collections[ $slug ] );
return true;
}
/**
* Checks if a font collection is registered.
*
* @since 6.5.0
*
* @param string $slug Font collection slug.
* @return bool True if the font collection is registered and false otherwise.
*/
private function is_collection_registered( string $slug ) {
return array_key_exists( $slug, $this->collections );
}
/**
* Gets all the font collections available.
*
* @since 6.5.0
*
* @return array List of font collections.
*/
public function get_font_collections() {
return $this->collections;
}
/**
* Gets a font collection.
*
* @since 6.5.0
*
* @param string $slug Font collection slug.
* @return WP_Font_Collection|null Font collection object, or null if the font collection doesn't exist.
*/
public function get_font_collection( string $slug ) {
if ( $this->is_collection_registered( $slug ) ) {
return $this->collections[ $slug ];
}
return null;
}
/**
* Utility method to retrieve the main instance of the class.
*
* The instance will be created if it does not exist yet.
*
* @since 6.5.0
*
* @return WP_Font_Library The main instance.
*/
public static function get_instance() {
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
}

View File

@ -0,0 +1,259 @@
<?php
/**
* Font Utils class.
*
* Provides utility functions for working with font families.
*
* @package WordPress
* @subpackage Fonts
* @since 6.5.0
*/
/**
* A class of utilities for working with the Font Library.
*
* These utilities may change or be removed in the future and are intended for internal use only.
*
* @since 6.5.0
* @access private
*/
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%'.
* @type string $unicodeRange Optional unicode range, defaults to 'U+0-10FFFF'.
* }
* @return string Font face slug.
*/
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 ) );
}
/**
* Sanitizes a tree of data using a schema.
*
* The schema structure should mirror the data tree. Each value provided in the
* schema should be a callable that will be applied to sanitize the corresponding
* value in the data tree. Keys that are in the data tree, but not present in the
* schema, will be removed in the sanitized data. Nested arrays are traversed recursively.
*
* @since 6.5.0
*
* @access private
*
* @param array $tree The data to sanitize.
* @param array $schema The schema used for sanitization.
* @return array The sanitized data.
*/
public static function sanitize_from_schema( $tree, $schema ) {
if ( ! is_array( $tree ) || ! is_array( $schema ) ) {
return array();
}
foreach ( $tree as $key => $value ) {
// Remove keys not in the schema or with null/empty values.
if ( ! array_key_exists( $key, $schema ) ) {
unset( $tree[ $key ] );
continue;
}
$is_value_array = is_array( $value );
$is_schema_array = is_array( $schema[ $key ] ) && ! is_callable( $schema[ $key ] );
if ( $is_value_array && $is_schema_array ) {
if ( wp_is_numeric_array( $value ) ) {
// If indexed, process each item in the array.
foreach ( $value as $item_key => $item_value ) {
$tree[ $key ][ $item_key ] = isset( $schema[ $key ][0] ) && is_array( $schema[ $key ][0] )
? self::sanitize_from_schema( $item_value, $schema[ $key ][0] )
: self::apply_sanitizer( $item_value, $schema[ $key ][0] );
}
} else {
// If it is an associative or indexed array, process as a single object.
$tree[ $key ] = self::sanitize_from_schema( $value, $schema[ $key ] );
}
} elseif ( ! $is_value_array && $is_schema_array ) {
// If the value is not an array but the schema is, remove the key.
unset( $tree[ $key ] );
} elseif ( ! $is_schema_array ) {
// If the schema is not an array, apply the sanitizer to the value.
$tree[ $key ] = self::apply_sanitizer( $value, $schema[ $key ] );
}
// Remove keys with null/empty values.
if ( empty( $tree[ $key ] ) ) {
unset( $tree[ $key ] );
}
}
return $tree;
}
/**
* Applies a sanitizer function to a value.
*
* @since 6.5.0
*
* @param mixed $value The value to sanitize.
* @param mixed $sanitizer The sanitizer function to apply.
* @return mixed The sanitized value.
*/
private static function apply_sanitizer( $value, $sanitizer ) {
if ( null === $sanitizer ) {
return $value;
}
return call_user_func( $sanitizer, $value );
}
/**
* Returns the expected mime-type values for font files, depending on PHP version.
*
* This is needed because font mime types vary by PHP version, so checking the PHP version
* is necessary until a list of valid mime-types for each file extension can be provided to
* the 'upload_mimes' filter.
*
* @since 6.5.0
*
* @access private
*
* @return array A collection of mime types keyed by file extension.
*/
public static function get_allowed_font_mime_types() {
$php_7_ttf_mime_type = PHP_VERSION_ID >= 70300 ? 'application/font-sfnt' : 'application/x-font-ttf';
return array(
'otf' => 'application/vnd.ms-opentype',
'ttf' => PHP_VERSION_ID >= 70400 ? 'font/sfnt' : $php_7_ttf_mime_type,
'woff' => PHP_VERSION_ID >= 80112 ? 'font/woff' : 'application/font-woff',
'woff2' => PHP_VERSION_ID >= 80112 ? 'font/woff2' : 'application/font-woff2',
);
}
}

Binary file not shown.

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 122 KiB

Binary file not shown.

Binary file not shown.

Binary file not shown.