No Description

class-wp-block-pattern-categories-registry.php 5.2KB

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