-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Site Editor: Revert templates to their original theme-provided files #27208
Changes from 7 commits
f205fbb
8017d7d
f55893b
37246df
19951bd
e511f3e
560f903
5fbf2a1
f29d7f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,13 @@ | |
*/ | ||
import { controls } from '@wordpress/data'; | ||
import { apiFetch } from '@wordpress/data-controls'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { findTemplate } from './controls'; | ||
import { isTemplateRevertable } from '../utils'; | ||
|
||
/** | ||
* Returns an action object used to toggle a feature flag. | ||
|
@@ -228,3 +230,83 @@ export function updateSettings( settings ) { | |
settings, | ||
}; | ||
} | ||
|
||
/** | ||
* Reverts a template to its original theme-provided file. | ||
* | ||
* This works with the assumption that the template being reverted | ||
* belongs to the current theme. | ||
* | ||
* @param {Object} template The template to revert. | ||
*/ | ||
export function* revertTemplate( template ) { | ||
const postType = yield controls.resolveSelect( | ||
'core', | ||
'getPostType', | ||
'wp_template' | ||
); | ||
|
||
const currentTheme = yield controls.resolveSelect( | ||
'core', | ||
'getCurrentTheme' | ||
); | ||
|
||
if ( ! isTemplateRevertable( template, currentTheme?.stylesheet ) ) { | ||
yield controls.dispatch( | ||
'core/notices', | ||
'createErrorNotice', | ||
__( 'This template is not revertable' ), | ||
{ type: 'snackbar' } | ||
); | ||
return; | ||
} | ||
|
||
try { | ||
yield apiFetch( { | ||
path: `/wp/v2/${ postType.rest_base }/${ template.id }`, | ||
method: 'DELETE', | ||
} ); | ||
|
||
const fileTemplates = yield controls.resolveSelect( | ||
'core', | ||
'getEntityRecords', | ||
'postType', | ||
'wp_template', | ||
{ | ||
slug: template.slug, | ||
status: 'auto-draft', | ||
per_page: 1, | ||
theme: currentTheme?.stylesheet, | ||
} | ||
); | ||
|
||
if ( ! fileTemplates.length ) { | ||
yield controls.dispatch( | ||
'core/notices', | ||
'createErrorNotice', | ||
__( 'This template is not revertable' ), | ||
{ type: 'snackbar' } | ||
); | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like we can create this notice in 2 places:
Should we be checking that autodraft and conditionally sending this notice before sending the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, Yeah I see why we have to do it this way and missed your recent note above:
The unexpected error message may make more sense in this case. 🤔 Perhaps it would make sense to always create the auto-draft if the auto-draft doesn't exist. So the new auto-draft would be created as soon as the original one was customized and published, as opposed to right after it is deleted? Not a blocker for this PR, but a follow up if necessary. |
||
|
||
yield setTemplate( fileTemplates[ 0 ].id ); | ||
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' ); | ||
yield controls.dispatch( | ||
'core/notices', | ||
'createErrorNotice', | ||
errorMessage, | ||
{ type: 'snackbar' } | ||
); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { default as findTemplate } from './find-template'; | ||
export { default as isTemplateRevertable } from './is-template-revertable'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Check if a template is revertable to its original theme-provided template file. | ||
* | ||
* @param {Object} template The template entity to check. | ||
* @param {string} currentTheme The current theme slug (stylesheet). | ||
* @return {boolean} Whether the template is revertable. | ||
*/ | ||
export default function isTemplateRevertable( template, currentTheme ) { | ||
if ( ! template || ! currentTheme ) { | ||
return false; | ||
} | ||
return ( | ||
'auto-draft' !== template.status && | ||
/* eslint-disable camelcase */ | ||
template?.file_based && | ||
template?.original_file_exists && | ||
currentTheme === template?.wp_theme_slug | ||
/* eslint-enable camelcase */ | ||
); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using the built-in deleteEntityRecord should take care of invalidating the cache and doing a rerequest.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch @david-szabo97, I didn't think of checking the sidebar. ✨