diff --git a/lib/templates.php b/lib/templates.php index d99d146e9eff42..d8c0da4ae5bcb1 100644 --- a/lib/templates.php +++ b/lib/templates.php @@ -374,6 +374,20 @@ function filter_rest_prepare_add_wp_theme_slug_and_file_based( $response ) { $file_based = in_array( '_wp_file_based', $wp_theme_slugs, true ); $response->data['file_based'] = $file_based; + $response->data['original_file_exists'] = false; + $template_files = array_unique( + array_merge( + _gutenberg_get_template_paths( get_stylesheet_directory() . DIRECTORY_SEPARATOR . 'block-templates' ), + _gutenberg_get_template_paths( get_template_directory() . DIRECTORY_SEPARATOR . 'block-templates' ) + ) + ); + foreach ( $template_files as $template_file ) { + if ( basename( $template_file, '.html' ) === $response->data['slug'] ) { + $response->data['original_file_exists'] = true; + break; + } + } + $theme_slug = array_values( array_filter( $wp_theme_slugs, diff --git a/packages/edit-site/src/components/template-details/index.js b/packages/edit-site/src/components/template-details/index.js index ae2d1ab4f01577..43ac7ae1d5dfbd 100644 --- a/packages/edit-site/src/components/template-details/index.js +++ b/packages/edit-site/src/components/template-details/index.js @@ -8,20 +8,33 @@ import { useDispatch, useSelect } from '@wordpress/data'; /** * Internal dependencies */ +import { isTemplateRevertable } from '../../utils'; import { MENU_TEMPLATES } from '../navigation-sidebar/navigation-panel/constants'; export default function TemplateDetails( { template, onClose } ) { - const { title, description } = useSelect( - ( select ) => - select( 'core/editor' ).__experimentalGetTemplateInfo( template ), - [] + const { title, description, currentTheme } = useSelect( ( select ) => { + const templateInfo = select( + 'core/editor' + ).__experimentalGetTemplateInfo( template ); + return { + title: templateInfo?.title, + description: templateInfo?.description, + currentTheme: select( 'core' ).getCurrentTheme()?.stylesheet, + }; + }, [] ); + const { openNavigationPanelToMenu, revertTemplate } = useDispatch( + 'core/edit-site' ); - const { openNavigationPanelToMenu } = useDispatch( 'core/edit-site' ); if ( ! template ) { return null; } + const revert = () => { + revertTemplate( template ); + onClose(); + }; + const showTemplateInSidebar = () => { onClose(); openNavigationPanelToMenu( MENU_TEMPLATES ); @@ -55,6 +68,19 @@ export default function TemplateDetails( { template, onClose } ) { ) } + { isTemplateRevertable( template, currentTheme ) && ( +