From a37c93075eea7188f8ef79088d80d02bb4bf4dfc Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 2 Dec 2021 14:18:29 +0100 Subject: [PATCH 1/6] Make `?_wp-find-template=true` work for new posts --- src/wp-includes/block-template-utils.php | 14 ++++++ src/wp-includes/block-template.php | 57 ++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index b0086f5d301fc..1d6ee1a0c3adf 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -184,6 +184,20 @@ function get_default_block_template_types() { return apply_filters( 'default_template_types', $default_template_types ); } +/** + * Return a list of all overrideable default template type slugs. + * + * @see get_query_template + * + * @access private + * @since 5.9.0 + * + * @return string[] List of all overrideable default template type slugs. + */ +function _get_template_type_slugs() { + return array_keys( get_default_block_template_types() ); +} + /** * Checks whether the input 'area' is a supported value. * Returns the input if supported, otherwise returns the 'uncategorized' value. diff --git a/src/wp-includes/block-template.php b/src/wp-includes/block-template.php index 945b96b027422..4e827104d8c4f 100644 --- a/src/wp-includes/block-template.php +++ b/src/wp-includes/block-template.php @@ -5,6 +5,31 @@ * @package WordPress */ +/** + * Adds necessary filters to use 'wp_template' posts instead of theme template files. + * + * @since 5.9.0 + */ +function add_template_loader_filters() { + if ( ! current_theme_supports( 'block-templates' ) ) { + return; + } + + foreach ( _get_template_type_slugs() 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' ); + } +} + +add_action( 'wp_loaded', 'add_template_loader_filters' ); + /** * Find a block template with equal or higher specificity than a given PHP template file. * @@ -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. + * + * @since 5.9.0 + * + * @param WP_Query $wp_query Current WP_Query instance, passed by reference. + * @return void + */ +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' ); + } +} From cb30070d02147ad8169ec5371cfb7724391f5597 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Fri, 3 Dec 2021 09:08:13 -0600 Subject: [PATCH 2/6] Make new callbacks private access. Co-authored-by: Robert Anderson --- src/wp-includes/block-template.php | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/wp-includes/block-template.php b/src/wp-includes/block-template.php index 4e827104d8c4f..8227f81f4a7c8 100644 --- a/src/wp-includes/block-template.php +++ b/src/wp-includes/block-template.php @@ -8,9 +8,10 @@ /** * Adds necessary filters to use 'wp_template' posts instead of theme template files. * - * @since 5.9.0 + * @access private + * @since 5.9.0 */ -function add_template_loader_filters() { +function _add_template_loader_filters() { if ( ! current_theme_supports( 'block-templates' ) ) { return; } @@ -28,7 +29,7 @@ function add_template_loader_filters() { } } -add_action( 'wp_loaded', 'add_template_loader_filters' ); +add_action( 'wp_loaded', '_add_template_loader_filters' ); /** * Find a block template with equal or higher specificity than a given PHP template file. @@ -259,10 +260,10 @@ function _block_template_render_without_post_block_context( $context ) { * 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. - * @return void */ function _resolve_template_for_new_post( $wp_query ) { remove_filter( 'pre_get_posts', '_resolve_template_for_new_post' ); From c97a6e640ba085e513da646db8274a7e3690ccd3 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Fri, 3 Dec 2021 09:54:37 -0600 Subject: [PATCH 3/6] Relocate hook into 'wp_loaded' to `wp-includes/default-filters.php`. --- src/wp-includes/default-filters.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/wp-includes/default-filters.php b/src/wp-includes/default-filters.php index 24396236484ff..23b0035d8fb3e 100644 --- a/src/wp-includes/default-filters.php +++ b/src/wp-includes/default-filters.php @@ -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 ); From a19dbb6bbf62137ff3d0794a7492c0edf3a45a03 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Fri, 3 Dec 2021 09:55:27 -0600 Subject: [PATCH 4/6] Remove hook into 'wp_loaded' from `wp-includes/block-template.php`. Relocated to `wp-includes/default-filters.php`. --- src/wp-includes/block-template.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/wp-includes/block-template.php b/src/wp-includes/block-template.php index 8227f81f4a7c8..9da85788dad59 100644 --- a/src/wp-includes/block-template.php +++ b/src/wp-includes/block-template.php @@ -29,8 +29,6 @@ function _add_template_loader_filters() { } } -add_action( 'wp_loaded', '_add_template_loader_filters' ); - /** * Find a block template with equal or higher specificity than a given PHP template file. * From 4934af7f50b21885222c56cb2cc3db3ec347913d Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Fri, 3 Dec 2021 10:26:16 -0600 Subject: [PATCH 5/6] Fix phpcs docblock alignment. --- src/wp-includes/block-template.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wp-includes/block-template.php b/src/wp-includes/block-template.php index 9da85788dad59..e9cd81040e2e0 100644 --- a/src/wp-includes/block-template.php +++ b/src/wp-includes/block-template.php @@ -8,8 +8,8 @@ /** * Adds necessary filters to use 'wp_template' posts instead of theme template files. * - * @access private - * @since 5.9.0 + * @access private + * @since 5.9.0 */ function _add_template_loader_filters() { if ( ! current_theme_supports( 'block-templates' ) ) { From 0a84ee9ea363683bbdedffaa93c4715886ac02a1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Fri, 3 Dec 2021 19:14:51 +0100 Subject: [PATCH 6/6] Inline _get_template_type_slugs() --- src/wp-includes/block-template-utils.php | 14 -------------- src/wp-includes/block-template.php | 3 ++- 2 files changed, 2 insertions(+), 15 deletions(-) diff --git a/src/wp-includes/block-template-utils.php b/src/wp-includes/block-template-utils.php index 1d6ee1a0c3adf..b0086f5d301fc 100644 --- a/src/wp-includes/block-template-utils.php +++ b/src/wp-includes/block-template-utils.php @@ -184,20 +184,6 @@ function get_default_block_template_types() { return apply_filters( 'default_template_types', $default_template_types ); } -/** - * Return a list of all overrideable default template type slugs. - * - * @see get_query_template - * - * @access private - * @since 5.9.0 - * - * @return string[] List of all overrideable default template type slugs. - */ -function _get_template_type_slugs() { - return array_keys( get_default_block_template_types() ); -} - /** * Checks whether the input 'area' is a supported value. * Returns the input if supported, otherwise returns the 'uncategorized' value. diff --git a/src/wp-includes/block-template.php b/src/wp-includes/block-template.php index e9cd81040e2e0..e96200b18d75e 100644 --- a/src/wp-includes/block-template.php +++ b/src/wp-includes/block-template.php @@ -16,7 +16,8 @@ function _add_template_loader_filters() { return; } - foreach ( _get_template_type_slugs() as $template_type ) { + $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; }