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

@@ -17,26 +17,73 @@ class Wiaas_Document_API {
private static $namespace = 'wiaas';
public static function register_routes() {
register_rest_route( self::$namespace, 'download-package-file', array(
register_rest_route( self::$namespace, 'documents', array(
'methods' => 'GET',
'permission_callback' => 'is_user_logged_in',
'callback' => array(__CLASS__, 'download_package_file'),
'args' => array(
'document_id' => array(
'description' => __( 'Document ID.', 'wiaas' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
)
)
) );
register_rest_route( self::$namespace, 'documents/order/(?P<id>\d+)/(?P<type>[\w-]+)', array(
'args' => array(
'id' => array(
'description' => __( 'Order ID.', 'wiaas' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
),
'type' => array(
'description' => __( 'Order document type.', 'wiaas' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
),
),
array(
'methods' => 'GET',
'permission_callback' => 'is_user_logged_in',
'callback' => array(__CLASS__, 'download_order_document'),
'args' => array(
'item_id' => array(
'description' => __( 'Package Order Item ID.', 'wiaas' ),
'type' => 'integer',
'sanitize_callback' => 'absint',
),
'document_key' => array(
'description' => __( 'Unique key identifier for order document.', 'wiaas' ),
'type' => 'string',
'sanitize_callback' => 'sanitize_key',
'required' => true
),
)
)
) );
}
/**
* Download package document
*
* @param WP_REST_Request $request
*/
public static function download_package_file() {
$document_id = $_GET['document_id'];
$package_id = $_GET['package_id'];
public static function download_package_file($request) {
$document_id = $request['document_id'];
$package = wc_get_product($package_id);
Wiaas_Document_Download::download_document($document_id);
}
$file = $package->get_file($document_id);
if ($file) {
WC_Download_Handler::download_file_force($package->get_file_download_path($document_id), $file->get_name());
}
/**
* Download order document
* @param WP_REST_Request $request
*/
public static function download_order_document($request) {
Wiaas_Document_Download::download_order_item_document(
$request['id'],
$request['item_id'],
$request['type'],
$request['document_key']);
}
}