Make WordPress Core


Ignore:
Timestamp:
02/21/2024 07:24:14 PM (15 months ago)
Author:
swissspidy
Message:

Editor: Ensure font collection metadata can be properly localized.

Updates wp_register_font_collection() and WP_Font_Collection so that only font families can be loaded from a file or URL.
All metadata, such as name, description, and list of font categories, needs to be passed directly in PHP so that it can be properly localized.

Props swissspidy, mmaattiiaass, grantmkin, youknowriad.
Fixes #60509.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/fonts.php

    r57558 r57686  
    5454
    5555/**
    56  * Registers a new Font Collection in the Font Library.
    57  *
    58  * @since 6.5.0
    59  *
    60  * @param string       $slug Font collection slug. May only contain alphanumeric characters, dashes,
     56 * Registers a new font collection in the font library.
     57 *
     58 * See {@link https://schemas.wp.org/trunk/font-collection.json} for the schema
     59 * the font collection data must adhere to.
     60 *
     61 * @since 6.5.0
     62 *
     63 * @param string $slug Font collection slug. May only contain alphanumeric characters, dashes,
    6164 *                     and underscores. See sanitize_title().
    62  * @param array|string $data_or_file {
    63  *     Font collection data array or a path/URL to a JSON file containing the font collection.
    64  *
    65  *     @link https://schemas.wp.org/trunk/font-collection.json
    66  *
    67  *     @type string $name           Required. Name of the font collection shown in the Font Library.
    68  *     @type string $description    Optional. A short descriptive summary of the font collection. Default empty.
    69  *     @type array  $font_families  Required. Array of font family definitions that are in the collection.
    70  *     @type array  $categories     Optional. Array of categories, each with a name and slug, that are used by the
    71  *                                  fonts in the collection. Default empty.
     65 * @param array  $args {
     66 *     Font collection data.
     67 *
     68 *     @type string       $name          Required. Name of the font collection shown in the Font Library.
     69 *     @type string       $description   Optional. A short descriptive summary of the font collection. Default empty.
     70 *     @type array|string $font_families Required. Array of font family definitions that are in the collection,
     71 *                                       or a string containing the path or URL to a JSON file containing the font collection.
     72 *     @type array        $categories    Optional. Array of categories, each with a name and slug, that are used by the
     73 *                                       fonts in the collection. Default empty.
    7274 * }
    7375 * @return WP_Font_Collection|WP_Error A font collection if it was registered
    7476 *                                     successfully, or WP_Error object on failure.
    7577 */
    76 function wp_register_font_collection( $slug, $data_or_file ) {
    77     return WP_Font_Library::get_instance()->register_font_collection( $slug, $data_or_file );
     78function wp_register_font_collection( string $slug, array $args ) {
     79    return WP_Font_Library::get_instance()->register_font_collection( $slug, $args );
    7880}
    7981
     
    8688 * @return bool True if the font collection was unregistered successfully, else false.
    8789 */
    88 function wp_unregister_font_collection( $slug ) {
     90function wp_unregister_font_collection( string $slug ) {
    8991    return WP_Font_Library::get_instance()->unregister_font_collection( $slug );
    9092}
     
    197199 */
    198200function _wp_register_default_font_collections() {
    199     wp_register_font_collection( 'google-fonts', 'https://s.w.org/images/fonts/17.7/collections/google-fonts-with-preview.json' );
    200 }
     201    wp_register_font_collection(
     202        'google-fonts',
     203        array(
     204            'name'          => _x( 'Google Fonts', 'font collection name' ),
     205            'description'   => __( 'Install from Google Fonts. Fonts are copied to and served from your site.' ),
     206            'font_families' => 'https://s.w.org/images/fonts/17.7/collections/google-fonts-with-preview.json',
     207            'categories'    => array(
     208                array(
     209                    'name' => _x( 'Sans Serif', 'font category' ),
     210                    'slug' => 'sans-serif',
     211                ),
     212                array(
     213                    'name' => _x( 'Display', 'font category' ),
     214                    'slug' => 'display',
     215                ),
     216                array(
     217                    'name' => _x( 'Serif', 'font category' ),
     218                    'slug' => 'serif',
     219                ),
     220                array(
     221                    'name' => _x( 'Handwriting', 'font category' ),
     222                    'slug' => 'handwriting',
     223                ),
     224                array(
     225                    'name' => _x( 'Monospace', 'font category' ),
     226                    'slug' => 'monospace',
     227                ),
     228            ),
     229        )
     230    );
     231}
Note: See TracChangeset for help on using the changeset viewer.