2016-05-09 21:16:48 +00:00
< ? php
if ( ! defined ( 'ABSPATH' ) ) {
exit ;
}
/**
* Abstract Rest Controler Class
*
* @ author WooThemes
* @ category API
* @ package WooCommerce / Abstracts
* @ extends WC_REST_Controller
* @ version 2.6 . 0
*/
abstract class WC_REST_Controller extends WP_REST_Controller {
/**
* Endpoint namespace .
*
* @ var string
*/
protected $namespace = 'wc/v1' ;
/**
* Route base .
*
* @ var string
*/
protected $rest_base = '' ;
/**
* Get normalized rest base .
*
* @ return string
*/
protected function get_normalized_rest_base () {
return preg_replace ( '/\(.*\)\//i' , '' , $this -> rest_base );
}
/**
* Check batch limit .
*
* @ param array $items Request items .
* @ return bool | WP_Error
*/
protected function check_batch_limit ( $items ) {
$limit = apply_filters ( 'woocommerce_rest_batch_items_limit' , 100 , $this -> get_normalized_rest_base () );
$total = 0 ;
if ( ! empty ( $items [ 'create' ] ) ) {
$total += count ( $items [ 'create' ] );
}
if ( ! empty ( $items [ 'update' ] ) ) {
$total += count ( $items [ 'update' ] );
}
if ( ! empty ( $items [ 'delete' ] ) ) {
$total += count ( $items [ 'delete' ] );
}
if ( $total > $limit ) {
return new WP_Error ( 'woocommerce_rest_request_entity_too_large' , sprintf ( __ ( 'Unable to accept more than %s items for this request.' , 'woocommerce' ), $limit ), array ( 'status' => 413 ) );
}
return true ;
}
/**
* Bulk create , update and delete items .
*
* @ param WP_REST_Request $request Full details about the request .
* @ return array Of WP_Error or WP_REST_Response .
*/
public function batch_items ( $request ) {
/** @var WP_REST_Server $wp_rest_server */
global $wp_rest_server ;
// Get the request params.
$items = array_filter ( $request -> get_params () );
$response = array ();
// Check batch limit.
$limit = $this -> check_batch_limit ( $items );
if ( is_wp_error ( $limit ) ) {
return $limit ;
}
if ( ! empty ( $items [ 'create' ] ) ) {
foreach ( $items [ 'create' ] as $item ) {
$_item = new WP_REST_Request ( 'POST' );
$_item -> set_body_params ( $item );
$_response = $this -> create_item ( $_item );
if ( is_wp_error ( $_response ) ) {
2016-05-09 21:21:36 +00:00
$response [ 'create' ][] = array (
2016-05-09 21:16:48 +00:00
'id' => 0 ,
'error' => array ( 'code' => $_response -> get_error_code (), 'message' => $_response -> get_error_message (), 'data' => $_response -> get_error_data () ),
);
} else {
$response [ 'create' ][] = $wp_rest_server -> response_to_data ( $_response , '' );
}
}
}
if ( ! empty ( $items [ 'update' ] ) ) {
foreach ( $items [ 'update' ] as $item ) {
$_item = new WP_REST_Request ( 'PUT' );
$_item -> set_body_params ( $item );
$_response = $this -> update_item ( $_item );
if ( is_wp_error ( $_response ) ) {
2016-05-09 21:21:36 +00:00
$response [ 'update' ][] = array (
2016-05-09 21:16:48 +00:00
'id' => $item [ 'id' ],
'error' => array ( 'code' => $_response -> get_error_code (), 'message' => $_response -> get_error_message (), 'data' => $_response -> get_error_data () ),
);
} else {
$response [ 'update' ][] = $wp_rest_server -> response_to_data ( $_response , '' );
}
}
}
if ( ! empty ( $items [ 'delete' ] ) ) {
foreach ( $items [ 'delete' ] as $id ) {
$_item = new WP_REST_Request ( 'DELETE' );
$_item -> set_query_params ( array ( 'id' => $id , 'force' => true ) );
$_response = $this -> delete_item ( $_item );
if ( is_wp_error ( $_response ) ) {
2016-05-09 21:21:36 +00:00
$response [ 'delete' ][] = array (
2016-05-09 21:16:48 +00:00
'id' => $id ,
'error' => array ( 'code' => $_response -> get_error_code (), 'message' => $_response -> get_error_message (), 'data' => $_response -> get_error_data () ),
);
} else {
$response [ 'delete' ][] = $wp_rest_server -> response_to_data ( $_response , '' );
}
}
}
return $response ;
}
/**
* Get the batch schema , conforming to JSON Schema .
*
* @ return array
*/
public function get_public_batch_schema () {
$schema = array (
'$schema' => 'http://json-schema.org/draft-04/schema#' ,
'title' => 'batch' ,
'type' => 'object' ,
'properties' => array (
'create' => array (
'description' => __ ( 'List of created resources.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
),
'update' => array (
'description' => __ ( 'List of updated resources.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
),
'delete' => array (
'description' => __ ( 'List of delete resources.' , 'woocommerce' ),
'type' => 'array' ,
'context' => array ( 'view' , 'edit' ),
),
),
);
return $schema ;
}
}