Checkout logic
This commit is contained in:
@@ -9,23 +9,24 @@ class Wiaas_Document_Download {
|
||||
|
||||
public static function init() {
|
||||
// register download trigger for downloads from admin panel
|
||||
if ( is_admin() && isset($_GET['wiaasdoc']) ) {
|
||||
if ( isset($_GET['wiaasdoc']) ) {
|
||||
add_action( 'init', array( __CLASS__, 'admin_download' ) );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Download document from admin panel
|
||||
* 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' ) );
|
||||
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' ) );
|
||||
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
|
||||
}
|
||||
|
||||
$version = isset($_GET['v']) ? (int)$_GET['v'] : null;
|
||||
@@ -44,7 +45,7 @@ class Wiaas_Document_Download {
|
||||
$file_path = wiaas_get_document_version_path($version);
|
||||
|
||||
if (!file_exists($file_path)) {
|
||||
wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ) );
|
||||
wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 404 ) );
|
||||
}
|
||||
|
||||
WC_Download_Handler::download_file_force(
|
||||
@@ -64,15 +65,15 @@ class Wiaas_Document_Download {
|
||||
public static function download_order_item_document($order_id, $item_id, $type, $document_key) {
|
||||
$order = wc_get_order($order_id);
|
||||
if (!$order) {
|
||||
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) );
|
||||
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' ) );
|
||||
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 400 ) );
|
||||
}
|
||||
|
||||
$item_documents = $item['wiaas_documents'];
|
||||
$item_documents = wiaas_get_standard_package_order_item_documents($order, $item_id);
|
||||
|
||||
$order_document = null;
|
||||
foreach ($item_documents as $item_document) {
|
||||
@@ -83,13 +84,13 @@ class Wiaas_Document_Download {
|
||||
}
|
||||
|
||||
if (!isset($order_document)) {
|
||||
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) );
|
||||
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' ) );
|
||||
wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 404 ) );
|
||||
}
|
||||
|
||||
WC_Download_Handler::download_file_force(
|
||||
@@ -109,25 +110,25 @@ class Wiaas_Document_Download {
|
||||
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' ) );
|
||||
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' ) );
|
||||
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' ) );
|
||||
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' ) );
|
||||
wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ), array( 'response' => 404 ) );
|
||||
}
|
||||
|
||||
WC_Download_Handler::download_file_force(
|
||||
|
||||
@@ -34,7 +34,7 @@ class Wiaas_Document_Upload {
|
||||
// validate file
|
||||
|
||||
if (!isset($_FILES[$file_id])) {
|
||||
return new WP_Error( 'wiaas_upload_missing_file', 'No file!' );
|
||||
return new WP_Error( 'wiaas_upload_error_missing_file', 'No file!' );
|
||||
}
|
||||
|
||||
$file_name = $_FILES[$file_id]['name'];
|
||||
@@ -45,7 +45,7 @@ class Wiaas_Document_Upload {
|
||||
$ext = strtolower($ext);
|
||||
|
||||
if(!in_array($ext, self::$allowed_extensions)) {
|
||||
return new WP_Error( 'wiaas_upload_error', 'Invalid file extension!' );
|
||||
return new WP_Error( 'wiaas_upload_error_invalid_extension', 'Invalid file extension!' );
|
||||
}
|
||||
|
||||
$file = wp_handle_upload(
|
||||
|
||||
@@ -91,7 +91,7 @@ function wiaas_get_object_attached_documents($object_id) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all documents for single wiaas package
|
||||
* Retrieve all documents for single standard wiaas package
|
||||
*
|
||||
* this includes:
|
||||
* - package linked documents
|
||||
@@ -104,7 +104,7 @@ function wiaas_get_object_attached_documents($object_id) {
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function wiaas_get_all_package_documents($package, $only_visible = false) {
|
||||
function wiaas_get_standard_package_documents($package, $only_visible = false) {
|
||||
// retrieve addons and option packages ids
|
||||
$all_package_ids = array_merge(
|
||||
Wiaas_Package_Addon::get_package_addons_ids($package),
|
||||
@@ -143,14 +143,14 @@ function wiaas_get_all_package_documents($package, $only_visible = false) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve all documents for single order package item
|
||||
* Retrieve all documents for single order standard package item
|
||||
*
|
||||
* @param WC_Order $order
|
||||
* @param int $package_item_id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function wiaas_get_package_order_item_documents($order, $package_item_id) {
|
||||
function wiaas_get_standard_package_order_item_documents($order, $package_item_id) {
|
||||
|
||||
$order_items = $order->get_items( 'line_item' );
|
||||
$package_order_item = $order->get_item($package_item_id);
|
||||
|
||||
Reference in New Issue
Block a user