$post_id ) { $file_name = get_attached_file( $post_id ); $path_info = pathinfo( $file_name ); $local_name = $path_info['basename']; $suffix = 0; while( array_key_exists( $local_name, $file_names ) ) { $suffix++; $local_name = $path_info['filename'] . $suffix . '.' . $path_info['extension']; } $file_names[ $local_name ] = $file_name; } /* * Create the ZIP archive */ $upload_dir = wp_upload_dir(); $prefix = ( defined( MLA_OPTION_PREFIX ) ) ? MLA_OPTION_PREFIX : 'mla_'; $date = date("Ymd_B"); $archive_name = $upload_dir['basedir'] . '/' . "{$prefix}_options_{$date}.zip"; if ( file_exists( $archive_name ) ) { @unlink( $archive_name ); } $zip = new ZipArchive(); if ( true !== $zip->open( $archive_name, ZIPARCHIVE::CREATE ) ) { /* translators: 1: ZIP archive file name */ $_REQUEST['mla_admin_message'] = sprintf( __( 'ERROR: The ZIP archive ( %1$s ) could not be created.', 'mla-zip-archive-example' ), $archive_name ); return; } foreach( $file_names as $local_name => $file_name ) { if ( true !== $zip->addFile( $file_name, $local_name ) ) { /* translators: 1: ZIP archive file name */ $_REQUEST['mla_admin_message'] = sprintf( __( 'ERROR: The file ( %1$s ) could not be added to the ZIP archive.', 'mla-zip-archive-example' ), $file_name ); return; } } if ( true !== $zip->close() ) { /* translators: 1: ZIP archive file name */ $_REQUEST['mla_admin_message'] = sprintf( __( 'ERROR: The ZIP archive ( %1$s ) could not be closed.', 'mla-zip-archive-example' ), $archive_name ); return; } $download_args = array( 'page' => MLA::ADMIN_PAGE_SLUG, 'mla_download_file' => urlencode( $archive_name ), 'mla_download_type' => 'application/zip', 'mla_download_disposition' => 'delete' ); wp_redirect( add_query_arg( $download_args, wp_nonce_url( 'upload.php', MLA::MLA_ADMIN_NONCE ) ), 302 ); exit; } // admin_init_action /** * Filter the MLA_List_Table bulk actions * * This MLA-specific filter gives you an opportunity to filter the list of bulk actions; * a good alternative to the 'bulk_actions-media_page_mla-menu' filter. * * @since 1.01 * * @param array $actions An array of bulk actions. * Format: 'slug' => 'Label' * * @return array updated array of actions. */ public static function mla_list_table_get_bulk_actions( $actions ) { //error_log( 'MLADownloadZIPExample::mla_list_table_get_bulk_actions $actions = ' . var_export( $actions, true ), 0 ); $actions[ 'download-zip' ] = __( 'Download', 'mla-zip-archive-example' ); return $actions; } // mla_list_table_get_bulk_actions } // Class MLADownloadZIPExample /* * Install the filters at an early opportunity */ add_action('init', 'MLADownloadZIPExample::initialize'); ?>