[REST API] Allow batch for product attributes

This commit is contained in:
Claudio Sanches 2016-05-24 19:00:49 -03:00
parent 8709d31f41
commit 0bb3534195
1 changed files with 27 additions and 3 deletions

View File

@ -95,6 +95,16 @@ class WC_REST_Product_Attributes_Controller extends WC_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' ),
) );
}
/**
@ -179,6 +189,20 @@ class WC_REST_Product_Attributes_Controller extends WC_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_manager_permissions( 'attributes', '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 attributes.
*
@ -209,9 +233,9 @@ class WC_REST_Product_Attributes_Controller extends WC_REST_Controller {
$args = array(
'attribute_label' => $request['name'],
'attribute_name' => $request['slug'],
'attribute_type' => $request['type'],
'attribute_orderby' => $request['order_by'],
'attribute_public' => $request['has_archives'],
'attribute_type' => ! empty( $request['type'] ) ? $request['type'] : 'select',
'attribute_orderby' => ! empty( $request['order_by'] ) ? $request['order_by'] : 'menu_order',
'attribute_public' => true === $request['has_archives'],
);
// Set the attribute slug.