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

@@ -0,0 +1,142 @@
<?php
/**
* Handle download requests for wiaas documents
*
* Class Wiaas_Document_Download
*/
class Wiaas_Document_Download {
public static function init() {
// register download trigger for downloads from admin panel
if ( is_admin() && isset($_GET['wiaasdoc']) ) {
add_action( 'init', array( __CLASS__, 'admin_download' ) );
}
}
/**
* Download document from admin panel
*/
public static function admin_download() {
if (!is_user_logged_in()) {
wp_die( __( 'No Access.', 'wiaas' ), __( 'Download Error', 'wiaas' ) );
}
$document_id = (int)$_GET['wiaasdoc'];
if ($document_id <= 0) {
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) );
}
$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' ) );
}
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, $type, $document_key) {
$order = wc_get_order($order_id);
if (!$order) {
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) );
}
$item = $order->get_item($item_id);
if (!$item) {
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) );
}
$item_documents = $item['wiaas_documents'];
$order_document = null;
foreach ($item_documents as $item_document) {
if ($item_document['key'] === $document_key && $item_document['type'] === $type) {
$order_document = $item_document;
break;
}
}
if (!isset($order_document)) {
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) );
}
$file_path = wiaas_get_document_version_path($order_document['version']);
if (!file_exists($file_path)) {
wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ) );
}
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' ) );
}
$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' ) );
}
$cart_document = $item_documents[$type];
if ($cart_document['key'] !== $document_key) {
wp_die( __( 'Invalid Document Request.', 'wiaas' ), __( 'Download Error', 'wiaas' ) );
}
$file_path = wiaas_get_document_version_path($cart_document['version']);
if (!file_exists($file_path)) {
wp_die( __( 'Document not found.', 'wiaas' ), __( 'Download Error', 'wiaas' ) );
}
WC_Download_Handler::download_file_force(
$file_path,
pathinfo( $file_path, PATHINFO_FILENAME ) . '.' . pathinfo( $file_path, PATHINFO_EXTENSION )
);
}
}
Wiaas_Document_Download::init();

View File

@@ -0,0 +1,88 @@
<?php
class Wiaas_Document_Upload {
private static $allowed_extensions = array(
'pdf','docx','doc','xlsx','xls','odt','ods', 'zip'
);
public static function init() {
add_filter( 'upload_dir', array( __CLASS__, 'upload_dir' ) );
}
/**
* Allowed extensions for wiaas documents uploads
* @return array
*/
public static function allowed_extensions() {
return self::$allowed_extensions;
}
/**
* Handle wiaas document upload
*
* @param string $file_id
*
* @return int|WP_Error
*/
public static function upload_document_version($file_id = 'file') {
wc_maybe_define_constant( 'WIAAS_DOCUMENT_UPLOAD', true );
// validate file
if (!isset($_FILES[$file_id])) {
return new WP_Error( 'wiaas_upload_missing_file', 'No file!' );
}
$file_name = $_FILES[$file_id]['name'];
$name = esc_attr($file_name);
$ext = explode('.', $name);
$ext = end($ext);
$ext = strtolower($ext);
if(!in_array($ext, self::$allowed_extensions)) {
return new WP_Error( 'wiaas_upload_error', 'Invalid file extension!' );
}
$file = wp_handle_upload(
$_FILES[$file_id],
array( 'test_form' => false ),
current_time('mysql'));
if ( isset($file['error']) )
return new WP_Error( 'wiaas_upload_error', $file['error'] );
return wiaas_get_doc_version_from_path($file['file']);
}
/**
* Updates wordpress upload dir so that wiaas documents are uploaded to wiaas specific upload dir
* @param $pathdata
*
* @return mixed
*/
public static function upload_dir( $pathdata ) {
if (defined('WIAAS_DOCUMENT_UPLOAD') ) {
if ( empty( $pathdata['subdir'] ) ) {
$pathdata['path'] = $pathdata['path'] . wiaas_documents_base_dir();
$pathdata['url'] = $pathdata['url'] . wiaas_documents_base_dir();
$pathdata['subdir'] = wiaas_documents_base_dir();
} else {
$new_subdir = wiaas_documents_base_dir() . $pathdata['subdir'];
$pathdata['path'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['path'] );
$pathdata['url'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['url'] );
$pathdata['subdir'] = str_replace( $pathdata['subdir'], $new_subdir, $pathdata['subdir'] );
}
}
return $pathdata;
}
}
Wiaas_Document_Upload::init();

View File

@@ -0,0 +1,406 @@
<?php
/**
* Class Wiaas_Document
*/
class Wiaas_Document {
/**
* Default available document types for wiaas
* @var array
*/
private static $available_doc_types = array(
'template_questionaire' => array(
'name' => 'Template Questionaire',
'is_special_type' => false,
),
'order_questionaire' => array(
'name' => 'Order Questionaire',
'is_special_type' => true,
),
'configuration' => array(
'name' => 'Configuration',
'is_special_type' => true,
),
'install_guide' => array(
'name' => 'Install guide',
'is_special_type' => false,
),
'customer_acceptance' => array(
'name' => 'Customer acceptance',
'is_special_type' => true,
),
'template_agreement' => array(
'name' => 'Template Agreement',
'is_special_type' => false,
),
'order_agreement' => array(
'name' => 'Order Agreement',
'is_special_type' => true,
),
'installation_protocol' => array(
'name' => 'Installation protocol',
'is_special_type' => true,
),
'statements' => array(
'name' => 'Statements',
'is_special_type' => false,
),
'customer_acceptance_template' => array(
'name' => 'Customer acceptance template',
'is_special_type' => false,
),
);
private static $special_doc_types= array(
'order_questionaire', 'configuration', 'customer_acceptance', 'order_agreement', 'installation_protocol'
);
public static function init() {
add_action('registered_taxonomy', array( __CLASS__, 'register_wiaas_doc_categories' ));
add_action('init', array(__CLASS__, 'register_wiaas_document'));
}
/**
* Retrieve available categories for wiaas documents
*
* @return array Array of available document categories
*/
public static function get_available_doc_types() {
$terms = get_terms(array(
'taxonomy' => 'wiaas_doc_type',
'hide_empty' => false,
));
return array_map(function($term) {
$is_special = array_key_exists($term->slug, self::$available_doc_types) ?
self::$available_doc_types[$term->slug]['is_special_type'] :
false;
return array(
'id' => $term->slug,
'name' => $term->name,
'is_special_type' => $is_special,
);
}, $terms);
}
/**
* Create new document
* @param $name
* @param $path
* @param bool $visible
*
* @return bool|int|WP_Error
*/
public static function add_document($name, $path, $visible = false) {
// create
$id = wp_insert_post( array(
'post_title' => $name,
'post_author' => wp_get_current_user()->ID,
'post_type' => 'wiaas_doc',
'post_status' => 'publish',
) );
if ( is_wp_error( $id ) ) {
return false;
}
error_log(gettype($visible));
update_post_meta($id, '_wiaas_doc_versions', array( $path ));
self::set_is_doc_visible($id, $visible);
return $id;
}
/**
* Add new version for document
* @param $doc_id
* @param $url
*/
public static function add_document_version($doc_id, $url) {
$versions = get_post_meta($doc_id, '_wiaas_doc_versions', true);
$versions[] = $url;
update_post_meta($doc_id, '_wiaas_doc_versions', $versions);
}
/**
* Assign type to document
* @param $id
* @param $type
*/
public static function set_doc_type($id, $type) {
wp_set_object_terms($id, $type, 'wiaas_doc_type');
}
/**
* Set if document visible to customer
* @param $id
* @param $is_visible
*/
public static function set_is_doc_visible($id, $is_visible) {
update_post_meta($id, '_wiaas_doc_visible', $is_visible ? 'yes' : 'no');
}
/**
* Retrieve document type
* @param $id
*
* @return array|null
*/
public static function get_doc_type($id) {
$terms = wp_get_object_terms($id, 'wiaas_doc_type');
if (is_wp_error($terms) || count($terms) === 0) {
return null;
}
$term = $terms[0];
return array(
'id' => $term->slug,
'name' => $term->name,
'is_special_type' => in_array($term->slug, self::$special_doc_types),
);
}
/**
* Retrieve if document visible to customer
* @param $id
*
* @return bool
*/
public static function is_doc_visible($id) {
return get_post_meta($id, '_wiaas_doc_visible', true) === 'yes';
}
/**
* Retrieve all versions for document
* @param $doc_id
*
* @return mixed
*/
public static function get_document_versions($doc_id) {
return get_post_meta($doc_id, '_wiaas_doc_versions', true);
}
/**
* Get latest or specific document version
* @param $id
* @param null $version_id
*
* @return null
*/
public static function get_doc_version($id, $version_id = null) {
$versions = self::get_document_versions($id);
if (count($versions) === 0) {
return null;
}
return isset($versions[$version_id]) ?
$versions[$version_id] : $versions[count($versions) - 1];
}
/**
* Get document download link for latest or specified version
* @param $id
* @param null $version
*
* @return string
*/
public static function get_doc_download_link($id, $version = null) {
$permalink = get_permalink($id);
$sap = strpos($permalink, '?')?'&':'?';
$ver = isset($version) ? "&v={$version}" : '';
return $permalink.$sap."wiaasdoc={$id}{$ver}";
}
/**
* Retrieve document informations
* @param $id
*
* @return array|null
*/
public static function get_doc_info($id) {
$post = get_post($id, ARRAY_A);
if (!$post) {
return null;
}
$version = self::get_doc_version($id);
return array(
'id' => $id,
'name' => $post['post_title'],
'url' => self::get_doc_download_link($id),
'type' => self::get_doc_type($id),
'version' => $version,
'visible' => self::is_doc_visible($id),
'extension' => wiaas_get_doc_version_extension($version),
);
}
/**
* Search document by name
* @param $name
* @param int $limit
* @param bool $include_special
*
* @return array
*/
public static function search($name, $limit = 10, $include_special = false) {
$query = new WP_Query();
$args = array(
'post_status' => 'publish',
'post_type' => 'wiaas_doc',
'posts_per_page' => $limit,
's' => $name
);
if (!$include_special) {
$args['tax_query'] = array(
array(
'taxonomy' => 'wiaas_doc_type',
'field' => 'slug',
'operator' => 'NOT IN',
'terms' => self::$special_doc_types,
)
);
}
$items = $query->query($args);
return array_map(function($item) {
$id = $item->ID;
return array(
'id' => $id,
'name' => $item->post_title,
);
}, $items);
}
/**
* Registers taxonomy and default values for wiaas document types
*/
public static function register_wiaas_doc_categories($taxonomy) {
if ($taxonomy !== 'wpdmcategory') {
return;
}
foreach (self::$available_doc_types as $key => $available_doc_type) {
wp_insert_term($available_doc_type['name'], 'wpdmcategory', array(
'slug' => $key
));
}
}
public static function register_wiaas_document() {
// Register document object
self::_register_document();
// Register document types taxonomy
self::_register_doc_types();
}
// PRIVATE
/**
* Register wiaas_doc post type
*/
private static function _register_document() {
$labels = array(
'name' => __('Documents','wiaas'),
'singular_name' => __('Document','wiaas'),
'add_new' => __('Add New','wiaas'),
'add_new_item' => __('Add New Document','wiaas'),
'edit_item' => __('Edit Document','wiaas'),
'new_item' => __('New Document','wiaas'),
'all_items' => __('All Documents','wiaas'),
'view_item' => __('View Document','wiaas'),
'search_items' => __('Search Documents','wiaas'),
'not_found' => __('No Document Found','wiaas'),
'not_found_in_trash' => __('No Documents found in Trash','wiaas'),
'parent_item_colon' => '',
'menu_name' => __('Documents','wiaas')
);
$args = array(
'labels' => $labels,
'public' => true,
'publicly_queryable' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'query_var' => true,
'rewrite' => array('slug' => 'wiaas_doc', 'with_front' => false),
'capability_type' => 'post',
'has_archive' => false,
'hierarchical' => false,
'taxonomies' => array(),
'menu_icon' => 'dashicons-media-document',
'exclude_from_search' => false,
'supports' => array('title')
);
register_post_type('wiaas_doc', $args);
}
/**
* Register wiaas_doc_type taxonomy for wiaas_doc post type
*/
private static function _register_doc_types() {
$labels = array(
'name' => _x( 'Document type', 'taxonomy general name', 'wiaas' ),
'singular_name' => _x( 'Document type', 'taxonomy singular name', 'wiaas' ),
'menu_name' => _x( 'Document types', 'Admin menu name', 'wiaas' ),
'search_items' => __( 'Search Document types', 'wiaas' ),
'all_items' => __( 'All Document types', 'wiaas' ),
'parent_item' => __( 'Parent Document type', 'wiaas' ),
'parent_item_colon' => __( 'Parent Document type:', 'wiaas' ),
'edit_item' => __( 'Edit Document type', 'wiaas' ),
'update_item' => __( 'Update Document type', 'wiaas' ),
'add_new_item' => __( 'Add New Document type', 'wiaas' ),
'new_item_name' => __( 'New Document type Name', 'wiaas' ),
);
$args = array(
'hierarchical' => false,
'label' => __( 'Document type', 'wiaas' ),
'labels' => $labels,
'show_ui' => true,
'show_admin_column' => true,
'query_var' => true,
'rewrite' => array( 'slug' => 'wiaas_doc_type' ),
);
register_taxonomy( 'wiaas_doc_type', array( 'wiaas_doc' ), $args );
foreach (self::$available_doc_types as $key => $available_doc_type) {
wp_insert_term($available_doc_type['name'], 'wiaas_doc_type', array(
'slug' => $key
));
}
}
}
Wiaas_Document::init();

View File

@@ -0,0 +1,190 @@
<?php
/**
* Base directory for wiaas document uploads
* @return string
*/
function wiaas_documents_base_dir() {
return '/woocommerce_uploads';
}
/**
* Upload directory for wiaas document uploads
* @return string
*/
function wiaas_documents_upload_dir() {
$wp_uploads = wp_upload_dir();
$wp_uploads_dir = $wp_uploads['basedir'];
return $wp_uploads_dir . wiaas_documents_base_dir();
}
/**
* Retrieve upload path for wiaas document version
* @param string $version
*
* @return string
*/
function wiaas_get_document_version_path($version) {
return wiaas_documents_upload_dir() . '/' . $version;
}
/**
* Retrieve if wiaas document version exists
* @param string $version
*
* @return bool
*/
function wiaas_document_version_exists($version) {
return file_exists(wiaas_get_document_version_path($version));
}
/**
* Retrieve wiaas document version from file path
* (that is relative path from wiaas document upload dir)
* @param string $path
*
* @return string
*/
function wiaas_get_doc_version_from_path($path) {
return str_replace(wiaas_documents_upload_dir() . '/', '', $path);
}
/**
* Retrieve filename for wiaas document version
* @param string $version
*
* @return string
*/
function wiaas_get_doc_version_filename($version) {
return pathinfo( $version, PATHINFO_FILENAME );
}
/**
* Retrieve extension for wiaas document version
* @param string $version
*
* @return string
*/
function wiaas_get_doc_version_extension($version) {
return pathinfo( $version, PATHINFO_EXTENSION );
}
/**
* Attach wiaas document to post object
* @param int $object_id
* @param array $document_ids
*/
function wiaas_attach_documents_to_object($object_id, $document_ids) {
update_post_meta($object_id, '_wiaas_attached_documents', $document_ids);
}
/**
* Retrieve wiaas documents ids for post object
* @param int $object_id
*
* @return array
*/
function wiaas_get_object_attached_documents($object_id) {
$document_ids = get_post_meta($object_id, '_wiaas_attached_documents', true);
return isset($document_ids) && !empty($document_ids) ? $document_ids : array();
}
/**
* Retrieve all documents for single wiaas package
*
* this includes:
* - package linked documents
* - addons linked documents
* - options linked documents
* - all documents attacked to bundled products of package, addons and options
*
* @param int $package
* @param bool $only_visible
*
* @return array
*/
function wiaas_get_all_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),
Wiaas_Package_Option_Groups::get_package_option_ids($package),
array($package->get_id())
);
// retrieve all bundled products from all relevant packages
$all_product_ids = array_values(
WC_PB_DB::query_bundled_items(array(
'return' => 'id=>product_id',
'bundle_id' => $all_package_ids
))
);
$all_object_ids = array_unique(array_merge($all_package_ids, $all_product_ids));
// retrieve document ids from all packages and products
$document_ids = array();
foreach ($all_object_ids as $object_id) {
$document_ids = array_merge(
$document_ids,
wiaas_get_object_attached_documents($object_id));
}
$document_ids = array_unique($document_ids);
// filter only visible if needed and retrieve documents info
$documents = array();
foreach ($document_ids as $document_id) {
if ($only_visible && Wiaas_Document::is_doc_visible($document_id) || !$only_visible) {
$documents[] = Wiaas_Document::get_doc_info($document_id);
}
}
return $documents;
}
/**
* Retrieve all documents for single order package item
*
* @param WC_Order $order
* @param int $package_item_id
*
* @return array
*/
function wiaas_get_package_order_item_documents($order, $package_item_id) {
$order_items = $order->get_items( 'line_item' );
$package_order_item = $order->get_item($package_item_id);
// retrieve package order item addons and options
$all_package_order_items = array_merge(
wiaas_get_order_item_addons($order_items, $package_order_item),
wiaas_get_order_item_options($order_items, $package_order_item),
array( $package_order_item )
);
$all_order_items = $all_package_order_items;
// retrieve bundled product items for each package
foreach ($all_package_order_items as $item) {
$all_order_items = array_merge(
$all_order_items,
wc_pb_get_bundled_order_items($item, $order)
);
}
$order_documents = array();
foreach ($all_order_items as $order_item) {
$order_documents = array_merge(
$order_documents,
isset($order_item['wiaas_documents']) ? $order_item['wiaas_documents'] : array()
);
}
return array_map(function($doc) {
// append document extension and name information
$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;
}, $order_documents);
}