Created
August 13, 2024 13:16
-
-
Save JAW-Dev/b468d9ab6f701c1363c87fdcaa0afb4f 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
<?php | |
namespace YourNamespace\Services; | |
use Symfony\Component\Filesystem\Filesystem; | |
use Symfony\Component\Finder\Finder; | |
use \ZipArchive; | |
use \Exception; | |
class DownloadBundleService | |
{ | |
/** | |
* Generate a zip file containing the bundle's files. | |
* | |
* @param int $post_id The post ID of the bundle. | |
* @param string $temporary_files_dir The directory to store temporary files. | |
* @return string The full path to the generated zip file. | |
* @throws Exception | |
*/ | |
public function generateZip(int $post_id, string $temporary_files_dir): string | |
{ | |
$bundle_name = sanitize_file_name(get_the_title($post_id)) . '.zip'; | |
$full_bundle_path = $temporary_files_dir . $bundle_name; | |
$files = $this->writeTemporaryFiles($post_id, $temporary_files_dir); | |
$zip = new ZipArchive(); | |
if ($zip->open($full_bundle_path, ZipArchive::CREATE) === true) { | |
foreach ($files as $file_array) { | |
$zip->addFile($file_array['path'], $file_array['name']); | |
} | |
$zip->close(); | |
} else { | |
throw new Exception('Unable to create zip file.'); | |
} | |
return $full_bundle_path; | |
} | |
/** | |
* Write temporary files for the bundle. | |
* | |
* @param int $post_id The post ID of the bundle. | |
* @param string $temporary_files_dir The directory to store temporary files. | |
* @return array An array of file details with keys 'name' and 'path'. | |
*/ | |
private function writeTemporaryFiles(int $post_id, string $temporary_files_dir): array | |
{ | |
$posts = $this->getDownloadPosts($post_id); | |
return array_map( | |
function ($download_post) use ($temporary_files_dir) { | |
return $this->writeTemporaryFile($download_post, $temporary_files_dir); | |
}, | |
$posts | |
); | |
} | |
/** | |
* Write a temporary file for a download post. | |
* | |
* @param \WP_Post $download_post The download post object. | |
* @param string $temporary_files_dir The directory to store the file. | |
* @return array An array with keys 'name' and 'path'. | |
*/ | |
private function writeTemporaryFile($download_post, string $temporary_files_dir): array | |
{ | |
if (empty($download_post)) { | |
return []; | |
} | |
$download = new Download($download_post->ID); | |
$name = $download->get_filename(); | |
$path = $download->write_file($temporary_files_dir); | |
return [ | |
'name' => $name, | |
'path' => $path, | |
]; | |
} | |
/** | |
* Get the download posts associated with the bundle. | |
* | |
* @param int $post_id The post ID of the bundle. | |
* @return array An array of \WP_Post objects. | |
*/ | |
private function getDownloadPosts(int $post_id): array | |
{ | |
$field_array = get_field('bundle_downloads', $post_id); | |
return array_map( | |
function (array $array) { | |
return $array['download']; | |
}, | |
$field_array | |
); | |
} | |
/** | |
* Clean up temporary files. | |
* | |
* @param string $temporary_files_dir The directory of temporary files. | |
*/ | |
public function cleanUpTemporaryFiles(string $temporary_files_dir): void | |
{ | |
$file_system = new Filesystem(); | |
$file_system->remove($temporary_files_dir); | |
$finder = new Finder(); | |
$finder->directories()->in($this->getTempDirParent())->date('before yesterday'); | |
$file_system->remove($finder); | |
} | |
/** | |
* Get the parent directory for temporary files. | |
* | |
* @return string The path to the parent directory. | |
*/ | |
private function getTempDirParent(): string | |
{ | |
return wp_get_upload_dir()['basedir'] . '/pdf-bundles-temporary/'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment