Make WordPress Core

Changeset 57659


Ignore:
Timestamp:
02/20/2024 09:08:10 AM (2 years ago)
Author:
swissspidy
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r57623 r57659  
    744744                                        }
    745745
     746                                        // Embedded resources get passed context=embed.
     747                                        if ( empty( $request['context'] ) ) {
     748                                                $request['context'] = 'embed';
     749                                        }
     750
    746751                                        if ( empty( $request['per_page'] ) ) {
    747752                                                $matched = $this->match_request_to_handler( $request );
     
    749754                                                        $request['per_page'] = (int) $matched[1]['args']['per_page']['maximum'];
    750755                                                }
    751                                         }
    752 
    753                                         // Embedded resources get passed context=embed.
    754                                         if ( empty( $request['context'] ) ) {
    755                                                 $request['context'] = 'embed';
    756756                                        }
    757757
  • trunk/tests/phpunit/tests/rest-api/rest-navigation-fallback-controller.php

    r57656 r57659  
    161161                $request  = new WP_REST_Request( 'GET', '/wp-block-editor/v1/navigation-fallback' );
    162162                $response = rest_get_server()->dispatch( $request );
    163                 $links    = $response->get_links();
    164 
    165                 // Extract the navigation endpoint URL from the response.
    166                 $embedded_navigation_href = $links['self'][0]['href'];
    167                 preg_match( '/\?rest_route=(.*)/', $embedded_navigation_href, $matches );
    168                 $navigation_endpoint = $matches[1];
    169 
    170                 // Fetch the "linked" navigation post from the endpoint, with the context parameter set to 'embed' to simulate fetching embedded links.
    171                 $request = new WP_REST_Request( 'GET', $navigation_endpoint );
    172                 $request->set_param( 'context', 'embed' );
    173                 $response = rest_get_server()->dispatch( $request );
    174                 $data     = $response->get_data();
     163                $data     = rest_get_server()->response_to_data( $response, true );
     164                $embedded = $data['_embedded']['self'][0];
    175165
    176166                // Verify that the additional status field is present.
    177                 $this->assertArrayHasKey( 'status', $data, 'Response title should contain a "status" field.' );
     167                $this->assertArrayHasKey( 'status', $embedded, 'Response title should contain a "status" field.' );
    178168
    179169                // Verify that the additional content fields are present.
    180                 $this->assertArrayHasKey( 'content', $data, 'Response should contain a "content" field.' );
    181                 $this->assertArrayHasKey( 'raw', $data['content'], 'Response content should contain a "raw" field.' );
    182                 $this->assertArrayHasKey( 'rendered', $data['content'], 'Response content should contain a "rendered" field.' );
    183                 $this->assertArrayHasKey( 'block_version', $data['content'], 'Response should contain a "block_version" field.' );
     170                $this->assertArrayHasKey( 'content', $embedded, 'Response should contain a "content" field.' );
     171                $this->assertArrayHasKey( 'raw', $embedded['content'], 'Response content should contain a "raw" field.' );
     172                $this->assertArrayHasKey( 'rendered', $embedded['content'], 'Response content should contain a "rendered" field.' );
     173                $this->assertArrayHasKey( 'block_version', $embedded['content'], 'Response should contain a "block_version" field.' );
    184174
    185175                // Verify that the additional title.raw field is present.
    186                 $this->assertArrayHasKey( 'raw', $data['title'], 'Response title should contain a "raw" key.' );
     176                $this->assertArrayHasKey( 'raw', $embedded['title'], 'Response title should contain a "raw" key.' );
    187177        }
    188178
Note: See TracChangeset for help on using the changeset viewer.