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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 block patterns.
  11. *
  12. * @since 5.5.0
  13. */
  14. #[AllowDynamicProperties]
  15. final class WP_Block_Patterns_Registry {
  16. /**
  17. * Registered block patterns array.
  18. *
  19. * @since 5.5.0
  20. * @var array[]
  21. */
  22. private $registered_patterns = array();
  23. /**
  24. * Patterns registered outside the `init` action.
  25. *
  26. * @since 6.0.0
  27. * @var array[]
  28. */
  29. private $registered_patterns_outside_init = array();
  30. /**
  31. * Container for the main instance of the class.
  32. *
  33. * @since 5.5.0
  34. * @var WP_Block_Patterns_Registry|null
  35. */
  36. private static $instance = null;
  37. /**
  38. * Registers a block pattern.
  39. *
  40. * @since 5.5.0
  41. * @since 5.8.0 Added support for the `blockTypes` property.
  42. *
  43. * @param string $pattern_name Block pattern name including namespace.
  44. * @param array $pattern_properties {
  45. * List of properties for the block pattern.
  46. *
  47. * @type string $title Required. A human-readable title for the pattern.
  48. * @type string $content Required. Block HTML markup for the pattern.
  49. * @type string $description Optional. Visually hidden text used to describe the pattern in the
  50. * inserter. A description is optional, but is strongly
  51. * encouraged when the title does not fully describe what the
  52. * pattern does. The description will help users discover the
  53. * pattern while searching.
  54. * @type int $viewportWidth Optional. The intended width of the pattern to allow for a scaled
  55. * preview within the pattern inserter.
  56. * @type array $categories Optional. A list of registered pattern categories used to group block
  57. * patterns. Block patterns can be shown on multiple categories.
  58. * A category must be registered separately in order to be used
  59. * here.
  60. * @type array $blockTypes Optional. A list of block names including namespace that could use
  61. * the block pattern in certain contexts (placeholder, transforms).
  62. * The block pattern is available in the block editor inserter
  63. * regardless of this list of block names.
  64. * Certain blocks support further specificity besides the block name
  65. * (e.g. for `core/template-part` you can specify areas
  66. * like `core/template-part/header` or `core/template-part/footer`).
  67. * @type array $keywords Optional. A list of aliases or keywords that help users discover the
  68. * pattern while searching.
  69. * }
  70. * @return bool True if the pattern was registered with success and false otherwise.
  71. */
  72. public function register( $pattern_name, $pattern_properties ) {
  73. if ( ! isset( $pattern_name ) || ! is_string( $pattern_name ) ) {
  74. _doing_it_wrong(
  75. __METHOD__,
  76. __( 'Pattern name must be a string.' ),
  77. '5.5.0'
  78. );
  79. return false;
  80. }
  81. if ( ! isset( $pattern_properties['title'] ) || ! is_string( $pattern_properties['title'] ) ) {
  82. _doing_it_wrong(
  83. __METHOD__,
  84. __( 'Pattern title must be a string.' ),
  85. '5.5.0'
  86. );
  87. return false;
  88. }
  89. if ( ! isset( $pattern_properties['content'] ) || ! is_string( $pattern_properties['content'] ) ) {
  90. _doing_it_wrong(
  91. __METHOD__,
  92. __( 'Pattern content must be a string.' ),
  93. '5.5.0'
  94. );
  95. return false;
  96. }
  97. $pattern = array_merge(
  98. $pattern_properties,
  99. array( 'name' => $pattern_name )
  100. );
  101. $this->registered_patterns[ $pattern_name ] = $pattern;
  102. // If the pattern is registered inside an action other than `init`, store it
  103. // also to a dedicated array. Used to detect deprecated registrations inside
  104. // `admin_init` or `current_screen`.
  105. if ( current_action() && 'init' !== current_action() ) {
  106. $this->registered_patterns_outside_init[ $pattern_name ] = $pattern;
  107. }
  108. return true;
  109. }
  110. /**
  111. * Unregisters a block pattern.
  112. *
  113. * @since 5.5.0
  114. *
  115. * @param string $pattern_name Block pattern name including namespace.
  116. * @return bool True if the pattern was unregistered with success and false otherwise.
  117. */
  118. public function unregister( $pattern_name ) {
  119. if ( ! $this->is_registered( $pattern_name ) ) {
  120. _doing_it_wrong(
  121. __METHOD__,
  122. /* translators: %s: Pattern name. */
  123. sprintf( __( 'Pattern "%s" not found.' ), $pattern_name ),
  124. '5.5.0'
  125. );
  126. return false;
  127. }
  128. unset( $this->registered_patterns[ $pattern_name ] );
  129. unset( $this->registered_patterns_outside_init[ $pattern_name ] );
  130. return true;
  131. }
  132. /**
  133. * Retrieves an array containing the properties of a registered block pattern.
  134. *
  135. * @since 5.5.0
  136. *
  137. * @param string $pattern_name Block pattern name including namespace.
  138. * @return array Registered pattern properties.
  139. */
  140. public function get_registered( $pattern_name ) {
  141. if ( ! $this->is_registered( $pattern_name ) ) {
  142. return null;
  143. }
  144. return $this->registered_patterns[ $pattern_name ];
  145. }
  146. /**
  147. * Retrieves all registered block patterns.
  148. *
  149. * @since 5.5.0
  150. *
  151. * @param bool $outside_init_only Return only patterns registered outside the `init` action.
  152. * @return array[] Array of arrays containing the registered block patterns properties,
  153. * and per style.
  154. */
  155. public function get_all_registered( $outside_init_only = false ) {
  156. return array_values(
  157. $outside_init_only
  158. ? $this->registered_patterns_outside_init
  159. : $this->registered_patterns
  160. );
  161. }
  162. /**
  163. * Checks if a block pattern is registered.
  164. *
  165. * @since 5.5.0
  166. *
  167. * @param string $pattern_name Block pattern name including namespace.
  168. * @return bool True if the pattern is registered, false otherwise.
  169. */
  170. public function is_registered( $pattern_name ) {
  171. return isset( $this->registered_patterns[ $pattern_name ] );
  172. }
  173. /**
  174. * Utility method to retrieve the main instance of the class.
  175. *
  176. * The instance will be created if it does not exist yet.
  177. *
  178. * @since 5.5.0
  179. *
  180. * @return WP_Block_Patterns_Registry The main instance.
  181. */
  182. public static function get_instance() {
  183. if ( null === self::$instance ) {
  184. self::$instance = new self();
  185. }
  186. return self::$instance;
  187. }
  188. }
  189. /**
  190. * Registers a new block pattern.
  191. *
  192. * @since 5.5.0
  193. *
  194. * @param string $pattern_name Block pattern name including namespace.
  195. * @param array $pattern_properties List of properties for the block pattern.
  196. * See WP_Block_Patterns_Registry::register() for accepted arguments.
  197. * @return bool True if the pattern was registered with success and false otherwise.
  198. */
  199. function register_block_pattern( $pattern_name, $pattern_properties ) {
  200. return WP_Block_Patterns_Registry::get_instance()->register( $pattern_name, $pattern_properties );
  201. }
  202. /**
  203. * Unregisters a block pattern.
  204. *
  205. * @since 5.5.0
  206. *
  207. * @param string $pattern_name Block pattern name including namespace.
  208. * @return bool True if the pattern was unregistered with success and false otherwise.
  209. */
  210. function unregister_block_pattern( $pattern_name ) {
  211. return WP_Block_Patterns_Registry::get_instance()->unregister( $pattern_name );
  212. }