Sin descripción

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

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