Editor: Resolve template request `?_wp-find-template=true` for new posts and pages.

The template resolution system makes a request like `/?page_id=1234&_wp-find-template=true`, depending on `WP_Query` to resolve a page or post using the page_id or p (post_id) in the query string. With new posts/pages, a placeholder post with the status auto-draft is created. But by default `WP_Query` will not resolve these posts, unless the query is specifically set to look for them.

This commit handles the query string to properly resolve a page or post. It adds 2 private callbacks for the processing.

Props poena, noisysocks, bernhard-reiter, costdev, hellofromTonya.
Fixes #54553.
Built from https://develop.svn.wordpress.org/trunk@52316


git-svn-id: http://core.svn.wordpress.org/trunk@51908 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
hellofromTonya 2021-12-03 19:39:00 +00:00
parent b32e8f7284
commit 64fe3efe28
3 changed files with 60 additions and 1 deletions

View File

@ -5,6 +5,32 @@
* @package WordPress
*/
/**
* Adds necessary filters to use 'wp_template' posts instead of theme template files.
*
* @access private
* @since 5.9.0
*/
function _add_template_loader_filters() {
if ( ! current_theme_supports( 'block-templates' ) ) {
return;
}
$template_types = array_keys( get_default_block_template_types() );
foreach ( $template_types as $template_type ) {
// Skip 'embed' for now because it is not a regular template type.
if ( 'embed' === $template_type ) {
continue;
}
add_filter( str_replace( '-', '', $template_type ) . '_template', 'locate_block_template', 20, 3 );
}
// Request to resolve a template.
if ( isset( $_GET['_wp-find-template'] ) ) {
add_filter( 'pre_get_posts', '_resolve_template_for_new_post' );
}
}
/**
* Find a block template with equal or higher specificity than a given PHP template file.
*
@ -266,3 +292,35 @@ function _block_template_render_without_post_block_context( $context ) {
return $context;
}
/**
* Sets the current WP_Query to return auto-draft posts.
*
* The auto-draft status indicates a new post, so allow the the WP_Query instance to
* return an auto-draft post for template resolution when editing a new post.
*
* @access private
* @since 5.9.0
*
* @param WP_Query $wp_query Current WP_Query instance, passed by reference.
*/
function _resolve_template_for_new_post( $wp_query ) {
remove_filter( 'pre_get_posts', '_resolve_template_for_new_post' );
// Pages.
$page_id = isset( $wp_query->query['page_id'] ) ? $wp_query->query['page_id'] : null;
// Posts, including custom post types.
$p = isset( $wp_query->query['p'] ) ? $wp_query->query['p'] : null;
$post_id = $page_id ? $page_id : $p;
$post = get_post( $post_id );
if (
$post &&
'auto-draft' === $post->post_status &&
current_user_can( 'edit_post', $post->ID )
) {
$wp_query->set( 'post_status', 'auto-draft' );
}
}

View File

@ -680,5 +680,6 @@ add_filter( 'pre_wp_unique_post_slug', 'wp_filter_wp_template_unique_post_slug',
add_action( 'save_post_wp_template_part', 'wp_set_unique_slug_on_create_template_part' );
add_action( 'wp_footer', 'the_block_template_skip_link' );
add_action( 'setup_theme', 'wp_enable_block_templates' );
add_action( 'wp_loaded', '_add_template_loader_filters' );
unset( $filter, $action );

View File

@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-beta1-52315';
$wp_version = '5.9-beta1-52316';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.