Changeset 55246 for trunk/src/wp-includes/blocks/navigation.php
- Timestamp:
- 02/07/2023 07:01:56 AM (4 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/blocks/navigation.php (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/navigation.php
r54632 r55246 249 249 } 250 250 251 /** 252 * Get the classic navigation menu to use as a fallback. 253 * 254 * @return object WP_Term The classic navigation. 255 */ 256 function block_core_navigation_get_classic_menu_fallback() { 257 $classic_nav_menus = wp_get_nav_menus(); 258 259 // If menus exist. 260 if ( $classic_nav_menus && ! is_wp_error( $classic_nav_menus ) ) { 261 // Handles simple use case where user has a classic menu and switches to a block theme. 262 263 // Returns the menu assigned to location `primary`. 264 $locations = get_nav_menu_locations(); 265 if ( isset( $locations['primary'] ) ) { 266 $primary_menu = wp_get_nav_menu_object( $locations['primary'] ); 267 if ( $primary_menu ) { 268 return $primary_menu; 269 } 270 } 271 272 // Returns a menu if `primary` is its slug. 273 foreach ( $classic_nav_menus as $classic_nav_menu ) { 274 if ( 'primary' === $classic_nav_menu->slug ) { 275 return $classic_nav_menu; 276 } 277 } 278 279 // Otherwise return the most recently created classic menu. 280 usort( 281 $classic_nav_menus, 282 function( $a, $b ) { 283 return $b->term_id - $a->term_id; 284 } 285 ); 286 return $classic_nav_menus[0]; 287 } 288 } 289 290 /** 291 * Converts a classic navigation to blocks. 292 * 293 * @param object $classic_nav_menu WP_Term The classic navigation object to convert. 294 * @return array the normalized parsed blocks. 295 */ 296 function block_core_navigation_get_classic_menu_fallback_blocks( $classic_nav_menu ) { 297 // BEGIN: Code that already exists in wp_nav_menu(). 298 $menu_items = wp_get_nav_menu_items( $classic_nav_menu->term_id, array( 'update_post_term_cache' => false ) ); 299 300 // Set up the $menu_item variables. 301 _wp_menu_item_classes_by_context( $menu_items ); 302 303 $sorted_menu_items = array(); 304 foreach ( (array) $menu_items as $menu_item ) { 305 $sorted_menu_items[ $menu_item->menu_order ] = $menu_item; 306 } 307 308 unset( $menu_items, $menu_item ); 309 310 // END: Code that already exists in wp_nav_menu(). 311 312 $menu_items_by_parent_id = array(); 313 foreach ( $sorted_menu_items as $menu_item ) { 314 $menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item; 315 } 316 317 $inner_blocks = block_core_navigation_parse_blocks_from_menu_items( 318 isset( $menu_items_by_parent_id[0] ) 319 ? $menu_items_by_parent_id[0] 320 : array(), 321 $menu_items_by_parent_id 322 ); 323 324 return serialize_blocks( $inner_blocks ); 325 } 326 327 /** 328 * If there's a the classic menu then use it as a fallback. 329 * 330 * @return array the normalized parsed blocks. 331 */ 332 function block_core_navigation_maybe_use_classic_menu_fallback() { 333 // See if we have a classic menu. 334 $classic_nav_menu = block_core_navigation_get_classic_menu_fallback(); 335 336 if ( ! $classic_nav_menu ) { 337 return; 338 } 339 340 // If we have a classic menu then convert it to blocks. 341 $classic_nav_menu_blocks = block_core_navigation_get_classic_menu_fallback_blocks( $classic_nav_menu ); 342 343 if ( empty( $classic_nav_menu_blocks ) ) { 344 return; 345 } 346 347 // Create a new navigation menu from the classic menu. 348 $wp_insert_post_result = wp_insert_post( 349 array( 350 'post_content' => $classic_nav_menu_blocks, 351 'post_title' => $classic_nav_menu->slug, 352 'post_name' => $classic_nav_menu->slug, 353 'post_status' => 'publish', 354 'post_type' => 'wp_navigation', 355 ), 356 true // So that we can check whether the result is an error. 357 ); 358 359 if ( is_wp_error( $wp_insert_post_result ) ) { 360 return; 361 } 362 363 // Fetch the most recently published navigation which will be the classic one created above. 364 return block_core_navigation_get_most_recently_published_navigation(); 365 } 251 366 252 367 /** … … 256 371 */ 257 372 function block_core_navigation_get_most_recently_published_navigation() { 258 // We default to the most recently created menu. 373 374 // Default to the most recently created menu. 259 375 $parsed_args = array( 260 376 'post_type' => 'wp_navigation', … … 296 412 297 413 /** 414 * Returns true if the navigation block contains a nested navigation block. 415 * 416 * @param WP_Block_List $inner_blocks Inner block instance to be normalized. 417 * @return bool true if the navigation block contains a nested navigation block. 418 */ 419 function block_core_navigation_block_contains_core_navigation( $inner_blocks ) { 420 foreach ( $inner_blocks as $block ) { 421 if ( 'core/navigation' === $block->name ) { 422 return true; 423 } 424 if ( $block->inner_blocks && block_core_navigation_block_contains_core_navigation( $block->inner_blocks ) ) { 425 return true; 426 } 427 } 428 429 return false; 430 } 431 432 /** 298 433 * Retrieves the appropriate fallback to be used on the front of the 299 434 * site when there is no menu assigned to the Nav block. … … 320 455 $navigation_post = block_core_navigation_get_most_recently_published_navigation(); 321 456 322 // Prefer using the first non-empty Navigation as fallback if available. 457 // If there are no navigation posts then try to find a classic menu 458 // and convert it into a block based navigation menu. 459 if ( ! $navigation_post ) { 460 $navigation_post = block_core_navigation_maybe_use_classic_menu_fallback(); 461 } 462 463 // Use the first non-empty Navigation as fallback if available. 323 464 if ( $navigation_post ) { 324 $maybe_fallback = block_core_navigation_filter_out_empty_blocks( parse_blocks( $navigation_post->post_content ) ); 465 $parsed_blocks = parse_blocks( $navigation_post->post_content ); 466 $maybe_fallback = block_core_navigation_filter_out_empty_blocks( $parsed_blocks ); 325 467 326 468 // Normalizing blocks may result in an empty array of blocks if they were all `null` blocks. … … 502 644 } 503 645 646 if ( block_core_navigation_block_contains_core_navigation( $inner_blocks ) ) { 647 return ''; 648 } 649 504 650 /** 505 651 * Filter navigation block $inner_blocks. … … 552 698 } 553 699 700 $list_item_nav_blocks = array( 701 'core/navigation-link', 702 'core/home-link', 703 'core/site-title', 704 'core/site-logo', 705 'core/navigation-submenu', 706 ); 707 708 $needs_list_item_wrapper = array( 709 'core/site-title', 710 'core/site-logo', 711 ); 712 554 713 $inner_blocks_html = ''; 555 714 $is_list_open = false; 556 715 foreach ( $inner_blocks as $inner_block ) { 557 if ( ( 'core/navigation-link' === $inner_block->name || 'core/home-link' === $inner_block->name || 'core/site-title' === $inner_block->name || 'core/site-logo' === $inner_block->name || 'core/navigation-submenu' === $inner_block->name ) && ! $is_list_open ) { 716 $is_list_item = in_array( $inner_block->name, $list_item_nav_blocks, true ); 717 718 if ( $is_list_item && ! $is_list_open ) { 558 719 $is_list_open = true; 559 720 $inner_blocks_html .= '<ul class="wp-block-navigation__container">'; 560 721 } 561 if ( 'core/navigation-link' !== $inner_block->name && 'core/home-link' !== $inner_block->name && 'core/site-title' !== $inner_block->name && 'core/site-logo' !== $inner_block->name && 'core/navigation-submenu' !== $inner_block->name && $is_list_open ) { 722 723 if ( ! $is_list_item && $is_list_open ) { 562 724 $is_list_open = false; 563 725 $inner_blocks_html .= '</ul>'; 564 726 } 727 565 728 $inner_block_content = $inner_block->render(); 566 if ( 'core/site-title' === $inner_block->name || ( 'core/site-logo' === $inner_block->name && $inner_block_content ) ) { 567 $inner_blocks_html .= '<li class="wp-block-navigation-item">' . $inner_block_content . '</li>'; 568 } else { 569 $inner_blocks_html .= $inner_block_content; 729 if ( ! empty( $inner_block_content ) ) { 730 if ( in_array( $inner_block->name, $needs_list_item_wrapper, true ) ) { 731 $inner_blocks_html .= '<li class="wp-block-navigation-item">' . $inner_block_content . '</li>'; 732 } else { 733 $inner_blocks_html .= $inner_block_content; 734 } 570 735 } 571 736 } … … 710 875 711 876 add_filter( 'render_block_data', 'block_core_navigation_typographic_presets_backcompatibility' ); 877 878 /** 879 * Enables animation of the block inspector for the Navigation block. 880 * 881 * See: 882 * - https://github.com/WordPress/gutenberg/pull/46342 883 * - https://github.com/WordPress/gutenberg/issues/45884 884 * 885 * @param array $settings Default editor settings. 886 * @return array Filtered editor settings. 887 */ 888 function block_core_navigation_enable_inspector_animation( $settings ) { 889 $current_animation_settings = _wp_array_get( 890 $settings, 891 array( '__experimentalBlockInspectorAnimation' ), 892 array() 893 ); 894 895 $settings['__experimentalBlockInspectorAnimation'] = array_merge( 896 $current_animation_settings, 897 array( 898 'core/navigation' => 899 array( 900 'enterDirection' => 'leftToRight', 901 ), 902 ) 903 ); 904 905 return $settings; 906 } 907 908 add_filter( 'block_editor_settings_all', 'block_core_navigation_enable_inspector_animation' );
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)