diff --git a/lib/edit-site-page.php b/lib/edit-site-page.php index abb7ced260c0b6..5d7df87db07281 100644 --- a/lib/edit-site-page.php +++ b/lib/edit-site-page.php @@ -152,8 +152,7 @@ function gutenberg_edit_site_init( $hook ) { continue; } - $template_hierarchy = array_merge( get_template_hierachy( $template_type ), get_template_hierachy( 'index' ) ); - $current_template = gutenberg_find_template_post_and_parts( $template_hierarchy ); + $current_template = gutenberg_find_template_post_and_parts( $template_type ); if ( isset( $current_template ) ) { $template_ids[ $current_template['template_post']->post_name ] = $current_template['template_post']->ID; $template_part_ids = $template_part_ids + $current_template['template_part_ids']; diff --git a/lib/template-loader.php b/lib/template-loader.php index 52bc9bb8637a57..c0be22bc759571 100644 --- a/lib/template-loader.php +++ b/lib/template-loader.php @@ -93,7 +93,7 @@ function get_template_hierachy( $template_type ) { function gutenberg_override_query_template( $template, $type, array $templates = array() ) { global $_wp_current_template_id, $_wp_current_template_content; - $current_template = gutenberg_find_template_post_and_parts( $templates ); + $current_template = gutenberg_find_template_post_and_parts( basename( $template, '.php' ), $templates ); if ( $current_template ) { $_wp_current_template_id = $current_template['template_post']->ID; @@ -191,19 +191,30 @@ function create_auto_draft_for_template_part_block( $block ) { } /** - * Return the correct 'wp_template' post and template part IDs for the current template hierarchy. + * Return the correct 'wp_template' post and template part IDs for the current template. * - * @param string[] $template_hierarchy The current template hierarchy, ordered by priority. + * Accepts an optional $template_hierarchy argument as a hint. + * + * @param string $template_type The current template type. + * @param string[] $template_hierarchy (optional) The current template hierarchy, ordered by priority. * @return null|array { * @type WP_Post|null template_post A template post object, or null if none could be found. * @type int[] A list of template parts IDs for the template. * } */ -function gutenberg_find_template_post_and_parts( $template_hierarchy ) { - if ( ! $template_hierarchy ) { +function gutenberg_find_template_post_and_parts( $template_type, $template_hierarchy = array() ) { + if ( ! $template_type ) { return null; } + if ( empty( $template_hierarchy ) ) { + if ( 'index' === $template_type ) { + $template_hierarchy = get_template_hierachy( 'index' ); + } else { + $template_hierarchy = array_merge( get_template_hierachy( $template_type ), get_template_hierachy( 'index' ) ); + } + } + $slugs = array_map( 'gutenberg_strip_php_suffix', $template_hierarchy diff --git a/lib/templates.php b/lib/templates.php index 95382cdc16fc56..9336cf44717cfe 100644 --- a/lib/templates.php +++ b/lib/templates.php @@ -155,3 +155,51 @@ function gutenberg_render_template_list_table_column( $column_name, $post_id ) { echo esc_html( $post->post_name ); } add_action( 'manage_wp_template_posts_custom_column', 'gutenberg_render_template_list_table_column', 10, 2 ); + +/** + * Filter for adding a `resolved` parameter to `wp_template` queries. + * + * @param array $query_params The query parameters. + * @return array Filtered $query_params. + */ +function filter_rest_wp_template_collection_params( $query_params ) { + $query_params += array( + 'resolved' => array( + 'description' => __( 'Whether to filter for resolved templates', 'gutenberg' ), + 'type' => 'boolean', + ), + ); + return $query_params; +} +apply_filters( 'rest_wp_template_collection_params', 'filter_rest_wp_template_collection_params', 99, 1 ); + +/** + * Filter for supporting the `resolved` parameter in `wp_template` queries. + * + * @param array $args The query arguments. + * @param WP_REST_Request $request The request object. + * @return array Filtered $args. + */ +function filter_rest_wp_template_query( $args, $request ) { + if ( $request['resolved'] ) { + $template_ids = array( 0 ); // Return nothing by default (the 0 is needed for `post__in`). + $template_types = $request['slug'] ? array( $request['slug'] ) : get_template_types(); + + foreach ( $template_types as $template_type ) { + // Skip 'embed' for now because it is not a regular template type. + // Skip 'index' because it's a fallback that we handle differently. + if ( in_array( $template_type, array( 'embed', 'index' ), true ) ) { + continue; + } + + $current_template = gutenberg_find_template_post_and_parts( $template_type ); + if ( isset( $current_template ) ) { + $template_ids[ $current_template['template_post']->post_name ] = $current_template['template_post']->ID; + } + } + $args['post__in'] = array_values( $template_ids ); + } + + return $args; +} +add_filter( 'rest_wp_template_query', 'filter_rest_wp_template_query', 99, 2 );