説明なし

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

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