Make WordPress Core

Changeset 59449


Ignore:
Timestamp:
11/22/2024 10:55:12 AM (4 months ago)
Author:
mikachan
Message:

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.

Location:
trunk/src/wp-includes
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/block-template.php

    r59201 r59449  
    1616        add_action( 'pre_get_posts', '_resolve_template_for_new_post' );
    1717    }
     18}
     19
     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 */
     28function 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    );
    1844}
    1945
     
    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;
  • trunk/src/wp-includes/script-loader.php

    r59135 r59449  
    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
     
    18101811        'login',
    18111812        'site-health',
     1813        'wp-empty-template-alert',
    18121814        // Includes CSS.
    18131815        'buttons',
Note: See TracChangeset for help on using the changeset viewer.