diff --git a/packages/editor/src/components/post-actions/actions.js b/packages/editor/src/components/post-actions/actions.js index fff6d6923e442..dde2f9a63f5c3 100644 --- a/packages/editor/src/components/post-actions/actions.js +++ b/packages/editor/src/components/post-actions/actions.js @@ -31,6 +31,7 @@ import { import { store as editorStore } from '../../store'; import { unlock } from '../../lock-unlock'; import isTemplateRevertable from '../../store/utils/is-template-revertable'; +import isTemplatePartRevertable from '../../store/utils/is-template-part-revertable'; import { exportPatternAsJSONAction } from './export-pattern-action'; import { CreateTemplatePartModalContents } from '../create-template-part-modal'; @@ -870,22 +871,6 @@ const useDuplicatePostAction = ( postType ) => { ); }; -const isTemplatePartRevertable = ( item ) => { - if ( ! item ) { - return false; - } - // In patterns list page we map the templates parts to a different object - // than the one returned from the endpoint. This is why we need to check for - // two props whether is custom or has a theme file. - const hasThemeFile = - item.has_theme_file || item.templatePart?.has_theme_file; - const isCustom = [ item.source, item.templatePart?.source ].includes( - TEMPLATE_ORIGINS.custom - ); - - return hasThemeFile && isCustom; -}; - const resetTemplateAction = { id: 'reset-template', label: __( 'Reset' ), diff --git a/packages/editor/src/store/private-actions.js b/packages/editor/src/store/private-actions.js index 5f4ff17534eef..16e2cf141c8b3 100644 --- a/packages/editor/src/store/private-actions.js +++ b/packages/editor/src/store/private-actions.js @@ -15,6 +15,8 @@ import { decodeEntities } from '@wordpress/html-entities'; * Internal dependencies */ import isTemplateRevertable from './utils/is-template-revertable'; +import isTemplatePartRevertable from './utils/is-template-part-revertable'; +import { TEMPLATE_PART_POST_TYPE } from './constants'; export * from '../dataviews/store/private-actions'; /** @@ -241,7 +243,12 @@ export const revertTemplate = async ( { registry } ) => { const noticeId = 'edit-site-template-reverted'; registry.dispatch( noticesStore ).removeNotice( noticeId ); - if ( ! isTemplateRevertable( template ) ) { + + const isRevertable = + template.type === TEMPLATE_PART_POST_TYPE + ? isTemplatePartRevertable + : isTemplateRevertable; + if ( ! isRevertable( template ) ) { registry .dispatch( noticesStore ) .createErrorNotice( __( 'This template is not revertable.' ), { diff --git a/packages/editor/src/store/utils/is-template-part-revertable.js b/packages/editor/src/store/utils/is-template-part-revertable.js new file mode 100644 index 0000000000000..aa4d725443b54 --- /dev/null +++ b/packages/editor/src/store/utils/is-template-part-revertable.js @@ -0,0 +1,28 @@ +/** + * Internal dependencies + */ +import { TEMPLATE_ORIGINS } from '../constants'; + +/** + * Check if a template part is revertable to its original theme-provided template file. + * + * @param {Object} templatePart The template part entity to check. + * @return {boolean} Whether the template part is revertable. + */ +export default function isTemplatePartRevertable( templatePart ) { + if ( ! templatePart ) { + return false; + } + // In patterns list page we map the templates parts to a different object + // than the one returned from the endpoint. This is why we need to check for + // two props whether is custom or has a theme file. + const hasThemeFile = + templatePart.has_theme_file || + templatePart.templatePart?.has_theme_file; + const isCustom = [ + templatePart.source, + templatePart.templatePart?.source, + ].includes( TEMPLATE_ORIGINS.custom ); + + return hasThemeFile && isCustom; +}