Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions gravity-forms/gw-add-attachments-by-field.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,20 @@
add_filter( 'gform_notification', function( $notification, $form, $entry ) {

$notification_name = 'Notification A';
$upload_field_id = 1;
$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_id = 2;
$upload_field_ids = array( 2, 3 );

return gw_add_attachments_by_field( $notification, $form, $entry, $notification_name, $upload_field_id );
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_id ) {
function gw_add_attachments_by_field( $notification, $form, $entry, $notification_name, $upload_field_ids ) {

if ( $notification['name'] !== $notification_name ) {
return $notification;
Expand All @@ -34,22 +34,24 @@ function gw_add_attachments_by_field( $notification, $form, $entry, $notificatio
$notification['attachments'] = rgar( $notification, 'attachments', array() );
$upload_root = RGFormsModel::get_upload_root();

$field = GFAPI::get_field( $form, $upload_field_id );
$url = rgar( $entry, $field->id );
foreach ( $upload_field_ids as $upload_field_id ) {
$field = GFAPI::get_field( $form, $upload_field_id );
$url = rgar( $entry, $field->id );

if ( empty( $url ) ) {
return $notification;
}
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 );
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;
}
} else {
$attachment = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $url );
$notification['attachments'][] = $attachment;
}

return $notification;
Expand Down
Loading