diff --git a/lib/edit-site-page.php b/lib/edit-site-page.php index c9cad42f3360cd..7a7b25053e109a 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,28 +168,27 @@ 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['templatePartIds'] = array_values( $template_part_ids ); @@ -210,7 +206,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 2525e25d7c94c2..393c4fd1e68978 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,8 +159,8 @@ 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 ) { - global $_wp_current_template_id, $_wp_current_template_name, $_wp_current_template_content, $_wp_current_template_hierarchy; +function gutenberg_template_include_filter( $template_file ) { + 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,9 +168,36 @@ function gutenberg_find_template( $template_file ) { return $template_file; } + $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_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 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. + */ +function gutenberg_find_template_post( $template_hierarchy ) { + if ( ! $template_hierarchy ) { + return null; + } + $slugs = array_map( 'gutenberg_strip_php_suffix', - $_wp_current_template_hierarchy + $template_hierarchy ); // Find most specific 'wp_template' post matching the hierarchy. @@ -257,18 +284,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; } /**