Make WordPress Core

Ticket #39696: 39696.1.diff

File 39696.1.diff, 4.8 KB (added by rheinardkorf, 8 years ago)
  • src/wp-includes/rest-api/class-wp-rest-server.php

    diff --git src/wp-includes/rest-api/class-wp-rest-server.php src/wp-includes/rest-api/class-wp-rest-server.php
    index ec8d2a0..cd7f9fe 100644
    class WP_REST_Server { 
    544544
    545545                $embedded = array();
    546546
     547                // Get link relationships to filter on, but only if not _embed=1 or _embed=true or empty _embed=
     548                $filtered_rels = false;
     549                if ( isset( $_GET['_embed'] ) && '1' !==  $_GET['_embed'] && 'true' !== $_GET['_embed'] ) {
     550                        $filtered_rels = array_filter( is_array( $_GET['_embed'] ) ? $_GET['_embed'] : explode( ',', $_GET['_embed'] ) );
     551                }
     552
    547553                foreach ( $data['_links'] as $rel => $links ) {
    548554                        // Ignore links to self, for obvious reasons.
    549555                        if ( 'self' === $rel ) {
    550556                                continue;
    551557                        }
    552558
     559                        // If filtered rels are specified and current $rel is not in the list, then continue.
     560                        if ( ! empty( $filtered_rels ) && ! in_array( $rel, $filtered_rels, true ) ) {
     561                                continue;
     562                        }
     563
    553564                        $embeds = array();
    554565
    555566                        foreach ( $links as $item ) {
  • tests/phpunit/tests/rest-api/rest-posts-controller.php

    diff --git tests/phpunit/tests/rest-api/rest-posts-controller.php tests/phpunit/tests/rest-api/rest-posts-controller.php
    index f48709e..98cf0ce 100644
    class WP_Test_REST_Posts_Controller extends WP_Test_REST_Post_Type_Controller_Te 
    942942                $this->assertEquals( rest_url( '/wp/v2/users/' . self::$author_id ), $links['author'][0]['href'] );
    943943        }
    944944
     945        public function test_get_item__embed() {
     946
     947                // Prepare a new post. This will be first item when querying /wp/v2/posts
     948                wp_set_current_user( self::$editor_id );
     949                $post_id   = $this->factory->post->create();
     950                $category1 = wp_insert_term( 'Embed Test Category', 'category' );
     951                $category2 = wp_insert_term( 'Second Embed Test Category', 'category' );
     952                wp_set_post_categories( $post_id, array( $category1['term_id'], $category2['term_id'] ) );
     953                $this->factory->comment->create_post_comments( $post_id, 1 );
     954
     955                // Not ideal, but since WP_REST_Server uses $_GET for _embed we need to use this in the tests.
     956                // ?_embed=1 should return all expected embeds. In this case 'author', 'replies' and 'wp:term'.
     957                $_GET     = array( '_embed' => '1' );
     958                $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     959                $response = $this->server->dispatch( $request );
     960                $data     = $response->get_data();
     961                $data     = array_shift( $data );
     962                $embeds   = $this->server->embed_links( $data );
     963
     964                $this->assertArrayHasKey( 'author', $embeds['_embedded'] );
     965                $this->assertArrayHasKey( 'replies', $embeds['_embedded'] );
     966                $this->assertArrayHasKey( 'wp:term', $embeds['_embedded'] );
     967
     968                // Not ideal, but since WP_REST_Server uses $_GET for _embed we need to use this in the tests.
     969                // ?_embed=true should return all expected embeds. In this case 'author', 'replies' and 'wp:term'.
     970                $_GET     = array( '_embed' => 'true' );
     971                $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     972                $response = $this->server->dispatch( $request );
     973                $data     = $response->get_data();
     974                $data     = array_shift( $data );
     975                $embeds   = $this->server->embed_links( $data );
     976
     977                $this->assertArrayHasKey( 'author', $embeds['_embedded'] );
     978                $this->assertArrayHasKey( 'replies', $embeds['_embedded'] );
     979                $this->assertArrayHasKey( 'wp:term', $embeds['_embedded'] );
     980
     981                // Not ideal, but since WP_REST_Server uses $_GET for _embed we need to use this in the tests.
     982                // ?_embed=author,wp:term should return only relevant embeds.
     983                $_GET     = array( '_embed' => 'author,wp:term' );
     984                $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     985                $response = $this->server->dispatch( $request );
     986                $data     = $response->get_data();
     987                $data     = array_shift( $data );
     988                $embeds   = $this->server->embed_links( $data );
     989
     990                $this->assertArrayHasKey( 'author', $embeds['_embedded'] );
     991                $this->assertArrayNotHasKey( 'replies', $embeds['_embedded'] );
     992                $this->assertArrayHasKey( 'wp:term', $embeds['_embedded'] );
     993
     994                // Not ideal, but since WP_REST_Server uses $_GET for _embed we need to use this in the tests.
     995                // ?_embed[]=author&_embed[]=replies should return only relevant embeds.
     996                $_GET = array(
     997                        '_embed' => array(
     998                                'author',
     999                                'replies',
     1000                        )
     1001                );
     1002
     1003                $request  = new WP_REST_Request( 'GET', '/wp/v2/posts' );
     1004                $response = $this->server->dispatch( $request );
     1005                $data     = $response->get_data();
     1006                $data     = array_shift( $data );
     1007                $embeds   = $this->server->embed_links( $data );
     1008
     1009                $this->assertArrayHasKey( 'author', $embeds['_embedded'] );
     1010                $this->assertArrayHasKey( 'replies', $embeds['_embedded'] );
     1011                $this->assertArrayNotHasKey( 'wp:term', $embeds['_embedded'] );
     1012        }
     1013
    9451014        public function test_get_post_without_permission() {
    9461015                $draft_id = $this->factory->post->create( array(
    9471016                        'post_status' => 'draft',