[REST API] Allow batch create, update and delete for almost all endpoints

This commit is contained in:
Claudio Sanches 2016-05-11 16:34:53 -03:00
parent dac471273f
commit 2b1d4eced6
16 changed files with 128 additions and 21 deletions

View File

@ -10,7 +10,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @author WooThemes
* @category API
* @package WooCommerce/Abstracts
* @extends WC_REST_Controller
* @extends WP_REST_Controller
* @version 2.6.0
*/
abstract class WC_REST_Controller extends WP_REST_Controller {

View File

@ -12,7 +12,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @package WooCommerce/Abstracts
* @version 2.6.0
*/
abstract class WC_REST_Posts_Controller extends WP_REST_Controller {
abstract class WC_REST_Posts_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.
@ -118,6 +118,20 @@ abstract class WC_REST_Posts_Controller extends WP_REST_Controller {
return true;
}
/**
* Check if a given request has access batch create, update and delete items.
*
* @param WP_REST_Request $request Full details about the request.
* @return boolean
*/
public function batch_items_permissions_check( $request ) {
if ( ! wc_rest_check_post_permissions( $this->post_type, 'batch' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to manipule this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Get a single item.
*

View File

@ -12,7 +12,7 @@ if ( ! defined( 'ABSPATH' ) ) {
* @package WooCommerce/Abstracts
* @version 2.6.0
*/
abstract class WC_REST_Terms_Controller extends WP_REST_Controller {
abstract class WC_REST_Terms_Controller extends WC_REST_Controller {
/**
* Route base.
@ -80,6 +80,16 @@ abstract class WC_REST_Terms_Controller extends WP_REST_Controller {
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'batch_items' ),
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
'schema' => array( $this, 'get_public_batch_schema' ),
) );
}
/**
@ -177,6 +187,25 @@ abstract class WC_REST_Terms_Controller extends WP_REST_Controller {
return true;
}
/**
* Check if a given request has access batch create, update and delete items.
*
* @param WP_REST_Request $request Full details about the request.
* @return boolean
*/
public function batch_items_permissions_check( $request ) {
$permissions = $this->check_permissions( $request, 'batch' );
if ( is_wp_error( $permissions ) ) {
return $permissions;
}
if ( ! $permissions ) {
return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to manipule this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Check permissions.
*

View File

@ -102,6 +102,16 @@ class WC_REST_Coupons_Controller extends WC_REST_Posts_Controller {
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'batch_items' ),
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
'schema' => array( $this, 'get_public_batch_schema' ),
) );
}
/**

View File

@ -18,9 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Customers controller class.
*
* @package WooCommerce/API
* @extends WP_REST_Controller
* @extends WC_REST_Controller
*/
class WC_REST_Customer_Downloads_Controller extends WP_REST_Controller {
class WC_REST_Customer_Downloads_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.

View File

@ -18,9 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Customers controller class.
*
* @package WooCommerce/API
* @extends WP_REST_Controller
* @extends WC_REST_Controller
*/
class WC_REST_Customers_Controller extends WP_REST_Controller {
class WC_REST_Customers_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.
@ -104,6 +104,16 @@ class WC_REST_Customers_Controller extends WP_REST_Controller {
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'batch_items' ),
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
'schema' => array( $this, 'get_public_batch_schema' ),
) );
}
/**
@ -182,6 +192,20 @@ class WC_REST_Customers_Controller extends WP_REST_Controller {
return true;
}
/**
* Check if a given request has access batch create, update and delete items.
*
* @param WP_REST_Request $request Full details about the request.
* @return boolean
*/
public function batch_items_permissions_check( $request ) {
if ( ! wc_rest_check_user_permissions( 'batch' ) ) {
return new WP_Error( 'woocommerce_rest_cannot_batch', __( 'Sorry, you are not allowed to manipule this resource.', 'woocommerce' ), array( 'status' => rest_authorization_required_code() ) );
}
return true;
}
/**
* Get all customers.
*

View File

@ -18,9 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Order Notes controller class.
*
* @package WooCommerce/API
* @extends WP_REST_Controller
* @extends WC_REST_Controller
*/
class WC_REST_Order_Notes_Controller extends WP_REST_Controller {
class WC_REST_Order_Notes_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.

View File

@ -99,6 +99,16 @@ class WC_REST_Orders_Controller extends WC_REST_Posts_Controller {
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'batch_items' ),
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
'schema' => array( $this, 'get_public_batch_schema' ),
) );
}
/**

View File

@ -18,9 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Product Attributes controller class.
*
* @package WooCommerce/API
* @extends WP_REST_Controller
* @extends WC_REST_Controller
*/
class WC_REST_Product_Attributes_Controller extends WP_REST_Controller {
class WC_REST_Product_Attributes_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.

View File

@ -18,9 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Products controller class.
*
* @package WooCommerce/API
* @extends WP_REST_Controller
* @extends WC_REST_Controller
*/
class WC_REST_Product_Reviews_Controller extends WP_REST_Controller {
class WC_REST_Product_Reviews_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.

View File

@ -100,6 +100,16 @@ class WC_REST_Products_Controller extends WC_REST_Posts_Controller {
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'batch_items' ),
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
'schema' => array( $this, 'get_public_batch_schema' ),
) );
}
/**

View File

@ -18,9 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Report Sales controller class.
*
* @package WooCommerce/API
* @extends WP_REST_Controller
* @extends WC_REST_Controller
*/
class WC_REST_Report_Sales_Controller extends WP_REST_Controller {
class WC_REST_Report_Sales_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.

View File

@ -18,9 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Reports controller class.
*
* @package WooCommerce/API
* @extends WP_REST_Controller
* @extends WC_REST_Controller
*/
class WC_REST_Reports_Controller extends WP_REST_Controller {
class WC_REST_Reports_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.

View File

@ -18,9 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Tax Classes controller class.
*
* @package WooCommerce/API
* @extends WP_REST_Controller
* @extends WC_REST_Controller
*/
class WC_REST_Tax_Classes_Controller extends WP_REST_Controller {
class WC_REST_Tax_Classes_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.

View File

@ -18,9 +18,9 @@ if ( ! defined( 'ABSPATH' ) ) {
* REST API Webhook Deliveries controller class.
*
* @package WooCommerce/API
* @extends WP_REST_Controller
* @extends WC_REST_Controller
*/
class WC_REST_Webhook_Deliveries_Controller extends WP_REST_Controller {
class WC_REST_Webhook_Deliveries_Controller extends WC_REST_Controller {
/**
* Endpoint namespace.

View File

@ -105,6 +105,16 @@ class WC_REST_Webhooks_Controller extends WC_REST_Posts_Controller {
),
'schema' => array( $this, 'get_public_item_schema' ),
) );
register_rest_route( $this->namespace, '/' . $this->rest_base . '/batch', array(
array(
'methods' => WP_REST_Server::EDITABLE,
'callback' => array( $this, 'batch_items' ),
'permission_callback' => array( $this, 'batch_items_permissions_check' ),
'args' => $this->get_endpoint_args_for_item_schema( WP_REST_Server::EDITABLE ),
),
'schema' => array( $this, 'get_public_batch_schema' ),
) );
}
/**