Changeset 57377 for trunk/src/wp-includes/blocks/navigation-link.php
- Timestamp:
- 01/29/2024 09:04:18 PM (7 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/navigation-link.php
r56065 r57377 1 1 <?php 2 2 /** 3 * Server-side re ndering of the `core/navigation-link` block.3 * Server-side registering and rendering of the `core/navigation-link` block. 4 4 * 5 5 * @package WordPress … … 324 324 325 325 /** 326 * Register a variation for a post type / taxonomy for the navigation link block. 327 * 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. 349 * 350 * @param string $name Name of the post type / taxonomy (which was used as variation name). 351 * @return void 352 */ 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; 360 } 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 ); 371 } 372 373 /** 326 374 * Register the navigation link block. 327 * 328 * @uses render_block_core_navigation() 329 * @throws WP_Error An WP_Error exception parsing the block definition. 330 */ 331 function register_block_core_navigation_link() { 375 * Returns an array of variations for the navigation link block. 376 * 377 * @return array 378 */ 379 function 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. 332 383 $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); 333 384 $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); … … 361 412 } 362 413 414 return array_merge( $built_ins, $variations ); 415 } 416 417 /** 418 * Register the navigation link block. 419 * 420 * @uses render_block_core_navigation() 421 * @throws WP_Error An WP_Error exception parsing the block definition. 422 */ 423 function register_block_core_navigation_link() { 363 424 register_block_type_from_metadata( 364 425 __DIR__ . '/navigation-link', 365 426 array( 366 'render_callback' => 'render_block_core_navigation_link',367 'variation s' => array_merge( $built_ins, $variations ),427 'render_callback' => 'render_block_core_navigation_link', 428 'variation_callback' => 'build_navigation_link_block_variations', 368 429 ) 369 430 ); 370 431 } 371 432 add_action( 'init', 'register_block_core_navigation_link' ); 433 // Register actions for all post types and taxonomies, to add variations when they are registered. 434 // All post types/taxonomies registered before register_block_core_navigation_link, will be handled by that function. 435 add_action( 'registered_post_type', 'block_core_navigation_link_register_post_type_variation', 10, 2 ); 436 add_action( 'registered_taxonomy', 'block_core_navigation_link_register_taxonomy_variation', 10, 3 ); 437 // Handle unregistering of post types and taxonomies and remove the variations. 438 add_action( 'unregistered_post_type', 'block_core_navigation_link_unregister_post_type_variation' ); 439 add_action( 'unregistered_taxonomy', 'block_core_navigation_link_unregister_taxonomy_variation' ); 440 441 /** 442 * Register custom post type variations for navigation link on post type registration 443 * Handles all post types registered after the block is registered in register_navigation_link_post_type_variations 444 * 445 * @param string $post_type The post type name passed from registered_post_type action hook. 446 * @param WP_Post_Type $post_type_object The post type object passed from registered_post_type. 447 * @return void 448 */ 449 function block_core_navigation_link_register_post_type_variation( $post_type, $post_type_object ) { 450 if ( $post_type_object->show_in_nav_menus ) { 451 $variation = build_variation_for_navigation_link( $post_type_object, 'post-type' ); 452 block_core_navigation_link_register_variation( $variation ); 453 } 454 } 455 456 /** 457 * Register a custom taxonomy variation for navigation link on taxonomy registration 458 * Handles all taxonomies registered after the block is registered in register_navigation_link_post_type_variations 459 * 460 * @param string $taxonomy Taxonomy slug. 461 * @param array|string $object_type Object type or array of object types. 462 * @param array $args Array of taxonomy registration arguments. 463 * @return void 464 */ 465 function block_core_navigation_link_register_taxonomy_variation( $taxonomy, $object_type, $args ) { 466 if ( isset( $args['show_in_nav_menus'] ) && $args['show_in_nav_menus'] ) { 467 $variation = build_variation_for_navigation_link( (object) $args, 'post-type' ); 468 block_core_navigation_link_register_variation( $variation ); 469 } 470 } 471 472 /** 473 * Unregisters a custom post type variation for navigation link on post type unregistration. 474 * 475 * @param string $post_type The post type name passed from unregistered_post_type action hook. 476 * @return void 477 */ 478 function block_core_navigation_link_unregister_post_type_variation( $post_type ) { 479 block_core_navigation_link_unregister_variation( $post_type ); 480 } 481 482 /** 483 * Unregisters a custom taxonomy variation for navigation link on taxonomy unregistration. 484 * 485 * @param string $taxonomy The taxonomy name passed from unregistered_taxonomy action hook. 486 * @return void 487 */ 488 function block_core_navigation_link_unregister_taxonomy_variation( $taxonomy ) { 489 block_core_navigation_link_unregister_variation( $taxonomy ); 490 }
Note: See TracChangeset
for help on using the changeset viewer.