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

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