From 7a95c4fd3eb79b066ebf8b9f768fd57c0185d90e Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 29 Nov 2023 14:47:00 +0000 Subject: [PATCH 01/31] remove varation --- src/wp-includes/blocks/template-part.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/wp-includes/blocks/template-part.php b/src/wp-includes/blocks/template-part.php index 3ad400906945b..debe9b771fc89 100644 --- a/src/wp-includes/blocks/template-part.php +++ b/src/wp-includes/blocks/template-part.php @@ -272,12 +272,12 @@ function build_template_part_block_variations() { * Registers the `core/template-part` block on the server. */ 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(), - ) + $args = array( + 'render_callback' => 'render_block_core_template_part', ); + if ( current_user_can( 'edit_theme_options' ) ) { + $args['variations'] = build_template_part_block_variations(); + } + register_block_type_from_metadata( __DIR__ . '/template-part', $args ); } add_action( 'init', 'register_block_core_template_part' ); From 70fb7aa384182cae2950f993ed8ebfcca503dcc6 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 1 Dec 2023 05:39:25 +0000 Subject: [PATCH 02/31] introduce get all variation --- src/wp-admin/includes/post.php | 2 +- src/wp-includes/blocks/template-part.php | 13 +++++++------ src/wp-includes/class-wp-block-type.php | 19 +++++++++++++++++++ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 3b1603a38cdcc..dccf7b6b5670b 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2315,7 +2315,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 ) { @@ -2330,6 +2329,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/template-part.php b/src/wp-includes/blocks/template-part.php index debe9b771fc89..9dd466918cdf3 100644 --- a/src/wp-includes/blocks/template-part.php +++ b/src/wp-includes/blocks/template-part.php @@ -272,12 +272,13 @@ function build_template_part_block_variations() { * Registers the `core/template-part` block on the server. */ function register_block_core_template_part() { - $args = array( - 'render_callback' => 'render_block_core_template_part', + register_block_type_from_metadata( + __DIR__ . '/template-part', + array( + 'render_callback' => 'render_block_core_template_part', + ) ); - if ( current_user_can( 'edit_theme_options' ) ) { - $args['variations'] = build_template_part_block_variations(); - } - register_block_type_from_metadata( __DIR__ . '/template-part', $args ); } add_action( 'init', 'register_block_core_template_part' ); + +add_filter( 'get_variations_block_core/template-part', 'build_template_part_block_variations' ); \ No newline at end of file diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index b8ffb92e559b2..045e015e33d15 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -538,4 +538,23 @@ public function get_attributes() { $this->attributes : array(); } + + /** + * Get all variations. + * + * @since 6.5.0 + * + * @return array Array of variations. + */ + public function get_variations() { + /** + * Filters the arguments for registering a variations. + * + * @since 6.5.0 + * + * @param array $variations Array of arguments for registering a block type. + * @param string $block_type Block type name including namespace. + */ + return apply_filters( 'get_variations_block_' . $this->name, $this->variations, $this->name ); + } } From a41c600f08ce41e8b84c6af4b2d5da0d145f73b8 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 1 Dec 2023 06:22:52 +0000 Subject: [PATCH 03/31] cs fixes --- src/wp-admin/includes/post.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index dccf7b6b5670b..041aac14d6f74 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2329,7 +2329,7 @@ function get_block_editor_server_block_settings() { $blocks[ $block_name ][ $key ] = $block_type->{ $field }; } - $blocks[ $block_name ][ 'variations' ] = $block_type->get_variations(); + $blocks[ $block_name ]['variations'] = $block_type->get_variations(); } return $blocks; From 04de4898e98890562cadd80f5acc831d5746d3df Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 1 Dec 2023 06:25:56 +0000 Subject: [PATCH 04/31] cs fixes --- src/wp-includes/blocks/template-part.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/blocks/template-part.php b/src/wp-includes/blocks/template-part.php index 9dd466918cdf3..9246ef988bca9 100644 --- a/src/wp-includes/blocks/template-part.php +++ b/src/wp-includes/blocks/template-part.php @@ -281,4 +281,4 @@ function register_block_core_template_part() { } add_action( 'init', 'register_block_core_template_part' ); -add_filter( 'get_variations_block_core/template-part', 'build_template_part_block_variations' ); \ No newline at end of file +add_filter( 'get_variations_block_core/template-part', 'build_template_part_block_variations' ); From 9146a554a6f86553ad3d0c34ca17cb49265e31e2 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 5 Dec 2023 15:40:12 +0000 Subject: [PATCH 05/31] filter to callback --- src/wp-admin/includes/post.php | 7 ++++++- src/wp-includes/blocks/template-part.php | 3 +-- src/wp-includes/class-wp-block-type.php | 21 +-------------------- 3 files changed, 8 insertions(+), 23 deletions(-) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index 041aac14d6f74..2d3f96cb3a20a 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2315,6 +2315,7 @@ 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 ) { @@ -2329,7 +2330,11 @@ function get_block_editor_server_block_settings() { $blocks[ $block_name ][ $key ] = $block_type->{ $field }; } - $blocks[ $block_name ]['variations'] = $block_type->get_variations(); + + // If the block has a variations callback, call it and add the variations to the block. + if ( is_callable( $block_type->variations ) ) { + $blocks[ $block_name ]['variations'] = call_user_func( $block_type->variations ); + } } return $blocks; diff --git a/src/wp-includes/blocks/template-part.php b/src/wp-includes/blocks/template-part.php index 9246ef988bca9..cc56169100f47 100644 --- a/src/wp-includes/blocks/template-part.php +++ b/src/wp-includes/blocks/template-part.php @@ -276,9 +276,8 @@ function register_block_core_template_part() { __DIR__ . '/template-part', array( 'render_callback' => 'render_block_core_template_part', + 'variations' => 'build_template_part_block_variations', ) ); } add_action( 'init', 'register_block_core_template_part' ); - -add_filter( 'get_variations_block_core/template-part', '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 045e015e33d15..6aab1afc6fa82 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -113,7 +113,7 @@ class WP_Block_Type { * Block variations. * * @since 5.8.0 - * @var array[] + * @var array[] | callable */ public $variations = array(); @@ -538,23 +538,4 @@ public function get_attributes() { $this->attributes : array(); } - - /** - * Get all variations. - * - * @since 6.5.0 - * - * @return array Array of variations. - */ - public function get_variations() { - /** - * Filters the arguments for registering a variations. - * - * @since 6.5.0 - * - * @param array $variations Array of arguments for registering a block type. - * @param string $block_type Block type name including namespace. - */ - return apply_filters( 'get_variations_block_' . $this->name, $this->variations, $this->name ); - } } From d495db0e756217daf3622acfa8a708bbd50ebce9 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 7 Dec 2023 09:27:06 +0000 Subject: [PATCH 06/31] Add Variation to block-types rest api --- .../endpoints/class-wp-rest-block-types-controller.php | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) 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..e9d445e836893 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,14 @@ public function prepare_item_for_response( $item, $request ) { } } + if ( rest_is_field_included( 'variations', $fields ) ) { + $field = $block_type->variations; + if( is_callable( $block_type->variations ) ) { + $field = call_user_func( $block_type->variations ); + } + $data['variations'] = rest_sanitize_value_from_schema( $field, $schema['properties']['variations'] ); + } + if ( rest_is_field_included( 'styles', $fields ) ) { $styles = $this->style_registry->get_registered_styles_for_block( $block_type->name ); $styles = array_values( $styles ); From 3f8b154c8d2d42f94e6b974e714b84f4e66d7916 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 7 Dec 2023 09:48:49 +0000 Subject: [PATCH 07/31] navigation link and post term to block varation to callback --- src/wp-includes/blocks/navigation-link.php | 18 +++++++++++++----- src/wp-includes/blocks/post-terms.php | 14 +++++++++++--- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/src/wp-includes/blocks/navigation-link.php b/src/wp-includes/blocks/navigation-link.php index 5333ab6ea3dc9..71922dce5c551 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 get_variations_core_navigation_link() { $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); @@ -359,12 +358,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 ), + 'variations' => 'get_variations_core_navigation_link', ) ); } diff --git a/src/wp-includes/blocks/post-terms.php b/src/wp-includes/blocks/post-terms.php index c97155b81e3a9..b04714e58aebd 100644 --- a/src/wp-includes/blocks/post-terms.php +++ b/src/wp-includes/blocks/post-terms.php @@ -59,9 +59,11 @@ function render_block_core_post_terms( $attributes, $content, $block ) { } /** - * Registers the `core/post-terms` block on the server. + * Returns the available variations for the `core/post-terms` block. + * + * @return array The available variations for the block. */ -function register_block_core_post_terms() { +function get_variations_core_post_terms() { $taxonomies = get_taxonomies( array( 'publicly_queryable' => true, @@ -102,12 +104,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 ), + 'variations' => 'get_variations_core_post_terms', ) ); } From d382bb92b73c7cb8e6c11fcfe01324b7b856720d Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Thu, 7 Dec 2023 13:50:58 +0000 Subject: [PATCH 08/31] Update src/wp-includes/class-wp-block-type.php Co-authored-by: Mukesh Panchal --- src/wp-includes/class-wp-block-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 6aab1afc6fa82..10fae2b8bb394 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -113,7 +113,7 @@ class WP_Block_Type { * Block variations. * * @since 5.8.0 - * @var array[] | callable + * @var callable|array[] */ public $variations = array(); From cc598f2949e879de9bb77308ff4bb4efdfdb50c9 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 7 Dec 2023 15:02:30 +0000 Subject: [PATCH 09/31] doc block --- src/wp-includes/class-wp-block-type.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 10fae2b8bb394..21c03f0171534 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -113,6 +113,7 @@ class WP_Block_Type { * Block variations. * * @since 5.8.0 + * @since 5.9.0 Support callback. * @var callable|array[] */ public $variations = array(); From c174980a826f20d8409788ff666c0c13f41c61de Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 7 Dec 2023 15:02:43 +0000 Subject: [PATCH 10/31] dock block --- src/wp-includes/class-wp-block-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 21c03f0171534..9a02a2b14c3b6 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -113,7 +113,7 @@ class WP_Block_Type { * Block variations. * * @since 5.8.0 - * @since 5.9.0 Support callback. + * @since 6.5.0 Support callback. * @var callable|array[] */ public $variations = array(); From 8ad972851e12613e28caab650957f1ad6d942166 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 7 Dec 2023 15:02:58 +0000 Subject: [PATCH 11/31] phpcs fixes --- .../endpoints/class-wp-rest-block-types-controller.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 e9d445e836893..fba290f8b7adb 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 @@ -314,10 +314,10 @@ public function prepare_item_for_response( $item, $request ) { } if ( rest_is_field_included( 'variations', $fields ) ) { - $field = $block_type->variations; - if( is_callable( $block_type->variations ) ) { + $field = $block_type->variations; + if ( is_callable( $block_type->variations ) ) { $field = call_user_func( $block_type->variations ); - } + } $data['variations'] = rest_sanitize_value_from_schema( $field, $schema['properties']['variations'] ); } From e8ebb6f692269cec8772cd1a7bc06dd8c207fb50 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Mon, 11 Dec 2023 16:15:32 +0000 Subject: [PATCH 12/31] Update src/wp-includes/class-wp-block-type.php Co-authored-by: Mukesh Panchal --- src/wp-includes/class-wp-block-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index b6912e30c50de..416393eff8446 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -113,7 +113,7 @@ class WP_Block_Type { * Block variations. * * @since 5.8.0 - * @since 6.5.0 Support callback. + * @since 6.5.0 Added callback support. * @var callable|array[] */ public $variations = array(); From 9d5812d9f5153220c218549c1e92dfe7f62a26c0 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 11 Dec 2023 16:16:55 +0000 Subject: [PATCH 13/31] Making name consistant --- src/wp-includes/blocks/navigation-link.php | 4 ++-- src/wp-includes/blocks/post-terms.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/blocks/navigation-link.php b/src/wp-includes/blocks/navigation-link.php index 71922dce5c551..43b0b02d72cf2 100644 --- a/src/wp-includes/blocks/navigation-link.php +++ b/src/wp-includes/blocks/navigation-link.php @@ -327,7 +327,7 @@ function build_variation_for_navigation_link( $entity, $kind ) { * * @return array */ -function get_variations_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' ); @@ -372,7 +372,7 @@ function register_block_core_navigation_link() { __DIR__ . '/navigation-link', array( 'render_callback' => 'render_block_core_navigation_link', - 'variations' => 'get_variations_core_navigation_link', + 'variations' => 'build_navigation_link_block_variations', ) ); } diff --git a/src/wp-includes/blocks/post-terms.php b/src/wp-includes/blocks/post-terms.php index b04714e58aebd..525dfbf2ff63c 100644 --- a/src/wp-includes/blocks/post-terms.php +++ b/src/wp-includes/blocks/post-terms.php @@ -63,7 +63,7 @@ function render_block_core_post_terms( $attributes, $content, $block ) { * * @return array The available variations for the block. */ -function get_variations_core_post_terms() { +function build_post_term_block_variations() { $taxonomies = get_taxonomies( array( 'publicly_queryable' => true, @@ -115,7 +115,7 @@ function register_block_core_post_terms() { __DIR__ . '/post-terms', array( 'render_callback' => 'render_block_core_post_terms', - 'variations' => 'get_variations_core_post_terms', + 'variations' => 'build_post_term_block_variations', ) ); } From 5c3ad2c8afd4e15521066f9a176d18e9bc275435 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 5 Jan 2024 13:37:48 +0000 Subject: [PATCH 14/31] callback support with backward compatibility --- src/wp-admin/includes/post.php | 5 --- src/wp-includes/blocks/navigation-link.php | 4 +- src/wp-includes/blocks/post-terms.php | 4 +- src/wp-includes/blocks/template-part.php | 4 +- src/wp-includes/class-wp-block-type.php | 39 +++++++++++++++++-- .../class-wp-rest-block-types-controller.php | 9 +---- 6 files changed, 43 insertions(+), 22 deletions(-) diff --git a/src/wp-admin/includes/post.php b/src/wp-admin/includes/post.php index f8212b4595c93..b9986d1bdec7a 100644 --- a/src/wp-admin/includes/post.php +++ b/src/wp-admin/includes/post.php @@ -2317,11 +2317,6 @@ function get_block_editor_server_block_settings() { $blocks[ $block_name ][ $key ] = $block_type->{ $field }; } - - // If the block has a variations callback, call it and add the variations to the block. - if ( is_callable( $block_type->variations ) ) { - $blocks[ $block_name ]['variations'] = call_user_func( $block_type->variations ); - } } return $blocks; diff --git a/src/wp-includes/blocks/navigation-link.php b/src/wp-includes/blocks/navigation-link.php index 43b0b02d72cf2..11f47e1f25f6a 100644 --- a/src/wp-includes/blocks/navigation-link.php +++ b/src/wp-includes/blocks/navigation-link.php @@ -371,8 +371,8 @@ function register_block_core_navigation_link() { register_block_type_from_metadata( __DIR__ . '/navigation-link', array( - 'render_callback' => 'render_block_core_navigation_link', - 'variations' => 'build_navigation_link_block_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 525dfbf2ff63c..409253b4fd08a 100644 --- a/src/wp-includes/blocks/post-terms.php +++ b/src/wp-includes/blocks/post-terms.php @@ -114,8 +114,8 @@ function register_block_core_post_terms() { register_block_type_from_metadata( __DIR__ . '/post-terms', array( - 'render_callback' => 'render_block_core_post_terms', - 'variations' => 'build_post_term_block_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 cc56169100f47..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 416393eff8446..e5f9981297830 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -113,10 +113,18 @@ class WP_Block_Type { * Block variations. * * @since 5.8.0 - * @since 6.5.0 Added callback support. - * @var callable|array[] + * @since 6.5.0 Only accessible through magic getter. + * @var array[] */ - public $variations = array(); + private $variations = array(); + + /** + * Block variations callback. + * + * @since 6.5.0 + * @var callable[] + */ + public $variation_callback = null; /** * Custom CSS selectors for theme.json style generation. @@ -326,6 +334,10 @@ 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 ) { + return $this->get_variations(); + } + if ( ! in_array( $name, $this->deprecated_properties, true ) ) { return; } @@ -354,6 +366,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; } @@ -541,4 +557,21 @@ 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 ) + && ( empty( $this->variations ) || apply_filters( 'allow_variation_callback_recall', false ) ) + ) { + $this->variations = 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 fba290f8b7adb..852143d96ec35 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,6 +292,7 @@ public function prepare_item_for_response( $item, $request ) { 'view_script_handles', 'editor_style_handles', 'style_handles', + 'variations', 'block_hooks', ), $deprecated_fields @@ -313,14 +314,6 @@ public function prepare_item_for_response( $item, $request ) { } } - if ( rest_is_field_included( 'variations', $fields ) ) { - $field = $block_type->variations; - if ( is_callable( $block_type->variations ) ) { - $field = call_user_func( $block_type->variations ); - } - $data['variations'] = rest_sanitize_value_from_schema( $field, $schema['properties']['variations'] ); - } - if ( rest_is_field_included( 'styles', $fields ) ) { $styles = $this->style_registry->get_registered_styles_for_block( $block_type->name ); $styles = array_values( $styles ); From b1d36644ada68afe539c562b2229b214be87c5a1 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 5 Jan 2024 13:59:22 +0000 Subject: [PATCH 15/31] Allow setting of value. Doc block for variation callback --- src/wp-includes/class-wp-block-type.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index e5f9981297830..b881e9513c30a 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -122,7 +122,7 @@ class WP_Block_Type { * Block variations callback. * * @since 6.5.0 - * @var callable[] + * @var callable */ public $variation_callback = null; @@ -305,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. @@ -389,6 +390,11 @@ public function __isset( $name ) { * @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; From 9c475a92825b0a770dee54e4a3317be18cb7e7c3 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Mon, 8 Jan 2024 16:13:27 +0000 Subject: [PATCH 16/31] variations set to null initially --- src/wp-includes/class-wp-block-type.php | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index b881e9513c30a..1d9d65775888b 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -113,10 +113,10 @@ class WP_Block_Type { * Block variations. * * @since 5.8.0 - * @since 6.5.0 Only accessible through magic getter. - * @var array[] + * @since 6.5.0 Only accessible through magic getter. null by default. + * @var array[]|null */ - private $variations = array(); + private $variations = null; /** * Block variations callback. @@ -572,12 +572,21 @@ public function get_attributes() { * @return array[] */ public function get_variations() { - if ( isset( $this->variation_callback ) && is_callable( $this->variation_callback ) - && ( empty( $this->variations ) || apply_filters( 'allow_variation_callback_recall', false ) ) - ) { + if ( ! isset( $this->variations ) ) { + $this->variations = array(); + if ( is_callable( $this->variation_callback ) ) { $this->variations = call_user_func( $this->variation_callback ); + } } - return $this->variations; + /** + * 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 ); } } From 3cb7d40ca2ae213b582296000bf46d36dbebff52 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 9 Jan 2024 12:42:19 +0000 Subject: [PATCH 17/31] isset logic --- src/wp-includes/class-wp-block-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 1d9d65775888b..9f14cce616482 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -367,7 +367,7 @@ public function __get( $name ) { * or false otherwise. */ public function __isset( $name ) { - if ( 'variations' === $name && isset( $this->variation_callback ) ) { + if ( 'variations' === $name && ( isset( $this->variations ) || isset( $this->variation_callback ) ) ) { return true; } From 0351ae6e32bc23e8c7cc8bf0eab6a46a7b1b1681 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 10 Jan 2024 06:00:34 +0000 Subject: [PATCH 18/31] test cases for variation callback --- tests/phpunit/tests/blocks/wpBlockType.php | 43 +++++++++++++++++++ .../rest-api/rest-block-type-controller.php | 29 +++++++++++++ 2 files changed, 72 insertions(+) diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index d2c7a13e6b345..40d33326bad71 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -460,4 +460,47 @@ public function data_block_version() { array( '', 0 ), ); } + + /** + * @ticket 59969 + */ + public function test_variation_callback() { + $block_type = new WP_Block_Type( + 'test/block', + array( + 'title' => 'Test title', + 'variation_callback' => array( $this, 'mock_variation_callback' ), + ) + ); + + $this->assertSameSets( $this->mock_variation_callback(), $block_type->variations ); + } + + /** + * @ticket 59969 + * @covers WP_Block_Type::get_variations + */ + public function test_get_variations() { + $block_type = new WP_Block_Type( + 'test/block', + array( + 'title' => 'Test title', + 'variation_callback' => array( $this, 'mock_variation_callback' ), + ) + ); + + $this->assertSameSets( $this->mock_variation_callback(), $block_type->get_variations() ); + } + + /** + * Mock variation callback. + * + * @return array + */ + public function mock_variation_callback() { + return array( + array( 'name' => 'var1' ), + array( 'name' => 'var2' ), + ); + } } diff --git a/tests/phpunit/tests/rest-api/rest-block-type-controller.php b/tests/phpunit/tests/rest-api/rest-block-type-controller.php index 8b49d2c034a20..59c77e03b68b2 100644 --- a/tests/phpunit/tests/rest-api/rest-block-type-controller.php +++ b/tests/phpunit/tests/rest-api/rest-block-type-controller.php @@ -734,6 +734,35 @@ protected function check_block_type_object( $block_type, $data, $links ) { } } + /** + * @ticket 59969 + */ + public function test_variation_callback() { + $block_type = 'test/block'; + $settings = array( + 'title' => true, + 'variation_callback' => array( $this, 'mock_variation_callback' ), + ); + register_block_type( $block_type, $settings ); + wp_set_current_user( self::$admin_id ); + $request = new WP_REST_Request( 'GET', '/wp/v2/block-types/' . $block_type ); + $response = rest_get_server()->dispatch( $request ); + $data = $response->get_data(); + $this->assertSameSets( $this->mock_variation_callback(), $data['variations'] ); + } + + /** + * Mock variation callback. + * + * @return array + */ + public function mock_variation_callback() { + return array( + array( 'name' => 'var1' ), + array( 'name' => 'var2' ), + ); + } + /** * The create_item() method does not exist for block types. * From 5480c0f9268e7d5748cc5bfcbe6da04da213771f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 10 Jan 2024 06:41:04 +0000 Subject: [PATCH 19/31] remove isset as variations will never be returned as null. --- src/wp-includes/class-wp-block-type.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 9f14cce616482..6254e447b900b 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -367,10 +367,6 @@ public function __get( $name ) { * or false otherwise. */ public function __isset( $name ) { - if ( 'variations' === $name && ( isset( $this->variations ) || isset( $this->variation_callback ) ) ) { - return true; - } - if ( ! in_array( $name, $this->deprecated_properties, true ) ) { return false; } From a68e5e00637c8dcb92afea76bfd8ce6fbc340ad7 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 10 Jan 2024 06:53:53 +0000 Subject: [PATCH 20/31] isset always true for variations --- src/wp-includes/class-wp-block-type.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index 6254e447b900b..da305d6cb9c73 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -367,6 +367,10 @@ public function __get( $name ) { * or false otherwise. */ public function __isset( $name ) { + if ( 'variations' === $name ) { + return true; + } + if ( ! in_array( $name, $this->deprecated_properties, true ) ) { return false; } From cd38417f29f6c1a42d371120e148418f42e9d254 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 16 Jan 2024 16:32:56 +0000 Subject: [PATCH 21/31] revert back navigation-link to --- src/wp-includes/blocks/navigation-link.php | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/blocks/navigation-link.php b/src/wp-includes/blocks/navigation-link.php index 11f47e1f25f6a..5333ab6ea3dc9 100644 --- a/src/wp-includes/blocks/navigation-link.php +++ b/src/wp-includes/blocks/navigation-link.php @@ -323,11 +323,12 @@ function build_variation_for_navigation_link( $entity, $kind ) { } /** - * Returns an array of variations for the navigation link block. + * Register the navigation link block. * - * @return array + * @uses render_block_core_navigation() + * @throws WP_Error An WP_Error exception parsing the block definition. */ -function build_navigation_link_block_variations() { +function register_block_core_navigation_link() { $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); @@ -358,21 +359,12 @@ function build_navigation_link_block_variations() { } } } - 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', - 'variation_callback' => 'build_navigation_link_block_variations', + 'render_callback' => 'render_block_core_navigation_link', + 'variations' => array_merge( $built_ins, $variations ), ) ); } From b60446432b73bc73923a46dc66fc4be34dfa5869 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Tue, 16 Jan 2024 16:34:12 +0000 Subject: [PATCH 22/31] revert back to trunk --- src/wp-includes/blocks/post-terms.php | 16 ++++------------ src/wp-includes/blocks/template-part.php | 4 ++-- 2 files changed, 6 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/blocks/post-terms.php b/src/wp-includes/blocks/post-terms.php index 409253b4fd08a..c97155b81e3a9 100644 --- a/src/wp-includes/blocks/post-terms.php +++ b/src/wp-includes/blocks/post-terms.php @@ -59,11 +59,9 @@ function render_block_core_post_terms( $attributes, $content, $block ) { } /** - * Returns the available variations for the `core/post-terms` block. - * - * @return array The available variations for the block. + * Registers the `core/post-terms` block on the server. */ -function build_post_term_block_variations() { +function register_block_core_post_terms() { $taxonomies = get_taxonomies( array( 'publicly_queryable' => true, @@ -104,18 +102,12 @@ function build_post_term_block_variations() { $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', - 'variation_callback' => 'build_post_term_block_variations', + 'render_callback' => 'render_block_core_post_terms', + 'variations' => array_merge( $built_ins, $custom_variations ), ) ); } diff --git a/src/wp-includes/blocks/template-part.php b/src/wp-includes/blocks/template-part.php index b3d0c9ce3c693..3ad400906945b 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', - 'variation_callback' => 'build_template_part_block_variations', + 'render_callback' => 'render_block_core_template_part', + 'variations' => build_template_part_block_variations(), ) ); } From 39ee3ee034d930a5445fe4eaff319c8baaebb714 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Wed, 17 Jan 2024 20:37:42 +0000 Subject: [PATCH 23/31] set to null --- src/wp-includes/class-wp-block-type.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/wp-includes/class-wp-block-type.php b/src/wp-includes/class-wp-block-type.php index da305d6cb9c73..6ad8ebcd6790e 100644 --- a/src/wp-includes/class-wp-block-type.php +++ b/src/wp-includes/class-wp-block-type.php @@ -122,7 +122,7 @@ class WP_Block_Type { * Block variations callback. * * @since 6.5.0 - * @var callable + * @var callable|null */ public $variation_callback = null; From 34bcc84466b3cc3ba4b2787ee7267967d72e453c Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 18 Jan 2024 02:53:56 +0000 Subject: [PATCH 24/31] test cases for multiple cases --- tests/phpunit/tests/blocks/wpBlockType.php | 89 ++++++++++++++++++++++ 1 file changed, 89 insertions(+) diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index 40d33326bad71..d9761dbfd2ba9 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -476,6 +476,95 @@ public function test_variation_callback() { $this->assertSameSets( $this->mock_variation_callback(), $block_type->variations ); } + /** + * @ticket 59969 + */ + public function test_variations_precedence_over_callback() { + $test_variations = array( 'name' => 'test1' ); + + $block_type = new WP_Block_Type( + 'test/block', + array( + 'title' => 'Test title', + 'variations' => $test_variations, + 'variation_callback' => array( $this, 'mock_variation_callback' ), + ) + ); + + // If the variations are defined, the callback should not be used. + $this->assertSameSets( $test_variations, $block_type->variations ); + } + + /** + * @ticket 59969 + */ + public function test_variations_callback_are_lazy_loaded() { + $callback_called = false; + + $block_type = new WP_Block_Type( + 'test/block', + array( + 'title' => 'Test title', + 'variation_callback' => function () use ( &$callback_called ) { + $callback_called = true; + return $this->mock_variation_callback(); + }, + ) + ); + + $this->assertSame( false, $callback_called, 'The callback should not be called before the variations are accessed.' ); + $block_type->variations; // access the variations. + $this->assertSame( true, $callback_called, 'The callback should be called when the variations are accessed.' ); + } + + /** + * @ticket 59969 + */ + public function test_variations_precedence_over_callback_post_registration() { + $test_variations = array( 'name' => 'test1' ); + $callback_called = false; + + $block_type = new WP_Block_Type( + 'test/block', + array( + 'title' => 'Test title', + 'variation_callback' => function () use ( &$callback_called ) { + $callback_called = true; + return $this->mock_variation_callback(); + }, + ) + ); + $block_type->variations = $test_variations; + + // If the variations are defined after registration but before first access, the callback should not override it. + $this->assertSameSets( $test_variations, $block_type->get_variations(), 'Variations are same as variations set' ); + $this->assertSame( false, $callback_called, 'The callback was never called.' ); + } + + /** + * @ticket 59969 + */ + public function test_variations_callback_happens_only_once() { + $callback_count = 0; + + $block_type = new WP_Block_Type( + 'test/block', + array( + 'title' => 'Test title', + 'variation_callback' => function () use ( &$callback_count ) { + $callback_count++; + return $this->mock_variation_callback(); + }, + ) + ); + + $this->assertSame( 0, $callback_count, 'The callback should not be called before the variations are accessed.' ); + $block_type->get_variations(); // access the variations. + $this->assertSame( 1, $callback_count, 'The callback should be called when the variations are accessed.' ); + $block_type->get_variations(); // access the variations again. + $this->assertSame( 1, $callback_count, 'The callback should not be called again.' ); + } + /** * @ticket 59969 * @covers WP_Block_Type::get_variations From 94aa0fe479e269d55fc3e1bd7a8050d35499ad05 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 18 Jan 2024 02:54:11 +0000 Subject: [PATCH 25/31] additional test cases --- tests/phpunit/tests/blocks/wpBlockType.php | 61 ++++++++++++++++++++-- 1 file changed, 57 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index d9761dbfd2ba9..f44e23c6b0d09 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -476,6 +476,22 @@ public function test_variation_callback() { $this->assertSameSets( $this->mock_variation_callback(), $block_type->variations ); } + /** + * @ticket 59969 + * @covers WP_Block_Type::get_variations + */ + public function test_get_variations() { + $block_type = new WP_Block_Type( + 'test/block', + array( + 'title' => 'Test title', + 'variation_callback' => array( $this, 'mock_variation_callback' ), + ) + ); + + $this->assertSameSets( $this->mock_variation_callback(), $block_type->get_variations() ); + } + /** * @ticket 59969 */ @@ -519,6 +535,7 @@ public function test_variations_callback_are_lazy_loaded() { /** * @ticket 59969 + * @covers WP_Block_Type::get_variations */ public function test_variations_precedence_over_callback_post_registration() { $test_variations = array( 'name' => 'test1' ); @@ -543,6 +560,7 @@ public function test_variations_precedence_over_callback_post_registration() { /** * @ticket 59969 + * @covers WP_Block_Type::get_variations */ public function test_variations_callback_happens_only_once() { $callback_count = 0; @@ -567,18 +585,53 @@ public function test_variations_callback_happens_only_once() { /** * @ticket 59969 - * @covers WP_Block_Type::get_variations */ - public function test_get_variations() { + public function test_get_block_type_variations_filter_with_variation_callback() { + // Filter will override the variations obtained from the callback. + add_filter( 'get_block_type_variations', array( $this, 'filter_test_variations' ), 10, 2 ); + $expected_variations = array( array( 'name' => 'test1' ) ); + + $callback_called = false; $block_type = new WP_Block_Type( 'test/block', array( 'title' => 'Test title', - 'variation_callback' => array( $this, 'mock_variation_callback' ), + 'variation_callback' => function () use ( &$callback_called ) { + $callback_called = true; + return $this->mock_variation_callback(); + }, ) ); - $this->assertSameSets( $this->mock_variation_callback(), $block_type->get_variations() ); + $obtained_variations = $block_type->variations; // access the variations. + remove_filter( 'get_block_type_variations', array( $this, 'filter_test_variations' ), 10 ); + + $this->assertSame( true, $callback_called, 'The callback should be called when the variations are accessed.' ); + $this->assertSameSets( $obtained_variations, $expected_variations, 'The variations obtained from the callback should be filtered.' ); + } + public function filter_test_variations( $variations, $block_type ) { + return array( array( 'name' => 'test1' ) ); + } + + /** + * @ticket 59969 + */ + public function test_get_block_type_variations_filter_variations() { + // Filter will override the variations set during resistration. + add_filter( 'get_block_type_variations', array( $this, 'filter_test_variations' ), 10, 2 ); + $expected_variations = array( array( 'name' => 'test1' ) ); + + $block_type = new WP_Block_Type( + 'test/block', + array( + 'title' => 'Test title', + 'variations' => $this->mock_variation_callback() + ) + ); + + $obtained_variations = $block_type->variations; // access the variations. + remove_filter( 'get_block_type_variations', array( $this, 'filter_test_variations' ), 10 ); + $this->assertSameSets( $obtained_variations, $expected_variations, 'The variations obtained from the callback should be filtered.' ); } /** From bae4e713b6727aeda47987d83046268ac3d3f34d Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 18 Jan 2024 02:55:05 +0000 Subject: [PATCH 26/31] change message --- tests/phpunit/tests/blocks/wpBlockType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index f44e23c6b0d09..b8875107fa577 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -631,7 +631,7 @@ public function test_get_block_type_variations_filter_variations() { $obtained_variations = $block_type->variations; // access the variations. remove_filter( 'get_block_type_variations', array( $this, 'filter_test_variations' ), 10 ); - $this->assertSameSets( $obtained_variations, $expected_variations, 'The variations obtained from the callback should be filtered.' ); + $this->assertSameSets( $obtained_variations, $expected_variations, 'The variations that was initially set should be filtered.' ); } /** From 2918539b291c7a1ca4f350c35f884ed567abc055 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Thu, 18 Jan 2024 02:58:41 +0000 Subject: [PATCH 27/31] cs fixes --- tests/phpunit/tests/blocks/wpBlockType.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index b8875107fa577..9a6adb8b2a3f8 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -592,7 +592,7 @@ public function test_get_block_type_variations_filter_with_variation_callback() $expected_variations = array( array( 'name' => 'test1' ) ); $callback_called = false; - $block_type = new WP_Block_Type( + $block_type = new WP_Block_Type( 'test/block', array( 'title' => 'Test title', @@ -625,7 +625,7 @@ public function test_get_block_type_variations_filter_variations() { 'test/block', array( 'title' => 'Test title', - 'variations' => $this->mock_variation_callback() + 'variations' => $this->mock_variation_callback(), ) ); From 686c24332318cdc65b2e67177cdb211e2427df8f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Thu, 18 Jan 2024 09:00:49 +0000 Subject: [PATCH 28/31] Update tests/phpunit/tests/blocks/wpBlockType.php Fix typo Co-authored-by: Mukesh Panchal --- tests/phpunit/tests/blocks/wpBlockType.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index 9a6adb8b2a3f8..d036ee753a2cd 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -617,7 +617,7 @@ public function filter_test_variations( $variations, $block_type ) { * @ticket 59969 */ public function test_get_block_type_variations_filter_variations() { - // Filter will override the variations set during resistration. + // Filter will override the variations set during registration. add_filter( 'get_block_type_variations', array( $this, 'filter_test_variations' ), 10, 2 ); $expected_variations = array( array( 'name' => 'test1' ) ); From 689d48cce82a469403f030bc0951403f85bba46f Mon Sep 17 00:00:00 2001 From: Karthik Thayyil <30643833+kt-12@users.noreply.github.com> Date: Thu, 18 Jan 2024 14:26:58 +0000 Subject: [PATCH 29/31] Update tests/phpunit/tests/blocks/wpBlockType.php New line Co-authored-by: Mukesh Panchal --- tests/phpunit/tests/blocks/wpBlockType.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index d036ee753a2cd..cd7a4b5c2697b 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -609,6 +609,7 @@ public function test_get_block_type_variations_filter_with_variation_callback() $this->assertSame( true, $callback_called, 'The callback should be called when the variations are accessed.' ); $this->assertSameSets( $obtained_variations, $expected_variations, 'The variations obtained from the callback should be filtered.' ); } + public function filter_test_variations( $variations, $block_type ) { return array( array( 'name' => 'test1' ) ); } From aafceda6670280dab71bf59203b283043cf72560 Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 19 Jan 2024 13:22:05 +0000 Subject: [PATCH 30/31] Move filter function one up --- tests/phpunit/tests/blocks/wpBlockType.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index cd7a4b5c2697b..bad38fb3bd321 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -583,6 +583,13 @@ public function test_variations_callback_happens_only_once() { $this->assertSame( 1, $callback_count, 'The callback should not be called again.' ); } + /** + * Test filter function for get_block_type_variations filter. + */ + public function filter_test_variations( $variations, $block_type ) { + return array( array( 'name' => 'test1' ) ); + } + /** * @ticket 59969 */ @@ -610,10 +617,6 @@ public function test_get_block_type_variations_filter_with_variation_callback() $this->assertSameSets( $obtained_variations, $expected_variations, 'The variations obtained from the callback should be filtered.' ); } - public function filter_test_variations( $variations, $block_type ) { - return array( array( 'name' => 'test1' ) ); - } - /** * @ticket 59969 */ From b0730c76ddc497fc55ff5e4135aa1dba0a70f0ba Mon Sep 17 00:00:00 2001 From: Karthik Thayyil Date: Fri, 19 Jan 2024 13:45:34 +0000 Subject: [PATCH 31/31] remove filters and expand doc block defnetion --- tests/phpunit/tests/blocks/wpBlockType.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/phpunit/tests/blocks/wpBlockType.php b/tests/phpunit/tests/blocks/wpBlockType.php index bad38fb3bd321..b826faf575946 100644 --- a/tests/phpunit/tests/blocks/wpBlockType.php +++ b/tests/phpunit/tests/blocks/wpBlockType.php @@ -585,6 +585,11 @@ public function test_variations_callback_happens_only_once() { /** * Test filter function for get_block_type_variations filter. + * + * @param array $variations Block variations before filter. + * @param WP_Block_Type $block_type Block type. + * + * @return array Block variations after filter. */ public function filter_test_variations( $variations, $block_type ) { return array( array( 'name' => 'test1' ) ); @@ -611,7 +616,6 @@ public function test_get_block_type_variations_filter_with_variation_callback() ); $obtained_variations = $block_type->variations; // access the variations. - remove_filter( 'get_block_type_variations', array( $this, 'filter_test_variations' ), 10 ); $this->assertSame( true, $callback_called, 'The callback should be called when the variations are accessed.' ); $this->assertSameSets( $obtained_variations, $expected_variations, 'The variations obtained from the callback should be filtered.' ); @@ -634,7 +638,6 @@ public function test_get_block_type_variations_filter_variations() { ); $obtained_variations = $block_type->variations; // access the variations. - remove_filter( 'get_block_type_variations', array( $this, 'filter_test_variations' ), 10 ); $this->assertSameSets( $obtained_variations, $expected_variations, 'The variations that was initially set should be filtered.' ); }