Skip to content

Commit

Permalink
Revert: Navigation: Always create a fallback menu
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed Feb 28, 2023
1 parent 8a0eede commit b3bc7a7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 117 deletions.
103 changes: 33 additions & 70 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import {
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import { createBlock, getBlockType } from '@wordpress/blocks';
import { createBlock } from '@wordpress/blocks';
import { close, Icon } from '@wordpress/icons';

/**
Expand Down Expand Up @@ -210,40 +210,6 @@ function Navigation( {
[ navigationMenus ]
);

// This useEffect adds snackbar and speak status notices when menus are created.
// If there are no fallback navigation menus then we don't show these messages,
// because this means that we are creating the first, fallback navigation menu.
useEffect( () => {
hideNavigationMenuStatusNotice();

if ( fallbackNavigationMenus && isCreatingNavigationMenu ) {
speak( __( `Creating Navigation Menu.` ) );
}

if ( createNavigationMenuIsSuccess ) {
handleUpdateMenu( createNavigationMenuPost.id, {
focusNavigationBlock: true,
} );

if ( fallbackNavigationMenus ) {
showNavigationMenuStatusNotice(
__( `Navigation Menu successfully created.` )
);
}
}

if ( createNavigationMenuIsError ) {
showNavigationMenuStatusNotice(
__( 'Failed to create Navigation Menu.' )
);
}
}, [
createNavigationMenuStatus,
createNavigationMenuError,
createNavigationMenuPost,
fallbackNavigationMenus,
] );

// Attempt to retrieve and prioritize any existing navigation menu unless:
// - the are uncontrolled inner blocks already present in the block.
// - the user is creating a new menu.
Expand Down Expand Up @@ -279,10 +245,10 @@ function Navigation( {

useEffect( () => {
if (
ref ||
! hasResolvedNavigationMenus ||
isConvertingClassicMenu ||
fallbackNavigationMenus?.length > 0
fallbackNavigationMenus?.length > 0 ||
! classicMenus?.length
) {
return;
}
Expand All @@ -291,41 +257,25 @@ function Navigation( {
// a classic menu with a `primary` location or slug,
// then create a new navigation menu based on it.
// Otherwise, use the most recently created classic menu.
if ( classicMenus?.length ) {
const primaryMenus = classicMenus.filter(
( classicMenu ) =>
classicMenu.locations.includes( 'primary' ) ||
classicMenu.slug === 'primary'
);
const primaryMenus = classicMenus.filter(
( classicMenu ) =>
classicMenu.locations.includes( 'primary' ) ||
classicMenu.slug === 'primary'
);

if ( primaryMenus.length ) {
convertClassicMenu(
primaryMenus[ 0 ].id,
primaryMenus[ 0 ].name,
'publish'
);
} else {
classicMenus.sort( ( a, b ) => {
return b.id - a.id;
} );
convertClassicMenu(
classicMenus[ 0 ].id,
classicMenus[ 0 ].name,
'publish'
);
}
if ( primaryMenus.length ) {
convertClassicMenu(
primaryMenus[ 0 ].id,
primaryMenus[ 0 ].name,
'publish'
);
} else {
// If there are no fallback navigation menus and no classic menus,
// then create a new navigation menu.

// Check that we have a page-list block type.
let defaultBlocks = [];
if ( getBlockType( 'core/page-list' ) ) {
defaultBlocks = [ createBlock( 'core/page-list' ) ];
}
createNavigationMenu(
'Navigation', // TODO - use the template slug in future
defaultBlocks,
classicMenus.sort( ( a, b ) => {
return b.id - a.id;
} );
convertClassicMenu(
classicMenus[ 0 ].id,
classicMenus[ 0 ].name,
'publish'
);
}
Expand All @@ -349,6 +299,19 @@ function Navigation( {
classicMenus?.length === 0 &&
! hasUncontrolledInnerBlocks;

useEffect( () => {
if ( isPlaceholder ) {
/**
* this fallback only displays (both in editor and on front)
* the list of pages block if no menu is available as a fallback.
* We don't want the fallback to request a save,
* nor to be undoable, hence we mark it non persistent.
*/
__unstableMarkNextChangeAsNotPersistent();
replaceInnerBlocks( clientId, [ createBlock( 'core/page-list' ) ] );
}
}, [ clientId, isPlaceholder, ref ] );

const isEntityAvailable =
! isNavigationMenuMissing && isNavigationMenuResolved;

Expand Down
55 changes: 17 additions & 38 deletions packages/block-library/src/navigation/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -431,37 +431,6 @@ function block_core_navigation_block_contains_core_navigation( $inner_blocks ) {
return false;
}

/**
* Create and returns a navigation menu containing a page-list as a fallback.
*
* @return array the newly created navigation menu.
*/
function block_core_navigation_get_default_pages_fallback() {
$registry = WP_Block_Type_Registry::get_instance();

// If `core/page-list` is not registered then use empty blocks.
$default_blocks = $registry->is_registered( 'core/page-list' ) ? '<!-- wp:page-list /-->' : '';

// Create a new navigation menu from the fallback blocks.
$wp_insert_post_result = wp_insert_post(
array(
'post_content' => $default_blocks,
'post_title' => 'Navigation', // TODO - use the template slug in future.
'post_name' => 'Navigation', // TODO - use the template slug in future.
'post_status' => 'publish',
'post_type' => 'wp_navigation',
),
true // So that we can check whether the result is an error.
);

if ( is_wp_error( $wp_insert_post_result ) ) {
return;
}

// Fetch the most recently published navigation which will be the default one created above.
return block_core_navigation_get_most_recently_published_navigation();
}

/**
* Retrieves the appropriate fallback to be used on the front of the
* site when there is no menu assigned to the Nav block.
Expand All @@ -472,7 +441,18 @@ function block_core_navigation_get_default_pages_fallback() {
* @return array the array of blocks to be used as a fallback.
*/
function block_core_navigation_get_fallback_blocks() {
// Get the most recently published Navigation post.
$page_list_fallback = array(
array(
'blockName' => 'core/page-list',
),
);

$registry = WP_Block_Type_Registry::get_instance();

// If `core/page-list` is not registered then return empty blocks.
$fallback_blocks = $registry->is_registered( 'core/page-list' ) ? $page_list_fallback : array();

// Default to a list of Pages.
$navigation_post = block_core_navigation_get_most_recently_published_navigation();

// If there are no navigation posts then try to find a classic menu
Expand All @@ -481,15 +461,14 @@ function block_core_navigation_get_fallback_blocks() {
$navigation_post = block_core_navigation_maybe_use_classic_menu_fallback();
}

// If there are no navigation posts then default to a list of Pages.
if ( ! $navigation_post ) {
$navigation_post = block_core_navigation_get_default_pages_fallback();
}

// Use the first non-empty Navigation as fallback, there should always be one.
// Use the first non-empty Navigation as fallback if available.
if ( $navigation_post ) {
$parsed_blocks = parse_blocks( $navigation_post->post_content );
$maybe_fallback = block_core_navigation_filter_out_empty_blocks( $parsed_blocks );

// Normalizing blocks may result in an empty array of blocks if they were all `null` blocks.
// In this case default to the (Page List) fallback.
$fallback_blocks = ! empty( $maybe_fallback ) ? $maybe_fallback : $fallback_blocks;
}

// Normalizing blocks may result in an empty array of blocks if they were all `null` blocks.
Expand Down
15 changes: 6 additions & 9 deletions test/e2e/specs/editor/blocks/navigation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ test.describe(
test( 'default to a list of pages if there are no menus', async ( {
admin,
editor,
requestUtils,
} ) => {
await admin.createNewPost();
await editor.insertBlock( { name: 'core/navigation' } );
Expand All @@ -48,14 +47,12 @@ test.describe(

// Check the markup of the block is correct.
await editor.publishPost();
const navigationMenus = await requestUtils.getNavigationMenus();
const latestNavigationMenu = navigationMenus[ 0 ];
await expect.poll( editor.getBlocks ).toMatchObject( [
{
name: 'core/navigation',
attributes: { ref: latestNavigationMenu.id },
},
] );
const content = await editor.getEditedPostContent();
expect( content ).toBe(
`<!-- wp:navigation -->
<!-- wp:page-list /-->
<!-- /wp:navigation -->`
);
} );

test( 'default to my only existing menu', async ( {
Expand Down

0 comments on commit b3bc7a7

Please sign in to comment.