Skip to content

Commit 849db39

Browse files
committed
Editor: Warn about empty templates on the frontend for logged in users.
Adds a new function, `wp_render_empty_block_template_warning`, that renders a warning for logged-in users when a block template is empty. Reviewed by get_dave, richtabor. Props vcanales, mikachan, peterwilsoncc, richtabor, get_dave, mrfoxtalbot, matveb, arielmaidana, seifradwane, annezazu. Fixes #62053. git-svn-id: https://develop.svn.wordpress.org/trunk@59449 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8cd0ec0 commit 849db39

File tree

3 files changed

+63
-7
lines changed

3 files changed

+63
-7
lines changed

src/wp-includes/block-template.php

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,32 @@ function _add_template_loader_filters() {
1717
}
1818
}
1919

20+
/**
21+
* Renders a warning screen for empty block templates.
22+
*
23+
* @since 6.8.0
24+
*
25+
* @param WP_Block_Template $block_template The block template object.
26+
* @return string The warning screen HTML.
27+
*/
28+
function wp_render_empty_block_template_warning( $block_template ) {
29+
wp_enqueue_style( 'wp-empty-template-alert' );
30+
return sprintf(
31+
/* translators: %1$s: Block template title. %2$s: Empty template warning message. %3$s: Edit template link. %4$s: Edit template button label. */
32+
'<div id="wp-empty-template-alert">
33+
<h2>%1$s</h2>
34+
<p>%2$s</p>
35+
<a href="%3$s" class="wp-element-button">
36+
%4$s
37+
</a>
38+
</div>',
39+
esc_html( $block_template->title ),
40+
__( 'This page is blank because the template is empty. You can reset or customize it in the Site Editor.' ),
41+
get_edit_post_link( $block_template->wp_id, 'site-editor' ),
42+
__( 'Edit template' )
43+
);
44+
}
45+
2046
/**
2147
* Finds a block template with equal or higher specificity than a given PHP template file.
2248
*
@@ -68,13 +94,18 @@ function locate_block_template( $template, $type, array $templates ) {
6894
if ( $block_template ) {
6995
$_wp_current_template_id = $block_template->id;
7096

71-
if ( empty( $block_template->content ) && is_user_logged_in() ) {
72-
$_wp_current_template_content =
73-
sprintf(
74-
/* translators: %s: Template title */
75-
__( 'Empty template: %s' ),
76-
$block_template->title
77-
);
97+
if ( empty( $block_template->content ) ) {
98+
if ( is_user_logged_in() ) {
99+
$_wp_current_template_content = wp_render_empty_block_template_warning( $block_template );
100+
} else {
101+
if ( $block_template->has_theme_file ) {
102+
// Show contents from theme template if user is not logged in.
103+
$theme_template = _get_block_template_file( 'wp_template', $block_template->slug );
104+
$_wp_current_template_content = file_get_contents( $theme_template['path'] );
105+
} else {
106+
$_wp_current_template_content = $block_template->content;
107+
}
108+
}
78109
} elseif ( ! empty( $block_template->content ) ) {
79110
$_wp_current_template_content = $block_template->content;
80111
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#wp-empty-template-alert {
2+
display: flex;
3+
padding: var(--wp--style--root--padding-right, 2rem);
4+
min-height: 60vh;
5+
flex-direction: column;
6+
align-items: center;
7+
justify-content: center;
8+
gap: var(--wp--style--block-gap, 2rem);
9+
}
10+
11+
#wp-empty-template-alert > * {
12+
max-width: 400px;
13+
}
14+
15+
#wp-empty-template-alert h2,
16+
#wp-empty-template-alert p {
17+
margin: 0;
18+
text-align: center;
19+
}
20+
21+
#wp-empty-template-alert a {
22+
margin-top: 1rem;
23+
}

src/wp-includes/script-loader.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,7 @@ function wp_default_styles( $styles ) {
16271627
$styles->add( 'wp-pointer', "/wp-includes/css/wp-pointer$suffix.css", array( 'dashicons' ) );
16281628
$styles->add( 'customize-preview', "/wp-includes/css/customize-preview$suffix.css", array( 'dashicons' ) );
16291629
$styles->add( 'wp-embed-template-ie', "/wp-includes/css/wp-embed-template-ie$suffix.css" );
1630+
$styles->add( 'wp-empty-template-alert', "/wp-includes/css/wp-empty-template-alert$suffix.css" );
16301631
$styles->add_data( 'wp-embed-template-ie', 'conditional', 'lte IE 8' );
16311632

16321633
// External libraries and friends.
@@ -1809,6 +1810,7 @@ function wp_default_styles( $styles ) {
18091810
'customize-preview',
18101811
'login',
18111812
'site-health',
1813+
'wp-empty-template-alert',
18121814
// Includes CSS.
18131815
'buttons',
18141816
'admin-bar',

0 commit comments

Comments
 (0)