diff --git a/assets/js/audio-diary.js b/assets/js/audio-diary.js index 9944562..fe63ade 100644 --- a/assets/js/audio-diary.js +++ b/assets/js/audio-diary.js @@ -57,6 +57,47 @@ jQuery(document).ready(function($) { 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() { if (!isRecording) { navigator.mediaDevices.getUserMedia({ audio: true }) @@ -109,7 +150,7 @@ jQuery(document).ready(function($) { allowToastClose: true, hideAfter: 3000, stack: 3, - position: 'top-center', + position: 'bottom-center', textAlign: 'left', loader: true, loaderBg: '#9EC600', @@ -131,22 +172,24 @@ jQuery(document).ready(function($) { } }); - $('.delete-audio').on('click', function() { - let fileName = $(this).data('file'); - let $audioRow = $(this).closest('tr'); // یافتن ردیف حاوی فایل صوتی برای حذف آن + $('#download-selected').on('click', function() { + let selectedFiles = []; + $('.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({ url: ajaxurl, type: 'POST', data: { - action: 'delete_audio', - file_name: fileName + action: 'download_zip', + files: selectedFiles }, success: function(response) { if (response.success) { $.toast({ - text: "Audio file deleted successfully", + text: "Zip file created successfully", heading: 'Note', icon: 'success', showHideTransition: 'fade', @@ -159,20 +202,26 @@ jQuery(document).ready(function($) { loaderBg: '#9EC600', }); - // پس از حذف موفق فایل، ردیف مربوطه را از لیست حذف کنید - $audioRow.fadeOut(400, function() { - $(this).remove(); - }); + // ایجاد لینک دانلود + let downloadLink = document.createElement('a'); + 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 { $.toast({ - text: "Failed to delete audio file: " + response.data, + text: "Failed to create zip file: " + response.data, heading: 'Error', icon: 'error', showHideTransition: 'fade', allowToastClose: true, hideAfter: 3000, stack: 3, - position: 'top-center', + position: 'bottom-center', textAlign: 'left', loader: true, loaderBg: '#FF0000', @@ -189,7 +238,7 @@ jQuery(document).ready(function($) { allowToastClose: true, hideAfter: 3000, stack: 3, - position: 'top-center', + position: 'bottom-center', textAlign: 'left', loader: true, 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() { @@ -245,7 +310,7 @@ jQuery(document).ready(function($) { allowToastClose: true, hideAfter: 3000, stack: 3, - position: 'top-center', + position: 'bottom-center', textAlign: 'left', loader: true, loaderBg: '#FF0000', @@ -262,13 +327,27 @@ jQuery(document).ready(function($) { allowToastClose: true, hideAfter: 3000, stack: 3, - position: 'top-center', + position: 'bottom-center', textAlign: 'left', loader: true, 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', + }); } }); }); \ No newline at end of file diff --git a/audio-diary.php b/audio-diary.php index 7cce304..26617a7 100644 --- a/audio-diary.php +++ b/audio-diary.php @@ -9,6 +9,35 @@ if ( ! defined( 'ABSPATH' ) ) { echo "what the hell are you doing here?"; 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{ /** diff --git a/classes/class_Audio_diary_loader.php b/classes/class_Audio_diary_loader.php index 3752c29..8acf423 100644 --- a/classes/class_Audio_diary_loader.php +++ b/classes/class_Audio_diary_loader.php @@ -33,6 +33,7 @@ class Audio_Diary_loader{ */ public function __construct() { $this->load_dependencies(); + } /** * Load the required dependencies for this plugin. diff --git a/classes/class_admin_page.php b/classes/class_admin_page.php index 5bfd8b7..5a8dc98 100644 --- a/classes/class_admin_page.php +++ b/classes/class_admin_page.php @@ -32,12 +32,67 @@ class Audio_Diary_Admin_Page { 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_selected_audios', array($this, 'delete_selected_audios')); - - - + add_action('wp_ajax_download_zip', array($this,'handle_download_zip')); $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() { if (!current_user_can('manage_options')) { wp_send_json_error('Unauthorized'); diff --git a/modules/audio-diary-admin-list-page.php b/modules/audio-diary-admin-list-page.php index cb5fcde..a8fcbdf 100644 --- a/modules/audio-diary-admin-list-page.php +++ b/modules/audio-diary-admin-list-page.php @@ -13,6 +13,8 @@ usort($audio_files, function($a, $b) {