add download zip file

This commit is contained in:
haakel 2024-06-21 16:43:16 +03:30
parent 42a3cfb5d2
commit b9ed728e86
5 changed files with 187 additions and 21 deletions

View File

@ -57,6 +57,47 @@ jQuery(document).ready(function($) {
draw(); draw();
} }
$('#download-zip').on('click', function() {
var selectedFiles = [];
$('.select-audio:checked').each(function() {
selectedFiles.push($(this).val());
});
if (selectedFiles.length > 0) {
var form = document.createElement('form');
form.method = 'POST';
form.action = '';
selectedFiles.forEach(function(file) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = 'selected_files[]';
input.value = file;
form.appendChild(input);
});
document.body.appendChild(form);
form.submit();
} else {
$.toast({
text: "Please select at least one audio file to download.",
heading: 'Note',
icon: 'error',
showHideTransition: 'fade',
allowToastClose: true,
hideAfter: 3000,
stack: 3,
position: 'bottom-center',
textAlign: 'left',
loader: true,
loaderBg: '#FF0000',
});
}
});
$('#recording-button').on('click', function() { $('#recording-button').on('click', function() {
if (!isRecording) { if (!isRecording) {
navigator.mediaDevices.getUserMedia({ audio: true }) navigator.mediaDevices.getUserMedia({ audio: true })
@ -109,7 +150,7 @@ jQuery(document).ready(function($) {
allowToastClose: true, allowToastClose: true,
hideAfter: 3000, hideAfter: 3000,
stack: 3, stack: 3,
position: 'top-center', position: 'bottom-center',
textAlign: 'left', textAlign: 'left',
loader: true, loader: true,
loaderBg: '#9EC600', loaderBg: '#9EC600',
@ -131,22 +172,24 @@ jQuery(document).ready(function($) {
} }
}); });
$('.delete-audio').on('click', function() { $('#download-selected').on('click', function() {
let fileName = $(this).data('file'); let selectedFiles = [];
let $audioRow = $(this).closest('tr'); // یافتن ردیف حاوی فایل صوتی برای حذف آن $('.select-audio:checked').each(function() {
selectedFiles.push($(this).val());
});
if (confirm("Are you sure you want to delete this audio file?")) { if (selectedFiles.length > 0) {
$.ajax({ $.ajax({
url: ajaxurl, url: ajaxurl,
type: 'POST', type: 'POST',
data: { data: {
action: 'delete_audio', action: 'download_zip',
file_name: fileName files: selectedFiles
}, },
success: function(response) { success: function(response) {
if (response.success) { if (response.success) {
$.toast({ $.toast({
text: "Audio file deleted successfully", text: "Zip file created successfully",
heading: 'Note', heading: 'Note',
icon: 'success', icon: 'success',
showHideTransition: 'fade', showHideTransition: 'fade',
@ -159,20 +202,26 @@ jQuery(document).ready(function($) {
loaderBg: '#9EC600', loaderBg: '#9EC600',
}); });
// پس از حذف موفق فایل، ردیف مربوطه را از لیست حذف کنید // ایجاد لینک دانلود
$audioRow.fadeOut(400, function() { let downloadLink = document.createElement('a');
$(this).remove(); downloadLink.href = response.data.zip_url;
}); downloadLink.download = response.data.zip_url.split('/').pop();
document.body.appendChild(downloadLink);
downloadLink.click();
document.body.removeChild(downloadLink);
// برداشتن انتخاب فایل‌های صوتی
$('.select-audio').prop('checked', false);
} else { } else {
$.toast({ $.toast({
text: "Failed to delete audio file: " + response.data, text: "Failed to create zip file: " + response.data,
heading: 'Error', heading: 'Error',
icon: 'error', icon: 'error',
showHideTransition: 'fade', showHideTransition: 'fade',
allowToastClose: true, allowToastClose: true,
hideAfter: 3000, hideAfter: 3000,
stack: 3, stack: 3,
position: 'top-center', position: 'bottom-center',
textAlign: 'left', textAlign: 'left',
loader: true, loader: true,
loaderBg: '#FF0000', loaderBg: '#FF0000',
@ -189,7 +238,7 @@ jQuery(document).ready(function($) {
allowToastClose: true, allowToastClose: true,
hideAfter: 3000, hideAfter: 3000,
stack: 3, stack: 3,
position: 'top-center', position: 'bottom-center',
textAlign: 'left', textAlign: 'left',
loader: true, loader: true,
loaderBg: '#FF0000', loaderBg: '#FF0000',
@ -198,6 +247,22 @@ jQuery(document).ready(function($) {
}); });
} }
}); });
$('#select-all').on('click', function() {
let isSelectAll = $(this).data('select-all');
if (isSelectAll) {
$('.select-audio').prop('checked', true);
$(this).text('Unselect All');
} else {
$('.select-audio').prop('checked', false);
$(this).text('Select All');
}
// تغییر حالت دکمه
$(this).data('select-all', !isSelectAll);
});
// حذف فایل‌های انتخاب‌شده // حذف فایل‌های انتخاب‌شده
$('#delete-selected').on('click', function() { $('#delete-selected').on('click', function() {
@ -245,7 +310,7 @@ jQuery(document).ready(function($) {
allowToastClose: true, allowToastClose: true,
hideAfter: 3000, hideAfter: 3000,
stack: 3, stack: 3,
position: 'top-center', position: 'bottom-center',
textAlign: 'left', textAlign: 'left',
loader: true, loader: true,
loaderBg: '#FF0000', loaderBg: '#FF0000',
@ -262,13 +327,27 @@ jQuery(document).ready(function($) {
allowToastClose: true, allowToastClose: true,
hideAfter: 3000, hideAfter: 3000,
stack: 3, stack: 3,
position: 'top-center', position: 'bottom-center',
textAlign: 'left', textAlign: 'left',
loader: true, loader: true,
loaderBg: '#FF0000', loaderBg: '#FF0000',
}); });
} }
}); });
} else {
$.toast({
text: "Please select at least one audio file to delete.",
heading: 'Error',
icon: 'error',
showHideTransition: 'fade',
allowToastClose: true,
hideAfter: 3000,
stack: 3,
position: 'bottom-center',
textAlign: 'left',
loader: true,
loaderBg: '#FF0000',
});
} }
}); });
}); });

View File

@ -9,6 +9,35 @@ if ( ! defined( 'ABSPATH' ) ) {
echo "what the hell are you doing here?"; echo "what the hell are you doing here?";
exit; exit;
} }
// فعال کردن کرون جاب در هنگام فعال‌سازی افزونه
register_activation_hook(__FILE__, 'audio_diary_activate');
function audio_diary_activate() {
if (!wp_next_scheduled('delete_old_zip_files_cron')) {
wp_schedule_event(time(), 'hourly', 'delete_old_zip_files_cron');
}
}
// غیرفعال کردن کرون جاب در هنگام غیرفعال‌سازی افزونه
register_deactivation_hook(__FILE__, 'audio_diary_deactivate');
function audio_diary_deactivate() {
$timestamp = wp_next_scheduled('delete_old_zip_files_cron');
wp_unschedule_event($timestamp, 'delete_old_zip_files_cron');
}
// تعریف اکشن برای حذف فایل‌های زیپ قدیمی
add_action('delete_old_zip_files_cron', 'delete_old_zip_files');
function delete_old_zip_files() {
$uploads = wp_upload_dir();
$files = glob($uploads['basedir'] . '/audio-diary-selected-*.zip');
$time_limit = 3600; // 1 ساعت به ثانیه
foreach ($files as $file) {
if (filemtime($file) < (time() - $time_limit)) {
unlink($file);
}
}
}
class Audio_diary{ class Audio_diary{
/** /**

View File

@ -33,6 +33,7 @@ class Audio_Diary_loader{
*/ */
public function __construct() { public function __construct() {
$this->load_dependencies(); $this->load_dependencies();
} }
/** /**
* Load the required dependencies for this plugin. * Load the required dependencies for this plugin.

View File

@ -32,12 +32,67 @@ class Audio_Diary_Admin_Page {
add_action('wp_ajax_save_audio', array($this, 'save_audio')); add_action('wp_ajax_save_audio', array($this, 'save_audio'));
add_action('wp_ajax_delete_audio', array($this, 'delete_audio')); add_action('wp_ajax_delete_audio', array($this, 'delete_audio'));
add_action('wp_ajax_delete_selected_audios', array($this, 'delete_selected_audios')); add_action('wp_ajax_delete_selected_audios', array($this, 'delete_selected_audios'));
add_action('wp_ajax_download_zip', array($this,'handle_download_zip'));
$this->create_audio_folder(); $this->create_audio_folder();
} }
function delete_old_zip_files() {
$uploads = wp_upload_dir();
$files = glob($uploads['basedir'] . '/audio-diary-selected-*.zip');
$time_limit = 3600; // 1 hour in seconds
foreach ($files as $file) {
if (filemtime($file) < (time() - $time_limit)) {
unlink($file);
}
}
}
function handle_download_zip() {
if (!current_user_can('manage_options')) {
wp_send_json_error(['message' => 'Unauthorized']);
return;
}
if (!isset($_POST['files']) || !is_array($_POST['files'])) {
wp_send_json_error(['message' => 'Invalid request']);
return;
}
$selected_files = $_POST['files'];
$uploads = wp_upload_dir();
$zip = new ZipArchive();
$zip_filename = 'audio-diary-selected-' . time() . '.zip';
$zip_filepath = $uploads['basedir'] . '/' . $zip_filename;
if ($zip->open($zip_filepath, ZipArchive::CREATE) !== TRUE) {
wp_send_json_error(['message' => 'Cannot create zip file']);
return;
}
foreach ($selected_files as $file) {
$file_path = $uploads['basedir'] . '/audio-diary/' . $file;
if (file_exists($file_path)) {
$zip->addFile($file_path, $file);
}
}
$zip->close();
$zip_url = $uploads['baseurl'] . '/' . $zip_filename;
wp_send_json_success(['zip_url' => $zip_url]);
}
function delete_audio() { function delete_audio() {
if (!current_user_can('manage_options')) { if (!current_user_can('manage_options')) {
wp_send_json_error('Unauthorized'); wp_send_json_error('Unauthorized');

View File

@ -13,6 +13,8 @@ usort($audio_files, function($a, $b) {
<div class="wrap audio-diary-admin-list-page"> <div class="wrap audio-diary-admin-list-page">
<h1><?php _e('Recorded Audios', 'audio-diary'); ?></h1> <h1><?php _e('Recorded Audios', 'audio-diary'); ?></h1>
<button id="delete-selected"><?php _e('Delete Selected', 'audio-diary'); ?></button> <button id="delete-selected"><?php _e('Delete Selected', 'audio-diary'); ?></button>
<button id="download-zip"><?php _e('Download Selected as ZIP', 'audio-diary'); ?></button>
<button id="select-all" data-select-all="true">Select All</button>
<table> <table>
<thead> <thead>
<tr> <tr>