_wp_theme_json_webfonts_handler() – Runs the theme.json webfonts handler.

You appear to be a bot. Output may be restricted

Description

Runs the theme.json webfonts handler.

Using `WP_Theme_JSON_Resolver`, it gets the fonts defined in the theme.json for the current selection and style variations, validates the font-face properties, generates the '@font-face' style declarations, and then enqueues the styles for both the editor and front-end. Design Notes: This is not a public API, but rather an internal handler. A future public Webfonts API will replace this stopgap code. This code design is intentional.

  • a. It hides the inner-workings.
  • b. It does not expose API ins or outs for consumption.
  • c. It only works with a theme's `theme.json`.

Why?

  • a. To avoid backwards-compatibility issues when
  • the Webfonts API is introduced in Core.
  • b. To make fontFace declarations in theme.json work.

Usage

_wp_theme_json_webfonts_handler();

Parameters

Returns

void

Source

File name: wordpress/wp-includes/script-loader.php


Lines:

1 to 100 of 486
function _wp_theme_json_webfonts_handler() {
  // Block themes are unavailable during installation.
  if ( wp_installing() ) {
    return;
  }

  // Webfonts to be processed.
  $registered_webfonts = array();

  
/**
 * Gets the webfonts from theme.json.
 *
 * @since 6.0.0
 *
 * @return array Array of defined webfonts.
 */
  $fn_get_webfonts_from_theme_json = static function() {
    // Get settings from theme.json.
    $settings = WP_Theme_JSON_Resolver::get_merged_data()->get_settings();

    // If in the editor, add webfonts defined in variations.
    if ( is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
      $variations = WP_Theme_JSON_Resolver::get_style_variations();
      foreach ( $variations as $variation ) {
        // Skip if fontFamilies are not defined in the variation.
        if ( empty( $variation['settings']['typography']['fontFamilies'] ) ) {
          continue;
        }

        // Initialize the array structure.
        if ( empty( $settings['typography'] ) ) {
          $settings['typography'] = array();
        }
        if ( empty( $settings['typography']['fontFamilies'] ) ) {
          $settings['typography']['fontFamilies'] = array();
        }
        if ( empty( $settings['typography']['fontFamilies']['theme'] ) ) {
          $settings['typography']['fontFamilies']['theme'] = array();
        }

        // Combine variations with settings. Remove duplicates.
        $settings['typography']['fontFamilies']['theme'] = array_merge( $settings['typography']['fontFamilies']['theme'], $variation['settings']['typography']['fontFamilies']['theme'] );
        $settings['typography']['fontFamilies']          = array_unique( $settings['typography']['fontFamilies'] );
      }
    }

    // Bail out early if there are no settings for webfonts.
    if ( empty( $settings['typography']['fontFamilies'] ) ) {
      return array();
    }

    $webfonts = array();

    // Look for fontFamilies.
    foreach ( $settings['typography']['fontFamilies'] as $font_families ) {
      foreach ( $font_families as $font_family ) {

        // Skip if fontFace is not defined.
        if ( empty( $font_family['fontFace'] ) ) {
          continue;
        }

        // Skip if fontFace is not an array of webfonts.
        if ( ! is_array( $font_family['fontFace'] ) ) {
          continue;
        }

        $webfonts = array_merge( $webfonts, $font_family['fontFace'] );
      }
    }

    return $webfonts;
  };

  
/**
 * Transforms each 'src' into an URI by replacing 'file:./'
 * placeholder from theme.json.
 *
 * The absolute path to the webfont file(s) cannot be defined in
 * theme.json. `file:./` is the placeholder which is replaced by
 * the theme's URL path to the theme's root.
 *
 * @since 6.0.0
 *
 * @param array $src Webfont file(s) `src`.
 * @return array Webfont's `src` in URI.
 */
  $fn_transform_src_into_uri = static function( array $src ) {
    foreach ( $src as $key => $url ) {
      // Tweak the URL to be relative to the theme root.
      if ( ! str_starts_with( $url, 'file:./' ) ) {
        continue;
      }

      $src[ $key ] = get_theme_file_uri( str_replace( 'file:./', '', $url ) );
    }

    return $src;

 View on GitHub View on Trac