wp/wp-includes/class-wp-block-type.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
equal deleted inserted replaced
17:34716fd837a4 18:be944660c56a
    15  * @see register_block_type()
    15  * @see register_block_type()
    16  */
    16  */
    17 class WP_Block_Type {
    17 class WP_Block_Type {
    18 
    18 
    19 	/**
    19 	/**
       
    20 	 * Block API version.
       
    21 	 *
       
    22 	 * @since 5.6.0
       
    23 	 * @var int
       
    24 	 */
       
    25 	public $api_version = 1;
       
    26 
       
    27 	/**
    20 	 * Block type key.
    28 	 * Block type key.
    21 	 *
    29 	 *
    22 	 * @since 5.0.0
    30 	 * @since 5.0.0
    23 	 * @var string
    31 	 * @var string
    24 	 */
    32 	 */
    25 	public $name;
    33 	public $name;
    26 
    34 
    27 	/**
    35 	/**
       
    36 	 * Human-readable block type label.
       
    37 	 *
    28 	 * @since 5.5.0
    38 	 * @since 5.5.0
    29 	 * @var string
    39 	 * @var string
    30 	 */
    40 	 */
    31 	public $title = '';
    41 	public $title = '';
    32 
    42 
    33 	/**
    43 	/**
       
    44 	 * Block type category classification, used in search interfaces
       
    45 	 * to arrange block types by category.
       
    46 	 *
    34 	 * @since 5.5.0
    47 	 * @since 5.5.0
    35 	 * @var string|null
    48 	 * @var string|null
    36 	 */
    49 	 */
    37 	public $category = null;
    50 	public $category = null;
    38 
    51 
    39 	/**
    52 	/**
       
    53 	 * Setting parent lets a block require that it is only available
       
    54 	 * when nested within the specified blocks.
       
    55 	 *
    40 	 * @since 5.5.0
    56 	 * @since 5.5.0
    41 	 * @var array|null
    57 	 * @var array|null
    42 	 */
    58 	 */
    43 	public $parent = null;
    59 	public $parent = null;
    44 
    60 
    45 	/**
    61 	/**
       
    62 	 * Block type icon.
       
    63 	 *
    46 	 * @since 5.5.0
    64 	 * @since 5.5.0
    47 	 * @var string|null
    65 	 * @var string|null
    48 	 */
    66 	 */
    49 	public $icon = null;
    67 	public $icon = null;
    50 
    68 
    51 	/**
    69 	/**
       
    70 	 * A detailed block type description.
       
    71 	 *
    52 	 * @since 5.5.0
    72 	 * @since 5.5.0
    53 	 * @var string
    73 	 * @var string
    54 	 */
    74 	 */
    55 	public $description = '';
    75 	public $description = '';
    56 
    76 
    57 	/**
    77 	/**
       
    78 	 * Additional keywords to produce block type as result
       
    79 	 * in search interfaces.
       
    80 	 *
    58 	 * @since 5.5.0
    81 	 * @since 5.5.0
    59 	 * @var array
    82 	 * @var array
    60 	 */
    83 	 */
    61 	public $keywords = array();
    84 	public $keywords = array();
    62 
    85 
    63 	/**
    86 	/**
       
    87 	 * The translation textdomain.
       
    88 	 *
    64 	 * @since 5.5.0
    89 	 * @since 5.5.0
    65 	 * @var string|null
    90 	 * @var string|null
    66 	 */
    91 	 */
    67 	public $textdomain = null;
    92 	public $textdomain = null;
    68 
    93 
    69 	/**
    94 	/**
       
    95 	 * Alternative block styles.
       
    96 	 *
    70 	 * @since 5.5.0
    97 	 * @since 5.5.0
    71 	 * @var array
    98 	 * @var array
    72 	 */
    99 	 */
    73 	public $styles = array();
   100 	public $styles = array();
    74 
   101 
    75 	/**
   102 	/**
       
   103 	 * Block variations.
       
   104 	 *
       
   105 	 * @since 5.8.0
       
   106 	 * @var array
       
   107 	 */
       
   108 	public $variations = array();
       
   109 
       
   110 	/**
       
   111 	 * Supported features.
       
   112 	 *
    76 	 * @since 5.5.0
   113 	 * @since 5.5.0
    77 	 * @var array|null
   114 	 * @var array|null
    78 	 */
   115 	 */
    79 	public $supports = null;
   116 	public $supports = null;
    80 
   117 
    81 	/**
   118 	/**
       
   119 	 * Structured data for the block preview.
       
   120 	 *
    82 	 * @since 5.5.0
   121 	 * @since 5.5.0
    83 	 * @var array|null
   122 	 * @var array|null
    84 	 */
   123 	 */
    85 	public $example = null;
   124 	public $example = null;
    86 
   125 
   152 	 * Constructor.
   191 	 * Constructor.
   153 	 *
   192 	 *
   154 	 * Will populate object properties from the provided arguments.
   193 	 * Will populate object properties from the provided arguments.
   155 	 *
   194 	 *
   156 	 * @since 5.0.0
   195 	 * @since 5.0.0
       
   196 	 * @since 5.5.0 Added the `title`, `category`, `parent`, `icon`, `description`,
       
   197 	 *              `keywords`, `textdomain`, `styles`, `supports`, `example`,
       
   198 	 *              `uses_context`, and `provides_context` properties.
       
   199 	 * @since 5.6.0 Added the `api_version` property.
       
   200 	 * @since 5.8.0 Added the `variations` property.
   157 	 *
   201 	 *
   158 	 * @see register_block_type()
   202 	 * @see register_block_type()
   159 	 *
   203 	 *
   160 	 * @param string       $block_type Block type name including namespace.
   204 	 * @param string       $block_type Block type name including namespace.
   161 	 * @param array|string $args       Optional. Array or string of arguments for registering a block type.
   205 	 * @param array|string $args       {
   162 	 *                                 Default empty array.
   206 	 *     Optional. Array or string of arguments for registering a block type. Any arguments may be defined,
       
   207 	 *     however the ones described below are supported by default. Default empty array.
       
   208 	 *
       
   209 	 *     @type string        $api_version      Block API version.
       
   210 	 *     @type string        $title            Human-readable block type label.
       
   211 	 *     @type string|null   $category         Block type category classification, used in
       
   212 	 *                                           search interfaces to arrange block types by category.
       
   213 	 *     @type array|null    $parent           Setting parent lets a block require that it is only
       
   214 	 *                                           available when nested within the specified blocks.
       
   215 	 *     @type string|null   $icon             Block type icon.
       
   216 	 *     @type string        $description      A detailed block type description.
       
   217 	 *     @type array         $keywords         Additional keywords to produce block type as
       
   218 	 *                                           result in search interfaces.
       
   219 	 *     @type string|null   $textdomain       The translation textdomain.
       
   220 	 *     @type array         $styles           Alternative block styles.
       
   221 	 *     @type array         $variations       Block variations.
       
   222 	 *     @type array|null    $supports         Supported features.
       
   223 	 *     @type array|null    $example          Structured data for the block preview.
       
   224 	 *     @type callable|null $render_callback  Block type render callback.
       
   225 	 *     @type array|null    $attributes       Block type attributes property schemas.
       
   226 	 *     @type array         $uses_context     Context values inherited by blocks of this type.
       
   227 	 *     @type array|null    $provides_context Context provided by blocks of this type.
       
   228 	 *     @type string|null   $editor_script    Block type editor script handle.
       
   229 	 *     @type string|null   $script           Block type front end script handle.
       
   230 	 *     @type string|null   $editor_style     Block type editor style handle.
       
   231 	 *     @type string|null   $style            Block type front end style handle.
       
   232 	 * }
   163 	 */
   233 	 */
   164 	public function __construct( $block_type, $args = array() ) {
   234 	public function __construct( $block_type, $args = array() ) {
   165 		$this->name = $block_type;
   235 		$this->name = $block_type;
   166 
   236 
   167 		$this->set_props( $args );
   237 		$this->set_props( $args );
   190 	 * Returns true if the block type is dynamic, or false otherwise. A dynamic
   260 	 * Returns true if the block type is dynamic, or false otherwise. A dynamic
   191 	 * block is one which defers its rendering to occur on-demand at runtime.
   261 	 * block is one which defers its rendering to occur on-demand at runtime.
   192 	 *
   262 	 *
   193 	 * @since 5.0.0
   263 	 * @since 5.0.0
   194 	 *
   264 	 *
   195 	 * @return boolean Whether block type is dynamic.
   265 	 * @return bool Whether block type is dynamic.
   196 	 */
   266 	 */
   197 	public function is_dynamic() {
   267 	public function is_dynamic() {
   198 		return is_callable( $this->render_callback );
   268 		return is_callable( $this->render_callback );
   199 	}
   269 	}
   200 
   270 
   207 	 * @param array $attributes Original block attributes.
   277 	 * @param array $attributes Original block attributes.
   208 	 * @return array Prepared block attributes.
   278 	 * @return array Prepared block attributes.
   209 	 */
   279 	 */
   210 	public function prepare_attributes_for_render( $attributes ) {
   280 	public function prepare_attributes_for_render( $attributes ) {
   211 		// If there are no attribute definitions for the block type, skip
   281 		// If there are no attribute definitions for the block type, skip
   212 		// processing and return vebatim.
   282 		// processing and return verbatim.
   213 		if ( ! isset( $this->attributes ) ) {
   283 		if ( ! isset( $this->attributes ) ) {
   214 			return $attributes;
   284 			return $attributes;
   215 		}
   285 		}
   216 
   286 
   217 		foreach ( $attributes as $attribute_name => $value ) {
   287 		foreach ( $attributes as $attribute_name => $value ) {
   225 
   295 
   226 			// Validate value by JSON schema. An invalid value should revert to
   296 			// Validate value by JSON schema. An invalid value should revert to
   227 			// its default, if one exists. This occurs by virtue of the missing
   297 			// its default, if one exists. This occurs by virtue of the missing
   228 			// attributes loop immediately following. If there is not a default
   298 			// attributes loop immediately following. If there is not a default
   229 			// assigned, the attribute value should remain unset.
   299 			// assigned, the attribute value should remain unset.
   230 			$is_valid = rest_validate_value_from_schema( $value, $schema );
   300 			$is_valid = rest_validate_value_from_schema( $value, $schema, $attribute_name );
   231 			if ( is_wp_error( $is_valid ) ) {
   301 			if ( is_wp_error( $is_valid ) ) {
   232 				unset( $attributes[ $attribute_name ] );
   302 				unset( $attributes[ $attribute_name ] );
   233 			}
   303 			}
   234 		}
   304 		}
   235 
   305 
   249 	 * Sets block type properties.
   319 	 * Sets block type properties.
   250 	 *
   320 	 *
   251 	 * @since 5.0.0
   321 	 * @since 5.0.0
   252 	 *
   322 	 *
   253 	 * @param array|string $args Array or string of arguments for registering a block type.
   323 	 * @param array|string $args Array or string of arguments for registering a block type.
       
   324 	 *                           See WP_Block_Type::__construct() for information on accepted arguments.
   254 	 */
   325 	 */
   255 	public function set_props( $args ) {
   326 	public function set_props( $args ) {
   256 		$args = wp_parse_args(
   327 		$args = wp_parse_args(
   257 			$args,
   328 			$args,
   258 			array(
   329 			array(