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.

Built from https://develop.svn.wordpress.org/trunk@52068


git-svn-id: http://core.svn.wordpress.org/trunk@51660 1a063a9b-81f0-0310-95a4-ce76da25c4cd
  • Loading branch information
TimothyBJacobs committed Nov 9, 2021
1 parent 5c47273 commit 84dae82
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 9 deletions.
10 changes: 10 additions & 0 deletions 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
16 changes: 13 additions & 3 deletions wp-includes/rest-api/endpoints/class-wp-rest-posts-controller.php
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
16 changes: 13 additions & 3 deletions wp-includes/rest-api/endpoints/class-wp-rest-terms-controller.php
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: 1 addition & 1 deletion wp-includes/version.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.9-alpha-52067';
$wp_version = '5.9-alpha-52068';

/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
Expand Down

0 comments on commit 84dae82

Please sign in to comment.