Skip to content

Commit

Permalink
REST API: Add batch support for posts and terms controllers.
Browse files Browse the repository at this point in the history
This also exposes the value of `allow_batch` in `OPTIONS` requests to a route.

A future commit will add batch support to more resources.

Props spacedmonkey, chrisvanpatten.
See #53063.


git-svn-id: https://develop.svn.wordpress.org/trunk@52068 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
TimothyBJacobs committed Nov 9, 2021
1 parent 29615a6 commit ecf1d6a
Show file tree
Hide file tree
Showing 13 changed files with 188 additions and 9 deletions.
10 changes: 10 additions & 0 deletions src/wp-includes/rest-api/class-wp-rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -1403,13 +1403,17 @@ public function get_data_for_route( $route, $callbacks, $context = 'view' ) {
'endpoints' => array(),
);

$allow_batch = false;

if ( isset( $this->route_options[ $route ] ) ) {
$options = $this->route_options[ $route ];

if ( isset( $options['namespace'] ) ) {
$data['namespace'] = $options['namespace'];
}

$allow_batch = isset( $options['allow_batch'] ) ? $options['allow_batch'] : false;

if ( isset( $options['schema'] ) && 'help' === $context ) {
$data['schema'] = call_user_func( $options['schema'] );
}
Expand All @@ -1430,6 +1434,12 @@ public function get_data_for_route( $route, $callbacks, $context = 'view' ) {
'methods' => array_keys( $callback['methods'] ),
);

$callback_batch = isset( $callback['allow_batch'] ) ? $callback['allow_batch'] : $allow_batch;

if ( $callback_batch ) {
$endpoint_data['allow_batch'] = $callback_batch;
}

if ( isset( $callback['args'] ) ) {
$endpoint_data['args'] = array();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
class WP_REST_Attachments_Controller extends WP_REST_Posts_Controller {

/**
* Whether the controller supports batching.
*
* @since 5.9.0
* @var false
*/
protected $allow_batch = false;

/**
* Registers the routes for attachments.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
*/
protected $password_check_passed = array();

/**
* Whether the controller supports batching.
*
* @since 5.9.0
* @var array
*/
protected $allow_batch = array( 'v1' => true );

/**
* Constructor.
*
Expand Down Expand Up @@ -80,7 +88,8 @@ public function register_routes() {
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_public_item_schema' ),
'allow_batch' => $this->allow_batch,
'schema' => array( $this, 'get_public_item_schema' ),
)
);

Expand All @@ -98,7 +107,7 @@ public function register_routes() {
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'args' => array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the post.' ),
'type' => 'integer',
Expand Down Expand Up @@ -128,7 +137,8 @@ public function register_routes() {
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
'allow_batch' => $this->allow_batch,
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
*/
protected $total_terms;

/**
* Whether the controller supports batching.
*
* @since 5.9.0
* @var array
*/
protected $allow_batch = array( 'v1' => true );

/**
* Constructor.
*
Expand Down Expand Up @@ -89,15 +97,16 @@ public function register_routes() {
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::CREATABLE ),
),
'schema' => array( $this, 'get_public_item_schema' ),
'allow_batch' => $this->allow_batch,
'schema' => array( $this, 'get_public_item_schema' ),
)
);

register_rest_route(
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'args' => array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the term.' ),
'type' => 'integer',
Expand Down Expand Up @@ -129,7 +138,8 @@ public function register_routes() {
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
'allow_batch' => $this->allow_batch,
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
*/
protected $widgets_retrieved = false;

/**
* Whether the controller supports batching.
*
* @since 5.9.0
* @var array
*/
protected $allow_batch = array( 'v1' => true );

/**
* Widgets controller constructor.
*
Expand Down Expand Up @@ -56,7 +64,7 @@ public function register_routes() {
'permission_callback' => array( $this, 'create_item_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema(),
),
'allow_batch' => array( 'v1' => true ),
'allow_batch' => $this->allow_batch,
'schema' => array( $this, 'get_public_item_schema' ),
)
);
Expand Down Expand Up @@ -90,7 +98,7 @@ public function register_routes() {
),
),
),
'allow_batch' => array( 'v1' => true ),
'allow_batch' => $this->allow_batch,
'schema' => array( $this, 'get_public_item_schema' ),
)
);
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/rest-api/rest-attachments-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public function test_context_param() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][0] );
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
// Single.
Expand All @@ -174,6 +175,7 @@ public function test_context_param() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/media/' . $attachment_id );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][0] );
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/rest-api/rest-categories-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ public function test_context_param() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/categories' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( array( 'v1' => true ), $data['endpoints'][0]['allow_batch'] );
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSameSets( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
// Single.
$category1 = $this->factory->category->create( array( 'name' => 'Season 5' ) );
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/categories/' . $category1 );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( array( 'v1' => true ), $data['endpoints'][0]['allow_batch'] );
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSameSets( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}
Expand Down
3 changes: 2 additions & 1 deletion tests/phpunit/tests/rest-api/rest-pages-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ public function test_registered_query_params() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/pages' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$keys = array_keys( $data['endpoints'][0]['args'] );
$this->assertSame( array( 'v1' => true ), $data['endpoints'][0]['allow_batch'] );
$keys = array_keys( $data['endpoints'][0]['args'] );
sort( $keys );
$this->assertSame(
array(
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/rest-api/rest-posts-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,14 @@ public function test_context_param() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( array( 'v1' => true ), $data['endpoints'][0]['allow_batch'] );
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
// Single.
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/posts/' . self::$post_id );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( array( 'v1' => true ), $data['endpoints'][0]['allow_batch'] );
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}
Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/rest-api/rest-server.php
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,38 @@ public function test_allow_header_send_only_permitted_methods() {
$this->assertSame( $sent_headers['Allow'], 'POST' );
}

/**
* @ticket 53063
*/
public function test_batched_options() {
register_rest_route(
'test-ns',
'/test',
array(
array(
'methods' => array( 'GET' ),
'callback' => '__return_null',
'permission_callback' => '__return_true',
),
array(
'methods' => array( 'POST' ),
'callback' => '__return_null',
'permission_callback' => '__return_null',
'allow_batch' => false,
),
'allow_batch' => array( 'v1' => true ),
)
);

$request = new WP_REST_Request( 'OPTIONS', '/test-ns/test' );
$response = rest_get_server()->dispatch( $request );

$data = $response->get_data();

$this->assertSame( array( 'v1' => true ), $data['endpoints'][0]['allow_batch'] );
$this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][1] );
}

public function test_allow_header_sent_on_options_request() {
register_rest_route(
'test-ns',
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/rest-api/rest-tags-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ public function test_context_param() {
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags' );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( array( 'v1' => true ), $data['endpoints'][0]['allow_batch'] );
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSameSets( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
// Single.
$tag1 = $this->factory->tag->create( array( 'name' => 'Season 5' ) );
$request = new WP_REST_Request( 'OPTIONS', '/wp/v2/tags/' . $tag1 );
$response = rest_get_server()->dispatch( $request );
$data = $response->get_data();
$this->assertSame( array( 'v1' => true ), $data['endpoints'][0]['allow_batch'] );
$this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] );
$this->assertSameSets( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] );
}
Expand Down
2 changes: 2 additions & 0 deletions tests/phpunit/tests/rest-api/rest-widgets-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,8 @@ public function test_get_item_schema() {
$data = $response->get_data();
$properties = $data['schema']['properties'];

$this->assertSame( array( 'v1' => true ), $data['endpoints'][0]['allow_batch'] );

$this->assertCount( 7, $properties );
$this->assertArrayHasKey( 'id', $properties );
$this->assertArrayHasKey( 'id_base', $properties );
Expand Down
Loading

0 comments on commit ecf1d6a

Please sign in to comment.