From acca0294f0783a2ab3ddbc4626fa5c255996822b Mon Sep 17 00:00:00 2001 From: benazeer-ben Date: Thu, 7 Nov 2024 17:48:10 +0530 Subject: [PATCH] Add command for publish post types --- .../editor/src/components/commands/index.js | 34 ++++++++++++++++--- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/packages/editor/src/components/commands/index.js b/packages/editor/src/components/commands/index.js index 16260bed3978fd..6818bb79aa5a61 100644 --- a/packages/editor/src/components/commands/index.js +++ b/packages/editor/src/components/commands/index.js @@ -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, @@ -14,6 +14,7 @@ import { external, keyboard, symbol, + check, } from '@wordpress/icons'; import { useCommandLoader } from '@wordpress/commands'; import { store as preferencesStore } from '@wordpress/preferences'; @@ -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 ); @@ -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 }; };