Make WordPress Core

Changeset 59260


Ignore:
Timestamp:
10/20/2024 11:23:11 PM (2 years ago)
Author:
peterwilsoncc
Message:

Editor: Load all style variation fonts within the editors.

Loads the font family files from style variations defined within a theme for user in the site and post editors. This is to ensure the fonts are shown while editing without the need for a reload after switching styles.

Props ironprogrammer, mmaattiiaass.
Fixes #62231.

Location:
trunk
Files:
2 added
5 edited

Legend:

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

    r56682 r59260  
    173173// Font management.
    174174add_action( 'admin_print_styles', 'wp_print_font_faces', 50 );
     175add_action( 'admin_print_styles', 'wp_print_font_faces_from_style_variations', 50 );
  • trunk/src/wp-includes/block-editor.php

    r59238 r59260  
    367367        wp_print_styles();
    368368        wp_print_font_faces();
     369        wp_print_font_faces_from_style_variations();
    369370        $styles = ob_get_clean();
    370371
  • trunk/src/wp-includes/fonts.php

    r58353 r59260  
    5656
    5757/**
     58 * Generates and prints font-face styles defined the the theme style variations.
     59 *
     60 * @since 6.7.0
     61 *
     62 */
     63function wp_print_font_faces_from_style_variations() {
     64        $fonts = WP_Font_Face_Resolver::get_fonts_from_style_variations();
     65
     66        if ( empty( $fonts ) ) {
     67                return;
     68        }
     69
     70        wp_print_font_faces( $fonts );
     71}
     72
     73/**
    5874 * Registers a new font collection in the font library.
    5975 *
  • trunk/src/wp-includes/fonts/class-wp-font-face-resolver.php

    r57720 r59260  
    3838
    3939        /**
     40         * Gets fonts defined in style variations.
     41         *
     42         * @since 6.7.0
     43         *
     44         * @return array Returns an array of font-families.
     45         */
     46        public static function get_fonts_from_style_variations() {
     47                $variations = WP_Theme_JSON_Resolver::get_style_variations();
     48                $fonts      = array();
     49
     50                if ( empty( $variations ) ) {
     51                        return $fonts;
     52                }
     53
     54                foreach ( $variations as $variation ) {
     55                        if ( ! empty( $variation['settings']['typography']['fontFamilies']['theme'] ) ) {
     56                                $fonts = array_merge( $fonts, $variation['settings']['typography']['fontFamilies']['theme'] );
     57                        }
     58                }
     59
     60                $settings = array(
     61                        'typography' => array(
     62                                'fontFamilies' => array(
     63                                        'theme' => $fonts,
     64                                ),
     65                        ),
     66                );
     67
     68                return static::parse_settings( $settings );
     69        }
     70
     71        /**
    4072         * Parse theme.json settings to extract font definitions with variations grouped by font-family.
    4173         *
  • trunk/tests/phpunit/tests/fonts/font-face/wp-font-face-tests-dataset.php

    r57720 r59260  
    404404                return $data;
    405405        }
     406
     407        public static function get_custom_style_variations( $key = '' ) {
     408                static $data = null;
     409
     410                $path                   = get_stylesheet_directory() . '/assets/fonts/';
     411                $uri                    = get_stylesheet_directory_uri() . '/assets/fonts/';
     412                $expected_font_families = array(
     413                        array(
     414                                array(
     415                                        'src'          => array(
     416                                                "{$path}dm-sans/DMSans-Regular.woff2",
     417                                        ),
     418                                        'font-family'  => 'DM Sans',
     419                                        'font-stretch' => 'normal',
     420                                        'font-style'   => 'normal',
     421                                        'font-weight'  => '400',
     422                                ),
     423                                array(
     424                                        'src'          => array(
     425                                                "{$path}dm-sans/DMSans-Bold.woff2",
     426                                        ),
     427                                        'font-family'  => 'DM Sans',
     428                                        'font-stretch' => 'normal',
     429                                        'font-style'   => 'normal',
     430                                        'font-weight'  => '700',
     431                                ),
     432                        ),
     433                        array(
     434                                array(
     435                                        'src'          => array(
     436                                                "{$path}open-sans/OpenSans-VariableFont_wdth,wght.ttf",
     437                                        ),
     438                                        'font-family'  => 'Open Sans',
     439                                        'font-stretch' => 'normal',
     440                                        'font-style'   => 'normal',
     441                                        'font-weight'  => '400',
     442                                ),
     443                                array(
     444                                        'src'          => array(
     445                                                "{$path}open-sans/OpenSans-Italic-VariableFont_wdth,wght.ttf",
     446                                        ),
     447                                        'font-family'  => 'Open Sans',
     448                                        'font-stretch' => 'normal',
     449                                        'font-style'   => 'italic',
     450                                        'font-weight'  => '400',
     451                                ),
     452                        ),
     453                        array(
     454                                array(
     455                                        'src'          => array(
     456                                                "{$path}dm-sans/DMSans-Medium.woff2",
     457                                        ),
     458                                        'font-family'  => 'DM Sans',
     459                                        'font-stretch' => 'normal',
     460                                        'font-style'   => 'normal',
     461                                        'font-weight'  => '500',
     462                                ),
     463                                array(
     464                                        'src'          => array(
     465                                                "{$path}dm-sans/DMSans-Medium-Italic.woff2",
     466                                        ),
     467                                        'font-family'  => 'DM Sans',
     468                                        'font-stretch' => 'normal',
     469                                        'font-style'   => 'italic',
     470                                        'font-weight'  => '500',
     471                                ),
     472                        ),
     473                );
     474
     475                $expected_styles = <<<CSS
     476@font-face{font-family:"DM Sans";font-style:normal;font-weight:400;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Regular.woff2') format('woff2');font-stretch:normal;}
     477@font-face{font-family:"DM Sans";font-style:normal;font-weight:700;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Bold.woff2') format('woff2');font-stretch:normal;}
     478@font-face{font-family:"Open Sans";font-style:normal;font-weight:400;font-display:fallback;src:url('{$uri}open-sans/OpenSans-VariableFont_wdth,wght.ttf') format('truetype');font-stretch:normal;}
     479@font-face{font-family:"Open Sans";font-style:italic;font-weight:400;font-display:fallback;src:url('{$uri}open-sans/OpenSans-Italic-VariableFont_wdth,wght.ttf') format('truetype');font-stretch:normal;}
     480@font-face{font-family:"DM Sans";font-style:normal;font-weight:500;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Medium.woff2') format('woff2');font-stretch:normal;}
     481@font-face{font-family:"DM Sans";font-style:italic;font-weight:500;font-display:fallback;src:url('{$uri}dm-sans/DMSans-Medium-Italic.woff2') format('woff2');font-stretch:normal;}
     482CSS;
     483
     484                if ( null === $data ) {
     485                        $data = array(
     486                                'expected'        => $expected_font_families,
     487                                'expected_styles' => $expected_styles,
     488                        );
     489                }
     490
     491                if ( isset( $data[ $key ] ) ) {
     492                        return $data[ $key ];
     493                }
     494
     495                return $data;
     496        }
    406497}
Note: See TracChangeset for help on using the changeset viewer.