-
Notifications
You must be signed in to change notification settings - Fork 0
/
gf-upload-to-email-attachment.php
92 lines (69 loc) · 3.97 KB
/
gf-upload-to-email-attachment.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
<?php
/*
Plugin Name: GF Upload to Email Attachment
Plugin URI: http://wpcms.ninja/
Description: Gravity Forms was built to be able to store all uploaded files to the server and email you a link. There are times that you need to have that file get attached to the notification email. By creating a notification in the form with GFUEA added to the end of it tells Gravity Forms to also attach any files to the outbound email as well as save it with the entry in the back-end. If multiple files are attached it attempts to create a zip file to send.
Author: Greg Whitehead
Author URI: http://wpcms.ninja
Version: 1.1
*/
add_filter('gform_notification', 'GFUEA_custom_notification_attachments', 10, 3);
function GFUEA_custom_notification_attachments( $notification, $form, $entry ) {
$log = 'rw_notification_attachments() - ';
GFCommon::log_debug( $log . 'starting.' );
# mail("greg@wpcms.ninja","Notification Fire" . date("Y-m-d h:i:s"), print_r($notification, true) . print_r($form,true) . print_r($entry,true));
if(substr($notification["name"],-5) == "GFUEA" ){
//mail("greg@wpcms.ninja","Notification Fire" . date("Y-m-d h:i:s"), print_r($notification, true) . print_r($form,true) . print_r($entry,true));
$fileupload_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload' ) );
if ( ! is_array( $fileupload_fields ) ) {
return $notification;
}
$attachments = array();
$upload_root = RGFormsModel::get_upload_root();
foreach( $fileupload_fields as $field ) {
$url = $entry[ $field['id'] ];
if ( empty( $url ) ) {
continue;
} elseif ( $field['multipleFiles'] ) {
$uploaded_files = json_decode( stripslashes( $url ), true );
$zip = new ZipArchive();
//$filetext = date("Y-m-d his");
$filename = $upload_root . "/uploaded_files".$entry['id'].".zip";
if ($zip->open($filename, ZipArchive::CREATE)!==TRUE) {
foreach ( $uploaded_files as $uploaded_file ) {
$attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $uploaded_file );
GFCommon::log_debug( $log . 'attaching the file: ' . print_r( $attachment, true ) );
$attachments[] = $attachment;
}
} else {
foreach ( $uploaded_files as $uploaded_file ) {
$attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $uploaded_file );
GFCommon::log_debug( $log . 'attaching the file: ' . print_r( $attachment, true ) );
$new_filename = substr($attachment,strrpos($attachment,'/') + 1);
$zip->addFile($attachment,$new_filename);
//$attachments[] = $attachment;
}
$zip->close();
$attachments[] = $filename;
add_filter( 'gform_confirmation', 'gfuea_clean_zips', 10, 4 );
}
} else {
$attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $url );
GFCommon::log_debug( $log . 'attaching the file: ' . print_r( $attachment, true ) );
$attachments[] = $attachment;
}
}
$notification['attachments'] = $attachments;
}
//mail("greg@wpcms.ninja","Notification Fire" . date("Y-m-d h:i:s"), "Attach IDs:\n" . print_r($attachIds, true) . "\nNotification:\n" . print_r($notification, true) . "\nForm:\n" .print_r($form,true) . "\nEntry:\n" .print_r($entry,true));
GFCommon::log_debug( $log . 'stopping.' );
return $notification;
}
function gfuea_clean_zips($confirmation, $form, $entry, $ajax) {
$upload_root = RGFormsModel::get_upload_root();
$filename = $upload_root . "/uploaded_files".$entry['id'].".zip";
if (is_file($filename)){
unlink($filename);
}
return $confirmation;
}