[ 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              $message = __( 'Block type names must be strings.' );
  56              _doing_it_wrong( __METHOD__, $message, '5.0.0' );
  57              return false;
  58          }
  59  
  60          if ( preg_match( '/[A-Z]+/', $name ) ) {
  61              $message = __( 'Block type names must not contain uppercase characters.' );
  62              _doing_it_wrong( __METHOD__, $message, '5.0.0' );
  63              return false;
  64          }
  65  
  66          $name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
  67          if ( ! preg_match( $name_matcher, $name ) ) {
  68              $message = __( 'Block type names must contain a namespace prefix. Example: my-plugin/my-custom-block-type' );
  69              _doing_it_wrong( __METHOD__, $message, '5.0.0' );
  70              return false;
  71          }
  72  
  73          if ( $this->is_registered( $name ) ) {
  74              /* translators: %s: Block name. */
  75              $message = sprintf( __( 'Block type "%s" is already registered.' ), $name );
  76              _doing_it_wrong( __METHOD__, $message, '5.0.0' );
  77              return false;
  78          }
  79  
  80          if ( ! $block_type ) {
  81              $block_type = new WP_Block_Type( $name, $args );
  82          }
  83  
  84          $this->registered_block_types[ $name ] = $block_type;
  85  
  86          return $block_type;
  87      }
  88  
  89      /**
  90       * Unregisters a block type.
  91       *
  92       * @since 5.0.0
  93       *
  94       * @param string|WP_Block_Type $name Block type name including namespace, or alternatively
  95       *                                   a complete WP_Block_Type instance.
  96       * @return WP_Block_Type|false The unregistered block type on success, or false on failure.
  97       */
  98  	public function unregister( $name ) {
  99          if ( $name instanceof WP_Block_Type ) {
 100              $name = $name->name;
 101          }
 102  
 103          if ( ! $this->is_registered( $name ) ) {
 104              /* translators: %s: Block name. */
 105              $message = sprintf( __( 'Block type "%s" is not registered.' ), $name );
 106              _doing_it_wrong( __METHOD__, $message, '5.0.0' );
 107              return false;
 108          }
 109  
 110          $unregistered_block_type = $this->registered_block_types[ $name ];
 111          unset( $this->registered_block_types[ $name ] );
 112  
 113          return $unregistered_block_type;
 114      }
 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  
 129          return $this->registered_block_types[ $name ];
 130      }
 131  
 132      /**
 133       * Retrieves all registered block types.
 134       *
 135       * @since 5.0.0
 136       *
 137       * @return WP_Block_Type[] Associative array of `$block_type_name => $block_type` pairs.
 138       */
 139  	public function get_all_registered() {
 140          return $this->registered_block_types;
 141      }
 142  
 143      /**
 144       * Checks if a block type is registered.
 145       *
 146       * @since 5.0.0
 147       *
 148       * @param string $name Block type name including namespace.
 149       * @return bool True if the block type is registered, false otherwise.
 150       */
 151  	public function is_registered( $name ) {
 152          return isset( $this->registered_block_types[ $name ] );
 153      }
 154  
 155      /**
 156       * Utility method to retrieve the main instance of the class.
 157       *
 158       * The instance will be created if it does not exist yet.
 159       *
 160       * @since 5.0.0
 161       *
 162       * @return WP_Block_Type_Registry The main instance.
 163       */
 164  	public static function get_instance() {
 165          if ( null === self::$instance ) {
 166              self::$instance = new self();
 167          }
 168  
 169          return self::$instance;
 170      }
 171  }


Generated: Mon Apr 19 01:00:04 2021 Cross-referenced by PHPXref 0.7.1