Skip to content

Commit

Permalink
Return empty object rather than 404 error
Browse files Browse the repository at this point in the history
  • Loading branch information
creativecoder committed May 1, 2024
1 parent 4644ea1 commit 3b9816e
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,6 @@ 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 @@ -166,11 +165,8 @@ 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 );
// To maintain original behavior, return an empty object rather than a 404 error when no template is found.
$response = $fallback_template ? $this->prepare_item_for_response( $fallback_template, $request ) : new stdClass();

return rest_ensure_response( $response );
}
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/tests/rest-api/wpRestTemplatesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -918,7 +918,8 @@ public function test_get_template_fallback_not_found() {
$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 );
$data = $response->get_data();
$this->assertEquals( new stdClass(), $data, 'Response should be an empty object when a fallback template is not found.' );
}

/**
Expand Down

0 comments on commit 3b9816e

Please sign in to comment.