Handle order documents

This commit is contained in:
Almira Krdzic
2018-11-15 11:29:15 +01:00
parent 0ba27b2f1d
commit 6c656a0b26
10 changed files with 216 additions and 108 deletions

View File

@@ -83,6 +83,49 @@ class Wiaas_Document_Download {
);
}
/**
* Download order other document (not binded to any bundle)
* @param int $order_id
* @param string $document_key
*/
public static function download_order_other_document($order_id, $document_key) {
$order = wc_get_order($order_id);
if (!$order) {
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
}
$order_other_documents = wiaas_get_order_other_documents($order_id);
$order_document = null;
foreach ($order_other_documents as $order_other_document) {
if ($order_other_document['key'] === $document_key) {
$order_document = $order_other_document;
break;
}
}
if (!isset($order_document)) {
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
}
$file_path = wiaas_get_document_version_path($order_document['version']);
error_log($file_path);
if (!file_exists($file_path)) {
wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 404 ) );
}
WC_Download_Handler::download_file_force(
$file_path,
pathinfo( $file_path, PATHINFO_FILENAME ) . '.' . pathinfo( $file_path, PATHINFO_EXTENSION )
);
}
/**
* Download document related to order item
*
@@ -91,7 +134,7 @@ class Wiaas_Document_Download {
* @param $type
* @param $document_key
*/
public static function download_order_item_document($order_id, $item_id, $type, $document_key) {
public static function download_order_item_document($order_id, $item_id, $document_key) {
$order = wc_get_order($order_id);
if (!$order) {
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
@@ -106,7 +149,7 @@ class Wiaas_Document_Download {
$order_document = null;
foreach ($item_documents as $item_document) {
if ($item_document['key'] === $document_key && $item_document['type'] === $type) {
if ($item_document['key'] === $document_key) {
$order_document = $item_document;
break;
}

View File

@@ -26,6 +26,17 @@ function wiaas_documents_upload_dir() {
* @return string
*/
function wiaas_get_document_version_path($version) {
// documents uploaded with gravity forms have relevant path to form entry upload folder
if (strpos($version, 'gravity_forms') !== false) {
$wp_uploads = wp_upload_dir();
$wp_uploads_dir = $wp_uploads['basedir'];
return $wp_uploads_dir . '/' . $version;
}
return wiaas_documents_upload_dir() . '/' . $version;
}
@@ -182,7 +193,7 @@ function wiaas_get_standard_package_order_item_documents($order, $package_item_i
return array_map(function($doc) {
// append document extension and name information
$doc['extension'] = wiaas_get_doc_version_extension($doc['version']);
$doc['extension'] = isset($doc['extension']) ? $doc['extension'] : wiaas_get_doc_version_extension($doc['version']);
$doc['name'] = isset($doc['name']) ? $doc['name'] : wiaas_get_doc_version_filename($doc['version']);
return $doc;
@@ -222,4 +233,20 @@ function wiaas_get_order_item_documents($order_item, $doc_type = null) {
}
return $filtered_documents;
}
/**
* Retrieve order other documents (not binded to any order item)
*
* @param int $order_id
*
* @return array|mixed
*/
function wiaas_get_order_other_documents($order_id) {
$order = wc_get_order($order_id);
$documents = $order->get_meta('_wiaas_other_documents', true);
return empty($documents) ? array() : $documents;
}