Make WordPress Core


Ignore:
Timestamp:
02/21/2024 07:24:14 PM (13 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/class-wp-font-collection.php

    r57657 r57686  
    4747     * @since 6.5.0
    4848     *
    49      * @param string       $slug         Font collection slug.
    50      * @param array|string $data_or_file Font collection data array or a path/URL to a JSON file
    51      *                                   containing the font collection.
    52      *                                   See {@see wp_register_font_collection()} for the supported fields.
    53      */
    54     public function __construct( $slug, $data_or_file ) {
     49     * @param string $slug Font collection slug. May only contain alphanumeric characters, dashes,
     50     *                     and underscores. See sanitize_title().
     51     * @param array  $args Font collection data. See wp_register_font_collection() for information on accepted arguments.
     52     */
     53    public function __construct( string $slug, array $args ) {
    5554        $this->slug = sanitize_title( $slug );
    5655        if ( $this->slug !== $slug ) {
     
    6362        }
    6463
    65         if ( is_array( $data_or_file ) ) {
    66             $this->data = $this->sanitize_and_validate_data( $data_or_file );
    67         } else {
     64        $required_properties = array( 'name', 'font_families' );
     65
     66        if ( isset( $args['font_families'] ) && is_string( $args['font_families'] ) ) {
    6867            // JSON data is lazy loaded by ::get_data().
    69             $this->src = $data_or_file;
    70         }
     68            $this->src = $args['font_families'];
     69            unset( $args['font_families'] );
     70
     71            $required_properties = array( 'name' );
     72        }
     73
     74        $this->data = $this->sanitize_and_validate_data( $args, $required_properties );
    7175    }
    7276
     
    7983     */
    8084    public function get_data() {
     85        if ( is_wp_error( $this->data ) ) {
     86            return $this->data;
     87        }
     88
    8189        // If the collection uses JSON data, load it and cache the data/error.
    82         if ( $this->src && empty( $this->data ) ) {
     90        if ( isset( $this->src ) ) {
    8391            $this->data = $this->load_from_json( $this->src );
    8492        }
     
    117125        }
    118126
    119         return $url ? $this->load_from_url( $url ) : $this->load_from_file( $file );
     127        $data = $url ? $this->load_from_url( $url ) : $this->load_from_file( $file );
     128
     129        if ( is_wp_error( $data ) ) {
     130            return $data;
     131        }
     132
     133        $data = array(
     134            'name'          => $this->data['name'],
     135            'font_families' => $data['font_families'],
     136        );
     137
     138        if ( isset( $this->data['description'] ) ) {
     139            $data['description'] = $this->data['description'];
     140        }
     141
     142        if ( isset( $this->data['categories'] ) ) {
     143            $data['categories'] = $this->data['categories'];
     144        }
     145
     146        return $data;
    120147    }
    121148
     
    135162        }
    136163
    137         return $this->sanitize_and_validate_data( $data );
     164        return $this->sanitize_and_validate_data( $data, array( 'font_families' ) );
    138165    }
    139166
     
    155182            $response = wp_safe_remote_get( $url );
    156183            if ( is_wp_error( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
    157                 // translators: %s: Font collection URL.
    158                 return new WP_Error( 'font_collection_request_error', sprintf( __( 'Error fetching the font collection data from "%s".' ), $url ) );
     184                return new WP_Error(
     185                    'font_collection_request_error',
     186                    sprintf(
     187                        // translators: %s: Font collection URL.
     188                        __( 'Error fetching the font collection data from "%s".' ),
     189                        $url
     190                    )
     191                );
    159192            }
    160193
     
    165198
    166199            // Make sure the data is valid before storing it in a transient.
    167             $data = $this->sanitize_and_validate_data( $data );
     200            $data = $this->sanitize_and_validate_data( $data, array( 'font_families' ) );
    168201            if ( is_wp_error( $data ) ) {
    169202                return $data;
     
    181214     * @since 6.5.0
    182215     *
    183      * @param array $data Font collection data to sanitize and validate.
     216     * @param array $data                Font collection data to sanitize and validate.
     217     * @param array $required_properties Required properties that must exist in the passed data.
    184218     * @return array|WP_Error Sanitized data if valid, otherwise a WP_Error instance.
    185219     */
    186     private function sanitize_and_validate_data( $data ) {
     220    private function sanitize_and_validate_data( $data, $required_properties = array() ) {
    187221        $schema = self::get_sanitization_schema();
    188222        $data   = WP_Font_Utils::sanitize_from_schema( $data, $schema );
    189223
    190         $required_properties = array( 'name', 'font_families' );
    191224        foreach ( $required_properties as $property ) {
    192225            if ( empty( $data[ $property ] ) ) {
Note: See TracChangeset for help on using the changeset viewer.