Make WordPress Core


Ignore:
Timestamp:
12/03/2021 07:37:32 PM (5 years ago)
Author:
hellofromTonya
Message:

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.

File:
1 edited

Legend:

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

    r52308 r52316  
    55 * @package WordPress
    66 */
     7
     8/**
     9 * Adds necessary filters to use 'wp_template' posts instead of theme template files.
     10 *
     11 * @access private
     12 * @since 5.9.0
     13 */
     14function _add_template_loader_filters() {
     15        if ( ! current_theme_supports( 'block-templates' ) ) {
     16                return;
     17        }
     18
     19        $template_types = array_keys( get_default_block_template_types() );
     20        foreach ( $template_types as $template_type ) {
     21                // Skip 'embed' for now because it is not a regular template type.
     22                if ( 'embed' === $template_type ) {
     23                        continue;
     24                }
     25                add_filter( str_replace( '-', '', $template_type ) . '_template', 'locate_block_template', 20, 3 );
     26        }
     27
     28        // Request to resolve a template.
     29        if ( isset( $_GET['_wp-find-template'] ) ) {
     30                add_filter( 'pre_get_posts', '_resolve_template_for_new_post' );
     31        }
     32}
    733
    834/**
     
    267293        return $context;
    268294}
     295
     296/**
     297 * Sets the current WP_Query to return auto-draft posts.
     298 *
     299 * The auto-draft status indicates a new post, so allow the the WP_Query instance to
     300 * return an auto-draft post for template resolution when editing a new post.
     301 *
     302 * @access private
     303 * @since 5.9.0
     304 *
     305 * @param WP_Query $wp_query Current WP_Query instance, passed by reference.
     306 */
     307function _resolve_template_for_new_post( $wp_query ) {
     308        remove_filter( 'pre_get_posts', '_resolve_template_for_new_post' );
     309
     310        // Pages.
     311        $page_id = isset( $wp_query->query['page_id'] ) ? $wp_query->query['page_id'] : null;
     312
     313        // Posts, including custom post types.
     314        $p = isset( $wp_query->query['p'] ) ? $wp_query->query['p'] : null;
     315
     316        $post_id = $page_id ? $page_id : $p;
     317        $post    = get_post( $post_id );
     318
     319        if (
     320                $post &&
     321                'auto-draft' === $post->post_status &&
     322                current_user_can( 'edit_post', $post->ID )
     323        ) {
     324                $wp_query->set( 'post_status', 'auto-draft' );
     325        }
     326}
Note: See TracChangeset for help on using the changeset viewer.