Skip to content

Commit

Permalink
Templates Endpoint: Add resolved query arg to return only relevant …
Browse files Browse the repository at this point in the history
…templates (#21981)

* Templates Endpoint: Add resolved filter

* Template Loader: Search by template type, make template hierarchy optional

* Add TODO comments about future optimization

* PHPDoc whitespace

Co-authored-by: Enrique Piqueras <[email protected]>

* Fix filter_rest_wp_template_query

* Revert "Add TODO comments about future optimization"

This reverts commit 6ca25eac8c060c69e32ff946fc586a2570aae749.

* phpcbf

Co-authored-by: Enrique Piqueras <[email protected]>
  • Loading branch information
ockham and epiqueras authored May 13, 2020
1 parent 37dc5e1 commit a390df4
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 7 deletions.
3 changes: 1 addition & 2 deletions lib/edit-site-page.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'];
Expand Down
21 changes: 16 additions & 5 deletions lib/template-loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions lib/templates.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );

0 comments on commit a390df4

Please sign in to comment.