-
Notifications
You must be signed in to change notification settings - Fork 91
/
Copy pathgw-add-attachments-by-field.php
58 lines (46 loc) · 1.95 KB
/
gw-add-attachments-by-field.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
<?php
/**
* Gravity Wiz // Gravity Forms // Add Attachments by Field
* https://gravitywiz.com/
*
* Experimental Snippet 🧪
*
* Gravity Forms provides an option to add all uploaded files as attachments but it does not provide the ability to
* choose which fields to attach. This snippet allows you to attach a specific field's uploaded files to a specific
* notification by name and field ID.
*/
add_filter( 'gform_notification', function( $notification, $form, $entry ) {
$notification_name = 'Notification A';
$upload_field_ids = array( 1 );
return gw_add_attachments_by_field( $notification, $form, $entry, $notification_name, $upload_field_id );
}, 10, 3 );
add_filter( 'gform_notification', function( $notification, $form, $entry ) {
$notification_name = 'Notification B';
$upload_field_ids = array( 2, 3 );
return gw_add_attachments_by_field( $notification, $form, $entry, $notification_name, $upload_field_ids );
}, 10, 3 );
function gw_add_attachments_by_field( $notification, $form, $entry, $notification_name, $upload_field_ids ) {
if ( $notification['name'] !== $notification_name ) {
return $notification;
}
$notification['attachments'] = rgar( $notification, 'attachments', array() );
$upload_root = RGFormsModel::get_upload_root();
foreach ( $upload_field_ids as $upload_field_id ) {
$field = GFAPI::get_field( $form, $upload_field_id );
$url = rgar( $entry, $field->id );
if ( empty( $url ) ) {
continue;
}
if ( $field->multipleFiles ) {
$uploaded_files = json_decode( stripslashes( $url ), true );
foreach ( $uploaded_files as $uploaded_file ) {
$attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $uploaded_file );
$notification['attachments'][] = $attachment;
}
} else {
$attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $url );
$notification['attachments'][] = $attachment;
}
}
return $notification;
}