Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ?_wp-find-template=true work for new posts #1997

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions src/wp-includes/block-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,31 @@
* @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 ) {
if ( 'embed' === $template_type ) { // Skip 'embed' for now because it is not a regular 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.
*
Expand Down Expand Up @@ -227,3 +252,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
hellofromtonya marked this conversation as resolved.
Show resolved Hide resolved
*
* @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' );
}
}
1 change: 1 addition & 0 deletions src/wp-includes/default-filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -680,5 +680,6 @@
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 );