Zip Uploaded Files
Create a zip file from all uploaded files and attach it to a notification.
Instructions
See “Where do I put snippets?” in our documentation for installation instructions.
Code
Filename: gw-zip-files.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php
/**
* Gravity Wiz // Gravity Forms // Zip Uploaded Files
*
* Create a zip file from all uploaded files and attach it to a notification.
*
* @todo
* - add support for multiple zip files per form (by field, by file type)
* - add support for removing file fields from {all_fields}
* - add support for linking to specific zip by 'zip_name' (i.e. {gwzip:zip_name}) or zip ID (i.e. gwzip:zipId})
* - add UI for naming zips
* - add UI for attaching zip to notification
*
* @version 1.5
* @author David Smith <david@gravitywiz.com>
* @license GPL-2.0+
* @link https://gravitywiz.com/
*/
class GW_Zip_Files {
private $_args = array();
public function __construct( $args = array() ) {
// make sure we're running the required minimum version of Gravity Forms
if ( ! property_exists( 'GFCommon', 'version' ) || ! version_compare( GFCommon::$version, '1.8', '>=' ) ) {
return;
}
// ZipArchive must be installed for PHP
if ( ! class_exists( 'ZipArchive' ) ) {
return;
}
// set our default arguments, parse against the provided arguments, and store for use throughout the class
$this->_args = wp_parse_args(
$args, array(
'form_id' => false,
'field_ids' => false,
'zip_name' => 'gf-uploads',
)
);
// time for hooks
add_filter( 'gform_entry_meta', array( $this, 'register_entry_meta' ), 10, 2 );
add_filter( 'gform_entries_field_value', array( $this, 'modify_zip_display_value' ), 10, 3 );
add_filter( 'gform_entry_post_save', array( $this, 'archive_files' ), 10, 2 );
add_filter( 'gform_notification', array( $this, 'add_zip_as_attachment' ), 10, 3 );
add_filter( 'gform_replace_merge_tags', array( $this, 'all_files_merge_tag' ), 10, 7 );
add_filter( 'gform_replace_merge_tags', array( $this, 'zip_url_merge_tag' ), 10, 3 );
}
public function get_nested_entries( $entry, $parent_form ) {
$entries = array();
foreach ( $parent_form['fields'] as $field ) {
if ( $field instanceof GP_Field_Nested_Form ) {
$_entries = explode( ',', $entry[ $field->id ] );
$entries = array_merge( $entries, $_entries );
}
}
return array_unique( $entries );
}
public function archive_files( $entry, $form ) {
if ( ! $this->is_applicable_form( $form ) || ! $this->has_applicable_field( $form ) ) {
return $entry;
}
$nested_entries = $this->get_nested_entries( $entry, $form );
$nested_archive_files = array();
if ( ! empty( $nested_entries ) ) {
foreach ( $nested_entries as $nested_entry_id ) {
$nested_entry = GFAPI::get_entry( $nested_entry_id );
if ( is_wp_error( $nested_entry ) ) {
continue;
}
$nested_form = GFAPI::get_form( $nested_entry['form_id'] );
// Add each nested entry files list as a separate array to avoid overriding previously saved files with the same associative array key (the field id).
$nested_archive_files = array_merge( $nested_archive_files, array( $this->get_entry_files( $nested_entry, $nested_form ) ) );
}
}
// For conformity with the $nested_archive_files, the current (parent) entry files are also loaded onto an array.
$archive_files = array( $this->get_entry_files( $entry, $form ) );
$archive_files = array_merge( $archive_files, $nested_archive_files );
if ( empty( $archive_files ) ) {
return $entry;
}
$archive_file_paths = array();
foreach ( $archive_files as $archive_file ) {
$archive_file_path = array_values( wp_list_pluck( $archive_file, 'path' ) );
$archive_file_paths = array_merge( $archive_file_paths, $archive_file_path );
}
$zip = $this->create_zip( $archive_file_paths, $this->get_zip_paths( $entry, 'path' ) );
if ( $zip ) {
gform_update_meta( $entry['id'], $this->get_meta_key(), $this->get_zip_paths( $entry, 'url' ) );
}
return $entry;
}
/**
* Get all files associated with an entry.
*
* @param $entry
* @param $form
*
* @return array $files An array of files, each "file" including the 'path' and 'url' grouped by field ID:
*
* array(
* FIELD_ID => array(
* 'path' => '/path/to/file.ext',
* 'url' => 'http://url.com/path/to/file.ext'
* ),
* FIELD_ID => array(
* 'path' => '/path/to/file.ext',
* 'url' => 'http://url.com/path/to/file.ext'
* )
* );
*
*/
public function get_entry_files( $entry, $form ) {
$archive_files = array();
foreach ( $form['fields'] as $field ) {
if ( ! $this->is_applicable_field( $field ) ) {
continue;
}
$files = GFFormsModel::get_lead_field_value( $entry, $field );
if ( $this->is_multi_file( $field ) ) {
$files = json_decode( $files );
}
if ( empty( $files ) ) {
continue;
}
if ( ! is_array( $files ) ) {
$files = array( $files );
}
$index = 1;
foreach ( $files as $file ) {
$file_path = $this->convert_url_to_path( $file );
if ( $file_path ) {
$id = $field['id'];
if ( $this->is_multi_file( $field ) ) {
$id = "{$id}.{$index}";
$index ++;
}
$archive_files[ $id ] = array(
'path' => $file_path,
'url' => $file,
);
}
}
}
return $archive_files;
}
public function register_entry_meta( $entry_meta, $form_id ) {
if ( ! empty( $this->_args['field_ids'] ) || ! $this->is_applicable_form( $form_id ) ) {
return $entry_meta;
}
$entry_meta[ $this->get_meta_key() ] = array(
'label' => __( 'Form Uploads Archive', 'gravityforms' ),
'is_numeric' => false,
'is_default_column' => false,
);
return $entry_meta;
}
public function modify_zip_display_value( $value, $form_id, $field_id ) {
if ( ! $this->is_applicable_form( $form_id ) ) {
return $value;
}
if ( $field_id !== $this->get_meta_key() ) {
return $value;
}
$value = sprintf( '<a href="%s">%s</a>', $value, __( 'Download Archive' ) );
return $value;
}
public function is_applicable_form( $form ) {
// @todo This really doesn't belong here... this should be checked where applicable and passed to this function.
if ( rgpost( 'gpnf_parent_form_id' ) ) {
$form_id = intval( rgpost( 'gpnf_parent_form_id' ) );
} elseif ( is_array( $form ) ) {
$form_id = $form['id'];
} else {
$form_id = $form;
}
return ! $this->_args['form_id'] || $form_id == $this->_args['form_id'];
}
public function has_applicable_field( $form ) {
foreach ( $form['fields'] as $field ) {
if ( $this->is_applicable_field( $field ) ) {
return true;
} elseif ( $field->type === 'form' ) {
$child_form = GFAPI::get_form( $field->gpnfForm );
if ( $this->has_applicable_field( $child_form ) ) {
return true;
}
}
}
return false;
}
public function is_applicable_field( $field ) {
$input_type = GFFormsModel::get_input_type( $field );
$is_applicable_input_type = in_array( $input_type, array( 'fileupload', 'post_image' ) );
$is_applicable_field_id = empty( $this->_args['field_ids'] ) || in_array( $field['id'], $this->_args['field_ids'] );
return $is_applicable_input_type && $is_applicable_field_id;
}
public function is_multi_file( $field ) {
return rgar( $field, 'multipleFiles' );
}
public function convert_url_to_path( $url ) {
$bits = explode( '|:|', $url );
$url = array_shift( $bits );
if ( ! $url ) {
return false;
}
if ( is_multisite() && get_site_option( 'ms_files_rewriting' ) ) {
$path = preg_replace( '|^(.*?)/files/gravity_forms/|', BLOGUPLOADDIR . 'gravity_forms/', $url );
} else {
$path = str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $url );
}
return file_exists( $path ) ? $path : false;
}
public function create_zip( $files = array(), $destination = '', $overwrite = false ) {
if ( ! is_array( $files ) ) {
return false;
}
if ( file_exists( $destination ) && ! $overwrite ) {
return false;
}
$valid_files = array();
foreach ( $files as $file ) {
if ( file_exists( $file ) ) {
$valid_files[] = $file;
}
}
if ( empty( $valid_files ) ) {
return false;
}
$zip = new ZipArchive();
if ( $zip->open( $destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE ) !== true ) {
return false;
}
foreach ( $valid_files as $file ) {
$zip->addFile( $file, basename( $file ) );
}
$zip->close();
return file_exists( $destination ) ? $destination : false;
}
public function get_zip_paths( $entry, $type = false ) {
$filename = $this->get_zip_filename( $entry );
$paths = GFFormsModel::get_file_upload_path( $entry['form_id'], $filename );
foreach ( $paths as &$path ) {
$path = str_replace( basename( $path ), $filename, $path );
}
return $type ? rgar( $paths, $type ) : $paths;
}
public function get_zip_filename( $entry ) {
$form_id = $entry['form_id'];
$form = GFAPI::get_form( $form_id );
// replace merge tags in the zip file name
$zip_name = GFCommon::replace_variables( $this->_args['zip_name'], $form, $entry, false, false, false, 'text' );
return $this->get_slug( $zip_name, false, $this->_args['field_ids'] ) . '.zip';
}
public function get_meta_key( $entry_id = false ) {
return $this->get_slug( 'gw_zip', $entry_id, $this->_args['field_ids'] );
}
public function get_slug( $name, $entry_id = false, $field_ids = array() ) {
$bits = array( $name );
if ( $entry_id ) {
$bits[] = $entry_id;
}
if ( ! empty( $field_ids ) ) {
$bits[] = md5( implode( $field_ids ) );
}
return implode( '_', $bits );
}
public function all_files_merge_tag( $text, $form, $entry ) {
$search = '{all_files}';
if ( strpos( $text, $search ) === false || ! $this->is_applicable_form( $form ) ) {
return $text;
}
$replace = $this->get_all_files_output( $entry );
return str_replace( $search, $replace, $text );
}
public function zip_url_merge_tag( $text, $form, $entry ) {
$search = '{zip_url}';
if ( strpos( $text, $search ) === false || ! $this->is_applicable_form( $form ) ) {
return $text;
}
$replace = '';
$zip_file = $this->get_zip_paths( $entry, 'path' );
if ( $zip_file ) {
$zip = new ZipArchive;
if ( $zip->open( $zip_file ) && $zip->numFiles > 0 ) {
$replace = $this->get_zip_paths( $entry, 'url' );
}
}
return str_replace( $search, $replace, $text );
}
public function get_all_files_output( $entry ) {
$zip_file = $this->get_zip_paths( $entry, 'path' );
if ( ! $zip_file ) {
return '';
}
$zip = new ZipArchive;
if ( ! $zip->open( $zip_file ) || $zip->numFiles <= 0 ) {
return '';
}
$files = $this->get_entry_files( $entry, GFAPI::get_form( $entry['form_id'] ) );
$file_urls = wp_list_pluck( $files, 'url' );
$file_links = array();
foreach ( $file_urls as $file_url ) {
$file_links[] = sprintf( '<a href="%s">%s</a>', $file_url, basename( $file_url ) );
}
$replace = array_merge(
array( __( 'Uploaded Files:' ) ),
$file_links,
array( sprintf( '<a href="%s">%s</a>', $this->get_zip_paths( $entry, 'url' ), __( 'All Files' ) ) )
);
$replace = implode( '', $replace );
return $replace;
}
public function is_applicable_notification( $notification ) {
if ( ! isset( $this->_args['notifications'] ) ) {
return true;
}
if ( isset( $this->_args['notifications'] ) && empty( $this->_args['notifications'] ) ) {
return true;
}
if ( isset( $this->_args['notifications'] ) && ! empty( $this->_args['notifications'] ) ) {
return in_array( $notification['id'], $this->_args['notifications'] );
}
return true;
}
public function get_nested_entries_archives( $notification, $form, $entry ) {
foreach ( $form['fields'] as $field ) {
if ( $field instanceof GP_Field_Nested_Form ) {
$value = RGFormsModel::get_lead_field_value( $entry, $field );
if ( ! empty( $value ) ) {
foreach ( explode( ',', $value ) as $nested_entry_id ) {
$nested_entry = GFAPI::get_entry( (int) $nested_entry_id );
$zip_path = $this->get_zip_paths( $nested_entry, 'path' );
if ( $zip_path ) {
if ( isset( $notification['attachments'] ) ) {
$notification['attachments'] = is_array( $notification['attachments'] ) ? $notification['attachments'] : array( $notification['attachments'] );
} else {
$notification['attachments'] = array();
}
$notification['attachments'][] = $zip_path;
}
}
}
}
}
return $notification;
}
public function add_zip_as_attachment( $notification, $form, $entry ) {
if ( ! $this->is_applicable_notification( $notification ) || ! $this->is_applicable_form( $form ) ) {
return $notification;
}
// phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure, WordPress.CodeAnalysis.AssignmentInCondition.Found
if ( $zip_path = $this->get_zip_paths( $entry, 'path' ) ) {
if ( isset( $notification['attachments'] ) ) {
$notification['attachments'] = is_array( $notification['attachments'] ) ? $notification['attachments'] : array( $notification['attachments'] );
} else {
$notification['attachments'] = array();
}
$notification['attachments'][] = $zip_path;
}
return $notification;
}
}
# Configuration
new GW_Zip_Files(
array(
'form_id' => 123,
'zip_name' => 'my-sweet-archive', // supports merge tags
'notifications' => array( '5f4668ec2afbb' ),
)
);
Tags:
featured
how to restrict this to a single form ID ? and to generate zip file on upload and on an update notification
Hi Jacinta,
The configuration at the bottom of the snippet allows you to restrict it to a single form and a specific notification. Does that work for you? If this isn’t what you want, please submit a ticket for this via our support form and provide us with additional information on what you’re trying to do.
Best,