From e0d58b398713f076ed313ff7ef7f2ec9ed76b3ff Mon Sep 17 00:00:00 2001 From: mikachan Date: Fri, 22 Nov 2024 10:57:19 +0000 Subject: [PATCH] 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 --- wp-includes/block-template.php | 45 ++++++++++++++++--- wp-includes/css/wp-empty-template-alert.css | 23 ++++++++++ .../css/wp-empty-template-alert.min.css | 2 + wp-includes/script-loader.php | 2 + wp-includes/version.php | 2 +- 5 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 wp-includes/css/wp-empty-template-alert.css create mode 100644 wp-includes/css/wp-empty-template-alert.min.css diff --git a/wp-includes/block-template.php b/wp-includes/block-template.php index 2a75231ae4..86e57d6550 100644 --- a/wp-includes/block-template.php +++ b/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. */ + '
+

%1$s

+

%2$s

+ + %4$s + +
', + 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/wp-includes/css/wp-empty-template-alert.css b/wp-includes/css/wp-empty-template-alert.css new file mode 100644 index 0000000000..53142ec84c --- /dev/null +++ b/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/wp-includes/css/wp-empty-template-alert.min.css b/wp-includes/css/wp-empty-template-alert.min.css new file mode 100644 index 0000000000..d98d26df43 --- /dev/null +++ b/wp-includes/css/wp-empty-template-alert.min.css @@ -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} \ No newline at end of file diff --git a/wp-includes/script-loader.php b/wp-includes/script-loader.php index 61409c4776..d2c8dbe97e 100644 --- a/wp-includes/script-loader.php +++ b/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', diff --git a/wp-includes/version.php b/wp-includes/version.php index 94e0498ee9..0aee31198d 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.