Skip to content

Commit

Permalink
Backporting changes from WordPress/gutenberg#46248
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Jun 25, 2023
1 parent 89676d8 commit 89ed89f
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,21 @@ public function register_routes() {
* Returns the fallback template for the given slug.
*
* @since 6.1.0
* @since 6.3.0 Ignore empty templates.
*
* @param WP_REST_Request $request The request instance.
* @return WP_REST_Response|WP_Error
*/
public function get_template_fallback( $request ) {
$hierarchy = get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] );
$fallback_template = resolve_block_template( $request['slug'], $hierarchy, '' );
$response = $this->prepare_item_for_response( $fallback_template, $request );

do {
$fallback_template = resolve_block_template( $request['slug'], $hierarchy, '' );
array_shift( $hierarchy );
} while ( ! empty( $hierarchy ) && empty( $fallback_template->content ) );

$response = $this->prepare_item_for_response( $fallback_template, $request );

return rest_ensure_response( $response );
}

Expand Down
Empty file.
7 changes: 7 additions & 0 deletions tests/phpunit/tests/rest-api/wpRestTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -837,5 +837,12 @@ public function test_get_template_fallback() {
$request->set_param( 'template_prefix', 'page' );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 'page', $response->get_data()['slug'], 'Should fallback to `page.html`.' );
// Should fallback to `index.html`.
$request->set_param( 'slug', 'author' );
$request->set_param( 'ignore_empty', true );
$request->set_param( 'template_prefix', 'tag' );
$request->set_param( 'is_custom', false );
$response = rest_get_server()->dispatch( $request );
$this->assertSame( 'index', $response->get_data()['slug'], 'Should fallback to `index.html` when ignore_empty is `true`.' );
}
}

0 comments on commit 89ed89f

Please sign in to comment.