Make WordPress Core

Changeset 58080


Ignore:
Timestamp:
05/02/2024 04:27:32 PM (5 months ago)
Author:
jorbin
Message:

REST API: Return empty object when no fallback templates are found (wp/v2/templates/lookup)

This prevents a number of php notices that are surfaced due to the endpoint being called on load of the post editor even when there are no templates.

Reviewed by joemcgill.
Merges [58079] to the 6.5 branch.

Props grantmkin, CookiesForDevo, britner, wildworks, jorbin.
Fixes #60909.

Location:
branches/6.5
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/6.5

  • branches/6.5/src/wp-includes/rest-api/endpoints/class-wp-rest-templates-controller.php

    r58041 r58080  
    166166        } while ( ! empty( $hierarchy ) && empty( $fallback_template->content ) );
    167167
    168         $response = $this->prepare_item_for_response( $fallback_template, $request );
     168        // To maintain original behavior, return an empty object rather than a 404 error when no template is found.
     169        $response = $fallback_template ? $this->prepare_item_for_response( $fallback_template, $request ) : new stdClass();
    169170
    170171        return rest_ensure_response( $response );
  • branches/6.5/tests/phpunit/tests/rest-api/wpRestTemplatesController.php

    r58041 r58080  
    911911
    912912    /**
     913     * @ticket 60909
     914     * @covers WP_REST_Templates_Controller::get_template_fallback
     915     */
     916    public function test_get_template_fallback_not_found() {
     917        wp_set_current_user( self::$admin_id );
     918        $request = new WP_REST_Request( 'GET', '/wp/v2/templates/lookup' );
     919        $request->set_param( 'slug', 'not-found' );
     920        $response = rest_get_server()->dispatch( $request );
     921        $data     = $response->get_data();
     922        $this->assertEquals( new stdClass(), $data, 'Response should be an empty object when a fallback template is not found.' );
     923    }
     924
     925    /**
    913926     * @ticket 57851
    914927     *
Note: See TracChangeset for help on using the changeset viewer.