Changeset 52324 for trunk/src/wp-includes/blocks/navigation.php
- Timestamp:
- 12/06/2021 06:18:27 AM (4 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/blocks/navigation.php
r52277 r52324 5 5 * @package WordPress 6 6 */ 7 8 // These functions are used for the __unstableLocation feature and only active 9 // when the gutenberg plugin is active. 10 if ( defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN ) { 11 /** 12 * Returns the menu items for a WordPress menu location. 13 * 14 * @param string $location The menu location. 15 * @return array Menu items for the location. 16 */ 17 function block_core_navigation_get_menu_items_at_location( $location ) { 18 if ( empty( $location ) ) { 19 return; 20 } 21 22 // Build menu data. The following approximates the code in 23 // `wp_nav_menu()` and `gutenberg_output_block_nav_menu`. 24 25 // Find the location in the list of locations, returning early if the 26 // location can't be found. 27 $locations = get_nav_menu_locations(); 28 if ( ! isset( $locations[ $location ] ) ) { 29 return; 30 } 31 32 // Get the menu from the location, returning early if there is no 33 // menu or there was an error. 34 $menu = wp_get_nav_menu_object( $locations[ $location ] ); 35 if ( ! $menu || is_wp_error( $menu ) ) { 36 return; 37 } 38 39 $menu_items = wp_get_nav_menu_items( $menu->term_id, array( 'update_post_term_cache' => false ) ); 40 _wp_menu_item_classes_by_context( $menu_items ); 41 42 return $menu_items; 43 } 44 45 46 /** 47 * Sorts a standard array of menu items into a nested structure keyed by the 48 * id of the parent menu. 49 * 50 * @param array $menu_items Menu items to sort. 51 * @return array An array keyed by the id of the parent menu where each element 52 * is an array of menu items that belong to that parent. 53 */ 54 function block_core_navigation_sort_menu_items_by_parent_id( $menu_items ) { 55 $sorted_menu_items = array(); 56 foreach ( (array) $menu_items as $menu_item ) { 57 $sorted_menu_items[ $menu_item->menu_order ] = $menu_item; 58 } 59 unset( $menu_items, $menu_item ); 60 61 $menu_items_by_parent_id = array(); 62 foreach ( $sorted_menu_items as $menu_item ) { 63 $menu_items_by_parent_id[ $menu_item->menu_item_parent ][] = $menu_item; 64 } 65 66 return $menu_items_by_parent_id; 67 } 68 69 /** 70 * Turns menu item data into a nested array of parsed blocks 71 * 72 * @param array $menu_items An array of menu items that represent 73 * an individual level of a menu. 74 * @param array $menu_items_by_parent_id An array keyed by the id of the 75 * parent menu where each element is an 76 * array of menu items that belong to 77 * that parent. 78 * @return array An array of parsed block data. 79 */ 80 function block_core_navigation_parse_blocks_from_menu_items( $menu_items, $menu_items_by_parent_id ) { 81 if ( empty( $menu_items ) ) { 82 return array(); 83 } 84 85 $blocks = array(); 86 87 foreach ( $menu_items as $menu_item ) { 88 $class_name = ! empty( $menu_item->classes ) ? implode( ' ', (array) $menu_item->classes ) : null; 89 $id = ( null !== $menu_item->object_id && 'custom' !== $menu_item->object ) ? $menu_item->object_id : null; 90 $opens_in_new_tab = null !== $menu_item->target && '_blank' === $menu_item->target; 91 $rel = ( null !== $menu_item->xfn && '' !== $menu_item->xfn ) ? $menu_item->xfn : null; 92 $kind = null !== $menu_item->type ? str_replace( '_', '-', $menu_item->type ) : 'custom'; 93 94 $block = array( 95 'blockName' => isset( $menu_items_by_parent_id[ $menu_item->ID ] ) ? 'core/navigation-submenu' : 'core/navigation-link', 96 'attrs' => array( 97 'className' => $class_name, 98 'description' => $menu_item->description, 99 'id' => $id, 100 'kind' => $kind, 101 'label' => $menu_item->title, 102 'opensInNewTab' => $opens_in_new_tab, 103 'rel' => $rel, 104 'title' => $menu_item->attr_title, 105 'type' => $menu_item->object, 106 'url' => $menu_item->url, 107 ), 108 ); 109 110 $block['innerBlocks'] = isset( $menu_items_by_parent_id[ $menu_item->ID ] ) 111 ? block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[ $menu_item->ID ], $menu_items_by_parent_id ) 112 : array(); 113 $block['innerContent'] = array_map( 'serialize_block', $block['innerBlocks'] ); 114 115 $blocks[] = $block; 116 } 117 118 return $blocks; 119 } 120 } 7 121 8 122 /** … … 146 260 // - https://github.com/WordPress/wordpress-develop/blob/ba943e113d3b31b121f77a2d30aebe14b047c69d/src/wp-includes/nav-menu.php#L613-L619. 147 261 // - https://developer.wordpress.org/reference/classes/wp_query/#order-orderby-parameters. 148 $navigation_posts = get_posts( 149 array( 150 'post_type' => 'wp_navigation', 151 'order' => 'ASC', 152 'orderby' => 'name', 153 'posts_per_page' => 1, // only the first post. 154 's' => '<!-- wp:', // look for block indicators to ensure we only include non-empty Navigations. 155 ) 156 ); 157 return count( $navigation_posts ) ? $navigation_posts[0] : null; 158 262 $parsed_args = array( 263 'post_type' => 'wp_navigation', 264 'no_found_rows' => true, 265 'order' => 'ASC', 266 'orderby' => 'name', 267 '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 } 276 } 277 278 return null; 159 279 } 160 280 … … 273 393 $inner_blocks = $block->inner_blocks; 274 394 275 // If `__unstableLocation` is defined, create inner blocks from the classic menu assigned to that location.276 if ( empty( $inner_blocks ) && array_key_exists( '__unstableLocation', $attributes ) ) {277 $menu_items = gutenberg_get_menu_items_at_location( $attributes['__unstableLocation'] );278 if ( empty( $menu_items ) ) {279 return '';280 }281 282 $menu_items_by_parent_id = gutenberg_sort_menu_items_by_parent_id( $menu_items );283 $parsed_blocks = gutenberg_parse_blocks_from_menu_items( $menu_items_by_parent_id[0], $menu_items_by_parent_id );284 $inner_blocks = new WP_Block_List( $parsed_blocks, $attributes );285 }286 287 395 // Ensure that blocks saved with the legacy ref attribute name (navigationMenuId) continue to render. 288 396 if ( array_key_exists( 'navigationMenuId', $attributes ) ) { 289 397 $attributes['ref'] = $attributes['navigationMenuId']; 290 398 } 399 400 // If: 401 // - the gutenberg plugin is active 402 // - `__unstableLocation` is defined 403 // - we have menu items at the defined location 404 // - we don't have a relationship to a `wp_navigation` Post (via `ref`). 405 // ...then create inner blocks from the classic menu assigned to that location. 406 if ( 407 defined( 'IS_GUTENBERG_PLUGIN' ) && IS_GUTENBERG_PLUGIN && 408 array_key_exists( '__unstableLocation', $attributes ) && 409 ! array_key_exists( 'ref', $attributes ) && 410 ! empty( block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] ) ) 411 ) { 412 $menu_items = block_core_navigation_get_menu_items_at_location( $attributes['__unstableLocation'] ); 413 if ( empty( $menu_items ) ) { 414 return ''; 415 } 416 417 $menu_items_by_parent_id = block_core_navigation_sort_menu_items_by_parent_id( $menu_items ); 418 $parsed_blocks = block_core_navigation_parse_blocks_from_menu_items( $menu_items_by_parent_id[0], $menu_items_by_parent_id ); 419 $inner_blocks = new WP_Block_List( $parsed_blocks, $attributes ); 420 } 421 291 422 // Load inner blocks from the navigation post. 292 423 if ( array_key_exists( 'ref', $attributes ) ) {
Note: See TracChangeset
for help on using the changeset viewer.