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


Generated : Sat Apr 27 08:20:02 2024 Cross-referenced by PHPXref