Skip to content
Next Next commit
First pass at forking the add page command depending on context.
  • Loading branch information
ramonjd committed Sep 19, 2024
commit 0cdcd4f4e0f2ef3e6cad4988dfc48170daaf2dfe
78 changes: 71 additions & 7 deletions packages/core-commands/src/admin-navigation-commands.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,76 @@
/**
* WordPress dependencies
*/
import { useCommand } from '@wordpress/commands';
import { useCommand, useCommandLoader } from '@wordpress/commands';
import { __ } from '@wordpress/i18n';
import { plus } from '@wordpress/icons';
import { getPath } from '@wordpress/url';
import { store as coreStore } from '@wordpress/core-data';
import { useDispatch, useSelect } from '@wordpress/data';
import { useMemo, useCallback } from '@wordpress/element';

function useAddNewPageCommand() {
const isSiteEditor = getPath( window.location.href )?.includes(
'site-editor.php'
);
const { userCanCreatePages } = useSelect( ( select ) => {
const { canUser } = select( coreStore );
return {
userCanCreatePages: canUser( 'create', {
kind: 'postType',
name: 'page',
} ),
};
}, [] );
const { saveEntityRecord } = useDispatch( coreStore );
/**
* Creates a Post entity.
*
* @param {Object} options parameters for the post being created. These mirror those used on 3rd param of saveEntityRecord.
* @return {Object} the post type object that was created.
*/
const createPageEntity = useCallback(
( options ) => {
if ( ! userCanCreatePages ) {
return Promise.reject( {
message: __(
'You do not have permission to create Pages.'
),
} );
}
return saveEntityRecord( 'postType', 'page', options );
},
[ saveEntityRecord, userCanCreatePages ]
);

const commands = useMemo( () => {
if ( ! userCanCreatePages ) {
return [];
}
return [
{
name: 'core/add-new-page',
label: __( 'Add new page' ),
icon: plus,
callback: async () => {
if ( isSiteEditor ) {
const page = await createPageEntity( {
status: 'draft',
} );
document.location.href = `/wp-admin/site-editor.php?postId=${ page.id }&postType=page&canvas=edit`;
return;
}
document.location.href = 'post-new.php?post_type=page';
},
},
];
}, [ createPageEntity, isSiteEditor, userCanCreatePages ] );

return {
isLoading: false,
commands,
};
}

export function useAdminNavigationCommands() {
useCommand( {
Expand All @@ -14,12 +81,9 @@ export function useAdminNavigationCommands() {
document.location.href = 'post-new.php';
},
} );
useCommand( {

useCommandLoader( {
name: 'core/add-new-page',
label: __( 'Add new page' ),
icon: plus,
callback: () => {
document.location.href = 'post-new.php?post_type=page';
},
hook: useAddNewPageCommand,
} );
}