403 ) ); } // relative file path from upload dir for document $version = urldecode($_GET['gf-wiaas-order-doc']); $file_path = wiaas_get_document_version_path($version); 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 ) ); } /** * Handle document download for wordpress administration interface * * Since only REST api uses JWT Authentication this endpoint will work only for users * that are logged in directly to wordpress */ public static function admin_download() { if (!is_user_logged_in()) { wp_die( __( 'No Access.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 403 ) ); } $document_id = (int)$_GET['wiaasdoc']; if ($document_id <= 0) { wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); } $version = isset($_GET['v']) ? (int)$_GET['v'] : null; self::download_document($document_id, $version); } /** * Download latest or specific document version * @param $document_id * @param null $version_id */ public static function download_document($document_id, $version_id = null) { $version = Wiaas_Document::get_doc_version($document_id, $version_id); $file_path = wiaas_get_document_version_path($version); 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 order other document (not related to any order item) * * @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']); 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 * * @param $order_id * @param $item_id * @param $type * @param $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 ) ); } $item = $order->get_item($item_id); if (!$item) { wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); } $item_documents = wiaas_get_standard_package_order_item_documents($order, $item_id); $order_document = null; foreach ($item_documents as $item_document) { if ($item_document['key'] === $document_key) { $order_document = $item_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']); 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 cart document * * @param $item_key * @param $type * @param $document_key */ public static function download_cart_document($item_key, $type, $document_key) { $item = WC()->cart->get_cart_item($item_key); if (!isset($item)) { wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); } $item_documents = $item['_wiaas_documents']; if (!isset($item_documents) || empty($item_documents) || !array_key_exists($type, $item_documents)) { wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); } $cart_document = $item_documents[$type]; if ($cart_document['key'] !== $document_key) { wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) ); } $file_path = wiaas_get_document_version_path($cart_document['version']); 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 ) ); } } Wiaas_Document_Download::init();