Make WordPress Core


Ignore:
Timestamp:
10/27/2016 01:41:08 AM (8 years ago)
Author:
boonebgorges
Message:

REST API: Use wp_get_object_terms() when fetching terms for a post object.

The WP-API plugin originally used a custom method for fetching object
terms in a way that supported the object cache and also accepted all
parameters for get_terms(). In [38667], the internals of
wp_get_object_terms() were modified to use WP_Term_Query, thus
delivering in a native fashion the features that the API had
previously achieved bespokely.

Fixes #38504.

File:
1 edited

Legend:

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

    r38960 r38974  
    771771    }
    772772
     773    /**
     774     * @ticket 38504
     775     */
     776    public function test_object_term_queries_are_cached() {
     777        global $wpdb;
     778
     779        $tags = $this->factory->tag->create_many( 2 );
     780        $p = $this->factory->post->create();
     781        wp_set_object_terms( $p, $tags[0], 'post_tag' );
     782
     783        $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     784        $request->set_param( 'post', $p );
     785        $response = $this->server->dispatch( $request );
     786        $found_1 = wp_list_pluck( $response->data, 'id' );
     787
     788        unset( $request, $response );
     789
     790        $num_queries = $wpdb->num_queries;
     791
     792        $request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
     793        $request->set_param( 'post', $p );
     794        $response = $this->server->dispatch( $request );
     795        $found_2 = wp_list_pluck( $response->data, 'id' );
     796
     797        $this->assertEqualSets( $found_1, $found_2 );
     798        $this->assertSame( $num_queries, $wpdb->num_queries );
     799    }
     800
    773801    public function additional_field_get_callback( $object, $request ) {
    774802        return 123;
Note: See TracChangeset for help on using the changeset viewer.