Make WordPress Core

Changeset 46434


Ignore:
Timestamp:
10/08/2019 04:43:10 AM (5 years ago)
Author:
kadamwhite
Message:

REST API: Permit embedding of the 'self' link relation in the /search endpoint.

Removes a special-case prohibition against embedding 'self' which prevented ?_embed from being used with the /wp/v2/search endpoint.

Props TimothyBlynJacobs, chrisvanpatten, kadamwhite.
Fixes #47684.

Location:
trunk
Files:
4 edited

Legend:

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

    r46206 r46434  
    565565
    566566        foreach ( $data['_links'] as $rel => $links ) {
    567             // Ignore links to self, for obvious reasons.
    568             if ( 'self' === $rel ) {
    569                 continue;
    570             }
    571 
    572567            $embeds = array();
    573568
  • trunk/tests/phpunit/tests/rest-api/rest-posts-controller.php

    r46280 r46434  
    13641364        $this->assertEquals( rest_url( '/wp/v2/posts/' . self::$post_id ), $links['self'][0]['href'] );
    13651365        $this->assertEquals( rest_url( '/wp/v2/posts' ), $links['collection'][0]['href'] );
     1366        $this->assertArrayNotHasKey( 'embeddable', $links['self'][0]['attributes'] );
    13661367
    13671368        $this->assertEquals( rest_url( '/wp/v2/types/' . get_post_type( self::$post_id ) ), $links['about'][0]['href'] );
  • trunk/tests/phpunit/tests/rest-api/rest-search-controller.php

    r44107 r46434  
    502502
    503503    /**
     504     * @ticket 47684
     505     */
     506    public function test_search_result_links_are_embedded() {
     507        $response = $this->do_request_with_params( array( 'per_page' => 1 ) );
     508        $data     = rest_get_server()->response_to_data( $response, true )[0];
     509
     510        $this->assertArrayHasKey( '_embedded', $data );
     511        $this->assertArrayHasKey( 'self', $data['_embedded'] );
     512        $this->assertCount( 1, $data['_embedded']['self'] );
     513        $this->assertArrayHasKey( WP_REST_Search_Controller::PROP_ID, $data['_embedded']['self'][0] );
     514        $this->assertEquals( $data[ WP_REST_Search_Controller::PROP_ID ], $data['_embedded']['self'][0][ WP_REST_Search_Controller::PROP_ID ] );
     515    }
     516
     517    /**
    504518     * Perform a REST request to our search endpoint with given parameters.
    505519     */
  • trunk/tests/phpunit/tests/rest-api/rest-server.php

    r45809 r46434  
    570570    /**
    571571     * @depends test_link_embedding
     572     * @ticket 47684
    572573     */
    573574    public function test_link_embedding_self() {
     
    583584        $response = new WP_REST_Response();
    584585
    585         // 'self' should be ignored.
    586         $response->add_link( 'self', rest_url( '/test/notembeddable' ), array( 'embeddable' => true ) );
     586        // 'self' should not be special-cased, and may be marked embeddable.
     587        $response->add_link( 'self', rest_url( '/test/embeddable' ), array( 'embeddable' => true ) );
     588
     589        $data = rest_get_server()->response_to_data( $response, true );
     590
     591        $this->assertArrayHasKey( '_embedded', $data );
     592    }
     593
     594    /**
     595     * @depends test_link_embedding
     596     * @ticket 47684
     597     */
     598    public function test_link_embedding_self_non_embeddable() {
     599        // Register our testing route.
     600        rest_get_server()->register_route(
     601            'test',
     602            '/test/embeddable',
     603            array(
     604                'methods'  => 'GET',
     605                'callback' => array( $this, 'embedded_response_callback' ),
     606            )
     607        );
     608        $response = new WP_REST_Response();
     609
     610        // 'self' should not be special-cased, and should be ignored if not marked embeddable.
     611        $response->add_link( 'self', rest_url( '/test/notembeddable' ) );
    587612
    588613        $data = rest_get_server()->response_to_data( $response, true );
Note: See TracChangeset for help on using the changeset viewer.