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.

git-svn-id: https://develop.svn.wordpress.org/trunk@38974 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Boone Gorges
2016-10-27 01:41:08 +00:00
parent 3e77cef88a
commit 5da7a1dccf
2 changed files with 42 additions and 82 deletions

View File

@@ -770,6 +770,34 @@ class WP_Test_REST_Tags_Controller extends WP_Test_REST_Controller_Testcase {
$wp_rest_additional_fields = array();
}
/**
* @ticket 38504
*/
public function test_object_term_queries_are_cached() {
global $wpdb;
$tags = $this->factory->tag->create_many( 2 );
$p = $this->factory->post->create();
wp_set_object_terms( $p, $tags[0], 'post_tag' );
$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
$request->set_param( 'post', $p );
$response = $this->server->dispatch( $request );
$found_1 = wp_list_pluck( $response->data, 'id' );
unset( $request, $response );
$num_queries = $wpdb->num_queries;
$request = new WP_REST_Request( 'GET', '/wp/v2/tags' );
$request->set_param( 'post', $p );
$response = $this->server->dispatch( $request );
$found_2 = wp_list_pluck( $response->data, 'id' );
$this->assertEqualSets( $found_1, $found_2 );
$this->assertSame( $num_queries, $wpdb->num_queries );
}
public function additional_field_get_callback( $object, $request ) {
return 123;
}