Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
9dd65bb
Warn about empty templates in the frontend
vcanales Sep 14, 2024
e32337a
remove trailing comma
vcanales Sep 14, 2024
e105ef9
get admin url directly
vcanales Sep 14, 2024
584f447
use get_edit_post_link instead of building url
vcanales Sep 14, 2024
2acc8a1
Update src/wp-includes/script-loader.php
vcanales Oct 4, 2024
a0083e0
Update src/wp-includes/script-loader.php
vcanales Oct 4, 2024
9e7c46f
Update src/wp-includes/block-template.php
vcanales Oct 4, 2024
963d514
Update src/wp-includes/block-template.php
vcanales Oct 4, 2024
641c6ea
Update src/wp-includes/block-template.php
vcanales Oct 4, 2024
cc241c0
rename emptyu template alert styles file
vcanales Oct 4, 2024
4e4c89d
Merge branch 'trunk' into warn-about-empty-template-in-frontend
mikachan Nov 1, 2024
76237fb
Default to template from theme on front end if logged out
mikachan Nov 4, 2024
5d23d4c
Wording feedback from GB #64027
mikachan Nov 4, 2024
f439c8b
Merge branch 'trunk' into warn-about-empty-template-in-frontend
mikachan Nov 11, 2024
1792a47
Remove icon from empty template alert
mikachan Nov 11, 2024
63ddc94
Apply simplified HTML and CSS
mikachan Nov 11, 2024
fdc95f5
Update translator comments
mikachan Nov 11, 2024
a906902
Remove whitespace from CSS
mikachan Nov 11, 2024
78fc03e
Remove dashicons dep from empty template styles
mikachan Nov 11, 2024
20c4c32
Add 6.8.0 to @since note
mikachan Nov 13, 2024
34f6e2d
Merge branch 'trunk' into warn-about-empty-template-in-frontend
mikachan Nov 20, 2024
2dc7af9
Update src/wp-includes/block-template.php
mikachan Nov 20, 2024
158d3b6
Update src/wp-includes/block-template.php
mikachan Nov 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 38 additions & 7 deletions src/wp-includes/block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
'<div id="wp-empty-template-alert">
<h2>%1$s</h2>
<p>%2$s</p>
<a href="%3$s" class="wp-element-button">
%4$s
</a>
</div>',
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.
*
Expand Down Expand Up @@ -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;
}
Expand Down
23 changes: 23 additions & 0 deletions src/wp-includes/css/wp-empty-template-alert.css
Original file line number Diff line number Diff line change
@@ -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;
}
2 changes: 2 additions & 0 deletions src/wp-includes/script-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -1809,6 +1810,7 @@ function wp_default_styles( $styles ) {
'customize-preview',
'login',
'site-health',
'wp-empty-template-alert',
// Includes CSS.
'buttons',
'admin-bar',
Expand Down
Loading