Editor: Support deferred block variation initialization on the server.

When registering blocks on the server using `register_block_type()` or similar functions, a set of block type variations can also be registered. However, in some cases building this variation data during block registration can be an expensive process, which is not needed in most contexts. 

To address this problem, this adds support to the `WP_Block_Type` object for a new property, `variation_callback`, which can be used to register a callback for building variation data only when the block variations data is needed. The `WP_Block_Type::variations` property has been changed to a private property that is now accessed through the magic `__get()` method. The magic getter makes use of a new public method, `WP_Block_Type::get_variations` which will build variations from a registered callback if variations have not already been built.

Props spacedmonkey, thekt12, Mamaduka, gaambo, gziolo, mukesh27, joemcgill.
Fixes #59969.

Built from https://develop.svn.wordpress.org/trunk@57315


git-svn-id: http://core.svn.wordpress.org/trunk@56821 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Joe McGill 2024-01-19 20:54:13 +00:00
parent 141ba4ff59
commit 5d2bccd66d
2 changed files with 52 additions and 3 deletions

View File

@ -113,9 +113,18 @@ class WP_Block_Type {
* Block variations.
*
* @since 5.8.0
* @var array[]
* @since 6.5.0 Only accessible through magic getter. null by default.
* @var array[]|null
*/
public $variations = array();
private $variations = null;
/**
* Block variations callback.
*
* @since 6.5.0
* @var callable|null
*/
public $variation_callback = null;
/**
* Custom CSS selectors for theme.json style generation.
@ -296,6 +305,7 @@ class WP_Block_Type {
* @type array|null $supports Supported features.
* @type array|null $example Structured data for the block preview.
* @type callable|null $render_callback Block type render callback.
* @type callable|null $variation_callback Block type variations callback.
* @type array|null $attributes Block type attributes property schemas.
* @type string[] $uses_context Context values inherited by blocks of this type.
* @type string[]|null $provides_context Context provided by blocks of this type.
@ -325,6 +335,10 @@ class WP_Block_Type {
* null when value not found, or void when unknown property name provided.
*/
public function __get( $name ) {
if ( 'variations' === $name ) {
return $this->get_variations();
}
if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
return;
}
@ -353,6 +367,10 @@ class WP_Block_Type {
* or false otherwise.
*/
public function __isset( $name ) {
if ( 'variations' === $name ) {
return true;
}
if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
return false;
}
@ -372,6 +390,11 @@ class WP_Block_Type {
* @param mixed $value Property value.
*/
public function __set( $name, $value ) {
if ( 'variations' === $name ) {
$this->variations = $value;
return;
}
if ( ! in_array( $name, $this->deprecated_properties, true ) ) {
$this->{$name} = $value;
return;
@ -540,4 +563,30 @@ class WP_Block_Type {
$this->attributes :
array();
}
/**
* Get block variations.
*
* @since 6.5.0
*
* @return array[]
*/
public function get_variations() {
if ( ! isset( $this->variations ) ) {
$this->variations = array();
if ( is_callable( $this->variation_callback ) ) {
$this->variations = call_user_func( $this->variation_callback );
}
}
/**
* Filters the registered variations for a block type.
*
* @since 6.5.0
*
* @param array $variations Array of registered variations for a block type.
* @param WP_Block_Type $block_type The full block type object.
*/
return apply_filters( 'get_block_type_variations', $this->variations, $this );
}
}

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.5-alpha-57314';
$wp_version = '6.5-alpha-57315';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.