Skip to content

Commit

Permalink
Return an empty object when no fallback templates are found (wp/v2/te…
Browse files Browse the repository at this point in the history
…mplates/lookup) (#60925)

Unlinked contributors: oryfoxer.

Co-authored-by: creativecoder <grantmkin@git.wordpress.org>
Co-authored-by: joemcgill <joemcgill@git.wordpress.org>
Co-authored-by: t-hamano <wildworks@git.wordpress.org>
Co-authored-by: david-binda <davidbinda@git.wordpress.org>
  • Loading branch information
5 people authored May 22, 2024
1 parent 6fbc6c7 commit f029914
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,8 @@ public function get_template_fallback( $request ) {
array_shift( $hierarchy );
} while ( ! empty( $hierarchy ) && empty( $fallback_template->content ) );

$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 packages/core-data/src/resolvers.js
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,8 @@ export const getDefaultTemplateId =
const template = await apiFetch( {
path: addQueryArgs( '/wp/v2/templates/lookup', query ),
} );
if ( template ) {
// Endpoint may return an empty object if no template is found.
if ( template?.id ) {
dispatch.receiveDefaultTemplateId( query, template.id );
}
};
Expand Down
13 changes: 8 additions & 5 deletions packages/edit-post/src/store/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,10 +609,13 @@ export const getEditedPostTemplate = createRegistrySelector(
const defaultTemplateId = select( coreStore ).getDefaultTemplateId( {
slug: slugToCheck,
} );
return select( coreStore ).getEditedEntityRecord(
'postType',
'wp_template',
defaultTemplateId
);

return defaultTemplateId
? select( coreStore ).getEditedEntityRecord(
'postType',
'wp_template',
defaultTemplateId
)
: null;
}
);

1 comment on commit f029914

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in f029914.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/9198852118
📝 Reported issues:

Please sign in to comment.