class-wp-block-type-registry.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. /**
  3. * Blocks API: WP_Block_Type_Registry class
  4. *
  5. * @package WordPress
  6. * @subpackage Blocks
  7. * @since 5.0.0
  8. */
  9. /**
  10. * Core class used for interacting with block types.
  11. *
  12. * @since 5.0.0
  13. */
  14. final class WP_Block_Type_Registry {
  15. /**
  16. * Registered block types, as `$name => $instance` pairs.
  17. *
  18. * @since 5.0.0
  19. * @var WP_Block_Type[]
  20. */
  21. private $registered_block_types = array();
  22. /**
  23. * Container for the main instance of the class.
  24. *
  25. * @since 5.0.0
  26. * @var WP_Block_Type_Registry|null
  27. */
  28. private static $instance = null;
  29. /**
  30. * Registers a block type.
  31. *
  32. * @since 5.0.0
  33. *
  34. * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
  35. * complete WP_Block_Type instance. In case a WP_Block_Type
  36. * is provided, the $args parameter will be ignored.
  37. * @param array $args {
  38. * Optional. Array of block type arguments. Any arguments may be defined, however the
  39. * ones described below are supported by default. Default empty array.
  40. *
  41. * @type callable $render_callback Callback used to render blocks of this block type.
  42. * @type array $attributes Block attributes mapping, property name to schema.
  43. * }
  44. * @return WP_Block_Type|false The registered block type on success, or false on failure.
  45. */
  46. public function register( $name, $args = array() ) {
  47. $block_type = null;
  48. if ( $name instanceof WP_Block_Type ) {
  49. $block_type = $name;
  50. $name = $block_type->name;
  51. }
  52. if ( ! is_string( $name ) ) {
  53. $message = __( 'Block type names must be strings.' );
  54. _doing_it_wrong( __METHOD__, $message, '5.0.0' );
  55. return false;
  56. }
  57. if ( preg_match( '/[A-Z]+/', $name ) ) {
  58. $message = __( 'Block type names must not contain uppercase characters.' );
  59. _doing_it_wrong( __METHOD__, $message, '5.0.0' );
  60. return false;
  61. }
  62. $name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
  63. if ( ! preg_match( $name_matcher, $name ) ) {
  64. $message = __( 'Block type names must contain a namespace prefix. Example: my-plugin/my-custom-block-type' );
  65. _doing_it_wrong( __METHOD__, $message, '5.0.0' );
  66. return false;
  67. }
  68. if ( $this->is_registered( $name ) ) {
  69. /* translators: %s: Block name. */
  70. $message = sprintf( __( 'Block type "%s" is already registered.' ), $name );
  71. _doing_it_wrong( __METHOD__, $message, '5.0.0' );
  72. return false;
  73. }
  74. if ( ! $block_type ) {
  75. $block_type = new WP_Block_Type( $name, $args );
  76. }
  77. $this->registered_block_types[ $name ] = $block_type;
  78. return $block_type;
  79. }
  80. /**
  81. * Unregisters a block type.
  82. *
  83. * @since 5.0.0
  84. *
  85. * @param string|WP_Block_Type $name Block type name including namespace, or alternatively a
  86. * complete WP_Block_Type instance.
  87. * @return WP_Block_Type|false The unregistered block type on success, or false on failure.
  88. */
  89. public function unregister( $name ) {
  90. if ( $name instanceof WP_Block_Type ) {
  91. $name = $name->name;
  92. }
  93. if ( ! $this->is_registered( $name ) ) {
  94. /* translators: %s: Block name. */
  95. $message = sprintf( __( 'Block type "%s" is not registered.' ), $name );
  96. _doing_it_wrong( __METHOD__, $message, '5.0.0' );
  97. return false;
  98. }
  99. $unregistered_block_type = $this->registered_block_types[ $name ];
  100. unset( $this->registered_block_types[ $name ] );
  101. return $unregistered_block_type;
  102. }
  103. /**
  104. * Retrieves a registered block type.
  105. *
  106. * @since 5.0.0
  107. *
  108. * @param string $name Block type name including namespace.
  109. * @return WP_Block_Type|null The registered block type, or null if it is not registered.
  110. */
  111. public function get_registered( $name ) {
  112. if ( ! $this->is_registered( $name ) ) {
  113. return null;
  114. }
  115. return $this->registered_block_types[ $name ];
  116. }
  117. /**
  118. * Retrieves all registered block types.
  119. *
  120. * @since 5.0.0
  121. *
  122. * @return WP_Block_Type[] Associative array of `$block_type_name => $block_type` pairs.
  123. */
  124. public function get_all_registered() {
  125. return $this->registered_block_types;
  126. }
  127. /**
  128. * Checks if a block type is registered.
  129. *
  130. * @since 5.0.0
  131. *
  132. * @param string $name Block type name including namespace.
  133. * @return bool True if the block type is registered, false otherwise.
  134. */
  135. public function is_registered( $name ) {
  136. return isset( $this->registered_block_types[ $name ] );
  137. }
  138. /**
  139. * Utility method to retrieve the main instance of the class.
  140. *
  141. * The instance will be created if it does not exist yet.
  142. *
  143. * @since 5.0.0
  144. *
  145. * @return WP_Block_Type_Registry The main instance.
  146. */
  147. public static function get_instance() {
  148. if ( null === self::$instance ) {
  149. self::$instance = new self();
  150. }
  151. return self::$instance;
  152. }
  153. }