[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> class-wp-block-type-registry.php (source)

   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  /**
  11   * Core class used for interacting with block types.
  12   *
  13   * @since 5.0.0
  14   */
  15  #[AllowDynamicProperties]
  16  final class WP_Block_Type_Registry {
  17      /**
  18       * Registered block types, as `$name => $instance` pairs.
  19       *
  20       * @since 5.0.0
  21       * @var WP_Block_Type[]
  22       */
  23      private $registered_block_types = array();
  24  
  25      /**
  26       * Container for the main instance of the class.
  27       *
  28       * @since 5.0.0
  29       * @var WP_Block_Type_Registry|null
  30       */
  31      private static $instance = null;
  32  
  33      /**
  34       * Registers a block type.
  35       *
  36       * @since 5.0.0
  37       *
  38       * @see WP_Block_Type::__construct()
  39       *
  40       * @param string|WP_Block_Type $name Block type name including namespace, or alternatively
  41       *                                   a complete WP_Block_Type instance. In case a WP_Block_Type
  42       *                                   is provided, the $args parameter will be ignored.
  43       * @param array                $args Optional. Array of block type arguments. Accepts any public property
  44       *                                   of `WP_Block_Type`. See WP_Block_Type::__construct() for information
  45       *                                   on accepted arguments. Default empty array.
  46       * @return WP_Block_Type|false The registered block type on success, or false on failure.
  47       */
  48  	public function register( $name, $args = array() ) {
  49          $block_type = null;
  50          if ( $name instanceof WP_Block_Type ) {
  51              $block_type = $name;
  52              $name       = $block_type->name;
  53          }
  54  
  55          if ( ! is_string( $name ) ) {
  56              _doing_it_wrong(
  57                  __METHOD__,
  58                  /* translators: %s: The received block type name type. */
  59                  sprintf( __( 'Block type names must be strings, received %s.' ), gettype( $name ) ),
  60                  '5.0.0'
  61              );
  62              return false;
  63          }
  64  
  65          if ( preg_match( '/[A-Z]+/', $name ) ) {
  66              _doing_it_wrong(
  67                  __METHOD__,
  68                  /* translators: %s: Block name. */
  69                  sprintf( __( 'Block type names must not contain uppercase characters. "%s" was given.' ), esc_html( $name ) ),
  70                  '5.0.0'
  71              );
  72              return false;
  73          }
  74  
  75          $name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
  76          if ( ! preg_match( $name_matcher, $name ) ) {
  77              _doing_it_wrong(
  78                  __METHOD__,
  79                  /* translators: %s: Block name. */
  80                  sprintf( __( 'Block type names must contain a namespace prefix. Example: my-plugin/my-custom-block-type. "%s" was given.' ), esc_html( $name ) ),
  81                  '5.0.0'
  82              );
  83              return false;
  84          }
  85  
  86          if ( $this->is_registered( $name ) ) {
  87              _doing_it_wrong(
  88                  __METHOD__,
  89                  /* translators: %s: Block name. */
  90                  sprintf( __( 'Block type "%s" is already registered.' ), $name ),
  91                  '5.0.0'
  92              );
  93              return false;
  94          }
  95  
  96          if ( ! $block_type ) {
  97              $block_type = new WP_Block_Type( $name, $args );
  98          }
  99  
 100          $this->registered_block_types[ $name ] = $block_type;
 101  
 102          return $block_type;
 103      }
 104  
 105      /**
 106       * Unregisters a block type.
 107       *
 108       * @since 5.0.0
 109       *
 110       * @param string|WP_Block_Type $name Block type name including namespace, or alternatively
 111       *                                   a complete WP_Block_Type instance.
 112       * @return WP_Block_Type|false The unregistered block type on success, or false on failure.
 113       */
 114  	public function unregister( $name ) {
 115          if ( $name instanceof WP_Block_Type ) {
 116              $name = $name->name;
 117          }
 118  
 119          if ( ! $this->is_registered( $name ) ) {
 120              _doing_it_wrong(
 121                  __METHOD__,
 122                  /* translators: %s: Block name. */
 123                  sprintf( __( 'Block type "%s" is not registered.' ), $name ),
 124                  '5.0.0'
 125              );
 126              return false;
 127          }
 128  
 129          $unregistered_block_type = $this->registered_block_types[ $name ];
 130          unset( $this->registered_block_types[ $name ] );
 131  
 132          return $unregistered_block_type;
 133      }
 134  
 135      /**
 136       * Retrieves a registered block type.
 137       *
 138       * @since 5.0.0
 139       *
 140       * @param string|null $name Block type name including namespace.
 141       * @return WP_Block_Type|null The registered block type, or null if it is not registered.
 142       */
 143  	public function get_registered( $name ) {
 144          if ( ! $this->is_registered( $name ) ) {
 145              return null;
 146          }
 147  
 148          return $this->registered_block_types[ $name ];
 149      }
 150  
 151      /**
 152       * Retrieves all registered block types.
 153       *
 154       * @since 5.0.0
 155       *
 156       * @return WP_Block_Type[] Associative array of `$block_type_name => $block_type` pairs.
 157       */
 158  	public function get_all_registered() {
 159          return $this->registered_block_types;
 160      }
 161  
 162      /**
 163       * Checks if a block type is registered.
 164       *
 165       * @since 5.0.0
 166       *
 167       * @param string|null $name Block type name including namespace.
 168       * @return bool True if the block type is registered, false otherwise.
 169       */
 170  	public function is_registered( $name ) {
 171          return isset( $name, $this->registered_block_types[ $name ] );
 172      }
 173  
 174  	public function __wakeup() {
 175          if ( ! $this->registered_block_types ) {
 176              return;
 177          }
 178          if ( ! is_array( $this->registered_block_types ) ) {
 179              throw new UnexpectedValueException();
 180          }
 181          foreach ( $this->registered_block_types as $value ) {
 182              if ( ! $value instanceof WP_Block_Type ) {
 183                  throw new UnexpectedValueException();
 184              }
 185          }
 186      }
 187  
 188      /**
 189       * Utility method to retrieve the main instance of the class.
 190       *
 191       * The instance will be created if it does not exist yet.
 192       *
 193       * @since 5.0.0
 194       *
 195       * @return WP_Block_Type_Registry The main instance.
 196       */
 197  	public static function get_instance() {
 198          if ( null === self::$instance ) {
 199              self::$instance = new self();
 200          }
 201  
 202          return self::$instance;
 203      }
 204  }


Generated : Sun Jun 14 08:20:09 2026 Cross-referenced by PHPXref