Nenhuma Descrição

class-wp-block-patterns-registry.php 4.4KB

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