@@ -323,70 +323,40 @@ function build_variation_for_navigation_link( $entity, $kind ) {
323323}
324324
325325/**
326- * Register a variation for a post type / taxonomy for the navigation link block.
326+ * Filters the registered variations for a block type.
327+ * Returns the dynamically built variations for all post-types and taxonomies.
327328 *
328- * @param array $variation Variation array from build_variation_for_navigation_link.
329- * @return void
330- */
331- function block_core_navigation_link_register_variation ( $ variation ) {
332- // Directly set the variations on the registered block type
333- // because there's no server side registration for variations (see #47170).
334- $ navigation_block_type = WP_Block_Type_Registry::get_instance ()->get_registered ( 'core/navigation-link ' );
335- // If the block is not registered yet, bail early.
336- // Variation will be registered in register_block_core_navigation_link then.
337- if ( ! $ navigation_block_type ) {
338- return ;
339- }
340-
341- $ navigation_block_type ->variations = array_merge (
342- $ navigation_block_type ->variations ,
343- array ( $ variation )
344- );
345- }
346-
347- /**
348- * Unregister a variation for a post type / taxonomy for the navigation link block.
329+ * @since 6.5.0
349330 *
350- * @param string $name Name of the post type / taxonomy (which was used as variation name) .
351- * @return void
331+ * @param array $variations Array of registered variations for a block type .
332+ * @param WP_Block_Type $block_type The full block type object.
352333 */
353- function block_core_navigation_link_unregister_variation ( $ name ) {
354- // Directly get the variations from the registered block type
355- // because there's no server side (un)registration for variations (see #47170).
356- $ navigation_block_type = WP_Block_Type_Registry::get_instance ()->get_registered ( 'core/navigation-link ' );
357- // If the block is not registered (yet), there's no need to remove a variation.
358- if ( ! $ navigation_block_type || empty ( $ navigation_block_type ->variations ) ) {
359- return ;
334+ function block_core_navigation_link_filter_variations ( $ variations , $ block_type ) {
335+ if ( 'core/navigation-link ' !== $ block_type ->name ) {
336+ return $ variations ;
360337 }
361- $ variations = $ navigation_block_type ->variations ;
362- // Search for the variation and remove it from the array.
363- foreach ( $ variations as $ i => $ variation ) {
364- if ( $ variation ['name ' ] === $ name ) {
365- unset( $ variations [ $ i ] );
366- break ;
367- }
368- }
369- // Reindex array after removing one variation.
370- $ navigation_block_type ->variations = array_values ( $ variations );
338+
339+ $ generated_variations = build_navigation_link_block_variations ();
340+ // TODO: Mabe check if there are already variations for this post type/taxonomy?
341+ return array_merge ( $ variations , $ generated_variations );
371342}
372343
373344/**
374- * Register the navigation link block.
375345 * Returns an array of variations for the navigation link block.
376346 *
377347 * @return array
378348 */
379349function build_navigation_link_block_variations () {
380- // This will only handle post types and taxonomies registered until this point (init on priority 9).
381- // See action hooks below for other post types and taxonomies.
382- // See https://github.com/WordPress/gutenberg/issues/53826 for details.
350+ // This will only handle post types and taxonomies registered until this point.
383351 $ post_types = get_post_types ( array ( 'show_in_nav_menus ' => true ), 'objects ' );
384352 $ taxonomies = get_taxonomies ( array ( 'show_in_nav_menus ' => true ), 'objects ' );
385353
386- // Use two separate arrays as a way to order the variations in the UI.
387- // Known variations (like Post Link and Page Link) are added to the
388- // `built_ins` array. Variations for custom post types and taxonomies are
389- // added to the `variations` array and will always appear after `built-ins.
354+ /*
355+ * Use two separate arrays as a way to order the variations in the UI.
356+ * Known variations (like Post Link and Page Link) are added to the
357+ * `built_ins` array. Variations for custom post types and taxonomies are
358+ * added to the `variations` array and will always appear after `built-ins.
359+ */
390360 $ built_ins = array ();
391361 $ variations = array ();
392362
@@ -415,7 +385,7 @@ function build_navigation_link_block_variations() {
415385}
416386
417387/**
418- * Register the navigation link block.
388+ * Registers the navigation link block.
419389 *
420390 * @uses render_block_core_navigation()
421391 * @throws WP_Error An WP_Error exception parsing the block definition.
@@ -424,69 +394,13 @@ function register_block_core_navigation_link() {
424394 register_block_type_from_metadata (
425395 __DIR__ . '/navigation-link ' ,
426396 array (
427- 'render_callback ' => 'render_block_core_navigation_link ' ,
428- 'variation_callback ' => 'build_navigation_link_block_variations ' ,
397+ 'render_callback ' => 'render_block_core_navigation_link ' ,
429398 )
430399 );
431-
432- // Register actions for all post types and taxonomies, to add variations when they are registered.
433- // All post types/taxonomies registered before register_block_core_navigation_link, will be handled by that function.
434- add_action ( 'registered_post_type ' , 'block_core_navigation_link_register_post_type_variation ' , 10 , 2 );
435- add_action ( 'registered_taxonomy ' , 'block_core_navigation_link_register_taxonomy_variation ' , 10 , 3 );
436400}
437401add_action ( 'init ' , 'register_block_core_navigation_link ' );
438-
439- // Handle unregistering of post types and taxonomies and remove the variations.
440- add_action ( 'unregistered_post_type ' , 'block_core_navigation_link_unregister_post_type_variation ' );
441- add_action ( 'unregistered_taxonomy ' , 'block_core_navigation_link_unregister_taxonomy_variation ' );
442-
443402/**
444- * Register custom post type variations for navigation link on post type registration
445- * Handles all post types registered after the block is registered in register_navigation_link_post_type_variations
446- *
447- * @param string $post_type The post type name passed from registered_post_type action hook.
448- * @param WP_Post_Type $post_type_object The post type object passed from registered_post_type.
449- * @return void
403+ * Use this filter to create all variations for post types / taxonomies dynamically.
404+ * Do not use variation_callback, to also account for unregistering post types/taxonomies later on.
450405 */
451- function block_core_navigation_link_register_post_type_variation ( $ post_type , $ post_type_object ) {
452- if ( $ post_type_object ->show_in_nav_menus ) {
453- $ variation = build_variation_for_navigation_link ( $ post_type_object , 'post-type ' );
454- block_core_navigation_link_register_variation ( $ variation );
455- }
456- }
457-
458- /**
459- * Register a custom taxonomy variation for navigation link on taxonomy registration
460- * Handles all taxonomies registered after the block is registered in register_navigation_link_post_type_variations
461- *
462- * @param string $taxonomy Taxonomy slug.
463- * @param array|string $object_type Object type or array of object types.
464- * @param array $args Array of taxonomy registration arguments.
465- * @return void
466- */
467- function block_core_navigation_link_register_taxonomy_variation ( $ taxonomy , $ object_type , $ args ) {
468- if ( isset ( $ args ['show_in_nav_menus ' ] ) && $ args ['show_in_nav_menus ' ] ) {
469- $ variation = build_variation_for_navigation_link ( (object ) $ args , 'post-type ' );
470- block_core_navigation_link_register_variation ( $ variation );
471- }
472- }
473-
474- /**
475- * Unregisters a custom post type variation for navigation link on post type unregistration.
476- *
477- * @param string $post_type The post type name passed from unregistered_post_type action hook.
478- * @return void
479- */
480- function block_core_navigation_link_unregister_post_type_variation ( $ post_type ) {
481- block_core_navigation_link_unregister_variation ( $ post_type );
482- }
483-
484- /**
485- * Unregisters a custom taxonomy variation for navigation link on taxonomy unregistration.
486- *
487- * @param string $taxonomy The taxonomy name passed from unregistered_taxonomy action hook.
488- * @return void
489- */
490- function block_core_navigation_link_unregister_taxonomy_variation ( $ taxonomy ) {
491- block_core_navigation_link_unregister_variation ( $ taxonomy );
492- }
406+ add_action ( 'get_block_type_variations ' , 'block_core_navigation_link_filter_variations ' , 10 , 2 );
0 commit comments