Make WordPress Core

Ticket #48838: 48838.3.diff

File 48838.3.diff, 5.4 KB (added by kadamwhite, 5 years ago)

Resolve PHPCS errors

  • 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 c869bdbc05..a14de5c225 100644
    class WP_REST_Server { 
    7878         */
    7979        protected $route_options = array();
    8080
     81        /**
     82         * Caches embedded requests.
     83         *
     84         * @since 5.4.0
     85         * @var array
     86         */
     87        protected $embed_cache = array();
     88
    8189        /**
    8290         * Instantiates the REST server.
    8391         *
    class WP_REST_Server { 
    462470                }
    463471
    464472                if ( $embed ) {
     473                        $this->embed_cache = array();
    465474                        // Determine if this is a numeric array.
    466475                        if ( wp_is_numeric_array( $data ) ) {
    467476                                $data = array_map( array( $this, 'embed_links' ), $data );
    468477                        } else {
    469478                                $data = $this->embed_links( $data );
    470479                        }
     480                        $this->embed_cache = array();
    471481                }
    472482
    473483                return $data;
    class WP_REST_Server { 
    588598                                        continue;
    589599                                }
    590600
    591                                 // Run through our internal routing and serve.
    592                                 $request = WP_REST_Request::from_url( $item['href'] );
    593                                 if ( ! $request ) {
    594                                         $embeds[] = array();
    595                                         continue;
    596                                 }
     601                                if ( ! array_key_exists( $item['href'], $this->embed_cache ) ) {
     602                                        // Run through our internal routing and serve.
     603                                        $request = WP_REST_Request::from_url( $item['href'] );
     604                                        if ( ! $request ) {
     605                                                $embeds[] = array();
     606                                                continue;
     607                                        }
    597608
    598                                 // Embedded resources get passed context=embed.
    599                                 if ( empty( $request['context'] ) ) {
    600                                         $request['context'] = 'embed';
    601                                 }
     609                                        // Embedded resources get passed context=embed.
     610                                        if ( empty( $request['context'] ) ) {
     611                                                $request['context'] = 'embed';
     612                                        }
     613
     614                                        $response = $this->dispatch( $request );
    602615
    603                                 $response = $this->dispatch( $request );
     616                                        /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
     617                                        $response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $this, $request );
    604618
    605                                 /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
    606                                 $response = apply_filters( 'rest_post_dispatch', rest_ensure_response( $response ), $this, $request );
     619                                        $this->embed_cache[ $item['href'] ] = $this->response_to_data( $response, false );
     620                                }
    607621
    608                                 $embeds[] = $this->response_to_data( $response, false );
     622                                $embeds[] = $this->embed_cache[ $item['href'] ];
    609623                        }
    610624
    611625                        // Determine if any real links were found.
  • tests/phpunit/tests/rest-api/rest-server.php

    diff --git tests/phpunit/tests/rest-api/rest-server.php tests/phpunit/tests/rest-api/rest-server.php
    index ec9870f033..671a860f07 100644
    class Tests_REST_Server extends WP_Test_REST_TestCase { 
    677677                $this->assertEquals( 403, $up_data['data']['status'] );
    678678        }
    679679
     680        /**
     681         * @ticket 48838
     682         */
     683        public function test_link_embedding_clears_cache() {
     684                $post_id = self::factory()->post->create();
     685
     686                $response = new WP_REST_Response();
     687                $response->add_link( 'post', rest_url( 'wp/v2/posts/' . $post_id ), array( 'embeddable' => true ) );
     688
     689                $data = rest_get_server()->response_to_data( $response, true );
     690                $this->assertArrayHasKey( 'post', $data['_embedded'] );
     691                $this->assertCount( 1, $data['_embedded']['post'] );
     692
     693                wp_update_post(
     694                        array(
     695                                'ID'         => $post_id,
     696                                'post_title' => 'My Awesome Title',
     697                        )
     698                );
     699
     700                $data = rest_get_server()->response_to_data( $response, true );
     701                $this->assertArrayHasKey( 'post', $data['_embedded'] );
     702                $this->assertCount( 1, $data['_embedded']['post'] );
     703                $this->assertEquals( 'My Awesome Title', $data['_embedded']['post'][0]['title']['rendered'] );
     704        }
     705
     706        /**
     707         * @ticket 48838
     708         */
     709        public function test_link_embedding_cache() {
     710                $response = new WP_REST_Response(
     711                        array(
     712                                'id' => 1,
     713                        )
     714                );
     715                $response->add_link(
     716                        'author',
     717                        rest_url( 'wp/v2/users/1' ),
     718                        array( 'embeddable' => true )
     719                );
     720                $response->add_link(
     721                        'author',
     722                        rest_url( 'wp/v2/users/1' ),
     723                        array( 'embeddable' => true )
     724                );
     725
     726                $mock = new MockAction();
     727                add_filter( 'rest_post_dispatch', array( $mock, 'filter' ) );
     728
     729                $data = rest_get_server()->response_to_data( $response, true );
     730
     731                $this->assertArrayHasKey( '_embedded', $data );
     732                $this->assertArrayHasKey( 'author', $data['_embedded'] );
     733                $this->assertCount( 2, $data['_embedded']['author'] );
     734
     735                $this->assertCount( 1, $mock->get_events() );
     736        }
     737
     738        /**
     739         * @ticket 48838
     740         */
     741        public function test_link_embedding_cache_collection() {
     742                $response = new WP_REST_Response(
     743                        array(
     744                                array(
     745                                        'id'     => 1,
     746                                        '_links' => array(
     747                                                'author' => array(
     748                                                        array(
     749                                                                'href'       => rest_url( 'wp/v2/users/1' ),
     750                                                                'embeddable' => true,
     751                                                        ),
     752                                                ),
     753                                        ),
     754                                ),
     755                                array(
     756                                        'id'     => 2,
     757                                        '_links' => array(
     758                                                'author' => array(
     759                                                        array(
     760                                                                'href'       => rest_url( 'wp/v2/users/1' ),
     761                                                                'embeddable' => true,
     762                                                        ),
     763                                                ),
     764                                        ),
     765                                ),
     766                        )
     767                );
     768
     769                $mock = new MockAction();
     770                add_filter( 'rest_post_dispatch', array( $mock, 'filter' ) );
     771
     772                $data = rest_get_server()->response_to_data( $response, true );
     773
     774                $embeds = wp_list_pluck( $data, '_embedded' );
     775                $this->assertCount( 2, $embeds );
     776                $this->assertArrayHasKey( 'author', $embeds[0] );
     777                $this->assertArrayHasKey( 'author', $embeds[1] );
     778
     779                $this->assertCount( 1, $mock->get_events() );
     780        }
     781
    680782        /**
    681783         * Ensure embedding is a no-op without links in the data.
    682784         */