From 58ae913489e8407d7eaa402e29b04ce0cc1d47f6 Mon Sep 17 00:00:00 2001 From: Roope Merikukka Date: Wed, 6 Jun 2018 17:29:28 +0300 Subject: [PATCH] Added possibility to cache zip files in temp folder --- README.md | 39 +++++++++++++ acf-gallery-zipper.php | 124 ++++++++++++++++++++++++++++++++++++++--- 2 files changed, 156 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 14d8350..be2e283 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,45 @@ function acf_gallery_zipper_change_default_field_name( $field_name ) { } ``` +#### acf_gallery_zipper_filename + +Use this filter to change the zip filename. Default: ``. + +Example: + +```php +add_filter( 'acf_gallery_zipper_filename', 'acf_gallery_zipper_change_default_filename' ); +function acf_gallery_zipper_change_default_filename( $filename ) { + return $filename . '-suffix'; // .zip will be added automatically. +} +``` + +#### acf_gallery_zipper_use_cache + +Use this filter to change the cache usage. By default the zip files are not stored anywhere but deleted right after they are sent to the user. Default: `false`. + +Example: + +```php +add_filter( 'acf_gallery_zipper_use_cache', 'acf_gallery_zipper_change_default_use_cache' ); +function acf_gallery_zipper_change_default_use_cache( $use_cache ) { + return true; +} +``` + +#### acf_gallery_zipper_removal_recurrence + +Use this filter to change the cache removal recurrence. Default: `daily`. Possible values: `hourly`, `twicedaily` and `daily`. + +Example: + +```php +add_filter( 'acf_gallery_zipper_removal_recurrence', 'acf_gallery_zipper_removal_change_default_recurrence' ); +function acf_gallery_zipper_removal_recurrence( $recurrence ) { + return 'hourly'; +} +``` + ## Contributing We are open for suggestions so open an issue if you have any feature requests or bug reports. Please do not create a pull request without creating related issue first. \ No newline at end of file diff --git a/acf-gallery-zipper.php b/acf-gallery-zipper.php index 0f029db..abce7f0 100644 --- a/acf-gallery-zipper.php +++ b/acf-gallery-zipper.php @@ -4,7 +4,7 @@ * Plugin URI: https://github.com/bond-agency/acf-gallery-zipper * GitHub Plugin URI: https://github.com/bond-agency/acf-gallery-zipper * Description: Plugin creates a REST endpoint for zipping an ACF gallery field contents. - * Version: 0.2.0 + * Version: 0.3.0 * Author: Bond Agency * Author uri: https://bond-agency.com * License: GPLv3 @@ -32,7 +32,8 @@ class ACF_Gallery_Zipper { protected static $required_php_extensions = []; // PHP extensions required by the plugin. protected static $namespace = 'acf-gallery-zipper'; protected static $api_version = 'v1'; - + protected static $use_cache = false; // Should we cache the zip files to temp folder? + protected static $recurrence = 'daily'; /** @@ -40,6 +41,7 @@ class ACF_Gallery_Zipper { */ public function __construct() { add_action( 'rest_api_init', array( $this, 'register_zip_endpoint' ) ); + add_action( 'acf_gallery_zipper_zip_removal', array( $this, 'acf_gallery_zipper_zip_removal' ) ); } @@ -92,16 +94,123 @@ public function zip_post_gallery_field( $request ) { ); } - $post_slug = get_post_field( 'post_name', $post_id ); - $field = get_field( $field_name, $post_id ); + $post_slug = get_post_field( 'post_name', $post_id ); + $filename = apply_filters( 'acf_gallery_zipper_filename', $post_slug ); + $use_cache = apply_filters( 'acf_gallery_zipper_use_cache', self::$use_cache ); + + /** + * If we are using cache, start scheduling the removal + * and try to retreve the zip form the temp folder. + */ + if ( $use_cache ) { + // Start schedule for removals. + $this->start_schedule_zip_removal(); + + // Try to get the file from cache. + $zip_path = $this->get_zip_path( $filename ); + + if ( file_exists( $zip_path ) ) { + $this->send_zip_attachment( $zip_path ); + wp_die(); + } + } else { + $this->stop_schedule_zip_removal(); + } + + $field = get_field( $field_name, $post_id ); + + if ( ! $field ) { + return new WP_Error( + 'invalid_field_name', + 'The defined field was not found from the post.', + array( 'status' => 400 ) + ); + } + $media_paths = $this->get_file_paths( $field ); - $zip_path = $this->create_zip_file( $media_paths, $post_slug ); + $zip_path = $this->create_zip_file( $media_paths, $filename ); + + $this->send_zip_attachment( $zip_path ); + if ( ! $use_cache ) { + unlink( $zip_path ); + $this->acf_gallery_zipper_zip_removal(); + } + } + + + + /** + * Function returns the absolute path for the given filename. + * + * @param String $filename - File basename. + * @return String - Absolute path to the given file. + */ + protected function get_zip_path( $filename ) { + return plugin_dir_path( __FILE__ ) . 'temp/' . $filename . '.zip'; + } + + + + /** + * Function sends the zip file. + * + * @param String $zip_path - The absolute path to the zip file. + * @return void + */ + protected function send_zip_attachment( $zip_path ) { header( 'Content-disposition: attachment; filename="' . basename( $zip_path ) . '"' ); header( 'Content-type: application/zip' ); readfile( $zip_path ); - unlink( $zip_path ); - return true; + } + + + + /** + * Function schedules the removal of the zip files. + */ + protected function start_schedule_zip_removal() { + $exists = wp_get_schedule( 'acf_gallery_zipper_zip_removal' ); + $recurrence = apply_filters( 'acf_gallery_zipper_removal_recurrence', self::$recurrence ); + $options = [ 'hourly', 'twicedaily', 'daily' ]; + + if ( ! in_array( $recurrence, $options ) ) { + $recurrence = self::$recurrence; + } + + if ( ! $exists && ! wp_next_scheduled( 'acf_gallery_zipper_zip_removal' ) ) { + wp_schedule_event( time(), $recurrence, 'acf_gallery_zipper_zip_removal' ); + } + + if ( $exists !== $recurrence ) { + self::stop_schedule_zip_removal(); + wp_schedule_event( time(), $recurrence, 'acf_gallery_zipper_zip_removal' ); + } + } + + + + /** + * Function is used to remove the scheduled removal. + */ + protected static function stop_schedule_zip_removal() { + wp_clear_scheduled_hook( 'acf_gallery_zipper_zip_removal' ); + } + + + + /** + * Function scans the temp folder and removes all zip files from it. + * This function is used with cron and its triggered with + * `start_schedule_zip_removal` function. + */ + public function acf_gallery_zipper_zip_removal() { + $files = glob( plugin_dir_path( __FILE__ ) . 'temp/*.zip' ); + if ( is_array( $files ) ) { + foreach ( $files as $file ) { + unlink( $file ); + } + } } @@ -231,6 +340,7 @@ public static function on_deactivation() { check_admin_referer( "deactivate-plugin_{$plugin}" ); // Your deactivation code below this line. + self::stop_schedule_zip_removal(); } } // Class ends