wp/wp-includes/block-template.php
changeset 22 8c2e4d02f4ef
parent 21 48c4eec2b7e6
equal deleted inserted replaced
21:48c4eec2b7e6 22:8c2e4d02f4ef
    13  */
    13  */
    14 function _add_template_loader_filters() {
    14 function _add_template_loader_filters() {
    15 	if ( isset( $_GET['_wp-find-template'] ) && current_theme_supports( 'block-templates' ) ) {
    15 	if ( isset( $_GET['_wp-find-template'] ) && current_theme_supports( 'block-templates' ) ) {
    16 		add_action( 'pre_get_posts', '_resolve_template_for_new_post' );
    16 		add_action( 'pre_get_posts', '_resolve_template_for_new_post' );
    17 	}
    17 	}
       
    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  */
       
    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 	);
    18 }
    44 }
    19 
    45 
    20 /**
    46 /**
    21  * Finds a block template with equal or higher specificity than a given PHP template file.
    47  * Finds a block template with equal or higher specificity than a given PHP template file.
    22  *
    48  *
    66 	$block_template = resolve_block_template( $type, $templates, $template );
    92 	$block_template = resolve_block_template( $type, $templates, $template );
    67 
    93 
    68 	if ( $block_template ) {
    94 	if ( $block_template ) {
    69 		$_wp_current_template_id = $block_template->id;
    95 		$_wp_current_template_id = $block_template->id;
    70 
    96 
    71 		if ( empty( $block_template->content ) && is_user_logged_in() ) {
    97 		if ( empty( $block_template->content ) ) {
    72 			$_wp_current_template_content =
    98 			if ( is_user_logged_in() ) {
    73 			sprintf(
    99 				$_wp_current_template_content = wp_render_empty_block_template_warning( $block_template );
    74 				/* translators: %s: Template title */
   100 			} else {
    75 				__( 'Empty template: %s' ),
   101 				if ( $block_template->has_theme_file ) {
    76 				$block_template->title
   102 					// Show contents from theme template if user is not logged in.
    77 			);
   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 			}
    78 		} elseif ( ! empty( $block_template->content ) ) {
   109 		} elseif ( ! empty( $block_template->content ) ) {
    79 			$_wp_current_template_content = $block_template->content;
   110 			$_wp_current_template_content = $block_template->content;
    80 		}
   111 		}
    81 		if ( isset( $_GET['_wp-find-template'] ) ) {
   112 		if ( isset( $_GET['_wp-find-template'] ) ) {
    82 			wp_send_json_success( $block_template );
   113 			wp_send_json_success( $block_template );
   356 		current_user_can( 'edit_post', $post->ID )
   387 		current_user_can( 'edit_post', $post->ID )
   357 	) {
   388 	) {
   358 		$wp_query->set( 'post_status', 'auto-draft' );
   389 		$wp_query->set( 'post_status', 'auto-draft' );
   359 	}
   390 	}
   360 }
   391 }
       
   392 
       
   393 /**
       
   394  * Register a block template.
       
   395  *
       
   396  * @since 6.7.0
       
   397  *
       
   398  * @param string       $template_name  Template name in the form of `plugin_uri//template_name`.
       
   399  * @param array|string $args           {
       
   400  *     @type string        $title                 Optional. Title of the template as it will be shown in the Site Editor
       
   401  *                                                and other UI elements.
       
   402  *     @type string        $description           Optional. Description of the template as it will be shown in the Site
       
   403  *                                                Editor.
       
   404  *     @type string        $content               Optional. Default content of the template that will be used when the
       
   405  *                                                template is rendered or edited in the editor.
       
   406  *     @type string[]      $post_types            Optional. Array of post types to which the template should be available.
       
   407  *     @type string        $plugin                Optional. Slug of the plugin that registers the template.
       
   408  * }
       
   409  * @return WP_Block_Template|WP_Error The registered template object on success, WP_Error object on failure.
       
   410  */
       
   411 function register_block_template( $template_name, $args = array() ) {
       
   412 	return WP_Block_Templates_Registry::get_instance()->register( $template_name, $args );
       
   413 }
       
   414 
       
   415 /**
       
   416  * Unregister a block template.
       
   417  *
       
   418  * @since 6.7.0
       
   419  *
       
   420  * @param string $template_name Template name in the form of `plugin_uri//template_name`.
       
   421  * @return WP_Block_Template|WP_Error The unregistered template object on success, WP_Error object on failure or if the
       
   422  *                                    template doesn't exist.
       
   423  */
       
   424 function unregister_block_template( $template_name ) {
       
   425 	return WP_Block_Templates_Registry::get_instance()->unregister( $template_name );
       
   426 }