Skip to content

Commit bf747e9

Browse files
Mamadukarichtaboryouknowriadjorgefilipecostaellatrix
authored andcommitted
Site Editor: Fix template parts 'Reset' action (WordPress#62951)
Co-authored-by: Mamaduka <[email protected]> Co-authored-by: richtabor <[email protected]> Co-authored-by: youknowriad <[email protected]> Co-authored-by: jorgefilipecosta <[email protected]> Co-authored-by: ellatrix <[email protected]> Co-authored-by: t-hamano <[email protected]>
1 parent 298483e commit bf747e9

File tree

2 files changed

+35
-61
lines changed

2 files changed

+35
-61
lines changed

packages/editor/src/components/post-actions/actions.js

Lines changed: 27 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,6 @@ function isTemplateRemovable( template ) {
5757
! template?.has_theme_file
5858
);
5959
}
60-
const canDeleteOrReset = ( item ) => {
61-
const isTemplatePart = item.type === TEMPLATE_PART_POST_TYPE;
62-
const isUserPattern = item.type === PATTERN_TYPES.user;
63-
return (
64-
isUserPattern ||
65-
( isTemplatePart && item.source === TEMPLATE_ORIGINS.custom )
66-
);
67-
};
6860

6961
function getItemTitle( item ) {
7062
if ( typeof item.title === 'string' ) {
@@ -795,68 +787,50 @@ const useDuplicatePostAction = ( postType ) => {
795787
);
796788
};
797789

798-
const isTemplatePartRevertable = ( item ) => {
799-
if ( ! item ) {
800-
return false;
801-
}
802-
const hasThemeFile = item?.has_theme_file;
803-
return canDeleteOrReset( item ) && hasThemeFile;
804-
};
805-
806790
const resetTemplateAction = {
807791
id: 'reset-template',
808792
label: __( 'Reset' ),
809793
isEligible: ( item ) => {
810-
return item.type === TEMPLATE_PART_POST_TYPE
811-
? isTemplatePartRevertable( item )
812-
: isTemplateRevertable( item );
794+
return isTemplateRevertable( item );
813795
},
814796
icon: backup,
815797
supportsBulk: true,
816798
hideModalHeader: true,
817799
RenderModal: ( { items, closeModal, onActionPerformed } ) => {
818800
const [ isBusy, setIsBusy ] = useState( false );
819-
const { revertTemplate, removeTemplates } = unlock(
820-
useDispatch( editorStore )
821-
);
801+
const { revertTemplate } = unlock( useDispatch( editorStore ) );
822802
const { saveEditedEntityRecord } = useDispatch( coreStore );
823803
const { createSuccessNotice, createErrorNotice } =
824804
useDispatch( noticesStore );
825805
const onConfirm = async () => {
826806
try {
827-
if ( items[ 0 ].type === TEMPLATE_PART_POST_TYPE ) {
828-
await removeTemplates( items );
829-
} else {
830-
for ( const template of items ) {
831-
if ( template.type === TEMPLATE_POST_TYPE ) {
832-
await revertTemplate( template, {
833-
allowUndo: false,
834-
} );
835-
await saveEditedEntityRecord(
836-
'postType',
837-
template.type,
838-
template.id
839-
);
840-
}
841-
}
842-
createSuccessNotice(
843-
items.length > 1
844-
? sprintf(
845-
/* translators: The number of items. */
846-
__( '%s items reset.' ),
847-
items.length
848-
)
849-
: sprintf(
850-
/* translators: The template/part's name. */
851-
__( '"%s" reset.' ),
852-
decodeEntities( getItemTitle( items[ 0 ] ) )
853-
),
854-
{
855-
type: 'snackbar',
856-
id: 'revert-template-action',
857-
}
807+
for ( const template of items ) {
808+
await revertTemplate( template, {
809+
allowUndo: false,
810+
} );
811+
await saveEditedEntityRecord(
812+
'postType',
813+
template.type,
814+
template.id
858815
);
859816
}
817+
createSuccessNotice(
818+
items.length > 1
819+
? sprintf(
820+
/* translators: The number of items. */
821+
__( '%s items reset.' ),
822+
items.length
823+
)
824+
: sprintf(
825+
/* translators: The template/part's name. */
826+
__( '"%s" reset.' ),
827+
decodeEntities( getItemTitle( items[ 0 ] ) )
828+
),
829+
{
830+
type: 'snackbar',
831+
id: 'revert-template-action',
832+
}
833+
);
860834
} catch ( error ) {
861835
let fallbackErrorMessage;
862836
if ( items[ 0 ].type === TEMPLATE_POST_TYPE ) {

packages/editor/src/store/utils/is-template-revertable.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@ import { TEMPLATE_ORIGINS } from '../constants';
66
// Copy of the function from packages/edit-site/src/utils/is-template-revertable.js
77

88
/**
9-
* Check if a template is revertable to its original theme-provided template file.
9+
* Check if a template or template part is revertable to its original theme-provided file.
1010
*
11-
* @param {Object} template The template entity to check.
12-
* @return {boolean} Whether the template is revertable.
11+
* @param {Object} templateOrTemplatePart The entity to check.
12+
* @return {boolean} Whether the entity is revertable.
1313
*/
14-
export default function isTemplateRevertable( template ) {
15-
if ( ! template ) {
14+
export default function isTemplateRevertable( templateOrTemplatePart ) {
15+
if ( ! templateOrTemplatePart ) {
1616
return false;
1717
}
18-
/* eslint-disable camelcase */
18+
1919
return (
20-
template?.source === TEMPLATE_ORIGINS.custom && template?.has_theme_file
20+
templateOrTemplatePart.source === TEMPLATE_ORIGINS.custom &&
21+
templateOrTemplatePart.has_theme_file
2122
);
22-
/* eslint-enable camelcase */
2323
}

0 commit comments

Comments
 (0)