wp/wp-includes/class-wp-block-type-registry.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 21 48c4eec2b7e6
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    32 	/**
    32 	/**
    33 	 * Registers a block type.
    33 	 * Registers a block type.
    34 	 *
    34 	 *
    35 	 * @since 5.0.0
    35 	 * @since 5.0.0
    36 	 *
    36 	 *
       
    37 	 * @see WP_Block_Type::__construct()
       
    38 	 *
    37 	 * @param string|WP_Block_Type $name Block type name including namespace, or alternatively
    39 	 * @param string|WP_Block_Type $name Block type name including namespace, or alternatively
    38 	 *                                   a complete WP_Block_Type instance. In case a WP_Block_Type
    40 	 *                                   a complete WP_Block_Type instance. In case a WP_Block_Type
    39 	 *                                   is provided, the $args parameter will be ignored.
    41 	 *                                   is provided, the $args parameter will be ignored.
    40 	 * @param array                $args {
    42 	 * @param array                $args Optional. Array of block type arguments. Accepts any public property
    41 	 *     Optional. Array of block type arguments. Accepts any public property of `WP_Block_Type`.
    43 	 *                                   of `WP_Block_Type`. See WP_Block_Type::__construct() for information
    42 	 *     Any arguments may be defined, however the ones described below are supported by default.
    44 	 *                                   on accepted arguments. Default empty array.
    43 	 *     Default empty array.
       
    44 	 *
       
    45 	 *     @type callable $render_callback Callback used to render blocks of this block type.
       
    46 	 *     @type array    $attributes      Block attributes mapping, property name to schema.
       
    47 	 * }
       
    48 	 * @return WP_Block_Type|false The registered block type on success, or false on failure.
    45 	 * @return WP_Block_Type|false The registered block type on success, or false on failure.
    49 	 */
    46 	 */
    50 	public function register( $name, $args = array() ) {
    47 	public function register( $name, $args = array() ) {
    51 		$block_type = null;
    48 		$block_type = null;
    52 		if ( $name instanceof WP_Block_Type ) {
    49 		if ( $name instanceof WP_Block_Type ) {
    53 			$block_type = $name;
    50 			$block_type = $name;
    54 			$name       = $block_type->name;
    51 			$name       = $block_type->name;
    55 		}
    52 		}
    56 
    53 
    57 		if ( ! is_string( $name ) ) {
    54 		if ( ! is_string( $name ) ) {
    58 			$message = __( 'Block type names must be strings.' );
    55 			_doing_it_wrong(
    59 			_doing_it_wrong( __METHOD__, $message, '5.0.0' );
    56 				__METHOD__,
       
    57 				__( 'Block type names must be strings.' ),
       
    58 				'5.0.0'
       
    59 			);
    60 			return false;
    60 			return false;
    61 		}
    61 		}
    62 
    62 
    63 		if ( preg_match( '/[A-Z]+/', $name ) ) {
    63 		if ( preg_match( '/[A-Z]+/', $name ) ) {
    64 			$message = __( 'Block type names must not contain uppercase characters.' );
    64 			_doing_it_wrong(
    65 			_doing_it_wrong( __METHOD__, $message, '5.0.0' );
    65 				__METHOD__,
       
    66 				__( 'Block type names must not contain uppercase characters.' ),
       
    67 				'5.0.0'
       
    68 			);
    66 			return false;
    69 			return false;
    67 		}
    70 		}
    68 
    71 
    69 		$name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
    72 		$name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
    70 		if ( ! preg_match( $name_matcher, $name ) ) {
    73 		if ( ! preg_match( $name_matcher, $name ) ) {
    71 			$message = __( 'Block type names must contain a namespace prefix. Example: my-plugin/my-custom-block-type' );
    74 			_doing_it_wrong(
    72 			_doing_it_wrong( __METHOD__, $message, '5.0.0' );
    75 				__METHOD__,
       
    76 				__( 'Block type names must contain a namespace prefix. Example: my-plugin/my-custom-block-type' ),
       
    77 				'5.0.0'
       
    78 			);
    73 			return false;
    79 			return false;
    74 		}
    80 		}
    75 
    81 
    76 		if ( $this->is_registered( $name ) ) {
    82 		if ( $this->is_registered( $name ) ) {
    77 			/* translators: %s: Block name. */
    83 			_doing_it_wrong(
    78 			$message = sprintf( __( 'Block type "%s" is already registered.' ), $name );
    84 				__METHOD__,
    79 			_doing_it_wrong( __METHOD__, $message, '5.0.0' );
    85 				/* translators: %s: Block name. */
       
    86 				sprintf( __( 'Block type "%s" is already registered.' ), $name ),
       
    87 				'5.0.0'
       
    88 			);
    80 			return false;
    89 			return false;
    81 		}
    90 		}
    82 
    91 
    83 		if ( ! $block_type ) {
    92 		if ( ! $block_type ) {
    84 			$block_type = new WP_Block_Type( $name, $args );
    93 			$block_type = new WP_Block_Type( $name, $args );
   102 		if ( $name instanceof WP_Block_Type ) {
   111 		if ( $name instanceof WP_Block_Type ) {
   103 			$name = $name->name;
   112 			$name = $name->name;
   104 		}
   113 		}
   105 
   114 
   106 		if ( ! $this->is_registered( $name ) ) {
   115 		if ( ! $this->is_registered( $name ) ) {
   107 			/* translators: %s: Block name. */
   116 			_doing_it_wrong(
   108 			$message = sprintf( __( 'Block type "%s" is not registered.' ), $name );
   117 				__METHOD__,
   109 			_doing_it_wrong( __METHOD__, $message, '5.0.0' );
   118 				/* translators: %s: Block name. */
       
   119 				sprintf( __( 'Block type "%s" is not registered.' ), $name ),
       
   120 				'5.0.0'
       
   121 			);
   110 			return false;
   122 			return false;
   111 		}
   123 		}
   112 
   124 
   113 		$unregistered_block_type = $this->registered_block_types[ $name ];
   125 		$unregistered_block_type = $this->registered_block_types[ $name ];
   114 		unset( $this->registered_block_types[ $name ] );
   126 		unset( $this->registered_block_types[ $name ] );