Nenhuma Descrição

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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. * @see WP_Block_Type::__construct()
  35. *
  36. * @param string|WP_Block_Type $name Block type name including namespace, or alternatively
  37. * a complete WP_Block_Type instance. In case a WP_Block_Type
  38. * is provided, the $args parameter will be ignored.
  39. * @param array $args Optional. Array of block type arguments. Accepts any public property
  40. * of `WP_Block_Type`. See WP_Block_Type::__construct() for information
  41. * on accepted arguments. Default empty array.
  42. * @return WP_Block_Type|false The registered block type on success, or false on failure.
  43. */
  44. public function register( $name, $args = array() ) {
  45. $block_type = null;
  46. if ( $name instanceof WP_Block_Type ) {
  47. $block_type = $name;
  48. $name = $block_type->name;
  49. }
  50. if ( ! is_string( $name ) ) {
  51. _doing_it_wrong(
  52. __METHOD__,
  53. __( 'Block type names must be strings.' ),
  54. '5.0.0'
  55. );
  56. return false;
  57. }
  58. if ( preg_match( '/[A-Z]+/', $name ) ) {
  59. _doing_it_wrong(
  60. __METHOD__,
  61. __( 'Block type names must not contain uppercase characters.' ),
  62. '5.0.0'
  63. );
  64. return false;
  65. }
  66. $name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
  67. if ( ! preg_match( $name_matcher, $name ) ) {
  68. _doing_it_wrong(
  69. __METHOD__,
  70. __( 'Block type names must contain a namespace prefix. Example: my-plugin/my-custom-block-type' ),
  71. '5.0.0'
  72. );
  73. return false;
  74. }
  75. if ( $this->is_registered( $name ) ) {
  76. _doing_it_wrong(
  77. __METHOD__,
  78. /* translators: %s: Block name. */
  79. sprintf( __( 'Block type "%s" is already registered.' ), $name ),
  80. '5.0.0'
  81. );
  82. return false;
  83. }
  84. if ( ! $block_type ) {
  85. $block_type = new WP_Block_Type( $name, $args );
  86. }
  87. $this->registered_block_types[ $name ] = $block_type;
  88. return $block_type;
  89. }
  90. /**
  91. * Unregisters a block type.
  92. *
  93. * @since 5.0.0
  94. *
  95. * @param string|WP_Block_Type $name Block type name including namespace, or alternatively
  96. * a complete WP_Block_Type instance.
  97. * @return WP_Block_Type|false The unregistered block type on success, or false on failure.
  98. */
  99. public function unregister( $name ) {
  100. if ( $name instanceof WP_Block_Type ) {
  101. $name = $name->name;
  102. }
  103. if ( ! $this->is_registered( $name ) ) {
  104. _doing_it_wrong(
  105. __METHOD__,
  106. /* translators: %s: Block name. */
  107. sprintf( __( 'Block type "%s" is not registered.' ), $name ),
  108. '5.0.0'
  109. );
  110. return false;
  111. }
  112. $unregistered_block_type = $this->registered_block_types[ $name ];
  113. unset( $this->registered_block_types[ $name ] );
  114. return $unregistered_block_type;
  115. }
  116. /**
  117. * Retrieves a registered block type.
  118. *
  119. * @since 5.0.0
  120. *
  121. * @param string $name Block type name including namespace.
  122. * @return WP_Block_Type|null The registered block type, or null if it is not registered.
  123. */
  124. public function get_registered( $name ) {
  125. if ( ! $this->is_registered( $name ) ) {
  126. return null;
  127. }
  128. return $this->registered_block_types[ $name ];
  129. }
  130. /**
  131. * Retrieves all registered block types.
  132. *
  133. * @since 5.0.0
  134. *
  135. * @return WP_Block_Type[] Associative array of `$block_type_name => $block_type` pairs.
  136. */
  137. public function get_all_registered() {
  138. return $this->registered_block_types;
  139. }
  140. /**
  141. * Checks if a block type is registered.
  142. *
  143. * @since 5.0.0
  144. *
  145. * @param string $name Block type name including namespace.
  146. * @return bool True if the block type is registered, false otherwise.
  147. */
  148. public function is_registered( $name ) {
  149. return isset( $this->registered_block_types[ $name ] );
  150. }
  151. /**
  152. * Utility method to retrieve the main instance of the class.
  153. *
  154. * The instance will be created if it does not exist yet.
  155. *
  156. * @since 5.0.0
  157. *
  158. * @return WP_Block_Type_Registry The main instance.
  159. */
  160. public static function get_instance() {
  161. if ( null === self::$instance ) {
  162. self::$instance = new self();
  163. }
  164. return self::$instance;
  165. }
  166. }