Handle wiaas documents
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Editor for wiaas document post screen
|
||||
*
|
||||
* Class Wiaas_Admin_Document_Editor
|
||||
*/
|
||||
class Wiaas_Admin_Document_Editor {
|
||||
|
||||
public static function init() {
|
||||
add_action( 'add_meta_boxes', array( __CLASS__, 'add_meta_boxes' ) );
|
||||
|
||||
add_action( 'save_post', array( __CLASS__, 'save' ), 1, 2 );
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Add wiaas document editor metaboxes
|
||||
*/
|
||||
public static function add_meta_boxes() {
|
||||
|
||||
add_meta_box( 'wiaas_document_details', __( 'Details', 'wiaas' ), array(
|
||||
__CLASS__,
|
||||
'document_info'
|
||||
), 'wiaas_doc', 'normal', 'low');
|
||||
}
|
||||
|
||||
/**
|
||||
* Save wiaas document informations
|
||||
* @param $post_id
|
||||
* @param $post
|
||||
*/
|
||||
public static function save ($post_id, $post) {
|
||||
if ( empty( $post_id ) || empty( $post ) || empty( $_POST ) ) {
|
||||
return;
|
||||
}
|
||||
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
|
||||
return;
|
||||
}
|
||||
if ( is_int( wp_is_post_revision( $post ) ) ) {
|
||||
return;
|
||||
}
|
||||
if ( is_int( wp_is_post_autosave( $post ) ) ) {
|
||||
return;
|
||||
}
|
||||
if ( empty( $_POST['wiaas_doc_nonce'] ) || ! wp_verify_nonce( $_POST['wiaas_doc_nonce'], 'save_wiaas_doc' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( ! current_user_can( 'edit_post', $post_id ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ( $post->post_type != 'wiaas_doc' ) {
|
||||
return;
|
||||
}
|
||||
|
||||
// unset nonce because it's only valid of 1 post
|
||||
unset( $_POST['wiaas_doc_nonce'] );
|
||||
|
||||
if (isset($_POST['wiaas_doc_version']) && $_POST['wiaas_doc_version'] !== '') {
|
||||
$version = sanitize_text_field($_POST['wiaas_doc_version']);
|
||||
|
||||
if(wiaas_document_version_exists($version)) {
|
||||
Wiaas_Document::add_document_version($post_id, $version);
|
||||
}
|
||||
}
|
||||
|
||||
if (isset($_POST['wiaas_doc_type'])) {
|
||||
Wiaas_Document::set_doc_type($post_id, sanitize_key($_POST['wiaas_doc_type']));
|
||||
}
|
||||
|
||||
Wiaas_Document::set_is_doc_visible($post_id, $_POST['wiaas_doc_visible'] === 'on');
|
||||
}
|
||||
|
||||
/**
|
||||
* Render wiaas document info metabox
|
||||
*/
|
||||
public static function document_info() {
|
||||
global $post;
|
||||
|
||||
$parent_post_type = $post->post_type;
|
||||
$document_id = $post->ID;
|
||||
|
||||
$ajax_action = 'wiaas_upload_file';
|
||||
|
||||
require 'views/html-document-info.php';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Wiaas_Admin_Document_Editor::init();
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
class Wiaas_Admin_Product_Documents {
|
||||
|
||||
public static function init() {
|
||||
add_action( 'woocommerce_product_data_tabs', array( __CLASS__, 'add_linked_documents_tab' ) );
|
||||
add_action( 'woocommerce_product_data_panels', array( __CLASS__, 'linked_documents_tab' ) );
|
||||
|
||||
add_action( 'add_meta_boxes', array( __CLASS__, 'add_meta_boxes' ) );
|
||||
|
||||
add_action( 'woocommerce_process_product_meta', array( __CLASS__, 'process_meta_box' ));
|
||||
}
|
||||
|
||||
/**
|
||||
* Add upload and link document metabox to product screen
|
||||
*/
|
||||
public static function add_meta_boxes() {
|
||||
add_meta_box( 'wiaas_upload_and_link_document', __( 'Upload and link document', 'wiaas' ), array(
|
||||
__CLASS__,
|
||||
'upload_and_link_document'
|
||||
), 'product', 'side');
|
||||
}
|
||||
|
||||
/**
|
||||
* Render upload and link document metabox to product screen
|
||||
*/
|
||||
public static function upload_and_link_document() {
|
||||
|
||||
$ajax_action = 'wiaas_quick_add_document';
|
||||
|
||||
require 'views/html-document-form.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Add linked documents tabs for product screen
|
||||
*
|
||||
* @param array $tabs
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function add_linked_documents_tab($tabs) {
|
||||
$tabs['wiaas_documents'] = array(
|
||||
'label' => __( 'Linked Documents', 'wiaas' ),
|
||||
'target' => 'wiaas_documents',
|
||||
'class' => array('show_if_bundle', 'show_if_simple'),
|
||||
'priority' => 20,
|
||||
);
|
||||
|
||||
return $tabs;
|
||||
}
|
||||
|
||||
/**
|
||||
* Render linked documents tab content for product screen
|
||||
*/
|
||||
public static function linked_documents_tab() {
|
||||
|
||||
global $post;
|
||||
|
||||
$documents_ids = wiaas_get_object_attached_documents($post->ID);
|
||||
|
||||
include 'views/html-product-documents.php';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save linked document for product
|
||||
*
|
||||
* @param int $package_id
|
||||
*/
|
||||
public static function process_meta_box($package_id) {
|
||||
$documents = isset($_POST['wiaas_attached_documents']) && is_array($_POST['wiaas_attached_documents']) ?
|
||||
$_POST['wiaas_attached_documents'] : array();
|
||||
|
||||
wiaas_attach_documents_to_object($package_id, $documents);
|
||||
}
|
||||
}
|
||||
|
||||
Wiaas_Admin_Product_Documents::init();
|
||||
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* int $id ID of the attached document
|
||||
*/
|
||||
|
||||
$doc_info = Wiaas_Document::get_doc_info($id);
|
||||
$type = isset($doc_info['type']) && is_array($doc_info['type']) ? $doc_info['type']['name'] : ' - ';
|
||||
|
||||
$visible = Wiaas_Document::is_doc_visible($id);
|
||||
|
||||
?>
|
||||
|
||||
<tr id="wiaas_attached_document_<?php echo esc_attr($id); ?>">
|
||||
<td width="1%">
|
||||
<?php
|
||||
if ($visible) {
|
||||
echo '<span class="dashicons dashicons-visibility"></span>';
|
||||
} else {
|
||||
echo '<span class="dashicons dashicons-hidden"></span>';
|
||||
}
|
||||
?>
|
||||
</td>
|
||||
<td>
|
||||
<div>
|
||||
<input type="hidden" name="wiaas_attached_documents[]" value="<?php echo esc_attr($id); ?>" />
|
||||
<b>
|
||||
<?php esc_html_e($doc_info['name'], 'wiaas'); ?>
|
||||
</b>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<span>
|
||||
<?php esc_html_e($type, 'wiaas'); ?>
|
||||
</span>
|
||||
</td>
|
||||
<td width="1%"><a href="<?php echo esc_attr($doc_info['url']) ?>" download>
|
||||
<span class="dashicons dashicons-download"></span>
|
||||
</a>
|
||||
</td>
|
||||
<td width="1%">
|
||||
<button
|
||||
data-id="<?php echo esc_attr($id); ?>"
|
||||
class="button wiaas-remove-attached-document">
|
||||
<small><?php esc_html_e('Remove', 'wiaas'); ?></small>
|
||||
</button>
|
||||
</td>
|
||||
</tr>
|
||||
@@ -0,0 +1,258 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
$is_update = isset($document_id);
|
||||
|
||||
global $post;
|
||||
|
||||
$insert_new_document = $post->post_type !== 'wiaas_doc';
|
||||
|
||||
?>
|
||||
|
||||
<style>
|
||||
|
||||
#wiaas_document_form .form-group {
|
||||
margin-bottom:15px;
|
||||
min-height: 30px;
|
||||
}
|
||||
|
||||
#wiaas_document_form .form-control {
|
||||
width: 100%;
|
||||
height: 34px;
|
||||
padding:6px 12px;
|
||||
border: 1px solid #d5d5d5;
|
||||
|
||||
}
|
||||
|
||||
#wiaas_document_form #plupload-browse-button {
|
||||
color: #444 !important;
|
||||
background-color: #FFFFFF !important;
|
||||
border-color: #CCCCCC !important;
|
||||
text-transform: uppercase !important;
|
||||
letter-spacing: 1px !important;
|
||||
border-radius: 2px !important;
|
||||
}
|
||||
|
||||
#wiaas_document_form #plupload-upload-ui {
|
||||
height: 230px;
|
||||
}
|
||||
|
||||
#wiaas_document_form #drag-drop-area {
|
||||
max-height: 200px;
|
||||
}
|
||||
|
||||
#wiaas_document_form .drag-drop .drag-drop-inside {
|
||||
margin: 50px auto 0;
|
||||
}
|
||||
</style>
|
||||
|
||||
<script>
|
||||
jQuery(document).ready(function ($) {
|
||||
<?php
|
||||
|
||||
if ($insert_new_document) {
|
||||
$nonce = wp_create_nonce('wiaas_quick_add_document');
|
||||
$action = 'wiaas_quick_add_document';
|
||||
} else {
|
||||
$nonce = wp_create_nonce('wiaas_upload_file');
|
||||
$action = 'wiaas_upload_file';
|
||||
}
|
||||
|
||||
$plupload_init = array(
|
||||
'runtimes' => 'html5,silverlight,flash,html4',
|
||||
'browse_button' => 'plupload-browse-button',
|
||||
'container' => 'plupload-upload-ui',
|
||||
'drop_element' => 'drag-drop-area',
|
||||
'file_data_name' => 'wiaas_file',
|
||||
'multiple_queues' => false,
|
||||
'url' => admin_url('admin-ajax.php'),
|
||||
'flash_swf_url' => includes_url('js/plupload/plupload.flash.swf'),
|
||||
'silverlight_xap_url' => includes_url('js/plupload/plupload.silverlight.xap'),
|
||||
'filters' => array(array(
|
||||
'title' => __('Allowed Files'),
|
||||
'extensions' => join(',', Wiaas_Document_Upload::allowed_extensions()))
|
||||
),
|
||||
'multipart' => true,
|
||||
'urlstream_upload' => true,
|
||||
// additional post data to send to our ajax hook
|
||||
'multipart_params' => array(
|
||||
'_ajax_nonce' => $nonce,
|
||||
'action' => $action,
|
||||
'type' => 'wiaas_doc'
|
||||
),
|
||||
);
|
||||
|
||||
if(get_option('__wpdm_chunk_upload',0) == 1){
|
||||
$plupload_init['chunk_size'] = get_option('__wpdm_chunk_size', 1024).'kb';
|
||||
$plupload_init['max_retries'] = 3;
|
||||
} else
|
||||
$plupload_init['max_file_size'] = wp_max_upload_size().'b';
|
||||
?>
|
||||
|
||||
// create the uploader and pass the config from above
|
||||
var uploader = new plupload.Uploader(<?php echo json_encode($plupload_init); ?>);
|
||||
|
||||
// checks if browser supports drag and drop upload, makes some css adjustments if necessary
|
||||
uploader.bind('Init', function(up){
|
||||
var uploaddiv = jQuery('#plupload-upload-ui');
|
||||
|
||||
if(up.features.dragdrop){
|
||||
uploaddiv.addClass('drag-drop');
|
||||
jQuery('#drag-drop-area')
|
||||
.bind('dragover.wp-uploader', function(){ uploaddiv.addClass('drag-over'); })
|
||||
.bind('dragleave.wp-uploader, drop.wp-uploader', function(){ uploaddiv.removeClass('drag-over'); });
|
||||
|
||||
}else{
|
||||
uploaddiv.removeClass('drag-drop');
|
||||
jQuery('#drag-drop-area').unbind('.wp-uploader');
|
||||
}
|
||||
});
|
||||
|
||||
uploader.init();
|
||||
|
||||
// a file was added in the queue
|
||||
uploader.bind('FilesAdded', function(up, files){
|
||||
//var hundredmb = 100 * 1024 * 1024, max = parseInt(up.settings.max_file_size, 10);
|
||||
|
||||
<?php
|
||||
if ($insert_new_document) {
|
||||
?>
|
||||
var params = uploader.getOption('multipart_params');
|
||||
params['doc_title'] = $('#wiaas_new_doc_title').val();
|
||||
params['doc_type'] = $('#wiaas_new_doc_type').val();
|
||||
params['doc_visible'] = $('#wiaas_new_doc_visible').is(':checked');
|
||||
|
||||
uploader.setOption('multipart_params', params);
|
||||
|
||||
plupload.each(files, function(file){
|
||||
jQuery('#wiaas_selected_file').val(file.name);
|
||||
});
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
|
||||
uploader.refresh();
|
||||
uploader.start();
|
||||
});
|
||||
|
||||
$("#wiaas_document_add_version").click(function(e) {
|
||||
e.preventDefault();
|
||||
|
||||
uploader.start();
|
||||
});
|
||||
|
||||
// a file was uploaded
|
||||
uploader.bind('FileUploaded', function(up, file, response) {
|
||||
|
||||
response = response.response;
|
||||
|
||||
if (response.substring(0, 6) === 'ERROR:') {
|
||||
jQuery('#wiaas_upload_errors').html('<span class="text-danger">' +
|
||||
'<i class="fa fa-exclamation-triangle"></i>' +
|
||||
' ' + response.substring(6, response.length) +
|
||||
'</span>');
|
||||
return;
|
||||
}
|
||||
|
||||
<?php
|
||||
if ($insert_new_document) {
|
||||
?>
|
||||
$('#wiaas_attached_documents').find('tbody').append(response);
|
||||
<?php
|
||||
} else {
|
||||
?>
|
||||
$('#wiaas_uploaded_file').val(response);
|
||||
jQuery('#publish').click();
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
});
|
||||
|
||||
});
|
||||
</script>
|
||||
|
||||
<div id="wiaas_document_form">
|
||||
|
||||
<?php
|
||||
if ($insert_new_document) {
|
||||
?>
|
||||
<p class="form-group">
|
||||
<input
|
||||
id="wiaas_new_doc_title"
|
||||
type="text"
|
||||
placeholder="Title"
|
||||
class="form-control input-lg" />
|
||||
</p>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
|
||||
<p class="form-group">
|
||||
<select
|
||||
id="wiaas_new_doc_type"
|
||||
name="wiaas_doc_type"
|
||||
class="form-control input-lg">
|
||||
<?php
|
||||
$all_doc_types = Wiaas_Document::get_available_doc_types();
|
||||
|
||||
if (!$insert_new_document) {
|
||||
$selected_doc_type = Wiaas_Document::get_doc_type($post->ID);
|
||||
$selected_doc_type = is_array($selected_doc_type) ? $selected_doc_type['id'] : '';
|
||||
}
|
||||
|
||||
foreach ($all_doc_types as $doc_type) {
|
||||
if (!$doc_type['is_special_type']) {
|
||||
?>
|
||||
<option
|
||||
value="<?php echo $doc_type['id'] ?>"
|
||||
<?php selected($doc_type['id'], $selected_doc_type, true)?>
|
||||
>
|
||||
<?php esc_html_e($doc_type['name'], 'wiaas') ?>
|
||||
</option>
|
||||
<?php
|
||||
}
|
||||
}
|
||||
?>
|
||||
</select>
|
||||
</p>
|
||||
|
||||
<p class="form-group">
|
||||
<input
|
||||
id="wiaas_new_doc_visible"
|
||||
name="wiaas_doc_visible"
|
||||
<?php if (!$insert_new_document)
|
||||
checked(Wiaas_Document::is_doc_visible($post->ID), true, true) ?>
|
||||
type="checkbox"
|
||||
/>
|
||||
<label><?php echo __( 'Visible to customer?', 'wiaas' ); ?></label>
|
||||
</p>
|
||||
|
||||
<input id="wiaas_uploaded_file" name="wiaas_doc_version" type="hidden">
|
||||
|
||||
<div>
|
||||
<div id="plupload-upload-ui" class="hide-if-no-js">
|
||||
<div id="drag-drop-area" style="height:240px">
|
||||
<div class="drag-drop-inside">
|
||||
<p class="drag-drop-info"><?php _e( 'Drop file here', 'wiaas' ); ?></p>
|
||||
|
||||
<p><?php echo _x( 'or', 'Drop file here *or* select file', 'wiaas' ); ?></p>
|
||||
|
||||
<p class="drag-drop-buttons"><input id="plupload-browse-button" type="button"
|
||||
value="<?php esc_attr_e( 'Select File', 'wiaas' ); ?>"
|
||||
class="button"/></p>
|
||||
</div>
|
||||
</div>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="wiaas_upload_errors" style="color: darkred;">
|
||||
</div>
|
||||
|
||||
</div>
|
||||
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* int $document_id ID of the document
|
||||
*/
|
||||
?>
|
||||
|
||||
<style>
|
||||
|
||||
#wiaas_document_info table {
|
||||
margin-top: 25px;
|
||||
}
|
||||
|
||||
#wiaas_document_info table > thead {
|
||||
background: #f5f5f5;
|
||||
}
|
||||
|
||||
#wiaas_document_info table b {
|
||||
color: #72777c;
|
||||
}
|
||||
|
||||
#tagsdiv-wiaas_doc_type {
|
||||
display: none;
|
||||
}
|
||||
|
||||
#wiaas_document_new_version {
|
||||
max-width: 320px;
|
||||
margin: auto;
|
||||
}
|
||||
|
||||
</style>
|
||||
|
||||
|
||||
|
||||
<div id="wiaas_document_info">
|
||||
|
||||
<?php echo wp_nonce_field('save_wiaas_doc', 'wiaas_doc_nonce') ?>
|
||||
|
||||
<div id="wiaas_document_new_version">
|
||||
<?php
|
||||
|
||||
require 'html-document-form.php';
|
||||
|
||||
?>
|
||||
</div>
|
||||
|
||||
|
||||
<div class="form-group">
|
||||
<table class="widefat">
|
||||
<thead>
|
||||
<tr>
|
||||
<td></td>
|
||||
<td><b>Versions</b></td>
|
||||
<td></td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="wiaas_doc_versions">
|
||||
<?php
|
||||
$versions = Wiaas_Document::get_document_versions($document_id);
|
||||
|
||||
foreach ($versions as $index => $version) {
|
||||
?>
|
||||
<tr>
|
||||
<td><?php esc_html_e('#' . ($index + 1), 'wiaas') ?></td>
|
||||
<td><?php
|
||||
esc_html_e(
|
||||
wiaas_get_doc_version_filename($version).'.'.wiaas_get_doc_version_extension($version),
|
||||
'wiaas')
|
||||
?></td>
|
||||
<td>
|
||||
<a
|
||||
href="<?php echo esc_attr(Wiaas_Document::get_doc_download_link($document_id, $index)) ?>"
|
||||
download>
|
||||
<span class="dashicons dashicons-download"></span>
|
||||
</a>
|
||||
</td>
|
||||
</tr>
|
||||
<?php
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,84 @@
|
||||
<?php
|
||||
/**
|
||||
* Wiaas Linked Packages Editor
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
|
||||
<script type="text/javascript">
|
||||
jQuery(document).ready(function ($) {
|
||||
|
||||
$( '.wiaas-search-documents' ).each(function() {
|
||||
var element = $( this );
|
||||
var searchTarget = $('#' + element.data('target'));
|
||||
|
||||
element.autocomplete({
|
||||
source: function(request, response) {
|
||||
$.get( window.ajaxurl, {
|
||||
action: 'wiaas_json_search_documents',
|
||||
query: request.term,
|
||||
_ajax_nonce: '<?php echo wp_create_nonce('wiaas_json_search_documents') ?>'
|
||||
} ).done( function( documents ) {
|
||||
response( documents || []);
|
||||
}
|
||||
);
|
||||
},
|
||||
select: function(event, ui) {
|
||||
if (!searchTarget || $('#wiaas_attached_document_' + ui.item.id).length) {
|
||||
return;
|
||||
}
|
||||
|
||||
$.get(window.ajaxurl, {
|
||||
action: 'wiaas_link_document',
|
||||
_ajax_nonce: '<?php echo wp_create_nonce('wiaas_link_document') ?>',
|
||||
id: ui.item.id
|
||||
}).done( function (document) {
|
||||
searchTarget.find('tbody').append(document);
|
||||
});
|
||||
|
||||
}
|
||||
})
|
||||
.autocomplete( 'instance' )._renderItem = function( ul, item ) {
|
||||
return $( '<li role="option" id="wiaas-document-autocomplete-' + item.id + '">' )
|
||||
.text( item.name )
|
||||
.appendTo( ul );
|
||||
};
|
||||
});
|
||||
|
||||
$('#wiaas_attached_documents').delegate('.wiaas-remove-attached-document', 'click', function (e) {
|
||||
e.preventDefault();
|
||||
|
||||
var id = $(this).data('id');
|
||||
|
||||
$('#wiaas_attached_document_' + id).remove();
|
||||
});
|
||||
|
||||
});
|
||||
</script>
|
||||
|
||||
<div id="wiaas_documents" class="panel woocommerce_options_panel">
|
||||
<div class="options_group">
|
||||
<p class="form-field">
|
||||
<label style="font-weight: bold;" for="wiaas_addon_packages"><?php esc_html_e( 'Search documents:', 'wiaas' ); ?></label>
|
||||
<input type="text" data-target="wiaas_attached_documents" class="wiaas-search-documents"/>
|
||||
</p>
|
||||
</div>
|
||||
<div class="options_group">
|
||||
<div class="form-field">
|
||||
<div style="margin:20px">
|
||||
<table id="wiaas_attached_documents" class="widefat wp-list-table">
|
||||
<tbody>
|
||||
<?php
|
||||
foreach ($documents_ids as $id) {
|
||||
require 'html-attached-document.php';
|
||||
}
|
||||
?>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
@@ -0,0 +1,105 @@
|
||||
<?php
|
||||
|
||||
add_action( 'wp_ajax_wiaas_upload_file', 'wiaas_ajax_upload_document_version' );
|
||||
add_action( 'wp_ajax_wiaas_quick_add_document', 'wiaas_ajax_quick_add_document' );
|
||||
add_action( 'wp_ajax_wiaas_link_document', 'wiaas_ajax_link_document' );
|
||||
|
||||
add_action( 'wp_ajax_wiaas_json_search_documents','wiaas_ajax_json_search_documents' );
|
||||
|
||||
|
||||
/**
|
||||
* Upload document version file with ajax
|
||||
*/
|
||||
function wiaas_ajax_upload_document_version() {
|
||||
check_ajax_referer('wiaas_upload_file');
|
||||
|
||||
if (!isset($_FILES['wiaas_file']) || empty($_FILES['wiaas_file'])) {
|
||||
echo "ERROR: No file present!";
|
||||
die();
|
||||
}
|
||||
|
||||
$version = Wiaas_Document_Upload::upload_document_version('wiaas_file');
|
||||
|
||||
if (is_wp_error($version)) {
|
||||
echo 'ERROR:' . $version->get_error_message();
|
||||
die();
|
||||
}
|
||||
|
||||
echo $version;
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Quick add new document and return attachment view for it
|
||||
*/
|
||||
function wiaas_ajax_quick_add_document() {
|
||||
check_ajax_referer('wiaas_quick_add_document');
|
||||
|
||||
//validate file
|
||||
if (!isset($_FILES['wiaas_file']) || empty($_FILES['wiaas_file'])) {
|
||||
echo "ERROR: No file present!";
|
||||
die();
|
||||
}
|
||||
|
||||
// Upload document version file
|
||||
$version = Wiaas_Document_Upload::upload_document_version('wiaas_file');
|
||||
|
||||
// Upload failed so return the error
|
||||
if (is_wp_error($version)) {
|
||||
echo 'ERROR:' . $version->get_error_message();
|
||||
die();
|
||||
}
|
||||
|
||||
// Get document title
|
||||
$title = isset($_POST['doc_title']) && $_POST['doc_title'] !== '' ?
|
||||
sanitize_text_field($_POST['doc_title']) :
|
||||
pathinfo( $version, PATHINFO_FILENAME );
|
||||
|
||||
// Try to create new document
|
||||
$id = Wiaas_Document::add_document(
|
||||
$title,
|
||||
$version,
|
||||
$_POST['doc_visible'] === 'true');
|
||||
|
||||
// Document creation failed so return the error
|
||||
if (!$id) {
|
||||
echo "ERROR: Document could not be created!";
|
||||
die();
|
||||
}
|
||||
|
||||
// If document type is sent then assign new document to it
|
||||
if (isset($_POST['doc_type'])) {
|
||||
Wiaas_Document::set_doc_type( $id, sanitize_key($_POST['doc_type']) );
|
||||
}
|
||||
|
||||
require 'views/html-attached-document.php';
|
||||
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
* Render attacment view for linked document
|
||||
*/
|
||||
function wiaas_ajax_link_document() {
|
||||
check_ajax_referer('wiaas_link_document');
|
||||
|
||||
$id = absint($_GET['id']);
|
||||
|
||||
require 'views/html-attached-document.php';
|
||||
|
||||
die();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*Search wiaas documents by name
|
||||
*/
|
||||
function wiaas_ajax_json_search_documents() {
|
||||
check_ajax_referer('wiaas_json_search_documents');
|
||||
|
||||
$q = ( ! empty( $_GET['query'] ) ? sanitize_text_field($_GET['query']) : '' );
|
||||
|
||||
$result = Wiaas_Document::search($q);
|
||||
|
||||
wp_send_json( $result );
|
||||
}
|
||||
Reference in New Issue
Block a user