Changeset 54257 for trunk/src/wp-includes/blocks/navigation.php
- Timestamp:
- 09/20/2022 03:14:54 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/navigation.php
r53377 r54257 251 251 252 252 /** 253 * Finds the first non-empty`wp_navigation` Post.253 * Finds the most recently published `wp_navigation` Post. 254 254 * 255 255 * @return WP_Post|null the first non-empty Navigation or null. 256 256 */ 257 function block_core_navigation_get_first_non_empty_navigation() { 258 // Order and orderby args set to mirror those in `wp_get_nav_menus` 259 // see: 260 // - https://github.com/WordPress/wordpress-develop/blob/ba943e113d3b31b121f77a2d30aebe14b047c69d/src/wp-includes/nav-menu.php#L613-L619. 261 // - https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters. 257 function block_core_navigation_get_most_recently_published_navigation() { 258 // We default to the most recently created menu. 262 259 $parsed_args = array( 263 260 'post_type' => 'wp_navigation', 264 261 'no_found_rows' => true, 265 'order' => ' ASC',266 'orderby' => ' name',262 'order' => 'DESC', 263 'orderby' => 'date', 267 264 'post_status' => 'publish', 268 'posts_per_page' => 20, // Try the first 20 posts. 269 ); 270 271 $navigation_posts = new WP_Query( $parsed_args ); 272 foreach ( $navigation_posts->posts as $navigation_post ) { 273 if ( has_blocks( $navigation_post ) ) { 274 return $navigation_post; 275 } 265 'posts_per_page' => 1, // get only the most recent. 266 ); 267 268 $navigation_post = new WP_Query( $parsed_args ); 269 if ( count( $navigation_post->posts ) > 0 ) { 270 return $navigation_post->posts[0]; 276 271 } 277 272 … … 326 321 // Default to a list of Pages. 327 322 328 $navigation_post = block_core_navigation_get_ first_non_empty_navigation();323 $navigation_post = block_core_navigation_get_most_recently_published_navigation(); 329 324 330 325 // Prefer using the first non-empty Navigation as fallback if available. … … 378 373 379 374 if ( 'core/navigation-link' === $block->name || 'core/navigation-submenu' === $block->name ) { 380 if ( $block->attributes && isset( $block->attributes['kind'] ) && 'post-type' === $block->attributes['kind'] ) {375 if ( $block->attributes && isset( $block->attributes['kind'] ) && 'post-type' === $block->attributes['kind'] && isset( $block->attributes['id'] ) ) { 381 376 $post_ids[] = $block->attributes['id']; 382 377 } … … 430 425 if ( $should_load_view_script ) { 431 426 wp_enqueue_script( 'wp-block-navigation-view' ); 427 } 428 429 $should_load_modal_view_script = isset( $attributes['overlayMenu'] ) && 'never' !== $attributes['overlayMenu']; 430 if ( $should_load_modal_view_script ) { 431 wp_enqueue_script( 'wp-block-navigation-view-modal' ); 432 432 } 433 433 … … 468 468 } 469 469 470 $nav_menu_name = $navigation_post->post_title; 471 472 if ( isset( $seen_menu_names[ $nav_menu_name ] ) ) { 473 ++$seen_menu_names[ $nav_menu_name ]; 474 } else { 475 $seen_menu_names[ $nav_menu_name ] = 1; 476 } 477 478 $parsed_blocks = parse_blocks( $navigation_post->post_content ); 479 480 // 'parse_blocks' includes a null block with '\n\n' as the content when 481 // it encounters whitespace. This code strips it. 482 $compacted_blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks ); 483 484 // TODO - this uses the full navigation block attributes for the 485 // context which could be refined. 486 $inner_blocks = new WP_Block_List( $compacted_blocks, $attributes ); 470 // Only published posts are valid. If this is changed then a corresponding change 471 // must also be implemented in `use-navigation-menu.js`. 472 if ( 'publish' === $navigation_post->post_status ) { 473 $nav_menu_name = $navigation_post->post_title; 474 475 if ( isset( $seen_menu_names[ $nav_menu_name ] ) ) { 476 ++$seen_menu_names[ $nav_menu_name ]; 477 } else { 478 $seen_menu_names[ $nav_menu_name ] = 1; 479 } 480 481 $parsed_blocks = parse_blocks( $navigation_post->post_content ); 482 483 // 'parse_blocks' includes a null block with '\n\n' as the content when 484 // it encounters whitespace. This code strips it. 485 $compacted_blocks = block_core_navigation_filter_out_empty_blocks( $parsed_blocks ); 486 487 // TODO - this uses the full navigation block attributes for the 488 // context which could be refined. 489 $inner_blocks = new WP_Block_List( $compacted_blocks, $attributes ); 490 } 487 491 } 488 492 … … 499 503 500 504 $inner_blocks = new WP_Block_List( $fallback_blocks, $attributes ); 501 502 } 505 } 506 507 /** 508 * Filter navigation block $inner_blocks. 509 * Allows modification of a navigation block menu items. 510 * 511 * @since 6.1.0 512 * 513 * @param \WP_Block_List $inner_blocks 514 */ 515 $inner_blocks = apply_filters( 'block_core_navigation_render_inner_blocks', $inner_blocks ); 503 516 504 517 $layout_justification = array( … … 553 566 $inner_blocks_html .= '</ul>'; 554 567 } 555 if ( 'core/site-title' === $inner_block->name || 'core/site-logo' === $inner_block->name ) { 556 $inner_blocks_html .= '<li class="wp-block-navigation-item">' . $inner_block->render() . '</li>'; 568 $inner_block_content = $inner_block->render(); 569 if ( 'core/site-title' === $inner_block->name || ( 'core/site-logo' === $inner_block->name && $inner_block_content ) ) { 570 $inner_blocks_html .= '<li class="wp-block-navigation-item">' . $inner_block_content . '</li>'; 557 571 } else { 558 $inner_blocks_html .= $inner_block ->render();572 $inner_blocks_html .= $inner_block_content; 559 573 } 560 574 } … … 605 619 ); 606 620 621 $should_display_icon_label = isset( $attributes['hasIcon'] ) && true === $attributes['hasIcon']; 607 622 $toggle_button_icon = '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" aria-hidden="true" focusable="false"><rect x="4" y="7.5" width="16" height="1.5" /><rect x="4" y="15" width="16" height="1.5" /></svg>'; 608 $should_display_icon_label = isset( $attributes['hasIcon'] ) && true === $attributes['hasIcon']; 609 $toggle_button_content = $should_display_icon_label ? $toggle_button_icon : 'Menu'; 623 if ( isset( $attributes['icon'] ) ) { 624 if ( 'menu' === $attributes['icon'] ) { 625 $toggle_button_icon = '<svg width="24" height="24" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M5 5v1.5h14V5H5zm0 7.8h14v-1.5H5v1.5zM5 19h14v-1.5H5V19z" /></svg>'; 626 } 627 } 628 $toggle_button_content = $should_display_icon_label ? $toggle_button_icon : __( 'Menu' ); 629 $toggle_close_button_icon = '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg>'; 630 $toggle_close_button_content = $should_display_icon_label ? $toggle_close_button_icon : __( 'Close' ); 631 $toggle_aria_label_open = $should_display_icon_label ? 'aria-label="' . __( 'Open menu' ) . '"' : ''; // Open button label. 632 $toggle_aria_label_close = $should_display_icon_label ? 'aria-label="' . __( 'Close menu' ) . '"' : ''; // Close button label. 610 633 611 634 $responsive_container_markup = sprintf( 612 '<button aria-haspopup="true" aria-label="%3$s"class="%6$s" data-micromodal-trigger="%1$s">%9$s</button>635 '<button aria-haspopup="true" %3$s class="%6$s" data-micromodal-trigger="%1$s">%9$s</button> 613 636 <div class="%5$s" style="%7$s" id="%1$s"> 614 637 <div class="wp-block-navigation__responsive-close" tabindex="-1" data-micromodal-close> 615 638 <div class="wp-block-navigation__responsive-dialog" aria-label="%8$s"> 616 <button aria-label="%4$s" data-micromodal-close class="wp-block-navigation__responsive-container-close"><svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="24" height="24" aria-hidden="true" focusable="false"><path d="M13 11.8l6.1-6.3-1-1-6.1 6.2-6.1-6.2-1 1 6.1 6.3-6.5 6.7 1 1 6.5-6.6 6.5 6.6 1-1z"></path></svg></button>639 <button %4$s data-micromodal-close class="wp-block-navigation__responsive-container-close">%10$s</button> 617 640 <div class="wp-block-navigation__responsive-container-content" id="%1$s-content"> 618 641 %2$s … … 623 646 esc_attr( $modal_unique_id ), 624 647 $inner_blocks_html, 625 __( 'Open menu' ), // Open button label.626 __( 'Close menu' ), // Close button label.648 $toggle_aria_label_open, 649 $toggle_aria_label_close, 627 650 esc_attr( implode( ' ', $responsive_container_classes ) ), 628 651 esc_attr( implode( ' ', $open_button_classes ) ), 629 652 safecss_filter_attr( $colors['overlay_inline_styles'] ), 630 653 __( 'Menu' ), 631 $toggle_button_content 654 $toggle_button_content, 655 $toggle_close_button_content 632 656 ); 633 657
Note: See TracChangeset
for help on using the changeset viewer.