Skip to content
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

Command for save, delete and duplicate post types #66834

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 93 additions & 7 deletions packages/editor/src/components/commands/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { useSelect, useDispatch, dispatch } from '@wordpress/data';
import { __, isRTL, sprintf } from '@wordpress/i18n';
import {
blockDefault,
Expand All @@ -14,6 +14,8 @@ import {
external,
keyboard,
symbol,
trash,
update,
page,
layout,
rotateRight,
Expand Down Expand Up @@ -280,12 +282,31 @@ const getEditorCommandLoader = () =>

const getEditedEntityContextualCommands = () =>
function useEditedEntityContextualCommands() {
const { postType } = useSelect( ( select ) => {
const { getCurrentPostType } = select( editorStore );
return {
postType: getCurrentPostType(),
};
}, [] );
const { postType, isViewable, status, currentPost } = useSelect(
( select ) => {
const {
getCurrentPostType,
getEditedPostAttribute,
getCurrentPostId,
} = select( editorStore );
const { getPostType, getEntityRecord } = select( coreStore );
const postId = getCurrentPostId();

return {
postType: getCurrentPostType(),
isViewable:
getPostType( getCurrentPostType() )?.viewable ?? false,
status: getEditedPostAttribute( 'status' ),
currentPost: getEntityRecord(
'postType',
getCurrentPostType(),
postId
),
};
},
[]
);
const { getCurrentPostId } = useSelect( editorStore );
const { openModal } = useDispatch( interfaceStore );
const commands = [];

Expand All @@ -310,6 +331,71 @@ const getEditedEntityContextualCommands = () =>
} );
}

if ( postType !== 'page' && status !== 'publish' && isViewable ) {
commands.push( {
name: 'core/save-' + postType,
label: sprintf(
/* translators: %s: Post type name (e.g., "Page", "Product") */
__( 'Save %s' ),
postType.charAt( 0 ).toUpperCase() + postType.slice( 1 )
),
scope: 'core/edit-post',
icon: update, // Save icon
callback: async ( { close } ) => {
close();
await dispatch( editorStore ).savePost(); // Only save without changing status
},
} );
commands.push( {
name: 'core/delete-' + postType,
label: sprintf(
/* translators: %s: Post type name (e.g., "Page", "Product") */
__( 'Delete %s' ),
postType.charAt( 0 ).toUpperCase() + postType.slice( 1 )
),
scope: 'core/edit-post',
icon: trash, // Trash icon for delete
callback: async ( { close } ) => {
close();
const postId = getCurrentPostId();
await dispatch( coreStore ).deleteEntityRecord(
'postType',
postType,
postId
);
},
} );
commands.push( {
name: 'core/duplicate-' + postType,
label: sprintf(
/* translators: %s: Post type name (e.g., "Page", "Product") */
__( 'Duplicate %s' ),
postType.charAt( 0 ).toUpperCase() + postType.slice( 1 )
),
scope: 'core/edit-post',
icon: page,
callback: async ( { close } ) => {
close();

const { title, content, excerpt } = currentPost;
const newTitle =
typeof title === 'object' && title.rendered
? title.rendered
: title;
const newPost = await dispatch(
coreStore
).saveEntityRecord( 'postType', postType, {
title: newTitle + ' (Copy)',
content,
excerpt,
status: 'draft',
} );
if ( newPost ) {
window.open( newPost.link, '_blank' );
}
},
} );
}
return { isLoading: false, commands };
};

Expand Down
Loading