Skip to content

Commit

Permalink
Return a 404 error when no fallback templates are found
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecoder committed Apr 20, 2024
1 parent a137058 commit 4644ea1
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ public function register_routes() {
*
* @since 6.1.0
* @since 6.3.0 Ignore empty templates.
* @since 6.5.3 Return a 404 error if no fallback templates are found.
*
* @param WP_REST_Request $request The request instance.
* @return WP_REST_Response|WP_Error
Expand All @@ -165,6 +166,10 @@ public function get_template_fallback( $request ) {
array_shift( $hierarchy );
} while ( ! empty( $hierarchy ) && empty( $fallback_template->content ) );

if ( ! $fallback_template ) {
return new WP_Error( 'rest_template_not_found', __( 'No fallback templates exist for that slug.' ), array( 'status' => 404 ) );
}

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

return rest_ensure_response( $response );
Expand Down
12 changes: 12 additions & 0 deletions tests/phpunit/tests/rest-api/wpRestTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,18 @@ public function test_get_template_fallback() {
$this->assertSame( 'index', $response->get_data()['slug'], 'Should fallback to `index.html` when ignore_empty is `true`.' );
}

/**
* @ticket 60909
* @covers WP_REST_Templates_Controller::get_template_fallback
*/
public function test_get_template_fallback_not_found() {
wp_set_current_user( self::$admin_id );
$request = new WP_REST_Request( 'GET', '/wp/v2/templates/lookup' );
$request->set_param( 'slug', 'not-found' );
$response = rest_get_server()->dispatch( $request );
$this->assertErrorResponse( 'rest_template_not_found', $response, 404 );
}

/**
* @ticket 57851
*
Expand Down

0 comments on commit 4644ea1

Please sign in to comment.