Skip to content

Commit

Permalink
Added possibility to cache zip files in temp folder
Browse files Browse the repository at this point in the history
  • Loading branch information
Roope Merikukka committed Jun 6, 2018
1 parent c9dd46b commit 58ae913
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 7 deletions.
39 changes: 39 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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: `<post slug>`.

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.
124 changes: 117 additions & 7 deletions acf-gallery-zipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -32,14 +32,16 @@ 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';


/**
* Plugin hooks goes here.
*/
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' ) );
}


Expand Down Expand Up @@ -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 );
}
}
}


Expand Down Expand Up @@ -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

0 comments on commit 58ae913

Please sign in to comment.