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

Add duplicate post action #60637

Merged
merged 6 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
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
62 changes: 52 additions & 10 deletions packages/edit-post/src/components/sidebar/settings-sidebar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
useEffect,
useRef,
} from '@wordpress/element';
import { isRTL, __ } from '@wordpress/i18n';
import { isRTL, __, sprintf } from '@wordpress/i18n';
import { drawerLeft, drawerRight } from '@wordpress/icons';
import { store as keyboardShortcutsStore } from '@wordpress/keyboard-shortcuts';
import {
Expand All @@ -28,6 +28,7 @@ import {
privateApis as editorPrivateApis,
} from '@wordpress/editor';
import { addQueryArgs } from '@wordpress/url';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand All @@ -54,15 +55,6 @@ export const sidebars = {
block: 'edit-post/block',
};

function onActionPerformed( actionId, items ) {
if ( actionId === 'move-to-trash' ) {
const postType = items[ 0 ].type;
document.location.href = addQueryArgs( 'edit.php', {
post_type: postType,
} );
}
}

const SidebarContent = ( { tabName, keyboardShortcut, isEditingTemplate } ) => {
const tabListRef = useRef( null );
// Because `PluginSidebar` renders a `ComplementaryArea`, we
Expand Down Expand Up @@ -96,6 +88,56 @@ const SidebarContent = ( { tabName, keyboardShortcut, isEditingTemplate } ) => {
selectedTabElement?.focus();
}
}, [ tabName ] );
const { createSuccessNotice } = useDispatch( noticesStore );

const onActionPerformed = useCallback(
( actionId, items ) => {
switch ( actionId ) {
case 'move-to-trash':
{
const postType = items[ 0 ].type;
document.location.href = addQueryArgs( 'edit.php', {
post_type: postType,
} );
}
break;
case 'duplicate-post':
{
const newItem = items[ 0 ];
const title =
typeof newItem.title === 'string'
? newItem.title
: newItem.title?.rendered;
createSuccessNotice(
sprintf(
// translators: %s: Title of the created post e.g: "Post 1".
__( '"%s" successfully created.' ),
title
),
{
type: 'snackbar',
id: 'duplicate-post-action',
actions: [
{
label: __( 'Edit' ),
onClick: () => {
const postId = newItem.id;
document.location.href =
addQueryArgs( 'post.php', {
post: postId,
action: 'edit',
} );
},
},
],
}
);
}
break;
}
},
[ createSuccessNotice ]
);

return (
<PluginSidebar
Expand Down
1 change: 1 addition & 0 deletions packages/edit-site/src/components/page-pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ const PAGE_ACTIONS = [
'restore',
'permanently-delete',
'view-post-revisions',
'duplicate-post',
'rename-post',
'move-to-trash',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* WordPress dependencies
*/
import { PanelBody } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useSelect } from '@wordpress/data';
import { __, sprintf } from '@wordpress/i18n';
import { useSelect, useDispatch } from '@wordpress/data';
import { store as coreStore } from '@wordpress/core-data';
import {
PluginDocumentSettingPanel,
Expand All @@ -12,6 +12,7 @@ import {
} from '@wordpress/editor';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { useCallback } from '@wordpress/element';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand Down Expand Up @@ -48,20 +49,57 @@ export default function PagePanels() {
renderingMode: getRenderingMode(),
};
}, [] );

const { createSuccessNotice } = useDispatch( noticesStore );
const history = useHistory();
const onActionPerformed = useCallback(
( actionId, items ) => {
if ( actionId === 'move-to-trash' ) {
history.push( {
path: '/' + items[ 0 ].type,
postId: undefined,
postType: undefined,
canvas: 'view',
} );
switch ( actionId ) {
case 'move-to-trash':
{
history.push( {
path: '/' + items[ 0 ].type,
postId: undefined,
postType: undefined,
canvas: 'view',
} );
}
break;
case 'duplicate-post':
{
const newItem = items[ 0 ];
const title =
typeof newItem.title === 'string'
? newItem.title
: newItem.title?.rendered;
createSuccessNotice(
sprintf(
// translators: %s: Title of the created post e.g: "Post 1".
__( '"%s" successfully created.' ),
title
),
{
type: 'snackbar',
id: 'duplicate-post-action',
actions: [
{
label: __( 'Edit' ),
onClick: () => {
history.push( {
path: undefined,
postId: newItem.id,
postType: newItem.type,
canvas: 'edit',
} );
},
},
],
}
);
}
break;
}
},
[ history ]
[ history, createSuccessNotice ]
);

if ( ! hasResolved ) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { useDispatch, useSelect } from '@wordpress/data';
import {
__experimentalVStack as VStack,
Expand All @@ -17,6 +17,7 @@ import { safeDecodeURIComponent, filterURLForDisplay } from '@wordpress/url';
import { useEffect, useCallback } from '@wordpress/element';
import { privateApis as routerPrivateApis } from '@wordpress/router';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { store as noticesStore } from '@wordpress/notices';

/**
* Internal dependencies
Expand All @@ -34,6 +35,7 @@ const { PostActions } = unlock( editorPrivateApis );
export default function SidebarNavigationScreenPage( { backPath } ) {
const { setCanvasMode } = unlock( useDispatch( editSiteStore ) );
const history = useHistory();
const { createSuccessNotice } = useDispatch( noticesStore );
const {
params: { postId },
} = useLocation();
Expand Down Expand Up @@ -83,16 +85,53 @@ export default function SidebarNavigationScreenPage( { backPath } ) {

const onActionPerformed = useCallback(
( actionId, items ) => {
if ( actionId === 'move-to-trash' ) {
history.push( {
path: '/' + items[ 0 ].type,
postId: undefined,
postType: undefined,
canvas: 'view',
} );
switch ( actionId ) {
case 'move-to-trash':
{
history.push( {
path: '/' + items[ 0 ].type,
postId: undefined,
postType: undefined,
canvas: 'view',
} );
}
break;
case 'duplicate-post':
{
const newItem = items[ 0 ];
const title =
typeof newItem.title === 'string'
? newItem.title
: newItem.title?.rendered;
createSuccessNotice(
sprintf(
// translators: %s: Title of the created post e.g: "Post 1".
__( '"%s" successfully created.' ),
title
),
{
type: 'snackbar',
id: 'duplicate-post-action',
actions: [
{
label: __( 'Edit' ),
onClick: () => {
history.push( {
path: undefined,
postId: newItem.id,
postType: newItem.type,
canvas: 'edit',
} );
},
},
],
}
);
}
break;
}
},
[ history ]
[ history, createSuccessNotice ]
);

const featureImageAltText = featuredMediaAltText
Expand Down
Loading
Loading