From 2b1d4eced6ba534b7d9c15b5b8c7916f88a84610 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Wed, 11 May 2016 16:34:53 -0300 Subject: [PATCH] [REST API] Allow batch create, update and delete for almost all endpoints --- .../abstracts/abstract-wc-rest-controller.php | 2 +- .../abstract-wc-rest-posts-controller.php | 16 +++++++++- .../abstract-wc-rest-terms-controller.php | 31 ++++++++++++++++++- .../api/class-wc-rest-coupons-controller.php | 10 ++++++ ...-wc-rest-customer-downloads-controller.php | 4 +-- .../class-wc-rest-customers-controller.php | 28 +++++++++++++++-- .../class-wc-rest-order-notes-controller.php | 4 +-- .../api/class-wc-rest-orders-controller.php | 10 ++++++ ...-wc-rest-product-attributes-controller.php | 4 +-- ...ass-wc-rest-product-reviews-controller.php | 4 +-- .../api/class-wc-rest-products-controller.php | 10 ++++++ .../class-wc-rest-report-sales-controller.php | 4 +-- .../api/class-wc-rest-reports-controller.php | 4 +-- .../class-wc-rest-tax-classes-controller.php | 4 +-- .../api/class-wc-rest-webhook-deliveries.php | 4 +-- .../api/class-wc-rest-webhooks-controller.php | 10 ++++++ 16 files changed, 128 insertions(+), 21 deletions(-) diff --git a/includes/abstracts/abstract-wc-rest-controller.php b/includes/abstracts/abstract-wc-rest-controller.php index 9bad4111efb..eac54f4a357 100644 --- a/includes/abstracts/abstract-wc-rest-controller.php +++ b/includes/abstracts/abstract-wc-rest-controller.php @@ -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 { diff --git a/includes/abstracts/abstract-wc-rest-posts-controller.php b/includes/abstracts/abstract-wc-rest-posts-controller.php index bb3a96f7497..4674733f6cc 100644 --- a/includes/abstracts/abstract-wc-rest-posts-controller.php +++ b/includes/abstracts/abstract-wc-rest-posts-controller.php @@ -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. * diff --git a/includes/abstracts/abstract-wc-rest-terms-controller.php b/includes/abstracts/abstract-wc-rest-terms-controller.php index 4b7ec89d9b4..0766ab85da9 100644 --- a/includes/abstracts/abstract-wc-rest-terms-controller.php +++ b/includes/abstracts/abstract-wc-rest-terms-controller.php @@ -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. * diff --git a/includes/api/class-wc-rest-coupons-controller.php b/includes/api/class-wc-rest-coupons-controller.php index 2680d97b749..2f484ba2e84 100644 --- a/includes/api/class-wc-rest-coupons-controller.php +++ b/includes/api/class-wc-rest-coupons-controller.php @@ -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' ), + ) ); } /** diff --git a/includes/api/class-wc-rest-customer-downloads-controller.php b/includes/api/class-wc-rest-customer-downloads-controller.php index 2b5deed07d4..c51741c5a7c 100644 --- a/includes/api/class-wc-rest-customer-downloads-controller.php +++ b/includes/api/class-wc-rest-customer-downloads-controller.php @@ -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. diff --git a/includes/api/class-wc-rest-customers-controller.php b/includes/api/class-wc-rest-customers-controller.php index 0b3a6ecde4c..40626372687 100644 --- a/includes/api/class-wc-rest-customers-controller.php +++ b/includes/api/class-wc-rest-customers-controller.php @@ -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. * diff --git a/includes/api/class-wc-rest-order-notes-controller.php b/includes/api/class-wc-rest-order-notes-controller.php index fd142555ba7..3b1ea077ad0 100644 --- a/includes/api/class-wc-rest-order-notes-controller.php +++ b/includes/api/class-wc-rest-order-notes-controller.php @@ -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. diff --git a/includes/api/class-wc-rest-orders-controller.php b/includes/api/class-wc-rest-orders-controller.php index 813b5d67583..ab3699f2184 100644 --- a/includes/api/class-wc-rest-orders-controller.php +++ b/includes/api/class-wc-rest-orders-controller.php @@ -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' ), + ) ); } /** diff --git a/includes/api/class-wc-rest-product-attributes-controller.php b/includes/api/class-wc-rest-product-attributes-controller.php index 0b5eb2beeec..9a991fcd6b7 100644 --- a/includes/api/class-wc-rest-product-attributes-controller.php +++ b/includes/api/class-wc-rest-product-attributes-controller.php @@ -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. diff --git a/includes/api/class-wc-rest-product-reviews-controller.php b/includes/api/class-wc-rest-product-reviews-controller.php index 118e8b01875..1fac4db8ec2 100644 --- a/includes/api/class-wc-rest-product-reviews-controller.php +++ b/includes/api/class-wc-rest-product-reviews-controller.php @@ -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. diff --git a/includes/api/class-wc-rest-products-controller.php b/includes/api/class-wc-rest-products-controller.php index d48b615256a..267750a9f9f 100644 --- a/includes/api/class-wc-rest-products-controller.php +++ b/includes/api/class-wc-rest-products-controller.php @@ -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' ), + ) ); } /** diff --git a/includes/api/class-wc-rest-report-sales-controller.php b/includes/api/class-wc-rest-report-sales-controller.php index fa2c5e94f78..26108d9f50a 100644 --- a/includes/api/class-wc-rest-report-sales-controller.php +++ b/includes/api/class-wc-rest-report-sales-controller.php @@ -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. diff --git a/includes/api/class-wc-rest-reports-controller.php b/includes/api/class-wc-rest-reports-controller.php index 1a2062e08f6..a77d2852a61 100644 --- a/includes/api/class-wc-rest-reports-controller.php +++ b/includes/api/class-wc-rest-reports-controller.php @@ -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. diff --git a/includes/api/class-wc-rest-tax-classes-controller.php b/includes/api/class-wc-rest-tax-classes-controller.php index 046090f275b..abe59c682c8 100644 --- a/includes/api/class-wc-rest-tax-classes-controller.php +++ b/includes/api/class-wc-rest-tax-classes-controller.php @@ -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. diff --git a/includes/api/class-wc-rest-webhook-deliveries.php b/includes/api/class-wc-rest-webhook-deliveries.php index 55948b7d726..255ec0ad7f2 100644 --- a/includes/api/class-wc-rest-webhook-deliveries.php +++ b/includes/api/class-wc-rest-webhook-deliveries.php @@ -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. diff --git a/includes/api/class-wc-rest-webhooks-controller.php b/includes/api/class-wc-rest-webhooks-controller.php index 8a61f619ded..7653d41b36e 100644 --- a/includes/api/class-wc-rest-webhooks-controller.php +++ b/includes/api/class-wc-rest-webhooks-controller.php @@ -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' ), + ) ); } /**