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