-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Navigation: Allow Search block to be added alongside links #22656
Changes from all commits
c8d3bcc
50d49b7
c215042
5d402fb
8a5bfb5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ import { v4 as uuid } from 'uuid'; | |
* WordPress dependencies | ||
*/ | ||
import { __ } from '@wordpress/i18n'; | ||
import { serialize } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -167,7 +168,7 @@ function* batchSave( menuId, menuItemsByClientId, navigationBlock ) { | |
function computeCustomizedAttribute( blocks, menuId, menuItemsByClientId ) { | ||
const blocksList = blocksTreeToFlatList( blocks ); | ||
const dataList = blocksList.map( ( { block, parentId, position } ) => | ||
linkBlockToRequestItem( block, parentId, position ) | ||
blockToRequestItem( block, parentId, position ) | ||
); | ||
|
||
// Create an object like { "nav_menu_item[12]": {...}} } | ||
|
@@ -195,14 +196,29 @@ function computeCustomizedAttribute( blocks, menuId, menuItemsByClientId ) { | |
); | ||
} | ||
|
||
function linkBlockToRequestItem( block, parentId, position ) { | ||
function blockToRequestItem( block, parentId, position ) { | ||
const menuItem = omit( getMenuItemForBlock( block ), 'menus', 'meta' ); | ||
|
||
let attributes; | ||
|
||
if ( block.name === 'core/navigation-link' ) { | ||
attributes = { | ||
type: 'custom', | ||
title: block.attributes?.label, | ||
original_title: '', | ||
url: block.attributes.url, | ||
}; | ||
} else { | ||
attributes = { | ||
type: 'html', | ||
content: serialize( block ), | ||
}; | ||
} | ||
Comment on lines
+204
to
+216
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's totally too early for that, but a future refactor into a factory-like structure for transform/reverse transform could be a good idea to avoid a huge if/else mess. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
return { | ||
...menuItem, | ||
...attributes, | ||
position, | ||
title: block.attributes?.label, | ||
url: block.attributes.url, | ||
original_title: '', | ||
classes: ( menuItem.classes || [] ).join( ' ' ), | ||
xfn: ( menuItem.xfn || [] ).join( ' ' ), | ||
nav_menu_term_id: menuId, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ import { groupBy, sortBy } from 'lodash'; | |
/** | ||
* WordPress dependencies | ||
*/ | ||
import { createBlock } from '@wordpress/blocks'; | ||
import { parse, createBlock } from '@wordpress/blocks'; | ||
|
||
/** | ||
* Internal dependencies | ||
|
@@ -99,23 +99,32 @@ function createNavigationBlock( menuItems ) { | |
itemsByParentID[ item.id ] | ||
); | ||
} | ||
const linkBlock = convertMenuItemToLinkBlock( | ||
item, | ||
menuItemInnerBlocks | ||
); | ||
menuItemIdToClientId[ item.id ] = linkBlock.clientId; | ||
innerBlocks.push( linkBlock ); | ||
const block = convertMenuItemToBlock( item, menuItemInnerBlocks ); | ||
menuItemIdToClientId[ item.id ] = block.clientId; | ||
innerBlocks.push( block ); | ||
} | ||
return innerBlocks; | ||
}; | ||
|
||
// menuItemsToTreeOfLinkBlocks takes an array of top-level menu items and recursively creates all their innerBlocks | ||
// menuItemsToTreeOfBlocks takes an array of top-level menu items and recursively creates all their innerBlocks | ||
const innerBlocks = menuItemsToTreeOfBlocks( itemsByParentID[ 0 ] || [] ); | ||
const navigationBlock = createBlock( 'core/navigation', {}, innerBlocks ); | ||
return [ navigationBlock, menuItemIdToClientId ]; | ||
} | ||
|
||
function convertMenuItemToLinkBlock( menuItem, innerBlocks = [] ) { | ||
function convertMenuItemToBlock( menuItem, innerBlocks = [] ) { | ||
if ( menuItem.type === 'html' ) { | ||
const parsedBlocks = parse( menuItem.content.raw ); | ||
|
||
if ( parsedBlocks.length !== 1 ) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This will kick in once a block plugin gets disabled. I don't think there's a good way of addressing it, but just wanted to double check - is this behavior consistent with the post editor? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If a block plugin is disabled then The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I put it on my todo list to come back to this as will need to think on it further. |
||
return createBlock( 'core/freeform', { | ||
originalContent: menuItem.content.raw, | ||
} ); | ||
} | ||
|
||
return parsedBlocks[ 0 ]; | ||
} | ||
|
||
const attributes = { | ||
label: menuItem.title.rendered, | ||
url: menuItem.url, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a blocker but just flagging this means plenty of DB roundtrips if the object cache happens to be disabled.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's pretty par the course for
wp_setup_nav_menu_item()
which is where this code will eventually live.https://github.com/WordPress/wordpress-develop/blob/master/src/wp-includes/nav-menu.php#L806