[ Index ]

PHP Cross Reference of WordPress

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


Generated: Thu Nov 21 01:00:03 2024 Cross-referenced by PHPXref 0.7.1