wp/wp-includes/class-wp-block-type.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    12  *
    12  *
    13  * @since 5.0.0
    13  * @since 5.0.0
    14  *
    14  *
    15  * @see register_block_type()
    15  * @see register_block_type()
    16  */
    16  */
       
    17 #[AllowDynamicProperties]
    17 class WP_Block_Type {
    18 class WP_Block_Type {
    18 
    19 
    19 	/**
    20 	/**
    20 	 * Block API version.
    21 	 * Block API version.
    21 	 *
    22 	 *
    52 	/**
    53 	/**
    53 	 * Setting parent lets a block require that it is only available
    54 	 * Setting parent lets a block require that it is only available
    54 	 * when nested within the specified blocks.
    55 	 * when nested within the specified blocks.
    55 	 *
    56 	 *
    56 	 * @since 5.5.0
    57 	 * @since 5.5.0
    57 	 * @var array|null
    58 	 * @var string[]|null
    58 	 */
    59 	 */
    59 	public $parent = null;
    60 	public $parent = null;
    60 
    61 
    61 	/**
    62 	/**
    62 	 * Setting ancestor makes a block available only inside the specified
    63 	 * Setting ancestor makes a block available only inside the specified
    63 	 * block types at any position of the ancestor's block subtree.
    64 	 * block types at any position of the ancestor's block subtree.
    64 	 *
    65 	 *
    65 	 * @since 6.0.0
    66 	 * @since 6.0.0
    66 	 * @var array|null
    67 	 * @var string[]|null
    67 	 */
    68 	 */
    68 	public $ancestor = null;
    69 	public $ancestor = null;
       
    70 
       
    71 	/**
       
    72 	 * Limits which block types can be inserted as children of this block type.
       
    73 	 *
       
    74 	 * @since 6.5.0
       
    75 	 * @var string[]|null
       
    76 	 */
       
    77 	public $allowed_blocks = null;
    69 
    78 
    70 	/**
    79 	/**
    71 	 * Block type icon.
    80 	 * Block type icon.
    72 	 *
    81 	 *
    73 	 * @since 5.5.0
    82 	 * @since 5.5.0
   110 
   119 
   111 	/**
   120 	/**
   112 	 * Block variations.
   121 	 * Block variations.
   113 	 *
   122 	 *
   114 	 * @since 5.8.0
   123 	 * @since 5.8.0
       
   124 	 * @since 6.5.0 Only accessible through magic getter. null by default.
       
   125 	 * @var array[]|null
       
   126 	 */
       
   127 	private $variations = null;
       
   128 
       
   129 	/**
       
   130 	 * Block variations callback.
       
   131 	 *
       
   132 	 * @since 6.5.0
       
   133 	 * @var callable|null
       
   134 	 */
       
   135 	public $variation_callback = null;
       
   136 
       
   137 	/**
       
   138 	 * Custom CSS selectors for theme.json style generation.
       
   139 	 *
       
   140 	 * @since 6.3.0
   115 	 * @var array
   141 	 * @var array
   116 	 */
   142 	 */
   117 	public $variations = array();
   143 	public $selectors = array();
   118 
   144 
   119 	/**
   145 	/**
   120 	 * Supported features.
   146 	 * Supported features.
   121 	 *
   147 	 *
   122 	 * @since 5.5.0
   148 	 * @since 5.5.0
   150 
   176 
   151 	/**
   177 	/**
   152 	 * Context values inherited by blocks of this type.
   178 	 * Context values inherited by blocks of this type.
   153 	 *
   179 	 *
   154 	 * @since 5.5.0
   180 	 * @since 5.5.0
       
   181 	 * @var string[]
       
   182 	 */
       
   183 	private $uses_context = array();
       
   184 
       
   185 	/**
       
   186 	 * Context provided by blocks of this type.
       
   187 	 *
       
   188 	 * @since 5.5.0
       
   189 	 * @var string[]|null
       
   190 	 */
       
   191 	public $provides_context = null;
       
   192 
       
   193 	/**
       
   194 	 * Block hooks for this block type.
       
   195 	 *
       
   196 	 * A block hook is specified by a block type and a relative position.
       
   197 	 * The hooked block will be automatically inserted in the given position
       
   198 	 * next to the "anchor" block whenever the latter is encountered.
       
   199 	 *
       
   200 	 * @since 6.4.0
       
   201 	 * @var string[]
       
   202 	 */
       
   203 	public $block_hooks = array();
       
   204 
       
   205 	/**
       
   206 	 * Block type editor only script handles.
       
   207 	 *
       
   208 	 * @since 6.1.0
       
   209 	 * @var string[]
       
   210 	 */
       
   211 	public $editor_script_handles = array();
       
   212 
       
   213 	/**
       
   214 	 * Block type front end and editor script handles.
       
   215 	 *
       
   216 	 * @since 6.1.0
       
   217 	 * @var string[]
       
   218 	 */
       
   219 	public $script_handles = array();
       
   220 
       
   221 	/**
       
   222 	 * Block type front end only script handles.
       
   223 	 *
       
   224 	 * @since 6.1.0
       
   225 	 * @var string[]
       
   226 	 */
       
   227 	public $view_script_handles = array();
       
   228 
       
   229 	/**
       
   230 	 * Block type front end only script module IDs.
       
   231 	 *
       
   232 	 * @since 6.5.0
       
   233 	 * @var string[]
       
   234 	 */
       
   235 	public $view_script_module_ids = array();
       
   236 
       
   237 	/**
       
   238 	 * Block type editor only style handles.
       
   239 	 *
       
   240 	 * @since 6.1.0
       
   241 	 * @var string[]
       
   242 	 */
       
   243 	public $editor_style_handles = array();
       
   244 
       
   245 	/**
       
   246 	 * Block type front end and editor style handles.
       
   247 	 *
       
   248 	 * @since 6.1.0
       
   249 	 * @var string[]
       
   250 	 */
       
   251 	public $style_handles = array();
       
   252 
       
   253 	/**
       
   254 	 * Block type front end only style handles.
       
   255 	 *
       
   256 	 * @since 6.5.0
       
   257 	 * @var string[]
       
   258 	 */
       
   259 	public $view_style_handles = array();
       
   260 
       
   261 	/**
       
   262 	 * Deprecated block type properties for script and style handles.
       
   263 	 *
       
   264 	 * @since 6.1.0
       
   265 	 * @var string[]
       
   266 	 */
       
   267 	private $deprecated_properties = array(
       
   268 		'editor_script',
       
   269 		'script',
       
   270 		'view_script',
       
   271 		'editor_style',
       
   272 		'style',
       
   273 	);
       
   274 
       
   275 	/**
       
   276 	 * Attributes supported by every block.
       
   277 	 *
       
   278 	 * @since 6.0.0 Added `lock`.
       
   279 	 * @since 6.5.0 Added `metadata`.
   155 	 * @var array
   280 	 * @var array
   156 	 */
   281 	 */
   157 	public $uses_context = array();
       
   158 
       
   159 	/**
       
   160 	 * Context provided by blocks of this type.
       
   161 	 *
       
   162 	 * @since 5.5.0
       
   163 	 * @var array|null
       
   164 	 */
       
   165 	public $provides_context = null;
       
   166 
       
   167 	/**
       
   168 	 * Block type editor only script handle.
       
   169 	 *
       
   170 	 * @since 5.0.0
       
   171 	 * @var string|null
       
   172 	 */
       
   173 	public $editor_script = null;
       
   174 
       
   175 	/**
       
   176 	 * Block type front end and editor script handle.
       
   177 	 *
       
   178 	 * @since 5.0.0
       
   179 	 * @var string|null
       
   180 	 */
       
   181 	public $script = null;
       
   182 
       
   183 	/**
       
   184 	 * Block type front end only script handle.
       
   185 	 *
       
   186 	 * @since 5.9.0
       
   187 	 * @var string|null
       
   188 	 */
       
   189 	public $view_script = null;
       
   190 
       
   191 	/**
       
   192 	 * Block type editor only style handle.
       
   193 	 *
       
   194 	 * @since 5.0.0
       
   195 	 * @var string|null
       
   196 	 */
       
   197 	public $editor_style = null;
       
   198 
       
   199 	/**
       
   200 	 * Block type front end and editor style handle.
       
   201 	 *
       
   202 	 * @since 5.0.0
       
   203 	 * @var string|null
       
   204 	 */
       
   205 	public $style = null;
       
   206 
       
   207 	/**
       
   208 	 * Attributes supported by every block.
       
   209 	 *
       
   210 	 * @since 6.0.0
       
   211 	 * @var array
       
   212 	 */
       
   213 	const GLOBAL_ATTRIBUTES = array(
   282 	const GLOBAL_ATTRIBUTES = array(
   214 		'lock' => array( 'type' => 'object' ),
   283 		'lock'     => array( 'type' => 'object' ),
       
   284 		'metadata' => array( 'type' => 'object' ),
   215 	);
   285 	);
   216 
   286 
   217 	/**
   287 	/**
   218 	 * Constructor.
   288 	 * Constructor.
   219 	 *
   289 	 *
   225 	 *              `uses_context`, and `provides_context` properties.
   295 	 *              `uses_context`, and `provides_context` properties.
   226 	 * @since 5.6.0 Added the `api_version` property.
   296 	 * @since 5.6.0 Added the `api_version` property.
   227 	 * @since 5.8.0 Added the `variations` property.
   297 	 * @since 5.8.0 Added the `variations` property.
   228 	 * @since 5.9.0 Added the `view_script` property.
   298 	 * @since 5.9.0 Added the `view_script` property.
   229 	 * @since 6.0.0 Added the `ancestor` property.
   299 	 * @since 6.0.0 Added the `ancestor` property.
       
   300 	 * @since 6.1.0 Added the `editor_script_handles`, `script_handles`, `view_script_handles`,
       
   301 	 *              `editor_style_handles`, and `style_handles` properties.
       
   302 	 *              Deprecated the `editor_script`, `script`, `view_script`, `editor_style`, and `style` properties.
       
   303 	 * @since 6.3.0 Added the `selectors` property.
       
   304 	 * @since 6.4.0 Added the `block_hooks` property.
       
   305 	 * @since 6.5.0 Added the `allowed_blocks`, `variation_callback`, and `view_style_handles` properties.
   230 	 *
   306 	 *
   231 	 * @see register_block_type()
   307 	 * @see register_block_type()
   232 	 *
   308 	 *
   233 	 * @param string       $block_type Block type name including namespace.
   309 	 * @param string       $block_type Block type name including namespace.
   234 	 * @param array|string $args       {
   310 	 * @param array|string $args       {
   235 	 *     Optional. Array or string of arguments for registering a block type. Any arguments may be defined,
   311 	 *     Optional. Array or string of arguments for registering a block type. Any arguments may be defined,
   236 	 *     however the ones described below are supported by default. Default empty array.
   312 	 *     however the ones described below are supported by default. Default empty array.
   237 	 *
   313 	 *
   238 	 *     @type string        $api_version      Block API version.
   314 	 *     @type string        $api_version              Block API version.
   239 	 *     @type string        $title            Human-readable block type label.
   315 	 *     @type string        $title                    Human-readable block type label.
   240 	 *     @type string|null   $category         Block type category classification, used in
   316 	 *     @type string|null   $category                 Block type category classification, used in
   241 	 *                                           search interfaces to arrange block types by category.
   317 	 *                                                   search interfaces to arrange block types by category.
   242 	 *     @type array|null    $parent           Setting parent lets a block require that it is only
   318 	 *     @type string[]|null $parent                   Setting parent lets a block require that it is only
   243 	 *                                           available when nested within the specified blocks.
   319 	 *                                                   available when nested within the specified blocks.
   244 	 *     @type array|null    $ancestor         Setting ancestor makes a block available only inside the specified
   320 	 *     @type string[]|null $ancestor                 Setting ancestor makes a block available only inside the specified
   245 	 *                                           block types at any position of the ancestor's block subtree.
   321 	 *                                                   block types at any position of the ancestor's block subtree.
   246 	 *     @type string|null   $icon             Block type icon.
   322 	 *     @type string[]|null $allowed_blocks           Limits which block types can be inserted as children of this block type.
   247 	 *     @type string        $description      A detailed block type description.
   323 	 *     @type string|null   $icon                     Block type icon.
   248 	 *     @type string[]      $keywords         Additional keywords to produce block type as
   324 	 *     @type string        $description              A detailed block type description.
   249 	 *                                           result in search interfaces.
   325 	 *     @type string[]      $keywords                 Additional keywords to produce block type as
   250 	 *     @type string|null   $textdomain       The translation textdomain.
   326 	 *                                                   result in search interfaces.
   251 	 *     @type array         $styles           Alternative block styles.
   327 	 *     @type string|null   $textdomain               The translation textdomain.
   252 	 *     @type array         $variations       Block variations.
   328 	 *     @type array[]       $styles                   Alternative block styles.
   253 	 *     @type array|null    $supports         Supported features.
   329 	 *     @type array[]       $variations               Block variations.
   254 	 *     @type array|null    $example          Structured data for the block preview.
   330 	 *     @type array         $selectors                Custom CSS selectors for theme.json style generation.
   255 	 *     @type callable|null $render_callback  Block type render callback.
   331 	 *     @type array|null    $supports                 Supported features.
   256 	 *     @type array|null    $attributes       Block type attributes property schemas.
   332 	 *     @type array|null    $example                  Structured data for the block preview.
   257 	 *     @type array         $uses_context     Context values inherited by blocks of this type.
   333 	 *     @type callable|null $render_callback          Block type render callback.
   258 	 *     @type array|null    $provides_context Context provided by blocks of this type.
   334 	 *     @type callable|null $variation_callback       Block type variations callback.
   259 	 *     @type string|null   $editor_script    Block type editor only script handle.
   335 	 *     @type array|null    $attributes               Block type attributes property schemas.
   260 	 *     @type string|null   $script           Block type front end and editor script handle.
   336 	 *     @type string[]      $uses_context             Context values inherited by blocks of this type.
   261 	 *     @type string|null   $view_script      Block type front end only script handle.
   337 	 *     @type string[]|null $provides_context         Context provided by blocks of this type.
   262 	 *     @type string|null   $editor_style     Block type editor only style handle.
   338 	 *     @type string[]      $block_hooks              Block hooks.
   263 	 *     @type string|null   $style            Block type front end and editor style handle.
   339 	 *     @type string[]      $editor_script_handles    Block type editor only script handles.
       
   340 	 *     @type string[]      $script_handles           Block type front end and editor script handles.
       
   341 	 *     @type string[]      $view_script_handles      Block type front end only script handles.
       
   342 	 *     @type string[]      $editor_style_handles     Block type editor only style handles.
       
   343 	 *     @type string[]      $style_handles            Block type front end and editor style handles.
       
   344 	 *     @type string[]      $view_style_handles       Block type front end only style handles.
   264 	 * }
   345 	 * }
   265 	 */
   346 	 */
   266 	public function __construct( $block_type, $args = array() ) {
   347 	public function __construct( $block_type, $args = array() ) {
   267 		$this->name = $block_type;
   348 		$this->name = $block_type;
   268 
   349 
   269 		$this->set_props( $args );
   350 		$this->set_props( $args );
       
   351 	}
       
   352 
       
   353 	/**
       
   354 	 * Proxies getting values for deprecated properties for script and style handles for backward compatibility.
       
   355 	 * Gets the value for the corresponding new property if the first item in the array provided.
       
   356 	 *
       
   357 	 * @since 6.1.0
       
   358 	 *
       
   359 	 * @param string $name Deprecated property name.
       
   360 	 *
       
   361 	 * @return string|string[]|null|void The value read from the new property if the first item in the array provided,
       
   362 	 *                                   null when value not found, or void when unknown property name provided.
       
   363 	 */
       
   364 	public function __get( $name ) {
       
   365 		if ( 'variations' === $name ) {
       
   366 			return $this->get_variations();
       
   367 		}
       
   368 
       
   369 		if ( 'uses_context' === $name ) {
       
   370 			return $this->get_uses_context();
       
   371 		}
       
   372 
       
   373 		if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
       
   374 			return;
       
   375 		}
       
   376 
       
   377 		$new_name = $name . '_handles';
       
   378 
       
   379 		if ( ! property_exists( $this, $new_name ) || ! is_array( $this->{$new_name} ) ) {
       
   380 			return null;
       
   381 		}
       
   382 
       
   383 		if ( count( $this->{$new_name} ) > 1 ) {
       
   384 			return $this->{$new_name};
       
   385 		}
       
   386 		return isset( $this->{$new_name}[0] ) ? $this->{$new_name}[0] : null;
       
   387 	}
       
   388 
       
   389 	/**
       
   390 	 * Proxies checking for deprecated properties for script and style handles for backward compatibility.
       
   391 	 * Checks whether the corresponding new property has the first item in the array provided.
       
   392 	 *
       
   393 	 * @since 6.1.0
       
   394 	 *
       
   395 	 * @param string $name Deprecated property name.
       
   396 	 *
       
   397 	 * @return bool Returns true when for the new property the first item in the array exists,
       
   398 	 *              or false otherwise.
       
   399 	 */
       
   400 	public function __isset( $name ) {
       
   401 		if ( in_array( $name, array( 'variations', 'uses_context' ), true ) ) {
       
   402 			return true;
       
   403 		}
       
   404 
       
   405 		if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
       
   406 			return false;
       
   407 		}
       
   408 
       
   409 		$new_name = $name . '_handles';
       
   410 		return isset( $this->{$new_name}[0] );
       
   411 	}
       
   412 
       
   413 	/**
       
   414 	 * Proxies setting values for deprecated properties for script and style handles for backward compatibility.
       
   415 	 * Sets the value for the corresponding new property as the first item in the array.
       
   416 	 * It also allows setting custom properties for backward compatibility.
       
   417 	 *
       
   418 	 * @since 6.1.0
       
   419 	 *
       
   420 	 * @param string $name  Property name.
       
   421 	 * @param mixed  $value Property value.
       
   422 	 */
       
   423 	public function __set( $name, $value ) {
       
   424 		if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
       
   425 			$this->{$name} = $value;
       
   426 			return;
       
   427 		}
       
   428 
       
   429 		$new_name = $name . '_handles';
       
   430 
       
   431 		if ( is_array( $value ) ) {
       
   432 			$filtered = array_filter( $value, 'is_string' );
       
   433 
       
   434 			if ( count( $filtered ) !== count( $value ) ) {
       
   435 					_doing_it_wrong(
       
   436 						__METHOD__,
       
   437 						sprintf(
       
   438 							/* translators: %s: The '$value' argument. */
       
   439 							__( 'The %s argument must be a string or a string array.' ),
       
   440 							'<code>$value</code>'
       
   441 						),
       
   442 						'6.1.0'
       
   443 					);
       
   444 			}
       
   445 
       
   446 			$this->{$new_name} = array_values( $filtered );
       
   447 			return;
       
   448 		}
       
   449 
       
   450 		if ( ! is_string( $value ) ) {
       
   451 			return;
       
   452 		}
       
   453 
       
   454 		$this->{$new_name} = array( $value );
   270 	}
   455 	}
   271 
   456 
   272 	/**
   457 	/**
   273 	 * Renders the block type output for given attributes.
   458 	 * Renders the block type output for given attributes.
   274 	 *
   459 	 *
   402 	public function get_attributes() {
   587 	public function get_attributes() {
   403 		return is_array( $this->attributes ) ?
   588 		return is_array( $this->attributes ) ?
   404 			$this->attributes :
   589 			$this->attributes :
   405 			array();
   590 			array();
   406 	}
   591 	}
       
   592 
       
   593 	/**
       
   594 	 * Get block variations.
       
   595 	 *
       
   596 	 * @since 6.5.0
       
   597 	 *
       
   598 	 * @return array[]
       
   599 	 */
       
   600 	public function get_variations() {
       
   601 		if ( ! isset( $this->variations ) ) {
       
   602 			$this->variations = array();
       
   603 			if ( is_callable( $this->variation_callback ) ) {
       
   604 				$this->variations = call_user_func( $this->variation_callback );
       
   605 			}
       
   606 		}
       
   607 
       
   608 		/**
       
   609 		 * Filters the registered variations for a block type.
       
   610 		 *
       
   611 		 * @since 6.5.0
       
   612 		 *
       
   613 		 * @param array         $variations Array of registered variations for a block type.
       
   614 		 * @param WP_Block_Type $block_type The full block type object.
       
   615 		 */
       
   616 		return apply_filters( 'get_block_type_variations', $this->variations, $this );
       
   617 	}
       
   618 
       
   619 	/**
       
   620 	 * Get block uses context.
       
   621 	 *
       
   622 	 * @since 6.5.0
       
   623 	 *
       
   624 	 * @return string[]
       
   625 	 */
       
   626 	public function get_uses_context() {
       
   627 		/**
       
   628 		 * Filters the registered uses context for a block type.
       
   629 		 *
       
   630 		 * @since 6.5.0
       
   631 		 *
       
   632 		 * @param string[]      $uses_context Array of registered uses context for a block type.
       
   633 		 * @param WP_Block_Type $block_type   The full block type object.
       
   634 		 */
       
   635 		return apply_filters( 'get_block_type_uses_context', $this->uses_context, $this );
       
   636 	}
   407 }
   637 }