Created
March 15, 2020 07:05
-
-
Save mlbd/2bad9b26a9cc351a94a29a241dcfd037 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Download multiples files at once in WordPress | |
*/ | |
function vdm_files_downloads() { | |
if ( | |
! isset( $_POST['vdm_download_nonce'] ) | |
|| ! wp_verify_nonce( $_POST['vdm_download_nonce'], 'vdm_download_action' ) | |
|| !isset($_POST['vdm_download_files']) | |
|| empty($_POST['vdm_download_files']) | |
|| !is_user_logged_in() | |
) { | |
return; | |
} | |
/** | |
* Get current user data | |
*/ | |
$current_user = wp_get_current_user(); | |
/** | |
* Start Download Process | |
*/ | |
$download_files = (array) $_POST['vdm_download_files']; | |
$download_files = array_map( 'intval', $download_files ); | |
/** | |
* Folder name | |
*/ | |
$foldername = "vdm-downloaded-files-by-{$current_user->user_login}/"; | |
$new_data = array(); | |
/** | |
* Get current user ID | |
*/ | |
$user_id = $current_user->ID; | |
// get current date | |
$current = date('m/d/Y h:i:s a', time()); | |
$timestamp = strtotime($current); | |
// get old data | |
$old_data = get_user_meta($user_id, '_vdm_download_info', true); | |
/** | |
* Get site current upload dir and path | |
*/ | |
$upload_dir = wp_get_upload_dir(); | |
$filepath = $upload_dir['path'] . '/'; | |
/** | |
* Naming the file. you can change it as you wish. | |
*/ | |
$filename = 'vdm-download-files-' .uniqid() . "-" . $user_id . ".zip"; | |
/** | |
* Full location for the zip. where it should be saved. | |
* | |
*/ | |
$mainFile = $filepath.$filename; | |
/** | |
* Make sure ZipArchive is available | |
*/ | |
if( !class_exists( 'ZipArchive' ) ) { | |
esc_html_e('Sorry, this side did not support PHP default library "ZipArchive".', 'textdomain'); | |
exit; | |
} | |
$kire = []; | |
/** | |
* Start create zip file using default php library : ZipArchive | |
* | |
* @link https://www.php.net/manual/en/class.ziparchive.php | |
* @link https://www.php.net/manual/en/ziparchive.addfile.php | |
* | |
*/ | |
$zip = new ZipArchive; | |
$zip->open($mainFile, ZipArchive::CREATE); | |
foreach ($download_files as $file) { | |
/** | |
* Get attachment file url | |
*/ | |
$url = get_attached_file((int)$file); | |
if ( !isset( $url ) || !file_exists( $url ) ) { | |
return; | |
} | |
$kire[] = $url; | |
/** | |
* Then input in zip file | |
*/ | |
$zip->addFile($url, $foldername . basename($url)); | |
if ( !empty( $old_data ) && array_key_exists($file, $old_data) ) { | |
$old_data[$file]['date'][] = $current; | |
} else { | |
$new_data[$file]['date'][] = $current; | |
$new_data[$file]['size'] = $timestamp; | |
} | |
} | |
if ($zip->close() === false) { | |
exit("Error creating ZIP file"); | |
}; | |
/** | |
* Before download make sure your zip has been created successfully. | |
*/ | |
if (file_exists($mainFile)) { | |
if ( !empty( $old_data ) && !empty($new_data) ) { | |
$new_data = $old_data + $new_data; | |
} elseif ( !empty( $old_data ) && empty( $new_data ) ) { | |
$new_data = $old_data; | |
} | |
update_user_meta($user_id, '_vdm_download_info', $new_data); | |
/** | |
* Clean before start download | |
*/ | |
ob_end_clean(); | |
/** | |
* Get zip file size | |
*/ | |
$filesize = filesize($mainFile); | |
/** | |
* http headers for zip downloads | |
* | |
* @link https://perishablepress.com/http-headers-file-downloads/ | |
*/ | |
header("Pragma: public"); | |
header("Expires: 0"); | |
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); | |
header("Cache-Control: public"); | |
header("Content-Description: File Transfer"); | |
header("Content-type: application/octet-stream"); | |
header("Content-Disposition:attachment; filename=\"".$filename."\""); | |
header("Content-Transfer-Encoding: binary"); | |
header("Content-Length: ".$filesize); | |
ob_end_flush(); | |
@readfile($mainFile); | |
/** | |
* Now delete the zip file. IF you want to store it then you can. just remove this line. | |
*/ | |
unlink($mainFile); | |
} | |
} | |
add_action('wp_loaded', 'vdm_files_downloads'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment