-
Notifications
You must be signed in to change notification settings - Fork 0
/
acf-gallery-zipper.php
346 lines (272 loc) · 9.96 KB
/
acf-gallery-zipper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/**
* Plugin Name: ACF Gallery Zipper
* 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.3.0
* Author: Bond Agency
* Author uri: https://bond-agency.com
* License: GPLv3
* License URI: https://www.gnu.org/licenses/gpl.html
* Text Domain: acf-gallery-zipper
*/
// If this file is called directly, abort.
if ( ! defined( 'WPINC' ) ) {
die;
}
// Run installation function only once on activation.
register_activation_hook( __FILE__, [ 'ACF_Gallery_Zipper', 'on_activation' ] );
register_deactivation_hook( __FILE__, [ 'ACF_Gallery_Zipper', 'on_deactivation' ] );
add_action( 'plugins_loaded', [ 'ACF_Gallery_Zipper', 'init' ] );
class ACF_Gallery_Zipper {
protected static $instance;
protected static $version = '1.0.0'; // The current version of the plugin.
protected static $min_wp_version = '4.7.5'; // Minimum required WordPress version.
protected static $min_php_version = '7.0'; // Minimum required PHP version.
protected static $class_dependencies = [ 'acf' ]; // Class dependencies of the plugin.
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' ) );
}
/**
* Create instance of the plugin.
*/
public static function init() {
if ( is_null( self::$instance ) ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Register endpoint for zip creation
*/
public function register_zip_endpoint() {
register_rest_route(
self::$namespace . '/' . self::$api_version, '/zip/(?P<id>\d+)', array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( $this, 'zip_post_gallery_field' ),
)
);
}
/**
* Function creates zip file from given posts ACF gallery field.
*
* @param WP_REST_Request $request
*/
public function zip_post_gallery_field( $request ) {
$post_id = $request->get_param( 'id' );
$default_post_type = 'post';
$post_type = apply_filters( 'acf_gallery_zipper_post_type', $default_post_type );
$default_field_name = 'gallery';
$field_name = apply_filters( 'acf_gallery_zipper_field_name', $default_field_name );
if ( get_post_type( $post_id ) !== $post_type ) {
return new WP_Error(
'invalid_post_type',
'The defined post type didn\'t match with the given post id.',
array( 'status' => 400 )
);
}
$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, $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 );
}
/**
* 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 );
}
}
}
/**
* Function returns file paths from ACF gallery field contents.
*
* @param Array $gallery_field - ACF gallery field
* @return Array $paths - List of file paths
*/
protected function get_file_paths( $gallery_field ) {
$paths = [];
foreach ( $gallery_field as $media ) {
$paths[] = get_attached_file( $media['id'] );
}
return $paths;
}
/**
* Function creates zip file from list of file paths.
*
* @param Array $paths - Array of file paths to zip.
* @param String $filename - Name for the zip file.
* @return String $zip_path - Path of the newly created zip file.
*/
protected function create_zip_file( $paths, $filename ) {
// create new zip object
$zip = new ZipArchive();
$zip_path = plugin_dir_path( __FILE__ ) . 'temp/' . $filename . '.zip';
$zip->open( $zip_path, ZipArchive::CREATE );
foreach ( $paths as $path ) {
$zip->addFile( $path, basename( $path ) );
}
$zip->close();
return $zip_path;
}
/**
* Checks if plugin dependencies & requirements are met.
*/
protected static function are_requirements_met() {
// Check for WordPress version
if ( version_compare( get_bloginfo( 'version' ), self::$min_wp_version, '<' ) ) {
return false;
}
// Check the PHP version
if ( version_compare( PHP_VERSION, self::$min_php_version, '<' ) ) {
return false;
}
// Check PHP loaded extensions
foreach ( self::$required_php_extensions as $ext ) {
if ( ! extension_loaded( $ext ) ) {
return false;
}
}
// Check for required classes
foreach ( self::$class_dependencies as $class_name ) {
if ( ! class_exists( $class_name ) ) {
return false;
}
}
return true;
}
/**
* Checks if plugin dependencies & requirements are met. If they are it doesn't
* do anything if they aren't it will die.
*/
public static function ensure_requirements_are_met() {
if ( ! self::are_requirements_met() ) {
deactivate_plugins( __FILE__ );
$message = "<p>Some of the plugin dependencies aren't met and the plugin can't be enabled. ";
$message .= 'This plugin requires the followind dependencies:</p>';
$message .= '<ul><li>Minimum WP version: ' . self::$min_wp_version . '</li>';
$message .= '<li>Minimum PHP version: ' . self::$min_php_version . '</li>';
$message .= '<li>Classes / plugins: ' . implode( ', ', self::$class_dependencies ) . '</li>';
$message .= '<li>PHP extensions: ' . implode( ', ', self::$required_php_extensions ) . '</li></ul>';
wp_die( $message );
}
}
/**
* A function that's run once when the plugin is activated. We just create
* a scheduled run for the press release update.
*/
public static function on_activation() {
// Security stuff.
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "activate-plugin_{$plugin}" );
// Check requirements.
self::ensure_requirements_are_met();
// Your activation code below this line.
}
/**
* A function that's run once when the plugin is deactivated. We just delete
* the scheduled run for the press release update.
*/
public static function on_deactivation() {
// Security stuff.
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
check_admin_referer( "deactivate-plugin_{$plugin}" );
// Your deactivation code below this line.
self::stop_schedule_zip_removal();
}
} // Class ends