diff --git a/src/wp-includes/block-template.php b/src/wp-includes/block-template.php index 2a75231ae4e90..86e57d6550f31 100644 --- a/src/wp-includes/block-template.php +++ b/src/wp-includes/block-template.php @@ -17,6 +17,32 @@ function _add_template_loader_filters() { } } +/** + * Renders a warning screen for empty block templates. + * + * @since 6.8.0 + * + * @param WP_Block_Template $block_template The block template object. + * @return string The warning screen HTML. + */ +function wp_render_empty_block_template_warning( $block_template ) { + wp_enqueue_style( 'wp-empty-template-alert' ); + return sprintf( + /* translators: %1$s: Block template title. %2$s: Empty template warning message. %3$s: Edit template link. %4$s: Edit template button label. */ + '
', + esc_html( $block_template->title ), + __( 'This page is blank because the template is empty. You can reset or customize it in the Site Editor.' ), + get_edit_post_link( $block_template->wp_id, 'site-editor' ), + __( 'Edit template' ) + ); +} + /** * Finds a block template with equal or higher specificity than a given PHP template file. * @@ -68,13 +94,18 @@ function locate_block_template( $template, $type, array $templates ) { if ( $block_template ) { $_wp_current_template_id = $block_template->id; - if ( empty( $block_template->content ) && is_user_logged_in() ) { - $_wp_current_template_content = - sprintf( - /* translators: %s: Template title */ - __( 'Empty template: %s' ), - $block_template->title - ); + if ( empty( $block_template->content ) ) { + if ( is_user_logged_in() ) { + $_wp_current_template_content = wp_render_empty_block_template_warning( $block_template ); + } else { + if ( $block_template->has_theme_file ) { + // Show contents from theme template if user is not logged in. + $theme_template = _get_block_template_file( 'wp_template', $block_template->slug ); + $_wp_current_template_content = file_get_contents( $theme_template['path'] ); + } else { + $_wp_current_template_content = $block_template->content; + } + } } elseif ( ! empty( $block_template->content ) ) { $_wp_current_template_content = $block_template->content; } diff --git a/src/wp-includes/css/wp-empty-template-alert.css b/src/wp-includes/css/wp-empty-template-alert.css new file mode 100644 index 0000000000000..53142ec84c7db --- /dev/null +++ b/src/wp-includes/css/wp-empty-template-alert.css @@ -0,0 +1,23 @@ +#wp-empty-template-alert { + display: flex; + padding: var(--wp--style--root--padding-right, 2rem); + min-height: 60vh; + flex-direction: column; + align-items: center; + justify-content: center; + gap: var(--wp--style--block-gap, 2rem); +} + +#wp-empty-template-alert > * { + max-width: 400px; +} + +#wp-empty-template-alert h2, +#wp-empty-template-alert p { + margin: 0; + text-align: center; +} + +#wp-empty-template-alert a { + margin-top: 1rem; +} diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 61409c477638f..d2c8dbe97e393 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -1627,6 +1627,7 @@ function wp_default_styles( $styles ) { $styles->add( 'wp-pointer', "/wp-includes/css/wp-pointer$suffix.css", array( 'dashicons' ) ); $styles->add( 'customize-preview', "/wp-includes/css/customize-preview$suffix.css", array( 'dashicons' ) ); $styles->add( 'wp-embed-template-ie', "/wp-includes/css/wp-embed-template-ie$suffix.css" ); + $styles->add( 'wp-empty-template-alert', "/wp-includes/css/wp-empty-template-alert$suffix.css" ); $styles->add_data( 'wp-embed-template-ie', 'conditional', 'lte IE 8' ); // External libraries and friends. @@ -1809,6 +1810,7 @@ function wp_default_styles( $styles ) { 'customize-preview', 'login', 'site-health', + 'wp-empty-template-alert', // Includes CSS. 'buttons', 'admin-bar',