Changeset 57578 for trunk/src/wp-includes/blocks/navigation-link.php
- Timestamp:
- 02/09/2024 06:20:12 PM (14 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/navigation-link.php
r57377 r57578 133 133 134 134 foreach ( $query_params as $query_param ) { 135 $can_query_param_be_encoded = is_string( $query_param ) && ! empty( $query_param ); 136 if ( ! $can_query_param_be_encoded ) { 137 continue; 138 } 135 139 if ( rawurldecode( $query_param ) !== $query_param ) { 136 140 $is_url_encoded = true; … … 324 328 325 329 /** 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 /** 374 * Register the navigation link block. 330 * Filters the registered variations for a block type. 331 * Returns the dynamically built variations for all post-types and taxonomies. 332 * 333 * @since 6.5.0 334 * 335 * @param array $variations Array of registered variations for a block type. 336 * @param WP_Block_Type $block_type The full block type object. 337 */ 338 function block_core_navigation_link_filter_variations( $variations, $block_type ) { 339 if ( 'core/navigation-link' !== $block_type->name ) { 340 return $variations; 341 } 342 343 $generated_variations = block_core_navigation_link_build_variations(); 344 return array_merge( $variations, $generated_variations ); 345 } 346 347 /** 375 348 * Returns an array of variations for the navigation link block. 376 349 * 350 * @since 6.5.0 351 * 377 352 * @return array 378 353 */ 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. 354 function block_core_navigation_link_build_variations() { 383 355 $post_types = get_post_types( array( 'show_in_nav_menus' => true ), 'objects' ); 384 356 $taxonomies = get_taxonomies( array( 'show_in_nav_menus' => true ), 'objects' ); 385 357 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. 358 /* 359 * Use two separate arrays as a way to order the variations in the UI. 360 * Known variations (like Post Link and Page Link) are added to the 361 * `built_ins` array. Variations for custom post types and taxonomies are 362 * added to the `variations` array and will always appear after `built-ins. 363 */ 390 364 $built_ins = array(); 391 365 $variations = array(); … … 416 390 417 391 /** 418 * Register the navigation link block. 419 * 420 * @uses render_block_core_navigation() 392 * Registers the navigation link block. 393 * 394 * @uses render_block_core_navigation_link() 395 * @uses build_navigation_link_block_variations() 421 396 * @throws WP_Error An WP_Error exception parsing the block definition. 422 397 */ … … 425 400 __DIR__ . '/navigation-link', 426 401 array( 427 'render_callback' => 'render_block_core_navigation_link', 428 'variation_callback' => 'build_navigation_link_block_variations', 402 'render_callback' => 'render_block_core_navigation_link', 429 403 ) 430 404 ); 431 405 } 432 406 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 } 407 /** 408 * Creates all variations for post types / taxonomies dynamically (= each time when variations are requested). 409 * Do not use variation_callback, to also account for unregistering post types/taxonomies later on. 410 */ 411 add_action( 'get_block_type_variations', 'block_core_navigation_link_filter_variations', 10, 2 );
Note: See TracChangeset
for help on using the changeset viewer.