[ 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 Array containing the properties of the category: label.
  37       * @return bool True if the pattern was registered with success and false otherwise.
  38       */
  39  	public function register( $category_name, $category_properties ) {
  40          if ( ! isset( $category_name ) || ! is_string( $category_name ) ) {
  41              _doing_it_wrong( __METHOD__, __( 'Block pattern category name must be a string.' ), '5.5.0' );
  42              return false;
  43          }
  44  
  45          $this->registered_categories[ $category_name ] = array_merge(
  46              array( 'name' => $category_name ),
  47              $category_properties
  48          );
  49  
  50          return true;
  51      }
  52  
  53      /**
  54       * Unregisters a pattern category.
  55       *
  56       * @since 5.5.0
  57       *
  58       * @param string $category_name Pattern category name including namespace.
  59       * @return bool True if the pattern was unregistered with success and false otherwise.
  60       */
  61  	public function unregister( $category_name ) {
  62          if ( ! $this->is_registered( $category_name ) ) {
  63              /* translators: %s: Block pattern name. */
  64              $message = sprintf( __( 'Block pattern category "%s" not found.' ), $category_name );
  65              _doing_it_wrong( __METHOD__, $message, '5.5.0' );
  66              return false;
  67          }
  68  
  69          unset( $this->registered_categories[ $category_name ] );
  70  
  71          return true;
  72      }
  73  
  74      /**
  75       * Retrieves an array containing the properties of a registered pattern category.
  76       *
  77       * @since 5.5.0
  78       *
  79       * @param string $category_name Pattern category name including namespace.
  80       * @return array Registered pattern properties.
  81       */
  82  	public function get_registered( $category_name ) {
  83          if ( ! $this->is_registered( $category_name ) ) {
  84              return null;
  85          }
  86  
  87          return $this->registered_categories[ $category_name ];
  88      }
  89  
  90      /**
  91       * Retrieves all registered pattern categories.
  92       *
  93       * @since 5.5.0
  94       *
  95       * @return array Array of arrays containing the registered pattern categories properties.
  96       */
  97  	public function get_all_registered() {
  98          return array_values( $this->registered_categories );
  99      }
 100  
 101      /**
 102       * Checks if a pattern category is registered.
 103       *
 104       * @since 5.5.0
 105       *
 106       * @param string $category_name Pattern category name including namespace.
 107       * @return bool True if the pattern category is registered, false otherwise.
 108       */
 109  	public function is_registered( $category_name ) {
 110          return isset( $this->registered_categories[ $category_name ] );
 111      }
 112  
 113      /**
 114       * Utility method to retrieve the main instance of the class.
 115       *
 116       * The instance will be created if it does not exist yet.
 117       *
 118       * @since 5.5.0
 119       *
 120       * @return WP_Block_Pattern_Categories_Registry The main instance.
 121       */
 122  	public static function get_instance() {
 123          if ( null === self::$instance ) {
 124              self::$instance = new self();
 125          }
 126  
 127          return self::$instance;
 128      }
 129  }
 130  
 131  /**
 132   * Registers a new pattern category.
 133   *
 134   * @since 5.5.0
 135   *
 136   * @param string $category_name       Pattern category name including namespace.
 137   * @param array  $category_properties Array containing the properties of the category.
 138   * @return bool True if the pattern category was registered with success and false otherwise.
 139   */
 140  function register_block_pattern_category( $category_name, $category_properties ) {
 141      return WP_Block_Pattern_Categories_Registry::get_instance()->register( $category_name, $category_properties );
 142  }
 143  
 144  /**
 145   * Unregisters a pattern category.
 146   *
 147   * @since 5.5.0
 148   *
 149   * @param string $category_name Pattern category name including namespace.
 150   * @return bool True if the pattern category was unregistered with success and false otherwise.
 151   */
 152  function unregister_block_pattern_category( $category_name ) {
 153      return WP_Block_Pattern_Categories_Registry::get_instance()->unregister( $category_name );
 154  }


Generated: Sun Apr 11 01:00:05 2021 Cross-referenced by PHPXref 0.7.1