[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/fonts/ -> class-wp-font-library.php (source)

   1  <?php
   2  /**
   3   * Font Library class.
   4   *
   5   * This file contains the Font Library class definition.
   6   *
   7   * @package    WordPress
   8   * @subpackage Fonts
   9   * @since      6.5.0
  10   */
  11  
  12  /**
  13   * Font Library class.
  14   *
  15   * @since 6.5.0
  16   */
  17  class WP_Font_Library {
  18  
  19      /**
  20       * Font collections.
  21       *
  22       * @since 6.5.0
  23       * @var array
  24       */
  25      private $collections = array();
  26  
  27      /**
  28       * Container for the main instance of the class.
  29       *
  30       * @since 6.5.0
  31       * @var WP_Font_Library|null
  32       */
  33      private static $instance = null;
  34  
  35      /**
  36       * Register a new font collection.
  37       *
  38       * @since 6.5.0
  39       *
  40       * @param string $slug Font collection slug. May only contain alphanumeric characters, dashes,
  41       *                     and underscores. See sanitize_title().
  42       * @param array  $args Font collection data. See wp_register_font_collection() for information on accepted arguments.
  43       * @return WP_Font_Collection|WP_Error A font collection if it was registered successfully,
  44       *                                     or WP_Error object on failure.
  45       */
  46  	public function register_font_collection( string $slug, array $args ) {
  47          $new_collection = new WP_Font_Collection( $slug, $args );
  48  
  49          if ( $this->is_collection_registered( $new_collection->slug ) ) {
  50              $error_message = sprintf(
  51                  /* translators: %s: Font collection slug. */
  52                  __( 'Font collection with slug: "%s" is already registered.' ),
  53                  $new_collection->slug
  54              );
  55              _doing_it_wrong(
  56                  __METHOD__,
  57                  $error_message,
  58                  '6.5.0'
  59              );
  60              return new WP_Error( 'font_collection_registration_error', $error_message );
  61          }
  62          $this->collections[ $new_collection->slug ] = $new_collection;
  63          return $new_collection;
  64      }
  65  
  66      /**
  67       * Unregisters a previously registered font collection.
  68       *
  69       * @since 6.5.0
  70       *
  71       * @param string $slug Font collection slug.
  72       * @return bool True if the font collection was unregistered successfully and false otherwise.
  73       */
  74  	public function unregister_font_collection( string $slug ) {
  75          if ( ! $this->is_collection_registered( $slug ) ) {
  76              _doing_it_wrong(
  77                  __METHOD__,
  78                  /* translators: %s: Font collection slug. */
  79                  sprintf( __( 'Font collection "%s" not found.' ), $slug ),
  80                  '6.5.0'
  81              );
  82              return false;
  83          }
  84          unset( $this->collections[ $slug ] );
  85          return true;
  86      }
  87  
  88      /**
  89       * Checks if a font collection is registered.
  90       *
  91       * @since 6.5.0
  92       *
  93       * @param string $slug Font collection slug.
  94       * @return bool True if the font collection is registered and false otherwise.
  95       */
  96  	private function is_collection_registered( string $slug ) {
  97          return array_key_exists( $slug, $this->collections );
  98      }
  99  
 100      /**
 101       * Gets all the font collections available.
 102       *
 103       * @since 6.5.0
 104       *
 105       * @return array List of font collections.
 106       */
 107  	public function get_font_collections() {
 108          return $this->collections;
 109      }
 110  
 111      /**
 112       * Gets a font collection.
 113       *
 114       * @since 6.5.0
 115       *
 116       * @param string $slug Font collection slug.
 117       * @return WP_Font_Collection|null Font collection object, or null if the font collection doesn't exist.
 118       */
 119  	public function get_font_collection( string $slug ) {
 120          if ( $this->is_collection_registered( $slug ) ) {
 121              return $this->collections[ $slug ];
 122          }
 123          return null;
 124      }
 125  
 126      /**
 127       * Utility method to retrieve the main instance of the class.
 128       *
 129       * The instance will be created if it does not exist yet.
 130       *
 131       * @since 6.5.0
 132       *
 133       * @return WP_Font_Library The main instance.
 134       */
 135  	public static function get_instance() {
 136          if ( null === self::$instance ) {
 137              self::$instance = new self();
 138          }
 139  
 140          return self::$instance;
 141      }
 142  }


Generated : Wed May 1 08:20:02 2024 Cross-referenced by PHPXref