[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-block-pattern-categories-registry.php (source)

   1  <?php
   2  /**
   3   * Blocks API: WP_Block_Pattern_Categories_Registry class
   4   *
   5   * @package WordPress
   6   * @subpackage Blocks
   7   * @since 5.5.0
   8   */
   9  
  10  /**
  11   * Class used for interacting with block pattern categories.
  12   */
  13  final class WP_Block_Pattern_Categories_Registry {
  14      /**
  15       * Registered block pattern categories array.
  16       *
  17       * @since 5.5.0
  18       * @var array[]
  19       */
  20      private $registered_categories = array();
  21  
  22      /**
  23       * Container for the main instance of the class.
  24       *
  25       * @since 5.5.0
  26       * @var WP_Block_Pattern_Categories_Registry|null
  27       */
  28      private static $instance = null;
  29  
  30      /**
  31       * Registers a pattern category.
  32       *
  33       * @since 5.5.0
  34       *
  35       * @param string $category_name       Pattern category name including namespace.
  36       * @param array  $category_properties {
  37       *     List of properties for the block pattern category.
  38       *
  39       *     @type string $label Required. A human-readable label for the pattern category.
  40       * }
  41       * @return bool True if the pattern was registered with success and false otherwise.
  42       */
  43  	public function register( $category_name, $category_properties ) {
  44          if ( ! isset( $category_name ) || ! is_string( $category_name ) ) {
  45              _doing_it_wrong(
  46                  __METHOD__,
  47                  __( 'Block pattern category name must be a string.' ),
  48                  '5.5.0'
  49              );
  50              return false;
  51          }
  52  
  53          $this->registered_categories[ $category_name ] = array_merge(
  54              array( 'name' => $category_name ),
  55              $category_properties
  56          );
  57  
  58          return true;
  59      }
  60  
  61      /**
  62       * Unregisters a pattern category.
  63       *
  64       * @since 5.5.0
  65       *
  66       * @param string $category_name Pattern category name including namespace.
  67       * @return bool True if the pattern was unregistered with success and false otherwise.
  68       */
  69  	public function unregister( $category_name ) {
  70          if ( ! $this->is_registered( $category_name ) ) {
  71              _doing_it_wrong(
  72                  __METHOD__,
  73                  /* translators: %s: Block pattern name. */
  74                  sprintf( __( 'Block pattern category "%s" not found.' ), $category_name ),
  75                  '5.5.0'
  76              );
  77              return false;
  78          }
  79  
  80          unset( $this->registered_categories[ $category_name ] );
  81  
  82          return true;
  83      }
  84  
  85      /**
  86       * Retrieves an array containing the properties of a registered pattern category.
  87       *
  88       * @since 5.5.0
  89       *
  90       * @param string $category_name Pattern category name including namespace.
  91       * @return array Registered pattern properties.
  92       */
  93  	public function get_registered( $category_name ) {
  94          if ( ! $this->is_registered( $category_name ) ) {
  95              return null;
  96          }
  97  
  98          return $this->registered_categories[ $category_name ];
  99      }
 100  
 101      /**
 102       * Retrieves all registered pattern categories.
 103       *
 104       * @since 5.5.0
 105       *
 106       * @return array[] Array of arrays containing the registered pattern categories properties.
 107       */
 108  	public function get_all_registered() {
 109          return array_values( $this->registered_categories );
 110      }
 111  
 112      /**
 113       * Checks if a pattern category is registered.
 114       *
 115       * @since 5.5.0
 116       *
 117       * @param string $category_name Pattern category name including namespace.
 118       * @return bool True if the pattern category is registered, false otherwise.
 119       */
 120  	public function is_registered( $category_name ) {
 121          return isset( $this->registered_categories[ $category_name ] );
 122      }
 123  
 124      /**
 125       * Utility method to retrieve the main instance of the class.
 126       *
 127       * The instance will be created if it does not exist yet.
 128       *
 129       * @since 5.5.0
 130       *
 131       * @return WP_Block_Pattern_Categories_Registry The main instance.
 132       */
 133  	public static function get_instance() {
 134          if ( null === self::$instance ) {
 135              self::$instance = new self();
 136          }
 137  
 138          return self::$instance;
 139      }
 140  }
 141  
 142  /**
 143   * Registers a new pattern category.
 144   *
 145   * @since 5.5.0
 146   *
 147   * @param string $category_name       Pattern category name including namespace.
 148   * @param array  $category_properties List of properties for the block pattern.
 149   *                                    See WP_Block_Pattern_Categories_Registry::register() for
 150   *                                    accepted arguments.
 151   * @return bool True if the pattern category was registered with success and false otherwise.
 152   */
 153  function register_block_pattern_category( $category_name, $category_properties ) {
 154      return WP_Block_Pattern_Categories_Registry::get_instance()->register( $category_name, $category_properties );
 155  }
 156  
 157  /**
 158   * Unregisters a pattern category.
 159   *
 160   * @since 5.5.0
 161   *
 162   * @param string $category_name Pattern category name including namespace.
 163   * @return bool True if the pattern category was unregistered with success and false otherwise.
 164   */
 165  function unregister_block_pattern_category( $category_name ) {
 166      return WP_Block_Pattern_Categories_Registry::get_instance()->unregister( $category_name );
 167  }


Generated: Sat Apr 20 01:00:03 2024 Cross-referenced by PHPXref 0.7.1