Make WordPress Core

Changeset 53110


Ignore:
Timestamp:
04/08/2022 05:27:42 PM (2 years ago)
Author:
spacedmonkey
Message:

REST API: Use rest_parse_embed_param function in WP_REST_Server class.

Ensure that the value get parameter _embed that is passed to the envelope_response method, is run through the rest_parse_embed_param function.

Props Spacedmonkey, johnbillion, TimothyBlynJacobs.
Fixes #54015.

Location:
trunk
Files:
2 edited

Legend:

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

    r52796 r53110  
    439439        // Wrap the response in an envelope if asked for.
    440440        if ( isset( $_GET['_envelope'] ) ) {
    441             $result = $this->envelope_response( $result, isset( $_GET['_embed'] ) );
     441            $embed  = isset( $_GET['_embed'] ) ? rest_parse_embed_param( $_GET['_embed'] ) : false;
     442            $result = $this->envelope_response( $result, $embed );
    442443        }
    443444
     
    731732     *
    732733     * @since 4.4.0
     734     * @since 6.0.0 The $embed parameter can now contain a list of link relations to include
    733735     *
    734736     * @param WP_REST_Response $response Response object.
    735      * @param bool             $embed    Whether links should be embedded.
     737     * @param bool|string[]    $embed    Whether to embed all links, a filtered list of link relations, or no links.
    736738     * @return WP_REST_Response New response with wrapped data
    737739     */
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r52381 r53110  
    7474
    7575        $this->assertSame( $data, $enveloped['body'] );
     76        $this->assertSame( $status, $enveloped['status'] );
     77        $this->assertSame( $headers, $enveloped['headers'] );
     78    }
     79
     80    /**
     81     * @dataProvider data_envelope_params
     82     * @ticket 54015
     83     */
     84    public function test_envelope_param( $_embed ) {
     85        // Register our testing route.
     86        rest_get_server()->register_route(
     87            'test',
     88            '/test/embeddable',
     89            array(
     90                'methods'  => 'GET',
     91                'callback' => array( $this, 'embedded_response_callback' ),
     92            )
     93        );
     94        $data    = array(
     95            'amount of arbitrary data' => 'alot',
     96        );
     97        $status  = 987;
     98        $headers = array(
     99            'Arbitrary-Header' => 'value',
     100            'Multiple'         => 'maybe, yes',
     101        );
     102
     103        $response = new WP_REST_Response( $data, $status );
     104        $response->header( 'Arbitrary-Header', 'value' );
     105
     106        // Check header concatenation as well.
     107        $response->header( 'Multiple', 'maybe' );
     108        $response->header( 'Multiple', 'yes', false );
     109
     110        // All others should be embedded.
     111        $response->add_link( 'alternate', rest_url( '/test/embeddable' ), array( 'embeddable' => true ) );
     112
     113        $embed             = rest_parse_embed_param( $_embed );
     114        $envelope_response = rest_get_server()->envelope_response( $response, $embed );
     115
     116        // The envelope should still be a response, but with defaults.
     117        $this->assertInstanceOf( WP_REST_Response::class, $envelope_response );
     118        $this->assertSame( 200, $envelope_response->get_status() );
     119        $this->assertEmpty( $envelope_response->get_headers() );
     120        $this->assertEmpty( $envelope_response->get_links() );
     121
     122        $enveloped = $envelope_response->get_data();
     123
     124        $this->assertArrayHasKey( 'body', $enveloped );
     125        $this->assertArrayHasKey( '_links', $enveloped['body'] );
     126        $this->assertArrayHasKey( '_embedded', $enveloped['body'] );
     127        $this->assertArrayHasKey( 'alternate', $enveloped['body']['_embedded'] );
     128
     129        $alternate = $enveloped['body']['_embedded']['alternate'];
     130        $this->assertCount( 1, $alternate );
     131
    76132        $this->assertSame( $status, $enveloped['status'] );
    77133        $this->assertSame( $headers, $enveloped['headers'] );
     
    21742230        return rest_get_server()->sent_headers;
    21752231    }
     2232
     2233    /**
     2234     * Data provider.
     2235     *
     2236     * @return array
     2237     */
     2238    public function data_envelope_params() {
     2239        return array(
     2240            array( '1' ),
     2241            array( 'true' ),
     2242            array( false ),
     2243            array( 'alternate' ),
     2244            array( array( 'alternate' ) ),
     2245        );
     2246    }
    21762247}
Note: See TracChangeset for help on using the changeset viewer.