From 1a08254aaf6e5c7f29882a5c0bd7d0272c24a1f1 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 28 Apr 2020 22:00:00 +0200 Subject: [PATCH 1/7] Extract gutenberg_find_template_post from gutenberg_find_template, rename the latter to gutenberg_template_include_filter --- lib/template-loader.php | 47 +++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 14 deletions(-) diff --git a/lib/template-loader.php b/lib/template-loader.php index 2525e25d7c94c2..0c8298be9363f8 100644 --- a/lib/template-loader.php +++ b/lib/template-loader.php @@ -43,7 +43,7 @@ function gutenberg_add_template_loader_filters() { add_filter( $template_type . '_template', 'gutenberg_override_query_template', 20, 3 ); } - add_filter( 'template_include', 'gutenberg_find_template', 20 ); + add_filter( 'template_include', 'gutenberg_template_include_filter', 20 ); } add_action( 'wp_loaded', 'gutenberg_add_template_loader_filters' ); @@ -53,7 +53,7 @@ function gutenberg_add_template_loader_filters() { * The method returns an empty result for every template so that a 'wp_template' post * is used instead. * - * @see gutenberg_find_template + * @see gutenberg_template_include_filter * * @param string $template Path to the template. See locate_template(). * @param string $type Sanitized filename without extension. @@ -159,7 +159,7 @@ function create_auto_draft_for_template_part_block( $block ) { * @param string $template_file Original template file. Will be overridden. * @return string Path to the canvas file to include. */ -function gutenberg_find_template( $template_file ) { +function gutenberg_template_include_filter( $template_file ) { global $_wp_current_template_id, $_wp_current_template_name, $_wp_current_template_content, $_wp_current_template_hierarchy; // Bail if no relevant template hierarchy was determined, or if the template file @@ -173,6 +173,34 @@ function gutenberg_find_template( $template_file ) { $_wp_current_template_hierarchy ); + $current_template_post = gutenberg_find_template_post( $slugs ); + + if ( $current_template_post ) { + $_wp_current_template_id = $current_template_post->ID; + $_wp_current_template_name = $current_template_post->post_name; + $_wp_current_template_content = empty( $current_template_post->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template_post->post_content; + } + + // Add extra hooks for template canvas. + add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 ); + remove_action( 'wp_head', '_wp_render_title_tag', 1 ); + add_action( 'wp_head', 'gutenberg_render_title_tag', 1 ); + + // This file will be included instead of the theme's template file. + return gutenberg_dir_path() . 'lib/template-canvas.php'; +} + +/** + * Return the correct 'wp_template' post for the current hierarchy of template slugs. + * + * @param string[] $slugs A list of candidate template slugs, ordered by priority in the current template hierarchy. + * @return WP_Post|null A template post object, or null if none could be found. + */ +function gutenberg_find_template_post( $slugs ) { + if ( ! $slugs ) { + return null; + } + // Find most specific 'wp_template' post matching the hierarchy. $template_query = new WP_Query( array( @@ -257,18 +285,9 @@ function gutenberg_find_template( $template_file ) { create_auto_draft_for_template_part_block( $block ); } } - $_wp_current_template_id = $current_template_post->ID; - $_wp_current_template_name = $current_template_post->post_name; - $_wp_current_template_content = empty( $current_template_post->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template_post->post_content; + return $current_template_post; } - - // Add extra hooks for template canvas. - add_action( 'wp_head', 'gutenberg_viewport_meta_tag', 0 ); - remove_action( 'wp_head', '_wp_render_title_tag', 1 ); - add_action( 'wp_head', 'gutenberg_render_title_tag', 1 ); - - // This file will be included instead of the theme's template file. - return gutenberg_dir_path() . 'lib/template-canvas.php'; + return null; } /** From 7f9f47c8618229a63f9312678d4552ecda8726c9 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 28 Apr 2020 22:29:43 +0200 Subject: [PATCH 2/7] Use gutenberg_find_template_post in edit-site-page --- lib/edit-site-page.php | 25 +++++++++++-------------- lib/template-loader.php | 23 +++++++++++------------ 2 files changed, 22 insertions(+), 26 deletions(-) diff --git a/lib/edit-site-page.php b/lib/edit-site-page.php index c9cad42f3360cd..5dc77944b49936 100644 --- a/lib/edit-site-page.php +++ b/lib/edit-site-page.php @@ -89,9 +89,6 @@ function gutenberg_get_editor_styles() { */ function gutenberg_edit_site_init( $hook ) { global - $_wp_current_template_id, - $_wp_current_template_name, - $_wp_current_template_content, $_wp_current_template_hierarchy, $_wp_current_template_part_ids, $current_screen; @@ -171,30 +168,30 @@ function gutenberg_edit_site_init( $hook ) { $template_ids = array(); $template_part_ids = array(); foreach ( $template_getters as $template_getter ) { - call_user_func( $template_getter ); - apply_filters( 'template_include', null ); - if ( isset( $_wp_current_template_id ) ) { - $template_ids[ $_wp_current_template_name ] = $_wp_current_template_id; + call_user_func( $template_getter ); // This sets $_wp_current_template_hierarchy + + $current_template_post = gutenberg_find_template_post( $_wp_current_template_hierarchy ); + if ( isset( $current_template_post ) ) { + $template_ids[ $current_template_post->post_name ] = $current_template_post->ID; } if ( isset( $_wp_current_template_part_ids ) ) { $template_part_ids = $template_part_ids + $_wp_current_template_part_ids; } - $_wp_current_template_id = null; - $_wp_current_template_name = null; - $_wp_current_template_content = null; + $_wp_current_template_hierarchy = null; $_wp_current_template_part_ids = null; } get_front_page_template(); get_index_template(); - apply_filters( 'template_include', null ); - $template_ids[ $_wp_current_template_name ] = $_wp_current_template_id; + $current_template_post = gutenberg_find_template_post( $_wp_current_template_hierarchy ); + $template_ids[ $current_template_post->post_name ] = $current_template_post->ID; if ( isset( $_wp_current_template_part_ids ) ) { $template_part_ids = $template_part_ids + $_wp_current_template_part_ids; } - $settings['templateId'] = $_wp_current_template_id; + $settings['templateId'] = $current_template_post->ID; $settings['templateType'] = 'wp_template'; $settings['templateIds'] = array_values( $template_ids ); + $settings['templateIdsAll'] = $template_ids; $settings['templatePartIds'] = array_values( $template_part_ids ); $settings['styles'] = gutenberg_get_editor_styles(); @@ -210,7 +207,7 @@ function gutenberg_edit_site_init( $hook ) { '/wp/v2/types?context=edit', '/wp/v2/taxonomies?per_page=-1&context=edit', '/wp/v2/themes?status=active', - sprintf( '/wp/v2/templates/%s?context=edit', $_wp_current_template_id ), + sprintf( '/wp/v2/templates/%s?context=edit', $current_template_post->ID ), array( '/wp/v2/media', 'OPTIONS' ), ); $preload_data = array_reduce( diff --git a/lib/template-loader.php b/lib/template-loader.php index 0c8298be9363f8..b88349f52b6b81 100644 --- a/lib/template-loader.php +++ b/lib/template-loader.php @@ -160,7 +160,7 @@ function create_auto_draft_for_template_part_block( $block ) { * @return string Path to the canvas file to include. */ function gutenberg_template_include_filter( $template_file ) { - global $_wp_current_template_id, $_wp_current_template_name, $_wp_current_template_content, $_wp_current_template_hierarchy; + global $_wp_current_template_id, $_wp_current_template_content, $_wp_current_template_hierarchy; // Bail if no relevant template hierarchy was determined, or if the template file // was overridden another way. @@ -168,16 +168,10 @@ function gutenberg_template_include_filter( $template_file ) { return $template_file; } - $slugs = array_map( - 'gutenberg_strip_php_suffix', - $_wp_current_template_hierarchy - ); - - $current_template_post = gutenberg_find_template_post( $slugs ); + $current_template_post = gutenberg_find_template_post( $_wp_current_template_hierarchy ); if ( $current_template_post ) { $_wp_current_template_id = $current_template_post->ID; - $_wp_current_template_name = $current_template_post->post_name; $_wp_current_template_content = empty( $current_template_post->post_content ) ? __( 'Empty template.', 'gutenberg' ) : $current_template_post->post_content; } @@ -191,16 +185,21 @@ function gutenberg_template_include_filter( $template_file ) { } /** - * Return the correct 'wp_template' post for the current hierarchy of template slugs. + * Return the correct 'wp_template' post for the current template hierarchy. * - * @param string[] $slugs A list of candidate template slugs, ordered by priority in the current template hierarchy. + * @param string[] $slugs The current template hierarchy, ordered by priority. * @return WP_Post|null A template post object, or null if none could be found. */ -function gutenberg_find_template_post( $slugs ) { - if ( ! $slugs ) { +function gutenberg_find_template_post( $template_hierarchy ) { + if ( ! $template_hierarchy ) { return null; } + $slugs = array_map( + 'gutenberg_strip_php_suffix', + $template_hierarchy + ); + // Find most specific 'wp_template' post matching the hierarchy. $template_query = new WP_Query( array( From 3b2c1941798fac57573b1536a1e01f9886edd546 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Tue, 28 Apr 2020 22:40:22 +0200 Subject: [PATCH 3/7] Trailing period --- lib/edit-site-page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/edit-site-page.php b/lib/edit-site-page.php index 5dc77944b49936..fdbe90a3e2d599 100644 --- a/lib/edit-site-page.php +++ b/lib/edit-site-page.php @@ -168,7 +168,7 @@ function gutenberg_edit_site_init( $hook ) { $template_ids = array(); $template_part_ids = array(); foreach ( $template_getters as $template_getter ) { - call_user_func( $template_getter ); // This sets $_wp_current_template_hierarchy + call_user_func( $template_getter ); // This sets $_wp_current_template_hierarchy. $current_template_post = gutenberg_find_template_post( $_wp_current_template_hierarchy ); if ( isset( $current_template_post ) ) { From 176bc666cfb5c5fe86888e62dfb60524d5bec3ce Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 Apr 2020 12:23:21 +0200 Subject: [PATCH 4/7] Fix a PHPDoc arg name Co-Authored-By: Enrique Piqueras --- lib/template-loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/template-loader.php b/lib/template-loader.php index b88349f52b6b81..f6a628efaa4e10 100644 --- a/lib/template-loader.php +++ b/lib/template-loader.php @@ -187,7 +187,7 @@ function gutenberg_template_include_filter( $template_file ) { /** * Return the correct 'wp_template' post for the current template hierarchy. * - * @param string[] $slugs The current template hierarchy, ordered by priority. + * @param string[] $template_hierarchy The current template hierarchy, ordered by priority. * @return WP_Post|null A template post object, or null if none could be found. */ function gutenberg_find_template_post( $template_hierarchy ) { From 5dd510bec6fa757768010e6a641972050b958379 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 Apr 2020 12:23:37 +0200 Subject: [PATCH 5/7] PHPDoc whitespace Co-Authored-By: Enrique Piqueras --- lib/template-loader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/template-loader.php b/lib/template-loader.php index f6a628efaa4e10..393c4fd1e68978 100644 --- a/lib/template-loader.php +++ b/lib/template-loader.php @@ -188,7 +188,7 @@ function gutenberg_template_include_filter( $template_file ) { * Return the correct 'wp_template' post for the current template hierarchy. * * @param string[] $template_hierarchy The current template hierarchy, ordered by priority. - * @return WP_Post|null A template post object, or null if none could be found. + * @return WP_Post|null A template post object, or null if none could be found. */ function gutenberg_find_template_post( $template_hierarchy ) { if ( ! $template_hierarchy ) { From 7fb0bf6cf121f551f64fad332783760aceee1ee9 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 Apr 2020 12:27:04 +0200 Subject: [PATCH 6/7] Remove debugging leftover --- lib/edit-site-page.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/edit-site-page.php b/lib/edit-site-page.php index fdbe90a3e2d599..3874c26bb74f2d 100644 --- a/lib/edit-site-page.php +++ b/lib/edit-site-page.php @@ -191,7 +191,6 @@ function gutenberg_edit_site_init( $hook ) { $settings['templateId'] = $current_template_post->ID; $settings['templateType'] = 'wp_template'; $settings['templateIds'] = array_values( $template_ids ); - $settings['templateIdsAll'] = $template_ids; $settings['templatePartIds'] = array_values( $template_part_ids ); $settings['styles'] = gutenberg_get_editor_styles(); From e17d846fe325ceb0180a0b2fca62da2e57639741 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Wed, 29 Apr 2020 13:34:39 +0200 Subject: [PATCH 7/7] Whitespace --- lib/edit-site-page.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/edit-site-page.php b/lib/edit-site-page.php index 3874c26bb74f2d..7a7b25053e109a 100644 --- a/lib/edit-site-page.php +++ b/lib/edit-site-page.php @@ -183,7 +183,7 @@ function gutenberg_edit_site_init( $hook ) { } get_front_page_template(); get_index_template(); - $current_template_post = gutenberg_find_template_post( $_wp_current_template_hierarchy ); + $current_template_post = gutenberg_find_template_post( $_wp_current_template_hierarchy ); $template_ids[ $current_template_post->post_name ] = $current_template_post->ID; if ( isset( $_wp_current_template_part_ids ) ) { $template_part_ids = $template_part_ids + $_wp_current_template_part_ids;