Make WordPress Core

Changeset 50005


Ignore:
Timestamp:
01/23/2021 11:25:40 PM (6 years ago)
Author:
TimothyBlynJacobs
Message:

REST API: Support embedding links in rest_preload_api_request().

Props lpawlik, spacedmonkey.
Fixes #51722.

Location:
trunk
Files:
2 edited

Legend:

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

    r49611 r50005  
    25212521        if ( 200 === $response->status ) {
    25222522                $server = rest_get_server();
    2523                 $data   = (array) $response->get_data();
    2524                 $links  = $server::get_compact_response_links( $response );
    2525                 if ( ! empty( $links ) ) {
    2526                         $data['_links'] = $links;
    2527                 }
     2523                $embed  = $request->has_param( '_embed' ) ? rest_parse_embed_param( $request['_embed'] ) : false;
     2524                $data   = (array) $server->response_to_data( $response, $embed );
    25282525
    25292526                if ( 'OPTIONS' === $method ) {
  • trunk/tests/phpunit/tests/rest-api.php

    r49246 r50005  
    23292329                );
    23302330        }
     2331
     2332        /**
     2333         * @ticket 51722
     2334         * @dataProvider data_rest_preload_api_request_embeds_links
     2335         *
     2336         * @param string   $embed        The embed parameter.
     2337         * @param string[] $expected     The list of link relations that should be embedded.
     2338         * @param string[] $not_expected The list of link relations that should not be embedded.
     2339         */
     2340        public function test_rest_preload_api_request_embeds_links( $embed, $expected, $not_expected ) {
     2341                wp_set_current_user( 1 );
     2342                $post_id = self::factory()->post->create();
     2343                self::factory()->comment->create_post_comments( $post_id );
     2344
     2345                $url           = sprintf( '/wp/v2/posts/%d?%s', $post_id, $embed );
     2346                $preload_paths = array( $url );
     2347
     2348                $preload_data = array_reduce(
     2349                        $preload_paths,
     2350                        'rest_preload_api_request',
     2351                        array()
     2352                );
     2353
     2354                $this->assertSame( array_keys( $preload_data ), $preload_paths );
     2355                $this->assertArrayHasKey( 'body', $preload_data[ $url ] );
     2356                $this->assertArrayHasKey( '_links', $preload_data[ $url ]['body'] );
     2357
     2358                if ( $expected ) {
     2359                        $this->assertArrayHasKey( '_embedded', $preload_data[ $url ]['body'] );
     2360                } else {
     2361                        $this->assertArrayNotHasKey( '_embedded', $preload_data[ $url ]['body'] );
     2362                }
     2363
     2364                foreach ( $expected as $rel ) {
     2365                        $this->assertArrayHasKey( $rel, $preload_data[ $url ]['body']['_embedded'] );
     2366                }
     2367
     2368                foreach ( $not_expected as $rel ) {
     2369                        $this->assertArrayNotHasKey( $rel, $preload_data[ $url ]['body']['_embedded'] );
     2370                }
     2371        }
     2372
     2373        public function data_rest_preload_api_request_embeds_links() {
     2374                return array(
     2375                        array( '_embed=wp:term,author', array( 'wp:term', 'author' ), array( 'replies' ) ),
     2376                        array( '_embed[]=wp:term&_embed[]=author', array( 'wp:term', 'author' ), array( 'replies' ) ),
     2377                        array( '_embed', array( 'wp:term', 'author', 'replies' ), array() ),
     2378                        array( '_embed=1', array( 'wp:term', 'author', 'replies' ), array() ),
     2379                        array( '_embed=true', array( 'wp:term', 'author', 'replies' ), array() ),
     2380                        array( '', array(), array() ),
     2381                );
     2382        }
    23312383}
Note: See TracChangeset for help on using the changeset viewer.