Changeset 57578 for trunk/src/wp-includes/blocks/navigation.php
- Timestamp:
- 02/09/2024 06:20:12 PM (9 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/navigation.php
r57499 r57578 10 10 */ 11 11 class WP_Navigation_Block_Renderer { 12 /** 13 * Used to determine which blocks are wrapped in an <li>. 14 * 15 * @var array 16 */ 17 private static $nav_blocks_wrapped_in_list_item = array( 18 'core/navigation-link', 19 'core/home-link', 20 'core/site-title', 21 'core/site-logo', 22 'core/navigation-submenu', 23 ); 12 13 /** 14 * Used to determine whether or not a navigation has submenus. 15 */ 16 private static $has_submenus = false; 24 17 25 18 /** … … 59 52 * 60 53 * @param WP_Block_List $inner_blocks The list of inner blocks. 61 * @return bool Returns whether or not a navigation has a submenu .54 * @return bool Returns whether or not a navigation has a submenu and also sets the member variable. 62 55 */ 63 56 private static function has_submenus( $inner_blocks ) { 57 if ( true === static::$has_submenus ) { 58 return static::$has_submenus; 59 } 60 64 61 foreach ( $inner_blocks as $inner_block ) { 65 $inner_block_content = $inner_block->render(); 66 $p = new WP_HTML_Tag_Processor( $inner_block_content ); 67 if ( $p->next_tag( 68 array( 69 'name' => 'LI', 70 'class_name' => 'has-child', 71 ) 72 ) ) { 73 return true; 62 // If this is a page list then work out if any of the pages have children. 63 if ( 'core/page-list' === $inner_block->name ) { 64 $all_pages = get_pages( 65 array( 66 'sort_column' => 'menu_order,post_title', 67 'order' => 'asc', 68 ) 69 ); 70 foreach ( (array) $all_pages as $page ) { 71 if ( $page->post_parent ) { 72 static::$has_submenus = true; 73 break; 74 } 75 } 74 76 } 75 } 76 return false; 77 // If this is a navigation submenu then we know we have submenus. 78 if ( 'core/navigation-submenu' === $inner_block->name ) { 79 static::$has_submenus = true; 80 break; 81 } 82 } 83 84 return static::$has_submenus; 77 85 } 78 86 … … 97 105 */ 98 106 private static function does_block_need_a_list_item_wrapper( $block ) { 99 return in_array( $block->name, static::$needs_list_item_wrapper, true ); 107 108 /** 109 * Filter the list of blocks that need a list item wrapper. 110 * 111 * Affords the ability to customize which blocks need a list item wrapper when rendered 112 * within a core/navigation block. 113 * This is useful for blocks that are not list items but should be wrapped in a list 114 * item when used as a child of a navigation block. 115 * 116 * @since 6.5.0 117 * 118 * @param array $needs_list_item_wrapper The list of blocks that need a list item wrapper. 119 * @return array The list of blocks that need a list item wrapper. 120 */ 121 $needs_list_item_wrapper = apply_filters( 'block_core_navigation_listable_blocks', static::$needs_list_item_wrapper ); 122 123 return in_array( $block->name, $needs_list_item_wrapper, true ); 100 124 } 101 125 … … 141 165 142 166 foreach ( $inner_blocks as $inner_block ) { 143 $is_list_item = in_array( $inner_block->name, static::$nav_blocks_wrapped_in_list_item, true ); 167 $inner_block_markup = static::get_markup_for_inner_block( $inner_block ); 168 $p = new WP_HTML_Tag_Processor( $inner_block_markup ); 169 $is_list_item = $p->next_tag( 'LI' ); 144 170 145 171 if ( $is_list_item && ! $is_list_open ) { … … 156 182 } 157 183 158 $inner_blocks_html .= static::get_markup_for_inner_block( $inner_block );184 $inner_blocks_html .= $inner_block_markup; 159 185 } 160 186 … … 568 594 private static function handle_view_script_module_loading( $attributes, $block, $inner_blocks ) { 569 595 if ( static::is_interactive( $attributes, $inner_blocks ) ) { 596 $suffix = wp_scripts_get_suffix(); 597 if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) { 598 $module_url = gutenberg_url( '/build/interactivity/navigation.min.js' ); 599 } 600 601 wp_register_script_module( 602 '@wordpress/block-library/navigation', 603 isset( $module_url ) ? $module_url : includes_url( "blocks/navigation/view{$suffix}.js" ), 604 array( '@wordpress/interactivity' ), 605 defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' ) 606 ); 570 607 wp_enqueue_script_module( '@wordpress/block-library/navigation' ); 571 608 } … … 969 1006 $page_list_fallback = array( 970 1007 array( 971 'blockName' => 'core/page-list', 1008 'blockName' => 'core/page-list', 1009 'innerContent' => array(), 1010 'attrs' => array(), 972 1011 ), 973 1012 ); … … 977 1016 // If `core/page-list` is not registered then return empty blocks. 978 1017 $fallback_blocks = $registry->is_registered( 'core/page-list' ) ? $page_list_fallback : array(); 979 980 if ( class_exists( 'WP_Navigation_Fallback' ) ) { 981 $navigation_post = WP_Navigation_Fallback::get_fallback(); 982 } else { 983 $navigation_post = Gutenberg_Navigation_Fallback::get_fallback(); 984 } 1018 $navigation_post = WP_Navigation_Fallback::get_fallback(); 985 1019 986 1020 // Use the first non-empty Navigation as fallback if available. … … 1079 1113 'render_callback' => 'render_block_core_navigation', 1080 1114 ) 1081 );1082 1083 wp_register_script_module(1084 '@wordpress/block-library/navigation',1085 defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ? gutenberg_url( '/build/interactivity/navigation.min.js' ) : includes_url( 'blocks/navigation/view.min.js' ),1086 array( '@wordpress/interactivity' ),1087 defined( 'GUTENBERG_VERSION' ) ? GUTENBERG_VERSION : get_bloginfo( 'version' )1088 1115 ); 1089 1116 } … … 1419 1446 } 1420 1447 1448 // Before adding our filter, we verify if it's already added in Core. 1449 // However, during the build process, Gutenberg automatically prefixes our functions with "gutenberg_". 1450 // Therefore, we concatenate the Core's function name to circumvent this prefix for our check. 1451 $rest_insert_wp_navigation_core_callback = 'block_core_navigation_' . 'update_ignore_hooked_blocks_meta'; 1452 1421 1453 // Injection of hooked blocks into the Navigation block relies on some functions present in WP >= 6.5 1422 1454 // that are not present in Gutenberg's WP 6.5 compatibility layer. 1423 if ( function_exists( 'get_hooked_block_markup' ) ) {1455 if ( function_exists( 'get_hooked_block_markup' ) && ! has_filter( 'rest_insert_wp_navigation', $rest_insert_wp_navigation_core_callback ) ) { 1424 1456 add_action( 'rest_insert_wp_navigation', 'block_core_navigation_update_ignore_hooked_blocks_meta', 10, 3 ); 1425 1457 } … … 1451 1483 } 1452 1484 1485 // Before adding our filter, we verify if it's already added in Core. 1486 // However, during the build process, Gutenberg automatically prefixes our functions with "gutenberg_". 1487 // Therefore, we concatenate the Core's function name to circumvent this prefix for our check. 1488 $rest_prepare_wp_navigation_core_callback = 'block_core_navigation_' . 'insert_hooked_blocks_into_rest_response'; 1489 1453 1490 // Injection of hooked blocks into the Navigation block relies on some functions present in WP >= 6.5 1454 1491 // that are not present in Gutenberg's WP 6.5 compatibility layer. 1455 if ( function_exists( 'get_hooked_block_markup' ) ) {1492 if ( function_exists( 'get_hooked_block_markup' ) && ! has_filter( 'rest_prepare_wp_navigation', $rest_prepare_wp_navigation_core_callback ) ) { 1456 1493 add_filter( 'rest_prepare_wp_navigation', 'block_core_navigation_insert_hooked_blocks_into_rest_response', 10, 3 ); 1457 1494 }
Note: See TracChangeset
for help on using the changeset viewer.