diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index b9986d1bdec7a..2b028b2e95f56 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2302,7 +2302,6 @@ function get_block_editor_server_block_settings() { 'ancestor' => 'ancestor', 'keywords' => 'keywords', 'example' => 'example', - 'variations' => 'variations', ); foreach ( $block_registry->get_all_registered() as $block_name => $block_type ) { @@ -2317,6 +2316,7 @@ function get_block_editor_server_block_settings() { $blocks[ $block_name ][ $key ] = $block_type->{ $field }; } + $blocks[ $block_name ][ 'variations' ] = $block_type->get_variations(); } return $blocks; diff --git a/src/wp-includes/blocks/navigation-link.php b/src/wp-includes/blocks/navigation-link.php index 5333ab6ea3dc9..ee878451bc32d 100644 --- a/src/wp-includes/blocks/navigation-link.php +++ b/src/wp-includes/blocks/navigation-link.php @@ -323,12 +323,11 @@ function build_variation_for_navigation_link( $entity, $kind ) { } /** - * Register the navigation link block. + * Returns an array of variations for the navigation link block. * - * @uses render_block_core_navigation() - * @throws WP_Error An WP_Error exception parsing the block definition. + * @return array */ -function register_block_core_navigation_link() { +function build_navigation_link_block_variations() { $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); @@ -360,11 +359,21 @@ function register_block_core_navigation_link() { } } + return array_merge( $built_ins, $variations ); +} + +/** + * Register the navigation link block. + * + * @uses render_block_core_navigation() + * @throws WP_Error An WP_Error exception parsing the block definition. + */ +function register_block_core_navigation_link() { register_block_type_from_metadata( __DIR__ . '/navigation-link', array( - 'render_callback' => 'render_block_core_navigation_link', - 'variations' => array_merge( $built_ins, $variations ), + 'render_callback' => 'render_block_core_navigation_link', + 'variation_callback' => 'build_navigation_link_block_variations', ) ); } diff --git a/src/wp-includes/blocks/post-terms.php b/src/wp-includes/blocks/post-terms.php index c97155b81e3a9..41e7bd06df387 100644 --- a/src/wp-includes/blocks/post-terms.php +++ b/src/wp-includes/blocks/post-terms.php @@ -59,9 +59,9 @@ function render_block_core_post_terms( $attributes, $content, $block ) { } /** - * Registers the `core/post-terms` block on the server. + * @return array */ -function register_block_core_post_terms() { +function build_post_term_block_variations() { $taxonomies = get_taxonomies( array( 'publicly_queryable' => true, @@ -82,7 +82,7 @@ function register_block_core_post_terms() { 'name' => $taxonomy->name, 'title' => $taxonomy->label, 'description' => sprintf( - /* translators: %s: taxonomy's label */ + /* translators: %s: taxonomy's label */ __( 'Display a list of assigned terms from the taxonomy: %s' ), $taxonomy->label ), @@ -102,12 +102,18 @@ function register_block_core_post_terms() { $custom_variations[] = $variation; } } + return array_merge( $built_ins, $custom_variations ); +} +/** + * Registers the `core/post-terms` block on the server. + */ +function register_block_core_post_terms() { register_block_type_from_metadata( __DIR__ . '/post-terms', array( - 'render_callback' => 'render_block_core_post_terms', - 'variations' => array_merge( $built_ins, $custom_variations ), + 'render_callback' => 'render_block_core_post_terms', + 'variation_callback' => 'build_post_term_block_variations', ) ); } diff --git a/src/wp-includes/blocks/template-part.php b/src/wp-includes/blocks/template-part.php index 3ad400906945b..b3d0c9ce3c693 100644 --- a/src/wp-includes/blocks/template-part.php +++ b/src/wp-includes/blocks/template-part.php @@ -275,8 +275,8 @@ function register_block_core_template_part() { register_block_type_from_metadata( __DIR__ . '/template-part', array( - 'render_callback' => 'render_block_core_template_part', - 'variations' => build_template_part_block_variations(), + 'render_callback' => 'render_block_core_template_part', + 'variation_callback' => 'build_template_part_block_variations', ) ); } diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 9caa366032a75..73681aadec9ac 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -115,7 +115,15 @@ class WP_Block_Type { * @since 5.8.0 * @var array[] */ - public $variations = array(); + protected $variations = array(); + + /** + * Block variations callback. + * + * @since 6.5.0 + * @var callable[] + */ + public $variation_callback; /** * Custom CSS selectors for theme.json style generation. @@ -292,6 +300,7 @@ class WP_Block_Type { * @type string|null $textdomain The translation textdomain. * @type array[] $styles Alternative block styles. * @type array[] $variations Block variations. + * @type callable $variation_callback Block variations callback. * @type array $selectors Custom CSS selectors for theme.json style generation. * @type array|null $supports Supported features. * @type array|null $example Structured data for the block preview. @@ -325,6 +334,11 @@ public function __construct( $block_type, $args = array() ) { * null when value not found, or void when unknown property name provided. */ public function __get( $name ) { + if ( 'variations' === $name ) { + _doing_it_wrong( __CLASS__, __( 'Calling the variation property is not recommended, call get_variations instead.' ), '6.5.0' ); + return $this->get_variations(); + } + if ( ! in_array( $name, $this->deprecated_properties, true ) ) { return; } @@ -353,6 +367,10 @@ public function __get( $name ) { * or false otherwise. */ public function __isset( $name ) { + if ( 'variations' === $name && isset( $this->variation_callback ) ) { + return true; + } + if ( ! in_array( $name, $this->deprecated_properties, true ) ) { return false; } @@ -540,4 +558,19 @@ public function get_attributes() { $this->attributes : array(); } + + /** + * Get block variations. + * + * @since 6.5.0 + * + * @return array[] + */ + public function get_variations() { + if ( isset( $this->variation_callback ) && is_callable( $this->variation_callback ) ) { + return call_user_func( $this->variation_callback ); + } + + return $this->variations; + } } diff --git a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php index 852143d96ec35..c9e94b4e73803 100644 --- a/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php +++ b/src/wp-includes/rest-api/endpoints/class-wp-rest-block-types-controller.php @@ -292,7 +292,6 @@ public function prepare_item_for_response( $item, $request ) { 'view_script_handles', 'editor_style_handles', 'style_handles', - 'variations', 'block_hooks', ), $deprecated_fields @@ -314,6 +313,10 @@ public function prepare_item_for_response( $item, $request ) { } } + if ( rest_is_field_included( 'variations', $fields ) ) { + $data['variations'] = $block_type->get_variations(); + } + if ( rest_is_field_included( 'styles', $fields ) ) { $styles = $this->style_registry->get_registered_styles_for_block( $block_type->name ); $styles = array_values( $styles );