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 and ajlende committed May 22, 2024
1 parent 32f78d9 commit afe6c6b
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;
}
);

0 comments on commit afe6c6b

Please sign in to comment.