REST API: Add batch support for posts and terms controllers.

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
This commit is contained in:
TimothyBlynJacobs 2021-11-09 01:59:02 +00:00
parent 5c47273f06
commit 84dae82fd3
6 changed files with 55 additions and 9 deletions

View File

@ -1403,6 +1403,8 @@ class WP_REST_Server {
'endpoints' => array(),
);
$allow_batch = false;
if ( isset( $this->route_options[ $route ] ) ) {
$options = $this->route_options[ $route ];
@ -1410,6 +1412,8 @@ class WP_REST_Server {
$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'] );
}
@ -1430,6 +1434,12 @@ class WP_REST_Server {
'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();

View File

@ -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.
*

View File

@ -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.
*
@ -80,7 +88,8 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
'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' ),
)
);
@ -98,7 +107,7 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'args' => array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the post.' ),
'type' => 'integer',
@ -128,7 +137,8 @@ class WP_REST_Posts_Controller extends WP_REST_Controller {
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
'allow_batch' => $this->allow_batch,
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}

View File

@ -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.
*
@ -89,7 +97,8 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
'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' ),
)
);
@ -97,7 +106,7 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
$this->namespace,
'/' . $this->rest_base . '/(?P<id>[\d]+)',
array(
'args' => array(
'args' => array(
'id' => array(
'description' => __( 'Unique identifier for the term.' ),
'type' => 'integer',
@ -129,7 +138,8 @@ class WP_REST_Terms_Controller extends WP_REST_Controller {
),
),
),
'schema' => array( $this, 'get_public_item_schema' ),
'allow_batch' => $this->allow_batch,
'schema' => array( $this, 'get_public_item_schema' ),
)
);
}

View File

@ -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.
*
@ -56,7 +64,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
'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' ),
)
);
@ -90,7 +98,7 @@ class WP_REST_Widgets_Controller extends WP_REST_Controller {
),
),
),
'allow_batch' => array( 'v1' => true ),
'allow_batch' => $this->allow_batch,
'schema' => array( $this, 'get_public_item_schema' ),
)
);

View File

@ -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.