Make WordPress Core


Ignore:
Timestamp:
02/09/2020 08:52:06 PM (5 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Introduce selective link embedding.

Previously the _embed flag would embed all embeddable links in a response even if only a subset of the links were necessary. Now, a list of link relations can be passed in the _embed parameter to restrict the list of embedded objects.

Props rheinardkorf, adamsilverstein, jnylen0, cklosows, chrisvanpatten, TimothyBlynJacobs.
Fixes #39696.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r47138 r47224  
    844844        );
    845845        $this->assertEquals( $self_not_filtered, $data['_links']['self'][0] );
     846    }
     847
     848    /**
     849     * @dataProvider _dp_response_to_data_embedding
     850     */
     851    public function test_response_to_data_embedding( $expected, $embed ) {
     852        $response = new WP_REST_Response();
     853        $response->add_link( 'author', rest_url( '404' ), array( 'embeddable' => true ) );
     854        $response->add_link( 'https://api.w.org/term', rest_url( '404' ), array( 'embeddable' => true ) );
     855        $response->add_link( 'https://wordpress.org', rest_url( '404' ), array( 'embeddable' => true ) );
     856        $response->add_link( 'no-embed', rest_url( '404' ) );
     857
     858        $data = rest_get_server()->response_to_data( $response, $embed );
     859
     860        if ( false === $expected ) {
     861            $this->assertArrayNotHasKey( '_embedded', $data );
     862        } else {
     863            $this->assertEqualSets( $expected, array_keys( $data['_embedded'] ) );
     864        }
     865    }
     866
     867    public function _dp_response_to_data_embedding() {
     868        return array(
     869            array(
     870                array( 'author', 'wp:term', 'https://wordpress.org' ),
     871                true,
     872            ),
     873            array(
     874                array( 'author', 'wp:term', 'https://wordpress.org' ),
     875                array( 'author', 'wp:term', 'https://wordpress.org' ),
     876            ),
     877            array(
     878                array( 'author' ),
     879                array( 'author' ),
     880            ),
     881            array(
     882                array( 'wp:term' ),
     883                array( 'wp:term' ),
     884            ),
     885            array(
     886                array( 'https://wordpress.org' ),
     887                array( 'https://wordpress.org' ),
     888            ),
     889            array(
     890                array( 'author', 'wp:term' ),
     891                array( 'author', 'wp:term' ),
     892            ),
     893            array(
     894                false,
     895                false,
     896            ),
     897            array(
     898                false,
     899                array( 'no-embed' ),
     900            ),
     901            array(
     902                array( 'author' ),
     903                array( 'author', 'no-embed' ),
     904            ),
     905        );
    846906    }
    847907
Note: See TracChangeset for help on using the changeset viewer.