Skip to content

Commit

Permalink
Add a revertTemplate action (not fully working)
Browse files Browse the repository at this point in the history
  • Loading branch information
Copons committed Jan 12, 2021
1 parent e417634 commit bb315ee
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/edit-site/src/components/template-details/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
/**
* Internal dependencies
*/
import isTemplateRevertable from '../../utils/is-template-revertable';
import { MENU_TEMPLATES } from '../navigation-sidebar/navigation-panel/constants';

export default function TemplateDetails( { template, onClose } ) {
Expand All @@ -16,7 +17,9 @@ export default function TemplateDetails( { template, onClose } ) {
select( 'core/editor' ).__experimentalGetTemplateInfo( template ),
[]
);
const { openNavigationPanelToMenu } = useDispatch( 'core/edit-site' );
const { openNavigationPanelToMenu, revertTemplate } = useDispatch(
'core/edit-site'
);

if ( ! template ) {
return null;
Expand All @@ -27,6 +30,11 @@ export default function TemplateDetails( { template, onClose } ) {
openNavigationPanelToMenu( MENU_TEMPLATES );
};

const revert = () => {
revertTemplate( template );
onClose();
};

return (
<>
<div className="edit-site-template-details">
Expand Down Expand Up @@ -55,6 +63,19 @@ export default function TemplateDetails( { template, onClose } ) {
) }
</div>

{ isTemplateRevertable( template ) && (
<div className="edit-site-template-details">
<Button isLink onClick={ revert }>
{ __( 'Revert' ) }
</Button>
<Text variant="caption">
{ __(
'Reset this template to the theme supplied default'
) }
</Text>
</div>
) }

<Button
className="edit-site-template-details__show-all-button"
onClick={ showTemplateInSidebar }
Expand Down
71 changes: 71 additions & 0 deletions packages/edit-site/src/store/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
*/
import { controls } from '@wordpress/data';
import { apiFetch } from '@wordpress/data-controls';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import isTemplateRevertable from '../utils/is-template-revertable';

/**
* Returns an action object used to toggle a feature flag.
Expand Down Expand Up @@ -236,3 +242,68 @@ export function updateSettings( settings ) {
settings,
};
}

/**
* Reverts a template to its original theme-provided file.
*
* @param {Object} template The template to revert.
*/
export function* revertTemplate( template ) {
if ( ! isTemplateRevertable( template ) ) {
yield controls.dispatch(
'core/notices',
'createErrorNotice',
__( 'This template is not revertable.' ),
{ type: 'snackbar' }
);
return;
}

try {
yield controls.dispatch(
'core',
'deleteEntityRecord',
'postType',
'wp_template',
template.id
);

const fileTemplate = yield controls.resolveSelect(
'core',
'getEntityRecord',
'postType',
'wp_template',
template.id
);

if ( ! fileTemplate ) {
yield controls.dispatch(
'core/notices',
'createErrorNotice',
__(
'The editor has encountered an unexpected error. Please reload.'
),
{ type: 'snackbar' }
);
return;
}

yield controls.dispatch(
'core/notices',
'createSuccessNotice',
__( 'Template reverted.' ),
{ type: 'snackbar' }
);
} catch ( error ) {
const errorMessage =
error.message && error.code !== 'unknown_error'
? error.message
: __( 'Template revert failed. Please reload.' );
yield controls.dispatch(
'core/notices',
'createErrorNotice',
errorMessage,
{ type: 'snackbar' }
);
}
}
14 changes: 14 additions & 0 deletions packages/edit-site/src/utils/is-template-revertable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/**
* Check if a template is revertable to its original theme-provided template file.
*
* @param {Object} template The template entity to check.
* @return {boolean} Whether the template is revertable.
*/
export default function isTemplateRevertable( template ) {
if ( ! template ) {
return false;
}
/* eslint-disable camelcase */
return template?.is_custom && template?.original_file_exists;
/* eslint-enable camelcase */
}

0 comments on commit bb315ee

Please sign in to comment.