REST API: Pass correct context to embedded items.

Fixes a regression introduced in [57623] where navigation embed items were missing `raw` property values.

Props mamaduka, swissspidy, youknowriad, timothyblynjacobs.
Fixes #43439.

git-svn-id: https://develop.svn.wordpress.org/trunk@57659 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Pascal Birchler 2024-02-20 09:08:10 +00:00
parent 9173f8baa9
commit 51f0dd6bd8
2 changed files with 13 additions and 23 deletions

View File

@ -743,6 +743,11 @@ class WP_REST_Server {
continue;
}
// Embedded resources get passed context=embed.
if ( empty( $request['context'] ) ) {
$request['context'] = 'embed';
}
if ( empty( $request['per_page'] ) ) {
$matched = $this->match_request_to_handler( $request );
if ( ! is_wp_error( $matched ) && isset( $matched[1]['args']['per_page']['maximum'] ) ) {
@ -750,11 +755,6 @@ class WP_REST_Server {
}
}
// Embedded resources get passed context=embed.
if ( empty( $request['context'] ) ) {
$request['context'] = 'embed';
}
$response = $this->dispatch( $request );
/** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */

View File

@ -160,30 +160,20 @@ class WP_REST_Navigation_Fallback_Controller_Test extends WP_Test_REST_Controlle
// First we'll use the navigation fallback to get a link to the navigation endpoint.
$request = new WP_REST_Request( 'GET', '/wp-block-editor/v1/navigation-fallback' );
$response = rest_get_server()->dispatch( $request );
$links = $response->get_links();
// Extract the navigation endpoint URL from the response.
$embedded_navigation_href = $links['self'][0]['href'];
preg_match( '/\?rest_route=(.*)/', $embedded_navigation_href, $matches );
$navigation_endpoint = $matches[1];
// Fetch the "linked" navigation post from the endpoint, with the context parameter set to 'embed' to simulate fetching embedded links.
$request = new WP_REST_Request( 'GET', $navigation_endpoint );
$request->set_param( 'context', 'embed' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$data = rest_get_server()->response_to_data( $response, true );
$embedded = $data['_embedded']['self'][0];
// Verify that the additional status field is present.
$this->assertArrayHasKey( 'status', $data, 'Response title should contain a "status" field.' );
$this->assertArrayHasKey( 'status', $embedded, 'Response title should contain a "status" field.' );
// Verify that the additional content fields are present.
$this->assertArrayHasKey( 'content', $data, 'Response should contain a "content" field.' );
$this->assertArrayHasKey( 'raw', $data['content'], 'Response content should contain a "raw" field.' );
$this->assertArrayHasKey( 'rendered', $data['content'], 'Response content should contain a "rendered" field.' );
$this->assertArrayHasKey( 'block_version', $data['content'], 'Response should contain a "block_version" field.' );
$this->assertArrayHasKey( 'content', $embedded, 'Response should contain a "content" field.' );
$this->assertArrayHasKey( 'raw', $embedded['content'], 'Response content should contain a "raw" field.' );
$this->assertArrayHasKey( 'rendered', $embedded['content'], 'Response content should contain a "rendered" field.' );
$this->assertArrayHasKey( 'block_version', $embedded['content'], 'Response should contain a "block_version" field.' );
// Verify that the additional title.raw field is present.
$this->assertArrayHasKey( 'raw', $data['title'], 'Response title should contain a "raw" key.' );
$this->assertArrayHasKey( 'raw', $embedded['title'], 'Response title should contain a "raw" key.' );
}
private function get_navigations_in_database() {