From ff35fb0539b44851d9e3ab091bf6dc0b58d2be56 Mon Sep 17 00:00:00 2001 From: Grant Kinney Date: Fri, 19 Apr 2024 20:11:04 -0500 Subject: [PATCH] Return a 404 error when no fallback templates are found (wp/v2/templates/lookup) --- ...lass-gutenberg-rest-templates-controller-6-6.php | 4 ++++ packages/edit-post/src/store/selectors.js | 13 ++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php b/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php index 656e38ffe933f..63035960026ce 100644 --- a/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php +++ b/lib/compat/wordpress-6.6/class-gutenberg-rest-templates-controller-6-6.php @@ -113,6 +113,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.', 'default' ), array( 'status' => 404 ) ); + } + $response = $this->prepare_item_for_response( $fallback_template, $request ); return rest_ensure_response( $response ); diff --git a/packages/edit-post/src/store/selectors.js b/packages/edit-post/src/store/selectors.js index 787fc2f1d78d3..8510cbb501955 100644 --- a/packages/edit-post/src/store/selectors.js +++ b/packages/edit-post/src/store/selectors.js @@ -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; } );