Handle wiaas documents

This commit is contained in:
Almira Krdzic
2018-10-03 16:46:41 +02:00
parent 6afffc7eca
commit 3ad210f883
42 changed files with 2258 additions and 258 deletions

View File

@@ -32,14 +32,12 @@ class Wiaas_Cart_API {
'description' => __( 'Wiaas package ID.', 'wiaas' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
),
'price_id' => array(
'description' => __( 'Selected price ID for Wiaas package.', 'wiaas' ),
'type' => 'string',
'enum' => array_keys(Wiaas_Package_Pricing::get_available_pay_types()),
'sanitize_callback' => 'sanitize_key',
'validate_callback' => 'rest_validate_request_arg',
),
'options_ids' => array(
'description' => __( 'Wiaas package options IDs.', 'wiaas' ),
@@ -47,7 +45,6 @@ class Wiaas_Cart_API {
'items' => array(
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
)
),
'addons_ids' => array(
@@ -56,7 +53,6 @@ class Wiaas_Cart_API {
'items' => array(
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
)
)
)
@@ -69,7 +65,6 @@ class Wiaas_Cart_API {
'description' => __( 'Unique key identifier for cart package item.', 'wiaas' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
'validate_callback' => 'rest_validate_request_arg',
),
),
array(
@@ -86,12 +81,69 @@ class Wiaas_Cart_API {
'description' => __( 'New quantity cart package item.', 'wiaas' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
'validate_callback' => 'rest_validate_request_arg',
)
)
)
) );
register_rest_route(self::$namespace, '/' . self::$rest_base . '/documents', array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array(__CLASS__, 'get_cart_documents'),
'permission_callback' => 'is_user_logged_in',
),
array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array(__CLASS__, 'upload_cart_document'),
'permission_callback' => 'is_user_logged_in',
'args' => array(
'doc_type' => array(
'description' => __( 'Category of uploaded document.', 'wiaas' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
'required' => true
),
'package_key' => array(
'description' => __( 'Unique key identifier for cart package item.', 'wiaas' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
'required' => true
),
'type' => array(
'default' => 'wiaas_doc'
)
)
)
));
register_rest_route(self::$namespace, '/' . self::$rest_base . '/items/(?P<key>[\w-]+)/documents/(?P<type>[\w-]+)', array(
'args' => array(
'key' => array(
'description' => __( 'Unique key identifier for cart package item.', 'wiaas' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
),
'type' => array(
'description' => __( 'Cart document Type.', 'wiaas' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
),
),
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array(__CLASS__, 'download_cart_document'),
'permission_callback' => 'is_user_logged_in',
'args' => array(
'document_key' => array(
'description' => __( 'Unique key identifier for cart document.', 'wiaas' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
'required' => true
),
)
),
));
register_rest_route( self::$namespace, '/' . self::$rest_base . '/checkout', array(
'methods' => WP_REST_Server::CREATABLE,
'callback' => array(__CLASS__, 'checkout'),
@@ -158,6 +210,7 @@ class Wiaas_Cart_API {
public static function get_cart_items() {
return rest_ensure_response(array(
'items' => Wiaas_Cart::get_cart_packages(),
'raw' => WC()->cart->get_cart_contents(),
));
}
@@ -214,6 +267,54 @@ class Wiaas_Cart_API {
return wiaas_api_notice('QUANTITY_NOT_UPDATED', 'error');
}
/**
* Retrive cart documents info
*
* @return mixed|WP_REST_Response
*/
public static function get_cart_documents() {
return rest_ensure_response( Wiaas_Cart::get_cart_documents());
}
/**
* Upload document to cart
* @param WP_REST_Request $request
*
* @return mixed|WP_REST_Response
*/
public static function upload_cart_document($request) {
$result = Wiaas_Cart::upload_cart_document($request['doc_type'], $request['package_key']);
if (is_wp_error($result)) {
$error_code = $result->get_error_code();
if ($error_code === 'wiaas_upload_missing_file') {
return wiaas_api_generate_error('NO_FILE');
}
if ($error_code === 'wiaas_upload_error') {
return wiaas_api_generate_error('UPLOAD_ERROR');
}
return wiaas_api_generate_error('NOT_LINKED_TO_CART');
}
return wiaas_api_notice('FILE_UPLOADED', 'success', array(
'id_document' => $result,
));
}
/**
* Download cart document
* @param WP_REST_Request $request
*/
public static function download_cart_document($request) {
Wiaas_Document_Download::download_cart_document(
$request['key'],
$request['type'],
$request['document_key']);
}
/**
* @param WP_REST_Request $request Request data.