Skip to content

Commit

Permalink
Add command for publish post types
Browse files Browse the repository at this point in the history
  • Loading branch information
benazeer-ben committed Nov 7, 2024
1 parent 7874fc5 commit acca029
Showing 1 changed file with 30 additions and 4 deletions.
34 changes: 30 additions & 4 deletions packages/editor/src/components/commands/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* WordPress dependencies
*/
import { useSelect, useDispatch } from '@wordpress/data';
import { __, isRTL } from '@wordpress/i18n';
import { useSelect, useDispatch, dispatch } from '@wordpress/data';
import { __, isRTL, sprintf } from '@wordpress/i18n';
import {
blockDefault,
code,
Expand All @@ -14,6 +14,7 @@ import {
external,
keyboard,
symbol,
check,
} from '@wordpress/icons';
import { useCommandLoader } from '@wordpress/commands';
import { store as preferencesStore } from '@wordpress/preferences';
Expand Down Expand Up @@ -269,10 +270,16 @@ const getEditorCommandLoader = () =>

const getEditedEntityContextualCommands = () =>
function useEditedEntityContextualCommands() {
const { postType } = useSelect( ( select ) => {
const { getCurrentPostType } = select( editorStore );
const { postType, isViewable, status } = useSelect( ( select ) => {
const { getCurrentPostType, getEditedPostAttribute } =
select( editorStore );
const { getPostType } = select( coreStore );

return {
postType: getCurrentPostType(),
isViewable:
getPostType( getCurrentPostType() )?.viewable ?? false,
status: getEditedPostAttribute( 'status' ),
};
}, [] );
const { openModal } = useDispatch( interfaceStore );
Expand All @@ -299,6 +306,25 @@ const getEditedEntityContextualCommands = () =>
} );
}

if ( postType !== 'page' && status !== 'publish' && isViewable ) {
commands.push( {
name: 'core/publish-' + postType,
label: sprintf(
/* translators: %s: Post type name (e.g., "Page", "Product") */
__( 'Publish %s' ),
postType.charAt( 0 ).toUpperCase() + postType.slice( 1 )
),
scope: 'core/edit-post',
icon: check,
callback: async ( { close } ) => {
close();
await dispatch( editorStore ).editPost( {
status: 'publish',
} );
await dispatch( editorStore ).savePost();
},
} );
}
return { isLoading: false, commands };
};

Expand Down

0 comments on commit acca029

Please sign in to comment.