[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> fonts.php (source)

   1  <?php
   2  /**
   3   * Fonts functions.
   4   *
   5   * @package    WordPress
   6   * @subpackage Fonts
   7   * @since      6.4.0
   8   */
   9  
  10  /**
  11   * Generates and prints font-face styles for given fonts or theme.json fonts.
  12   *
  13   * @since 6.4.0
  14   *
  15   * @param array[][] $fonts {
  16   *     Optional. The font-families and their font faces. Default empty array.
  17   *
  18   *     @type array ...$0 {
  19   *         An indexed or associative (keyed by font-family) array of font variations for this font-family.
  20   *         Each font face has the following structure.
  21   *
  22   *         @type array ...$0 {
  23   *             The font face properties.
  24   *
  25   *             @type string          $font-family             The font-family property.
  26   *             @type string|string[] $src                     The URL(s) to each resource containing the font data.
  27   *             @type string          $font-style              Optional. The font-style property. Default 'normal'.
  28   *             @type string          $font-weight             Optional. The font-weight property. Default '400'.
  29   *             @type string          $font-display            Optional. The font-display property. Default 'fallback'.
  30   *             @type string          $ascent-override         Optional. The ascent-override property.
  31   *             @type string          $descent-override        Optional. The descent-override property.
  32   *             @type string          $font-stretch            Optional. The font-stretch property.
  33   *             @type string          $font-variant            Optional. The font-variant property.
  34   *             @type string          $font-feature-settings   Optional. The font-feature-settings property.
  35   *             @type string          $font-variation-settings Optional. The font-variation-settings property.
  36   *             @type string          $line-gap-override       Optional. The line-gap-override property.
  37   *             @type string          $size-adjust             Optional. The size-adjust property.
  38   *             @type string          $unicode-range           Optional. The unicode-range property.
  39   *         }
  40   *     }
  41   * }
  42   */
  43  function wp_print_font_faces( $fonts = array() ) {
  44  
  45      if ( empty( $fonts ) ) {
  46          $fonts = WP_Font_Face_Resolver::get_fonts_from_theme_json();
  47      }
  48  
  49      if ( empty( $fonts ) ) {
  50          return;
  51      }
  52  
  53      $wp_font_face = new WP_Font_Face();
  54      $wp_font_face->generate_and_print( $fonts );
  55  }
  56  
  57  /**
  58   * Generates and prints font-face styles defined the the theme style variations.
  59   *
  60   * @since 6.7.0
  61   *
  62   */
  63  function 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  /**
  74   * Registers a new font collection in the font library.
  75   *
  76   * See {@link https://schemas.wp.org/trunk/font-collection.json} for the schema
  77   * the font collection data must adhere to.
  78   *
  79   * @since 6.5.0
  80   *
  81   * @param string $slug Font collection slug. May only contain alphanumeric characters, dashes,
  82   *                     and underscores. See sanitize_title().
  83   * @param array  $args {
  84   *     Font collection data.
  85   *
  86   *     @type string       $name          Required. Name of the font collection shown in the Font Library.
  87   *     @type string       $description   Optional. A short descriptive summary of the font collection. Default empty.
  88   *     @type array|string $font_families Required. Array of font family definitions that are in the collection,
  89   *                                       or a string containing the path or URL to a JSON file containing the font collection.
  90   *     @type array        $categories    Optional. Array of categories, each with a name and slug, that are used by the
  91   *                                       fonts in the collection. Default empty.
  92   * }
  93   * @return WP_Font_Collection|WP_Error A font collection if it was registered
  94   *                                     successfully, or WP_Error object on failure.
  95   */
  96  function wp_register_font_collection( string $slug, array $args ) {
  97      return WP_Font_Library::get_instance()->register_font_collection( $slug, $args );
  98  }
  99  
 100  /**
 101   * Unregisters a font collection from the Font Library.
 102   *
 103   * @since 6.5.0
 104   *
 105   * @param string $slug Font collection slug.
 106   * @return bool True if the font collection was unregistered successfully, else false.
 107   */
 108  function wp_unregister_font_collection( string $slug ) {
 109      return WP_Font_Library::get_instance()->unregister_font_collection( $slug );
 110  }
 111  
 112  /**
 113   * Retrieves font uploads directory information.
 114   *
 115   * Same as wp_font_dir() but "light weight" as it doesn't attempt to create the font uploads directory.
 116   * Intended for use in themes, when only 'basedir' and 'baseurl' are needed, generally in all cases
 117   * when not uploading files.
 118   *
 119   * @since 6.5.0
 120   *
 121   * @see wp_font_dir()
 122   *
 123   * @return array See wp_font_dir() for description.
 124   */
 125  function wp_get_font_dir() {
 126      return wp_font_dir( false );
 127  }
 128  
 129  /**
 130   * Returns an array containing the current fonts upload directory's path and URL.
 131   *
 132   * @since 6.5.0
 133   *
 134   * @param bool $create_dir Optional. Whether to check and create the font uploads directory. Default true.
 135   * @return array {
 136   *     Array of information about the font upload directory.
 137   *
 138   *     @type string       $path    Base directory and subdirectory or full path to the fonts upload directory.
 139   *     @type string       $url     Base URL and subdirectory or absolute URL to the fonts upload directory.
 140   *     @type string       $subdir  Subdirectory
 141   *     @type string       $basedir Path without subdir.
 142   *     @type string       $baseurl URL path without subdir.
 143   *     @type string|false $error   False or error message.
 144   * }
 145   */
 146  function wp_font_dir( $create_dir = true ) {
 147      /*
 148       * Allow extenders to manipulate the font directory consistently.
 149       *
 150       * Ensures the upload_dir filter is fired both when calling this function
 151       * directly and when the upload directory is filtered in the Font Face
 152       * REST API endpoint.
 153       */
 154      add_filter( 'upload_dir', '_wp_filter_font_directory' );
 155      $font_dir = wp_upload_dir( null, $create_dir, false );
 156      remove_filter( 'upload_dir', '_wp_filter_font_directory' );
 157      return $font_dir;
 158  }
 159  
 160  /**
 161   * A callback function for use in the {@see 'upload_dir'} filter.
 162   *
 163   * This function is intended for internal use only and should not be used by plugins and themes.
 164   * Use wp_get_font_dir() instead.
 165   *
 166   * @since 6.5.0
 167   * @access private
 168   *
 169   * @param string $font_dir The font directory.
 170   * @return string The modified font directory.
 171   */
 172  function _wp_filter_font_directory( $font_dir ) {
 173      if ( doing_filter( 'font_dir' ) ) {
 174          // Avoid an infinite loop.
 175          return $font_dir;
 176      }
 177  
 178      $font_dir = array(
 179          'path'    => untrailingslashit( $font_dir['basedir'] ) . '/fonts',
 180          'url'     => untrailingslashit( $font_dir['baseurl'] ) . '/fonts',
 181          'subdir'  => '',
 182          'basedir' => untrailingslashit( $font_dir['basedir'] ) . '/fonts',
 183          'baseurl' => untrailingslashit( $font_dir['baseurl'] ) . '/fonts',
 184          'error'   => false,
 185      );
 186  
 187      /**
 188       * Filters the fonts directory data.
 189       *
 190       * This filter allows developers to modify the fonts directory data.
 191       *
 192       * @since 6.5.0
 193       *
 194       * @param array $font_dir {
 195       *     Array of information about the font upload directory.
 196       *
 197       *     @type string       $path    Base directory and subdirectory or full path to the fonts upload directory.
 198       *     @type string       $url     Base URL and subdirectory or absolute URL to the fonts upload directory.
 199       *     @type string       $subdir  Subdirectory
 200       *     @type string       $basedir Path without subdir.
 201       *     @type string       $baseurl URL path without subdir.
 202       *     @type string|false $error   False or error message.
 203       * }
 204       */
 205      return apply_filters( 'font_dir', $font_dir );
 206  }
 207  
 208  /**
 209   * Deletes child font faces when a font family is deleted.
 210   *
 211   * @access private
 212   * @since 6.5.0
 213   *
 214   * @param int     $post_id Post ID.
 215   * @param WP_Post $post    Post object.
 216   */
 217  function _wp_after_delete_font_family( $post_id, $post ) {
 218      if ( 'wp_font_family' !== $post->post_type ) {
 219          return;
 220      }
 221  
 222      $font_faces = get_children(
 223          array(
 224              'post_parent' => $post_id,
 225              'post_type'   => 'wp_font_face',
 226          )
 227      );
 228  
 229      foreach ( $font_faces as $font_face ) {
 230          wp_delete_post( $font_face->ID, true );
 231      }
 232  }
 233  
 234  /**
 235   * Deletes associated font files when a font face is deleted.
 236   *
 237   * @access private
 238   * @since 6.5.0
 239   *
 240   * @param int     $post_id Post ID.
 241   * @param WP_Post $post    Post object.
 242   */
 243  function _wp_before_delete_font_face( $post_id, $post ) {
 244      if ( 'wp_font_face' !== $post->post_type ) {
 245          return;
 246      }
 247  
 248      $font_files = get_post_meta( $post_id, '_wp_font_face_file', false );
 249      $font_dir   = untrailingslashit( wp_get_font_dir()['basedir'] );
 250  
 251      foreach ( $font_files as $font_file ) {
 252          wp_delete_file( $font_dir . '/' . $font_file );
 253      }
 254  }
 255  
 256  /**
 257   * Register the default font collections.
 258   *
 259   * @access private
 260   * @since 6.5.0
 261   */
 262  function _wp_register_default_font_collections() {
 263      wp_register_font_collection(
 264          'google-fonts',
 265          array(
 266              'name'          => _x( 'Google Fonts', 'font collection name' ),
 267              'description'   => __( 'Install from Google Fonts. Fonts are copied to and served from your site.' ),
 268              'font_families' => 'https://s.w.org/images/fonts/wp-6.7/collections/google-fonts-with-preview.json',
 269              'categories'    => array(
 270                  array(
 271                      'name' => _x( 'Sans Serif', 'font category' ),
 272                      'slug' => 'sans-serif',
 273                  ),
 274                  array(
 275                      'name' => _x( 'Display', 'font category' ),
 276                      'slug' => 'display',
 277                  ),
 278                  array(
 279                      'name' => _x( 'Serif', 'font category' ),
 280                      'slug' => 'serif',
 281                  ),
 282                  array(
 283                      'name' => _x( 'Handwriting', 'font category' ),
 284                      'slug' => 'handwriting',
 285                  ),
 286                  array(
 287                      'name' => _x( 'Monospace', 'font category' ),
 288                      'slug' => 'monospace',
 289                  ),
 290              ),
 291          )
 292      );
 293  }


Generated : Thu Nov 21 08:20:01 2024 Cross-referenced by PHPXref