From 54c97ab01564548ed2af46b252cf2914638b7146 Mon Sep 17 00:00:00 2001 From: isabel_brison Date: Tue, 27 Jun 2023 05:36:22 +0000 Subject: [PATCH] REST API: ignore empty templates. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Ignore empty templates when using the `/lookup` endpoint in the `WP_REST_Templates_Controller` class. Props ramonopoly, jorgefilipecosta, spacedmonkey, youknowriad, mukesh27. Fixes #58615. Built from https://develop.svn.wordpress.org/trunk@56051 git-svn-id: http://core.svn.wordpress.org/trunk@55563 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- .../class-wp-rest-templates-controller.php | 13 ++++++++++--- wp-includes/version.php | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php b/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php index d1854c5073..4434984af0 100644 --- a/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php +++ b/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php @@ -150,14 +150,21 @@ class WP_REST_Templates_Controller extends WP_REST_Controller { * Returns the fallback template for the given slug. * * @since 6.1.0 + * @since 6.3.0 Ignore empty templates. * * @param WP_REST_Request $request The request instance. * @return WP_REST_Response|WP_Error */ public function get_template_fallback( $request ) { - $hierarchy = get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] ); - $fallback_template = resolve_block_template( $request['slug'], $hierarchy, '' ); - $response = $this->prepare_item_for_response( $fallback_template, $request ); + $hierarchy = get_template_hierarchy( $request['slug'], $request['is_custom'], $request['template_prefix'] ); + + do { + $fallback_template = resolve_block_template( $request['slug'], $hierarchy, '' ); + array_shift( $hierarchy ); + } while ( ! empty( $hierarchy ) && empty( $fallback_template->content ) ); + + $response = $this->prepare_item_for_response( $fallback_template, $request ); + return rest_ensure_response( $response ); } diff --git a/wp-includes/version.php b/wp-includes/version.php index 2e8a5215a6..285dfd9d31 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -16,7 +16,7 @@ * * @global string $wp_version */ -$wp_version = '6.3-alpha-56050'; +$wp_version = '6.3-alpha-56051'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.