WP_Font_Collection
- Since
- 6.5.0
- Source
wp-includes/fonts/class-wp-font-collection.php:19
Font Collection class.
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).
Properties · 3
$slugstringpublic- The unique slug for the font collection.
$dataarray|WP_Error|nullprivate- Font collection data.
$srcstring|nullprivate- Font collection JSON file path or URL.
Methods · 7
- __construct()WP_Font_Collection constructor.
- get_data()Retrieves the font collection data.
- load_from_json()Loads font collection data from a JSON file or URL.
- load_from_file()Loads the font collection data from a JSON file path.
- load_from_url()Loads the font collection data from a JSON file URL.
- sanitize_and_validate_data()Sanitizes and validates the font collection data.
- get_sanitization_schema()Retrieves the font collection sanitization schema.
Source code
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.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-collection.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.