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.
Built from https://develop.svn.wordpress.org/trunk@59449


git-svn-id: http://core.svn.wordpress.org/trunk@58835 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
mikachan 2024-11-22 10:57:19 +00:00
parent 04111ef54f
commit e0d58b3987
5 changed files with 66 additions and 8 deletions

View File

@ -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.
*
@ -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;
}

View File

@ -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;
}

View File

@ -0,0 +1,2 @@
/*! This file is auto-generated */
#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}

View File

@ -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',

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '6.8-alpha-59448';
$wp_version = '6.8-alpha-59449';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.