From a0af054cfda7d605558681d6ec4f002b80c618c6 Mon Sep 17 00:00:00 2001 From: Fabian Todt Date: Mon, 29 Jan 2024 16:27:37 +0100 Subject: [PATCH 1/8] Nav Link: use get_block_type_variations to register variations --- .../src/navigation-link/index.php | 131 ++++-------------- .../block-navigation-link-variations-test.php | 38 +++-- 2 files changed, 48 insertions(+), 121 deletions(-) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 9a1421a663fb78..7d5a527af5c670 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -327,51 +327,22 @@ function build_variation_for_navigation_link( $entity, $kind ) { } /** - * Register a variation for a post type / taxonomy for the navigation link block. + * Filters the registered variations for a block type. + * Returns the dynamically built variations for all post-types and taxonomies. * - * @param array $variation Variation array from build_variation_for_navigation_link. - * @return void - */ -function block_core_navigation_link_register_variation( $variation ) { - // Directly set the variations on the registered block type - // because there's no server side registration for variations (see #47170). - $navigation_block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-link' ); - // If the block is not registered yet, bail early. - // Variation will be registered in register_block_core_navigation_link then. - if ( ! $navigation_block_type ) { - return; - } - - $navigation_block_type->variations = array_merge( - $navigation_block_type->variations, - array( $variation ) - ); -} - -/** - * Unregister a variation for a post type / taxonomy for the navigation link block. + * @since 6.5.0 * - * @param string $name Name of the post type / taxonomy (which was used as variation name). - * @return void + * @param array $variations Array of registered variations for a block type. + * @param WP_Block_Type $block_type The full block type object. */ -function block_core_navigation_link_unregister_variation( $name ) { - // Directly get the variations from the registered block type - // because there's no server side (un)registration for variations (see #47170). - $navigation_block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-link' ); - // If the block is not registered (yet), there's no need to remove a variation. - if ( ! $navigation_block_type || empty( $navigation_block_type->variations ) ) { - return; - } - $variations = $navigation_block_type->variations; - // Search for the variation and remove it from the array. - foreach ( $variations as $i => $variation ) { - if ( $variation['name'] === $name ) { - unset( $variations[ $i ] ); - break; - } +function block_core_navigation_link_filter_variations( $variations, $block_type ) { + if ( 'core/navigation-link' !== $block_type->name ) { + return $variations; } - // Reindex array after removing one variation. - $navigation_block_type->variations = array_values( $variations ); + + $generated_variations = block_core_navigation_link_build_variations(); + // TODO: Mabe check if there are already variations for this post type/taxonomy? + return array_merge( $variations, $generated_variations ); } /** @@ -380,16 +351,16 @@ function block_core_navigation_link_unregister_variation( $name ) { * @return array */ function block_core_navigation_link_build_variations() { - // This will only handle post types and taxonomies registered until this point (init on priority 9). - // See action hooks below for other post types and taxonomies. - // See https://github.com/WordPress/gutenberg/issues/53826 for details. + // This will only handle post types and taxonomies registered until this point. $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); - // Use two separate arrays as a way to order the variations in the UI. - // Known variations (like Post Link and Page Link) are added to the - // `built_ins` array. Variations for custom post types and taxonomies are - // added to the `variations` array and will always appear after `built-ins. + /* + * Use two separate arrays as a way to order the variations in the UI. + * Known variations (like Post Link and Page Link) are added to the + * `built_ins` array. Variations for custom post types and taxonomies are + * added to the `variations` array and will always appear after `built-ins. + */ $built_ins = array(); $variations = array(); @@ -418,7 +389,7 @@ function block_core_navigation_link_build_variations() { } /** - * Register the navigation link block. + * Registers the navigation link block. * * @uses render_block_core_navigation() * @throws WP_Error An WP_Error exception parsing the block definition. @@ -427,67 +398,13 @@ function register_block_core_navigation_link() { register_block_type_from_metadata( __DIR__ . '/navigation-link', array( - 'render_callback' => 'render_block_core_navigation_link', - 'variation_callback' => 'block_core_navigation_link_build_variations', + 'render_callback' => 'render_block_core_navigation_link', ) ); } add_action( 'init', 'register_block_core_navigation_link' ); -// Register actions for all post types and taxonomies, to add variations when they are registered. -// All post types/taxonomies registered before register_block_core_navigation_link, will be handled by that function. -add_action( 'registered_post_type', 'block_core_navigation_link_register_post_type_variation', 10, 2 ); -add_action( 'registered_taxonomy', 'block_core_navigation_link_register_taxonomy_variation', 10, 3 ); -// Handle unregistering of post types and taxonomies and remove the variations. -add_action( 'unregistered_post_type', 'block_core_navigation_link_unregister_post_type_variation' ); -add_action( 'unregistered_taxonomy', 'block_core_navigation_link_unregister_taxonomy_variation' ); - /** - * Register custom post type variations for navigation link on post type registration - * Handles all post types registered after the block is registered in register_navigation_link_post_type_variations - * - * @param string $post_type The post type name passed from registered_post_type action hook. - * @param WP_Post_Type $post_type_object The post type object passed from registered_post_type. - * @return void + * Use this filter to create all variations for post types / taxonomies dynamically. + * Do not use variation_callback, to also account for unregistering post types/taxonomies later on. */ -function block_core_navigation_link_register_post_type_variation( $post_type, $post_type_object ) { - if ( $post_type_object->show_in_nav_menus ) { - $variation = build_variation_for_navigation_link( $post_type_object, 'post-type' ); - block_core_navigation_link_register_variation( $variation ); - } -} - -/** - * Register a custom taxonomy variation for navigation link on taxonomy registration - * Handles all taxonomies registered after the block is registered in register_navigation_link_post_type_variations - * - * @param string $taxonomy Taxonomy slug. - * @param array|string $object_type Object type or array of object types. - * @param array $args Array of taxonomy registration arguments. - * @return void - */ -function block_core_navigation_link_register_taxonomy_variation( $taxonomy, $object_type, $args ) { - if ( isset( $args['show_in_nav_menus'] ) && $args['show_in_nav_menus'] ) { - $variation = build_variation_for_navigation_link( (object) $args, 'post-type' ); - block_core_navigation_link_register_variation( $variation ); - } -} - -/** - * Unregisters a custom post type variation for navigation link on post type unregistration. - * - * @param string $post_type The post type name passed from unregistered_post_type action hook. - * @return void - */ -function block_core_navigation_link_unregister_post_type_variation( $post_type ) { - block_core_navigation_link_unregister_variation( $post_type ); -} - -/** - * Unregisters a custom taxonomy variation for navigation link on taxonomy unregistration. - * - * @param string $taxonomy The taxonomy name passed from unregistered_taxonomy action hook. - * @return void - */ -function block_core_navigation_link_unregister_taxonomy_variation( $taxonomy ) { - block_core_navigation_link_unregister_variation( $taxonomy ); -} +add_action( 'get_block_type_variations', 'block_core_navigation_link_filter_variations', 10, 2 ); diff --git a/phpunit/blocks/block-navigation-link-variations-test.php b/phpunit/blocks/block-navigation-link-variations-test.php index 8eef5c393148b0..d961edde3e37cd 100644 --- a/phpunit/blocks/block-navigation-link-variations-test.php +++ b/phpunit/blocks/block-navigation-link-variations-test.php @@ -74,8 +74,9 @@ public function tear_down() { public function test_navigation_link_variations_custom_post_type() { $registry = WP_Block_Type_Registry::get_instance(); $nav_link_block = $registry->get_registered( 'core/navigation-link' ); - $this->assertNotEmpty( $nav_link_block->variations, 'Block has no variations' ); - $variation = $this->get_variation_by_name( 'custom_book', $nav_link_block->variations ); + $variations = $nav_link_block->get_variations(); + $this->assertNotEmpty( $variations, 'Block has no variations' ); + $variation = $this->get_variation_by_name( 'custom_book', $variations ); $this->assertIsArray( $variation, 'Block variation does not exist' ); $this->assertArrayHasKey( 'title', $variation, 'Block variation has no title' ); $this->assertEquals( 'Custom Book', $variation['title'], 'Variation title is different than the post type label' ); @@ -87,8 +88,9 @@ public function test_navigation_link_variations_custom_post_type() { public function test_navigation_link_variations_private_custom_post_type() { $registry = WP_Block_Type_Registry::get_instance(); $nav_link_block = $registry->get_registered( 'core/navigation-link' ); - $this->assertNotEmpty( $nav_link_block->variations, 'Block has no variations' ); - $variation = $this->get_variation_by_name( 'private_custom_book', $nav_link_block->variations ); + $variations = $nav_link_block->get_variations(); + $this->assertNotEmpty( $variations, 'Block has no variations' ); + $variation = $this->get_variation_by_name( 'private_custom_book', $variations ); $this->assertEmpty( $variation, 'Block variation for private post type exists.' ); } @@ -98,8 +100,9 @@ public function test_navigation_link_variations_private_custom_post_type() { public function test_navigation_link_variations_custom_taxonomy() { $registry = WP_Block_Type_Registry::get_instance(); $nav_link_block = $registry->get_registered( 'core/navigation-link' ); - $this->assertNotEmpty( $nav_link_block->variations, 'Block has no variations' ); - $variation = $this->get_variation_by_name( 'book_type', $nav_link_block->variations ); + $variations = $nav_link_block->get_variations(); + $this->assertNotEmpty( $variations, 'Block has no variations' ); + $variation = $this->get_variation_by_name( 'book_type', $variations ); $this->assertIsArray( $variation, 'Block variation does not exist' ); $this->assertArrayHasKey( 'title', $variation, 'Block variation has no title' ); $this->assertEquals( 'Book Type', $variation['title'], 'Variation title is different than the post type label' ); @@ -111,8 +114,9 @@ public function test_navigation_link_variations_custom_taxonomy() { public function test_navigation_link_variations_private_custom_taxonomy() { $registry = WP_Block_Type_Registry::get_instance(); $nav_link_block = $registry->get_registered( 'core/navigation-link' ); - $this->assertNotEmpty( $nav_link_block->variations, 'Block has no variations' ); - $variation = $this->get_variation_by_name( 'private_book_type', $nav_link_block->variations ); + $variations = $nav_link_block->get_variations(); + $this->assertNotEmpty( $variations, 'Block has no variations' ); + $variation = $this->get_variation_by_name( 'private_book_type', $variations ); $this->assertEmpty( $variation, 'Block variation for private taxonomy exists.' ); } @@ -134,13 +138,16 @@ public function test_navigation_link_variations_unregister_post_type() { $registry = WP_Block_Type_Registry::get_instance(); $nav_link_block = $registry->get_registered( 'core/navigation-link' ); - $this->assertNotEmpty( $nav_link_block->variations, 'Block has no variations' ); - $variation = $this->get_variation_by_name( 'temp_custom_book', $nav_link_block->variations ); + $variations = $nav_link_block->get_variations(); + $this->assertNotEmpty( $variations, 'Block has no variations' ); + $variation = $this->get_variation_by_name( 'temp_custom_book', $variations ); $this->assertIsArray( $variation, 'Block variation does not exist' ); unregister_post_type( 'temp_custom_book' ); - $variation = $this->get_variation_by_name( 'temp_custom_book', $nav_link_block->variations ); + // Update array, since it's an dynamic built array + $variations = $nav_link_block->get_variations(); + $variation = $this->get_variation_by_name( 'temp_custom_book', $variations ); $this->assertEmpty( $variation, 'Block variation still exists' ); } @@ -161,13 +168,16 @@ public function test_navigation_link_variations_unregister_taxonomy() { $registry = WP_Block_Type_Registry::get_instance(); $nav_link_block = $registry->get_registered( 'core/navigation-link' ); - $this->assertNotEmpty( $nav_link_block->variations, 'Block has no variations' ); - $variation = $this->get_variation_by_name( 'temp_book_type', $nav_link_block->variations ); + $variations = $nav_link_block->get_variations(); + $this->assertNotEmpty( $variations, 'Block has no variations' ); + $variation = $this->get_variation_by_name( 'temp_book_type', $variations ); $this->assertIsArray( $variation, 'Block variation does not exist' ); unregister_taxonomy( 'temp_book_type' ); - $variation = $this->get_variation_by_name( 'temp_book_type', $nav_link_block->variations ); + // Update array, since it's an dynamic built array + $variations = $nav_link_block->get_variations(); + $variation = $this->get_variation_by_name( 'temp_book_type', $variations ); $this->assertEmpty( $variation, 'Block variation still exists' ); } From 62491c056e10680e6830f3498b62305f3d1d3426 Mon Sep 17 00:00:00 2001 From: Fabian Todt Date: Tue, 30 Jan 2024 09:45:24 +0100 Subject: [PATCH 2/8] wp <6.5 compat --- .../src/navigation-link/index.php | 158 +++++++++++++++++- .../block-navigation-link-variations-test.php | 139 +++++++++++++-- 2 files changed, 278 insertions(+), 19 deletions(-) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 7d5a527af5c670..94cc8d4d3c66ad 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -266,6 +266,70 @@ function render_block_core_navigation_link( $attributes, $content, $block ) { return $html; } +/** + * Registers a variation for a post type / taxonomy for the navigation link block. + * + * @since 6.5.0 + * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. + * + * TODO: After two WP versions (6.7), we can remove this. + * + * @param array $variation Variation array from build_variation_for_navigation_link. + */ +function block_core_navigation_link_register_variation( $variation ) { + _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); + /* + * Directly set the variations on the registered block type + * because there's no server side registration for variations (see #47170). + */ + $navigation_block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-link' ); + /* + * If the block is not registered yet, bail early. + * Variation will be registered in register_block_core_navigation_link then. + */ + if ( ! $navigation_block_type ) { + return; + } + + $navigation_block_type->variations = array_merge( + $navigation_block_type->variations, + array( $variation ) + ); +} + +/** + * Unregisters a variation for a post type / taxonomy for the navigation link block. + * + * @since 6.5.0 + * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. + * + * TODO: After two WP versions (6.7), we can remove this. + * + * @param string $name Name of the post type / taxonomy (which was used as variation name). + */ +function block_core_navigation_link_unregister_variation( $name ) { + _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); + /* + * Directly get the variations from the registered block type + * because there's no server side (un)registration for variations (see #47170). + */ + $navigation_block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-link' ); + // If the block is not registered (yet), there's no need to remove a variation. + if ( ! $navigation_block_type || empty( $navigation_block_type->variations ) ) { + return; + } + $variations = $navigation_block_type->variations; + // Search for the variation and remove it from the array. + foreach ( $variations as $i => $variation ) { + if ( $variation['name'] === $name ) { + unset( $variations[ $i ] ); + break; + } + } + // Reindex array after removing one variation. + $navigation_block_type->variations = array_values( $variations ); +} + /** * Returns a navigation link variation * @@ -348,6 +412,8 @@ function block_core_navigation_link_filter_variations( $variations, $block_type /** * Returns an array of variations for the navigation link block. * + * @since 6.5.0 + * * @return array */ function block_core_navigation_link_build_variations() { @@ -391,20 +457,106 @@ function block_core_navigation_link_build_variations() { /** * Registers the navigation link block. * - * @uses render_block_core_navigation() + * @uses render_block_core_navigation_link() + * @uses build_navigation_link_block_variations() * @throws WP_Error An WP_Error exception parsing the block definition. */ function register_block_core_navigation_link() { + /* + * On Core versions < 6.5 the get_block_type_variations is not available. + * Therefore directly register the (until here) known variations. + * This keeps pre-6.5 behaviour in pre-6.5 versions. + * + * This callback will be called by a shim in lib/compat/wordpress-6.5/blocks.php + * + * TODO: After two WP versions (6.7), we can remove this. + */ + $variations_callback = null; + if ( ! method_exists( 'WP_Block_Type', 'get_variations' ) ) { + $variations_callback = 'build_navigation_link_block_variations'; + } register_block_type_from_metadata( __DIR__ . '/navigation-link', array( - 'render_callback' => 'render_block_core_navigation_link', + 'render_callback' => 'render_block_core_navigation_link', + 'variation_callback' => $variations_callback, ) ); } add_action( 'init', 'register_block_core_navigation_link' ); /** - * Use this filter to create all variations for post types / taxonomies dynamically. + * Creates all variations for post types / taxonomies dynamically (= each time when variations are requested). * Do not use variation_callback, to also account for unregistering post types/taxonomies later on. */ add_action( 'get_block_type_variations', 'block_core_navigation_link_filter_variations', 10, 2 ); + +/** + * Registers custom post type variations for navigation link on post type registration + * Handles all post types registered after the block is registered in register_navigation_link_post_type_variations + * + * @since 6.5.0 + * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. + * + * TODO: After two WP versions (6.7), we can remove this. + * + * @param string $post_type The post type name passed from registered_post_type action hook. + * @param WP_Post_Type $post_type_object The post type object passed from registered_post_type. + */ +function block_core_navigation_link_register_post_type_variation( $post_type, $post_type_object ) { + _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); + if ( $post_type_object->show_in_nav_menus ) { + $variation = build_variation_for_navigation_link( $post_type_object, 'post-type' ); + block_core_navigation_link_register_variation( $variation ); + } +} + +/** + * Registers a custom taxonomy variation for navigation link on taxonomy registration + * Handles all taxonomies registered after the block is registered in register_navigation_link_post_type_variations + * + * @since 6.5.0 + * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. + * + * TODO: After two WP versions (6.7), we can remove this. + * + * @param string $taxonomy Taxonomy slug. + * @param array|string $object_type Object type or array of object types. + * @param array $args Array of taxonomy registration arguments. + */ +function block_core_navigation_link_register_taxonomy_variation( $taxonomy, $object_type, $args ) { + _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); + if ( isset( $args['show_in_nav_menus'] ) && $args['show_in_nav_menus'] ) { + $variation = build_variation_for_navigation_link( (object) $args, 'post-type' ); + block_core_navigation_link_register_variation( $variation ); + } +} + +/** + * Unregisters a custom post type variation for navigation link on post type unregistration. + * + * @since 6.5.0 + * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. + * + * TODO: After two WP versions (6.7), we can remove this. + * + * @param string $post_type The post type name passed from unregistered_post_type action hook. + */ +function block_core_navigation_link_unregister_post_type_variation( $post_type ) { + _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); + block_core_navigation_link_unregister_variation( $post_type ); +} + +/** + * Unregisters a custom taxonomy variation for navigation link on taxonomy unregistration. + * + * @since 6.5.0 + * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. + * + * TODO: After two WP versions (6.7), we can remove this. + * + * @param string $taxonomy The taxonomy name passed from unregistered_taxonomy action hook. + */ +function block_core_navigation_link_unregister_taxonomy_variation( $taxonomy ) { + _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); + block_core_navigation_link_unregister_variation( $taxonomy ); +} diff --git a/phpunit/blocks/block-navigation-link-variations-test.php b/phpunit/blocks/block-navigation-link-variations-test.php index d961edde3e37cd..05a7dc24dd17af 100644 --- a/phpunit/blocks/block-navigation-link-variations-test.php +++ b/phpunit/blocks/block-navigation-link-variations-test.php @@ -12,9 +12,19 @@ * @group blocks */ class Block_Navigation_Link_Variations_Test extends WP_UnitTestCase { + + /** + * Whether to use a shim/workaround for WordPress Core versions < 6.5. + * See https://github.com/WordPress/gutenberg/pull/58389 for details. + * + * @var bool + */ + private $pre_65_compat = false; + public function set_up() { parent::set_up(); - register_post_type( + + $post_type = register_post_type( 'custom_book', array( 'labels' => array( @@ -25,7 +35,7 @@ public function set_up() { 'show_in_nav_menus' => true, ) ); - register_post_type( + $private_post_type = register_post_type( 'private_custom_book', array( 'labels' => array( @@ -36,7 +46,7 @@ public function set_up() { 'show_in_nav_menus' => false, ) ); - register_taxonomy( + $taxonomy = register_taxonomy( 'book_type', 'custom_book', array( @@ -46,7 +56,7 @@ public function set_up() { 'show_in_nav_menus' => true, ) ); - register_taxonomy( + $private_taxonomoy = register_taxonomy( 'private_book_type', 'private_custom_book', array( @@ -56,6 +66,22 @@ public function set_up() { 'show_in_nav_menus' => false, ) ); + + $this->pre_65_compat = ! method_exists( 'WP_Block_Type', 'get_variations' ); + + /* + * In Core versions < 6.5, variations for post types/taxonomies registered after init#10 (= after the block type was registered) + * need to be manually registered. + * set_up runs after init#10, therefore register the variations here with the old deprecated functions. + * + * TODO: After two WP versions (6.7), we can remove this. + */ + if ( $this->pre_65_compat ) { + $this->handle_pre_65_post_type_variation_registration( $post_type ); + $this->handle_pre_65_post_type_variation_registration( $private_post_type ); + $this->handle_pre_65_taxonomy_variation_registration( $taxonomy ); + $this->handle_pre_65_taxonomy_variation_registration( $private_taxonomoy ); + } } public function tear_down() { @@ -65,6 +91,17 @@ public function tear_down() { unregister_taxonomy( 'private_book_type' ); unregister_post_type( 'temp_custom_book' ); unregister_taxonomy( 'temp_book_type' ); + + // See comment in set_up for dexplanation. + if ( $this->pre_65_compat ) { + $this->handle_pre_65_post_type_variation_unregistration( 'custom_book' ); + $this->handle_pre_65_post_type_variation_unregistration( 'private_custom_book' ); + $this->handle_pre_65_post_type_variation_unregistration( 'temp_custom_book' ); + $this->handle_pre_65_taxonomy_variation_unregistration( 'book_type' ); + $this->handle_pre_65_taxonomy_variation_unregistration( 'private_book_type' ); + $this->handle_pre_65_taxonomy_variation_unregistration( 'temp_book_type' ); + } + parent::tear_down(); } @@ -74,7 +111,8 @@ public function tear_down() { public function test_navigation_link_variations_custom_post_type() { $registry = WP_Block_Type_Registry::get_instance(); $nav_link_block = $registry->get_registered( 'core/navigation-link' ); - $variations = $nav_link_block->get_variations(); + // Use property and let __get handle it, so it works for core versions before adding get_variations as well + $variations = $nav_link_block->variations; $this->assertNotEmpty( $variations, 'Block has no variations' ); $variation = $this->get_variation_by_name( 'custom_book', $variations ); $this->assertIsArray( $variation, 'Block variation does not exist' ); @@ -88,7 +126,8 @@ public function test_navigation_link_variations_custom_post_type() { public function test_navigation_link_variations_private_custom_post_type() { $registry = WP_Block_Type_Registry::get_instance(); $nav_link_block = $registry->get_registered( 'core/navigation-link' ); - $variations = $nav_link_block->get_variations(); + // Use property and let __get handle it, so it works for core versions before adding get_variations as well + $variations = $nav_link_block->variations; $this->assertNotEmpty( $variations, 'Block has no variations' ); $variation = $this->get_variation_by_name( 'private_custom_book', $variations ); $this->assertEmpty( $variation, 'Block variation for private post type exists.' ); @@ -100,7 +139,8 @@ public function test_navigation_link_variations_private_custom_post_type() { public function test_navigation_link_variations_custom_taxonomy() { $registry = WP_Block_Type_Registry::get_instance(); $nav_link_block = $registry->get_registered( 'core/navigation-link' ); - $variations = $nav_link_block->get_variations(); + // Use property and let __get handle it, so it works for core versions before adding get_variations as well + $variations = $nav_link_block->variations; $this->assertNotEmpty( $variations, 'Block has no variations' ); $variation = $this->get_variation_by_name( 'book_type', $variations ); $this->assertIsArray( $variation, 'Block variation does not exist' ); @@ -114,7 +154,8 @@ public function test_navigation_link_variations_custom_taxonomy() { public function test_navigation_link_variations_private_custom_taxonomy() { $registry = WP_Block_Type_Registry::get_instance(); $nav_link_block = $registry->get_registered( 'core/navigation-link' ); - $variations = $nav_link_block->get_variations(); + // Use property and let __get handle it, so it works for core versions before adding get_variations as well + $variations = $nav_link_block->variations; $this->assertNotEmpty( $variations, 'Block has no variations' ); $variation = $this->get_variation_by_name( 'private_book_type', $variations ); $this->assertEmpty( $variation, 'Block variation for private taxonomy exists.' ); @@ -124,7 +165,7 @@ public function test_navigation_link_variations_private_custom_taxonomy() { * @covers ::block_core_navigation_link_unregister_post_type_variation */ public function test_navigation_link_variations_unregister_post_type() { - register_post_type( + $post_type = register_post_type( 'temp_custom_book', array( 'labels' => array( @@ -136,17 +177,26 @@ public function test_navigation_link_variations_unregister_post_type() { ) ); + if ( $this->pre_65_compat ) { + $this->handle_pre_65_post_type_variation_registration( $post_type ); + } + $registry = WP_Block_Type_Registry::get_instance(); $nav_link_block = $registry->get_registered( 'core/navigation-link' ); - $variations = $nav_link_block->get_variations(); + // Use property and let __get handle it, so it works for core versions before adding get_variations as well + $variations = $nav_link_block->variations; $this->assertNotEmpty( $variations, 'Block has no variations' ); $variation = $this->get_variation_by_name( 'temp_custom_book', $variations ); $this->assertIsArray( $variation, 'Block variation does not exist' ); - unregister_post_type( 'temp_custom_book' ); + unregister_post_type( $post_type->name ); + + if ( $this->pre_65_compat ) { + $this->handle_pre_65_post_type_variation_unregistration( $post_type->name ); + } // Update array, since it's an dynamic built array - $variations = $nav_link_block->get_variations(); + $variations = $nav_link_block->variations; $variation = $this->get_variation_by_name( 'temp_custom_book', $variations ); $this->assertEmpty( $variation, 'Block variation still exists' ); } @@ -155,7 +205,7 @@ public function test_navigation_link_variations_unregister_post_type() { * @covers ::block_core_navigation_link_unregister_taxonomy_variation */ public function test_navigation_link_variations_unregister_taxonomy() { - register_taxonomy( + $taxonomy = register_taxonomy( 'temp_book_type', 'custom_book', array( @@ -166,17 +216,26 @@ public function test_navigation_link_variations_unregister_taxonomy() { ) ); + if ( $this->pre_65_compat ) { + $this->handle_pre_65_taxonomy_variation_registration( $taxonomy ); + } + $registry = WP_Block_Type_Registry::get_instance(); $nav_link_block = $registry->get_registered( 'core/navigation-link' ); - $variations = $nav_link_block->get_variations(); + // Use property and let __get handle it, so it works for core versions before adding get_variations as well + $variations = $nav_link_block->variations; $this->assertNotEmpty( $variations, 'Block has no variations' ); $variation = $this->get_variation_by_name( 'temp_book_type', $variations ); $this->assertIsArray( $variation, 'Block variation does not exist' ); - unregister_taxonomy( 'temp_book_type' ); + unregister_taxonomy( $taxonomy->name ); + + if ( $this->pre_65_compat ) { + $this->handle_pre_65_taxonomy_variation_unregistration( $taxonomy->name ); + } // Update array, since it's an dynamic built array - $variations = $nav_link_block->get_variations(); + $variations = $nav_link_block->variations; $variation = $this->get_variation_by_name( 'temp_book_type', $variations ); $this->assertEmpty( $variation, 'Block variation still exists' ); } @@ -198,4 +257,52 @@ private function get_variation_by_name( $variation_name, $variations ) { return $found_variation; } + + /** + * Registers a block variation for a post type with the deprecated methods for Core versions < 6.5. + * See comment in set_up for dexplanation. + * + * @param WP_Post_Type $post_type + */ + private function handle_pre_65_post_type_variation_registration( $post_type ) { + $this->setExpectedDeprecated( 'gutenberg_block_core_navigation_link_register_post_type_variation' ); + $this->setExpectedDeprecated( 'gutenberg_block_core_navigation_link_register_variation' ); + gutenberg_block_core_navigation_link_register_post_type_variation( $post_type->name, $post_type ); + } + + /** + * Unregisters a block variation for a post type with the deprecated methods for Core versions < 6.5. + * See comment in set_up for dexplanation. + * + * @param string $post_type + */ + private function handle_pre_65_post_type_variation_unregistration( $post_type ) { + $this->setExpectedDeprecated( 'gutenberg_block_core_navigation_link_unregister_post_type_variation' ); + $this->setExpectedDeprecated( 'gutenberg_block_core_navigation_link_unregister_variation' ); + gutenberg_block_core_navigation_link_unregister_post_type_variation( $post_type ); + } + + /** + * Registers a block variation for a taxonomy with the deprecated methods for Core versions < 6.5. + * See comment in set_up for dexplanation. + * + * @param WP_Taxonomy $post_type + */ + private function handle_pre_65_taxonomy_variation_registration( $taxonomy ) { + $this->setExpectedDeprecated( 'gutenberg_block_core_navigation_link_register_taxonomy_variation' ); + $this->setExpectedDeprecated( 'gutenberg_block_core_navigation_link_register_variation' ); + gutenberg_block_core_navigation_link_register_taxonomy_variation( $taxonomy->name, $taxonomy->object_type, (array) $taxonomy ); + } + + /** + * Unregisters a block variation for a taxonomy with the deprecated methods for Core versions < 6.5. + * See comment in set_up for dexplanation. + * + * @param string $post_type + */ + private function handle_pre_65_taxonomy_variation_unregistration( $taxonomy ) { + $this->setExpectedDeprecated( 'gutenberg_block_core_navigation_link_unregister_taxonomy_variation' ); + $this->setExpectedDeprecated( 'gutenberg_block_core_navigation_link_unregister_variation' ); + gutenberg_block_core_navigation_link_unregister_taxonomy_variation( $taxonomy ); + } } From a6ebdda09d773b696f7326ace3b87551e2967a30 Mon Sep 17 00:00:00 2001 From: Fabian Todt Date: Thu, 8 Feb 2024 13:30:57 +0100 Subject: [PATCH 3/8] update code comments according to PR feedback --- packages/block-library/src/navigation-link/index.php | 5 ++--- phpunit/blocks/block-navigation-link-variations-test.php | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 94cc8d4d3c66ad..c53d0cdcd78022 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -417,7 +417,6 @@ function block_core_navigation_link_filter_variations( $variations, $block_type * @return array */ function block_core_navigation_link_build_variations() { - // This will only handle post types and taxonomies registered until this point. $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); @@ -463,8 +462,8 @@ function block_core_navigation_link_build_variations() { */ function register_block_core_navigation_link() { /* - * On Core versions < 6.5 the get_block_type_variations is not available. - * Therefore directly register the (until here) known variations. + * On Core versions < 6.5 get_block_type_variations is not available. + * Therefore directly register the variations for post/taxonomies that have been registered at this point. * This keeps pre-6.5 behaviour in pre-6.5 versions. * * This callback will be called by a shim in lib/compat/wordpress-6.5/blocks.php diff --git a/phpunit/blocks/block-navigation-link-variations-test.php b/phpunit/blocks/block-navigation-link-variations-test.php index 05a7dc24dd17af..27711e1c03efb5 100644 --- a/phpunit/blocks/block-navigation-link-variations-test.php +++ b/phpunit/blocks/block-navigation-link-variations-test.php @@ -92,7 +92,7 @@ public function tear_down() { unregister_post_type( 'temp_custom_book' ); unregister_taxonomy( 'temp_book_type' ); - // See comment in set_up for dexplanation. + // See comment in set_up for explanation. if ( $this->pre_65_compat ) { $this->handle_pre_65_post_type_variation_unregistration( 'custom_book' ); $this->handle_pre_65_post_type_variation_unregistration( 'private_custom_book' ); From 641db4e520d309dd89f1c8db8c1d85605982425d Mon Sep 17 00:00:00 2001 From: Fabian Todt Date: Thu, 8 Feb 2024 13:33:13 +0100 Subject: [PATCH 4/8] remove todo --- packages/block-library/src/navigation-link/index.php | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index c53d0cdcd78022..52aaf6018d08d6 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -405,7 +405,6 @@ function block_core_navigation_link_filter_variations( $variations, $block_type } $generated_variations = block_core_navigation_link_build_variations(); - // TODO: Mabe check if there are already variations for this post type/taxonomy? return array_merge( $variations, $generated_variations ); } From dd6857cd725dbf3c3d94154388f4a1e3d4bd1513 Mon Sep 17 00:00:00 2001 From: Fabian Todt Date: Fri, 9 Feb 2024 14:21:36 +0100 Subject: [PATCH 5/8] moved deprecated functions into compat directory --- .../navigation-block-variations.php | 83 ++++++++++++++++++ lib/load.php | 1 + .../src/navigation-link/index.php | 87 +------------------ 3 files changed, 85 insertions(+), 86 deletions(-) create mode 100644 lib/compat/wordpress-6.5/navigation-block-variations.php diff --git a/lib/compat/wordpress-6.5/navigation-block-variations.php b/lib/compat/wordpress-6.5/navigation-block-variations.php new file mode 100644 index 00000000000000..990900ceac1b1f --- /dev/null +++ b/lib/compat/wordpress-6.5/navigation-block-variations.php @@ -0,0 +1,83 @@ +show_in_nav_menus ) { + $variation = build_variation_for_navigation_link( $post_type_object, 'post-type' ); + block_core_navigation_link_register_variation( $variation ); + } +} + +/** + * Registers a custom taxonomy variation for navigation link on taxonomy registration + * Handles all taxonomies registered after the block is registered in register_navigation_link_post_type_variations + * + * @since 6.5.0 + * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. + * + * @param string $taxonomy Taxonomy slug. + * @param array|string $object_type Object type or array of object types. + * @param array $args Array of taxonomy registration arguments. + */ +function block_core_navigation_link_register_taxonomy_variation( $taxonomy, $object_type, $args ) { // phpcs:ignore Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames.FunctionNotGuardedAgainstRedeclaration + _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); + if ( isset( $args['show_in_nav_menus'] ) && $args['show_in_nav_menus'] ) { + $variation = build_variation_for_navigation_link( (object) $args, 'post-type' ); + block_core_navigation_link_register_variation( $variation ); + } +} + +/** + * Unregisters a custom post type variation for navigation link on post type unregistration. + * + * @since 6.5.0 + * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. + * + * @param string $post_type The post type name passed from unregistered_post_type action hook. + */ +function block_core_navigation_link_unregister_post_type_variation( $post_type ) { // phpcs:ignore Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames.FunctionNotGuardedAgainstRedeclaration + _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); + block_core_navigation_link_unregister_variation( $post_type ); +} + +/** + * Unregisters a custom taxonomy variation for navigation link on taxonomy unregistration. + * + * @since 6.5.0 + * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. + * + * @param string $taxonomy The taxonomy name passed from unregistered_taxonomy action hook. + */ +function block_core_navigation_link_unregister_taxonomy_variation( $taxonomy ) { // phpcs:ignore Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames.FunctionNotGuardedAgainstRedeclaration + _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); + block_core_navigation_link_unregister_variation( $taxonomy ); +} diff --git a/lib/load.php b/lib/load.php index 27e67d2e6d320b..47d41fb50b3b5f 100644 --- a/lib/load.php +++ b/lib/load.php @@ -113,6 +113,7 @@ function gutenberg_is_experiment_enabled( $name ) { require __DIR__ . '/compat/wordpress-6.5/interactivity-api/interactivity-api.php'; require __DIR__ . '/compat/wordpress-6.5/class-wp-script-modules.php'; require __DIR__ . '/compat/wordpress-6.5/scripts-modules.php'; +require __DIR__ . '/compat/wordpress-6.5/navigation-block-variations.php'; if ( ! class_exists( 'WP_Block_Bindings_Source' ) ) { require __DIR__ . '/compat/wordpress-6.5/block-bindings/class-wp-block-bindings-source.php'; } diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index 52aaf6018d08d6..a8cf9a9d9325e0 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -460,24 +460,10 @@ function block_core_navigation_link_build_variations() { * @throws WP_Error An WP_Error exception parsing the block definition. */ function register_block_core_navigation_link() { - /* - * On Core versions < 6.5 get_block_type_variations is not available. - * Therefore directly register the variations for post/taxonomies that have been registered at this point. - * This keeps pre-6.5 behaviour in pre-6.5 versions. - * - * This callback will be called by a shim in lib/compat/wordpress-6.5/blocks.php - * - * TODO: After two WP versions (6.7), we can remove this. - */ - $variations_callback = null; - if ( ! method_exists( 'WP_Block_Type', 'get_variations' ) ) { - $variations_callback = 'build_navigation_link_block_variations'; - } register_block_type_from_metadata( __DIR__ . '/navigation-link', array( - 'render_callback' => 'render_block_core_navigation_link', - 'variation_callback' => $variations_callback, + 'render_callback' => 'render_block_core_navigation_link', ) ); } @@ -487,74 +473,3 @@ function register_block_core_navigation_link() { * Do not use variation_callback, to also account for unregistering post types/taxonomies later on. */ add_action( 'get_block_type_variations', 'block_core_navigation_link_filter_variations', 10, 2 ); - -/** - * Registers custom post type variations for navigation link on post type registration - * Handles all post types registered after the block is registered in register_navigation_link_post_type_variations - * - * @since 6.5.0 - * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. - * - * TODO: After two WP versions (6.7), we can remove this. - * - * @param string $post_type The post type name passed from registered_post_type action hook. - * @param WP_Post_Type $post_type_object The post type object passed from registered_post_type. - */ -function block_core_navigation_link_register_post_type_variation( $post_type, $post_type_object ) { - _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); - if ( $post_type_object->show_in_nav_menus ) { - $variation = build_variation_for_navigation_link( $post_type_object, 'post-type' ); - block_core_navigation_link_register_variation( $variation ); - } -} - -/** - * Registers a custom taxonomy variation for navigation link on taxonomy registration - * Handles all taxonomies registered after the block is registered in register_navigation_link_post_type_variations - * - * @since 6.5.0 - * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. - * - * TODO: After two WP versions (6.7), we can remove this. - * - * @param string $taxonomy Taxonomy slug. - * @param array|string $object_type Object type or array of object types. - * @param array $args Array of taxonomy registration arguments. - */ -function block_core_navigation_link_register_taxonomy_variation( $taxonomy, $object_type, $args ) { - _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); - if ( isset( $args['show_in_nav_menus'] ) && $args['show_in_nav_menus'] ) { - $variation = build_variation_for_navigation_link( (object) $args, 'post-type' ); - block_core_navigation_link_register_variation( $variation ); - } -} - -/** - * Unregisters a custom post type variation for navigation link on post type unregistration. - * - * @since 6.5.0 - * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. - * - * TODO: After two WP versions (6.7), we can remove this. - * - * @param string $post_type The post type name passed from unregistered_post_type action hook. - */ -function block_core_navigation_link_unregister_post_type_variation( $post_type ) { - _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); - block_core_navigation_link_unregister_variation( $post_type ); -} - -/** - * Unregisters a custom taxonomy variation for navigation link on taxonomy unregistration. - * - * @since 6.5.0 - * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. - * - * TODO: After two WP versions (6.7), we can remove this. - * - * @param string $taxonomy The taxonomy name passed from unregistered_taxonomy action hook. - */ -function block_core_navigation_link_unregister_taxonomy_variation( $taxonomy ) { - _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); - block_core_navigation_link_unregister_variation( $taxonomy ); -} From cca0b015258f2e46791ffcd427446dfa369b0e29 Mon Sep 17 00:00:00 2001 From: Fabian Todt Date: Fri, 9 Feb 2024 14:47:00 +0100 Subject: [PATCH 6/8] guard deprecated functions against redeclaration in core --- .../navigation-block-variations.php | 68 +++++++++++++++++-- .../src/navigation-link/index.php | 64 ----------------- 2 files changed, 64 insertions(+), 68 deletions(-) diff --git a/lib/compat/wordpress-6.5/navigation-block-variations.php b/lib/compat/wordpress-6.5/navigation-block-variations.php index 990900ceac1b1f..b5253d4c11274c 100644 --- a/lib/compat/wordpress-6.5/navigation-block-variations.php +++ b/lib/compat/wordpress-6.5/navigation-block-variations.php @@ -29,7 +29,7 @@ function gutenberg_navigation_link_variations_compat( $args ) { * @param string $post_type The post type name passed from registered_post_type action hook. * @param WP_Post_Type $post_type_object The post type object passed from registered_post_type. */ -function block_core_navigation_link_register_post_type_variation( $post_type, $post_type_object ) { // phpcs:ignore Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames.FunctionNotGuardedAgainstRedeclaration +function gutenberg_block_core_navigation_link_register_post_type_variation( $post_type, $post_type_object ) { _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); if ( $post_type_object->show_in_nav_menus ) { $variation = build_variation_for_navigation_link( $post_type_object, 'post-type' ); @@ -48,7 +48,7 @@ function block_core_navigation_link_register_post_type_variation( $post_type, $p * @param array|string $object_type Object type or array of object types. * @param array $args Array of taxonomy registration arguments. */ -function block_core_navigation_link_register_taxonomy_variation( $taxonomy, $object_type, $args ) { // phpcs:ignore Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames.FunctionNotGuardedAgainstRedeclaration +function gutenberg_block_core_navigation_link_register_taxonomy_variation( $taxonomy, $object_type, $args ) { _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); if ( isset( $args['show_in_nav_menus'] ) && $args['show_in_nav_menus'] ) { $variation = build_variation_for_navigation_link( (object) $args, 'post-type' ); @@ -64,7 +64,7 @@ function block_core_navigation_link_register_taxonomy_variation( $taxonomy, $obj * * @param string $post_type The post type name passed from unregistered_post_type action hook. */ -function block_core_navigation_link_unregister_post_type_variation( $post_type ) { // phpcs:ignore Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames.FunctionNotGuardedAgainstRedeclaration +function gutenberg_block_core_navigation_link_unregister_post_type_variation( $post_type ) { _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); block_core_navigation_link_unregister_variation( $post_type ); } @@ -77,7 +77,67 @@ function block_core_navigation_link_unregister_post_type_variation( $post_type ) * * @param string $taxonomy The taxonomy name passed from unregistered_taxonomy action hook. */ -function block_core_navigation_link_unregister_taxonomy_variation( $taxonomy ) { // phpcs:ignore Gutenberg.CodeAnalysis.GuardedFunctionAndClassNames.FunctionNotGuardedAgainstRedeclaration +function gutenberg_block_core_navigation_link_unregister_taxonomy_variation( $taxonomy ) { _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); block_core_navigation_link_unregister_variation( $taxonomy ); } + +/** + * Registers a variation for a post type / taxonomy for the navigation link block. + * + * @since 6.5.0 + * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. + * + * @param array $variation Variation array from build_variation_for_navigation_link. + */ +function gutenberg_eblock_core_navigation_link_register_variation( $variation ) { + _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); + /* + * Directly set the variations on the registered block type + * because there's no server side registration for variations (see #47170). + */ + $navigation_block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-link' ); + /* + * If the block is not registered yet, bail early. + * Variation will be registered in register_block_core_navigation_link then. + */ + if ( ! $navigation_block_type ) { + return; + } + + $navigation_block_type->variations = array_merge( + $navigation_block_type->variations, + array( $variation ) + ); +} + +/** + * Unregisters a variation for a post type / taxonomy for the navigation link block. + * + * @since 6.5.0 + * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. + * + * @param string $name Name of the post type / taxonomy (which was used as variation name). + */ +function gutenberg_block_core_navigation_link_unregister_variation( $name ) { + _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); + /* + * Directly get the variations from the registered block type + * because there's no server side (un)registration for variations (see #47170). + */ + $navigation_block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-link' ); + // If the block is not registered (yet), there's no need to remove a variation. + if ( ! $navigation_block_type || empty( $navigation_block_type->variations ) ) { + return; + } + $variations = $navigation_block_type->variations; + // Search for the variation and remove it from the array. + foreach ( $variations as $i => $variation ) { + if ( $variation['name'] === $name ) { + unset( $variations[ $i ] ); + break; + } + } + // Reindex array after removing one variation. + $navigation_block_type->variations = array_values( $variations ); +} diff --git a/packages/block-library/src/navigation-link/index.php b/packages/block-library/src/navigation-link/index.php index a8cf9a9d9325e0..afd34dcae779ba 100644 --- a/packages/block-library/src/navigation-link/index.php +++ b/packages/block-library/src/navigation-link/index.php @@ -266,70 +266,6 @@ function render_block_core_navigation_link( $attributes, $content, $block ) { return $html; } -/** - * Registers a variation for a post type / taxonomy for the navigation link block. - * - * @since 6.5.0 - * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. - * - * TODO: After two WP versions (6.7), we can remove this. - * - * @param array $variation Variation array from build_variation_for_navigation_link. - */ -function block_core_navigation_link_register_variation( $variation ) { - _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); - /* - * Directly set the variations on the registered block type - * because there's no server side registration for variations (see #47170). - */ - $navigation_block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-link' ); - /* - * If the block is not registered yet, bail early. - * Variation will be registered in register_block_core_navigation_link then. - */ - if ( ! $navigation_block_type ) { - return; - } - - $navigation_block_type->variations = array_merge( - $navigation_block_type->variations, - array( $variation ) - ); -} - -/** - * Unregisters a variation for a post type / taxonomy for the navigation link block. - * - * @since 6.5.0 - * @deprecated 6.5.0 Use WP_Block_Type::get_variations / get_block_type_variations filter instead. - * - * TODO: After two WP versions (6.7), we can remove this. - * - * @param string $name Name of the post type / taxonomy (which was used as variation name). - */ -function block_core_navigation_link_unregister_variation( $name ) { - _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); - /* - * Directly get the variations from the registered block type - * because there's no server side (un)registration for variations (see #47170). - */ - $navigation_block_type = WP_Block_Type_Registry::get_instance()->get_registered( 'core/navigation-link' ); - // If the block is not registered (yet), there's no need to remove a variation. - if ( ! $navigation_block_type || empty( $navigation_block_type->variations ) ) { - return; - } - $variations = $navigation_block_type->variations; - // Search for the variation and remove it from the array. - foreach ( $variations as $i => $variation ) { - if ( $variation['name'] === $name ) { - unset( $variations[ $i ] ); - break; - } - } - // Reindex array after removing one variation. - $navigation_block_type->variations = array_values( $variations ); -} - /** * Returns a navigation link variation * From 573ec7a16297c20e21bc4cec7850b167956165c5 Mon Sep 17 00:00:00 2001 From: Dave Smith Date: Fri, 9 Feb 2024 14:11:11 +0000 Subject: [PATCH 7/8] Fix typo --- lib/compat/wordpress-6.5/navigation-block-variations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/compat/wordpress-6.5/navigation-block-variations.php b/lib/compat/wordpress-6.5/navigation-block-variations.php index b5253d4c11274c..38d16f6dc7d39d 100644 --- a/lib/compat/wordpress-6.5/navigation-block-variations.php +++ b/lib/compat/wordpress-6.5/navigation-block-variations.php @@ -90,7 +90,7 @@ function gutenberg_block_core_navigation_link_unregister_taxonomy_variation( $ta * * @param array $variation Variation array from build_variation_for_navigation_link. */ -function gutenberg_eblock_core_navigation_link_register_variation( $variation ) { +function gutenberg_block_core_navigation_link_register_variation( $variation ) { _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); /* * Directly set the variations on the registered block type From d1e9bc4734fc713e4763b8e564e3c9f25a47df0e Mon Sep 17 00:00:00 2001 From: Fabian Todt Date: Fri, 9 Feb 2024 15:03:34 +0100 Subject: [PATCH 8/8] fix wrong function names --- lib/compat/wordpress-6.5/navigation-block-variations.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/compat/wordpress-6.5/navigation-block-variations.php b/lib/compat/wordpress-6.5/navigation-block-variations.php index 38d16f6dc7d39d..0bf789d42901e9 100644 --- a/lib/compat/wordpress-6.5/navigation-block-variations.php +++ b/lib/compat/wordpress-6.5/navigation-block-variations.php @@ -33,7 +33,7 @@ function gutenberg_block_core_navigation_link_register_post_type_variation( $pos _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); if ( $post_type_object->show_in_nav_menus ) { $variation = build_variation_for_navigation_link( $post_type_object, 'post-type' ); - block_core_navigation_link_register_variation( $variation ); + gutenberg_block_core_navigation_link_register_variation( $variation ); } } @@ -52,7 +52,7 @@ function gutenberg_block_core_navigation_link_register_taxonomy_variation( $taxo _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); if ( isset( $args['show_in_nav_menus'] ) && $args['show_in_nav_menus'] ) { $variation = build_variation_for_navigation_link( (object) $args, 'post-type' ); - block_core_navigation_link_register_variation( $variation ); + gutenberg_block_core_navigation_link_register_variation( $variation ); } } @@ -66,7 +66,7 @@ function gutenberg_block_core_navigation_link_register_taxonomy_variation( $taxo */ function gutenberg_block_core_navigation_link_unregister_post_type_variation( $post_type ) { _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); - block_core_navigation_link_unregister_variation( $post_type ); + gutenberg_block_core_navigation_link_unregister_variation( $post_type ); } /** @@ -79,7 +79,7 @@ function gutenberg_block_core_navigation_link_unregister_post_type_variation( $p */ function gutenberg_block_core_navigation_link_unregister_taxonomy_variation( $taxonomy ) { _deprecated_function( __FUNCTION__, '6.5.0', 'WP_Block_Type::get_variations' ); - block_core_navigation_link_unregister_variation( $taxonomy ); + gutenberg_block_core_navigation_link_unregister_variation( $taxonomy ); } /**