|
11 | 11 | * Plugin URI: http://gravitywiz.com/documentation/gravity-forms-nested-forms/ |
12 | 12 | * Description: Auto-attach Uploaded Files from Child to Parent Notifications |
13 | 13 | * Author: Gravity Wiz |
14 | | - * Version: 0.1 |
| 14 | + * Version: 0.2 |
15 | 15 | * Author URI: https://gravitywiz.com/ |
16 | 16 | */ |
17 | | -add_filter( 'gform_notification', function( $notification, $form, $entry ) { |
18 | | - // Notes: |
19 | | - // - If notifications IDs are specified this snippet will only work for those specific forms. |
20 | | - // - If the parent form has upload fields, they will be attached instead of the child form's fields. |
21 | | - // - If neither of these conditions are present, child form uploads will be attached by default. |
22 | | - // Configuration: |
23 | | - // 1) Ensure that the parent form's notification has the "Attach uploaded fields to notification" checked. |
24 | | - // 2) [Optional] Modify the following array to limit the snippet to specific notification IDs. |
25 | | - // Notification IDs can be retrieved from the nid parameter in the URL when editing a notification. |
26 | | - // Example: $notification_ids = array( '5daaedb49dc32', '5dbce25cc21c2' ); |
27 | | - $notification_ids = array(); |
28 | | - |
29 | | - if ( ! class_exists( 'GPNF_Entry' ) ) { |
30 | | - return $notification; |
| 17 | +class GPNF_Auto_Attach_Child_Files { |
| 18 | + /** |
| 19 | + * The ID of the parent form to apply this to (optional). |
| 20 | + */ |
| 21 | + private $parent_form_id = null; |
| 22 | + |
| 23 | + /** |
| 24 | + * The IDs of the child form field to apply this to (optional). |
| 25 | + */ |
| 26 | + private $child_form_field_ids = null; |
| 27 | + |
| 28 | + /** |
| 29 | + * The IDs of the parent form notifications to which child uploads should be attached (optional). |
| 30 | + */ |
| 31 | + private $notification_ids = array(); |
| 32 | + |
| 33 | + /** |
| 34 | + * The IDs of child form fields whose files should be attached to notifications (optional). |
| 35 | + */ |
| 36 | + private $child_form_upload_field_ids = array(); |
| 37 | + |
| 38 | + function __construct( $config = array() ) { |
| 39 | + $this->set_config_data( $config ); |
| 40 | + $this->init_hooks(); |
31 | 41 | } |
32 | 42 |
|
33 | | - $upload_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload' ) ); |
| 43 | + function init_hooks() { |
| 44 | + add_filter( 'gform_notification', array( $this, 'handle_notification_attachments' ), 10, 3 ); |
| 45 | + } |
34 | 46 |
|
35 | | - if ( ! empty( $notification_ids ) && ! in_array( $notification['id'], $notification_ids ) ) { |
36 | | - return $notification; |
37 | | - } else { |
38 | | - // If parent form has upload fields, rely on the notification's Attachments setting. |
39 | | - if ( ! empty( $upload_fields ) ) { |
40 | | - if ( ! rgar( $notification, 'enableAttachments', false ) ) { |
41 | | - return $notification; |
42 | | - } |
| 47 | + function set_config_data( $config ) { |
| 48 | + if ( ! empty( $config['parent_form_id'] ) ) { |
| 49 | + $this->parent_form_id = $config['parent_form_id']; |
| 50 | + } |
| 51 | + |
| 52 | + if ( ! empty( $config['child_form_field_ids'] ) ) { |
| 53 | + $this->child_form_field_ids = $config['child_form_field_ids']; |
| 54 | + } |
| 55 | + |
| 56 | + if ( ! empty( $config['notification_ids'] ) ) { |
| 57 | + $this->notification_ids = $config['notification_ids']; |
| 58 | + } |
| 59 | + |
| 60 | + if ( ! empty( $config['child_form_upload_field_ids'] ) ) { |
| 61 | + $this->child_form_upload_field_ids = $config['child_form_upload_field_ids']; |
43 | 62 | } |
44 | 63 | } |
45 | 64 |
|
46 | | - $attachments =& $notification['attachments']; |
47 | | - $parent_entry = new GPNF_Entry( $entry ); |
| 65 | + function handle_notification_attachments( $notification, $form, $entry ) { |
| 66 | + if ( ! class_exists( 'GPNF_Entry' ) ) { |
| 67 | + return $notification; |
| 68 | + } |
48 | 69 |
|
49 | | - foreach ( $form['fields'] as $field ) { |
| 70 | + if ( $this->parent_form_id && $form['id'] != $this->parent_form_id ) { |
| 71 | + return $notification; |
| 72 | + } |
50 | 73 |
|
51 | | - if ( $field->get_input_type() !== 'form' ) { |
52 | | - continue; |
| 74 | + $upload_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload' ) ); |
| 75 | + |
| 76 | + if ( ! $this->is_applicable_notification( $notification ) ) { |
| 77 | + return $notification; |
53 | 78 | } |
54 | 79 |
|
55 | | - $upload_root = GFFormsModel::get_upload_root(); |
56 | | - $upload_fields = GFCommon::get_fields_by_type( GFAPI::get_form( $field->gpnfForm ), array( 'fileupload' ) ); |
57 | | - $child_entries = $parent_entry->get_child_entries( $field->id ); |
| 80 | + // If parent form has upload fields, rely on the notification's Attachments setting. |
| 81 | + if ( ! empty( $upload_fields ) && ! rgar( $notification, 'enableAttachments', false ) ) { |
| 82 | + return $notification; |
| 83 | + } |
58 | 84 |
|
59 | | - foreach ( $child_entries as $child_entry ) { |
60 | | - foreach ( $upload_fields as $upload_field ) { |
| 85 | + $attachments =& $notification['attachments']; |
| 86 | + $parent_entry = new GPNF_Entry( $entry ); |
61 | 87 |
|
62 | | - $attachment_urls = rgar( $child_entry, $upload_field->id ); |
63 | | - if ( empty( $attachment_urls ) ) { |
64 | | - continue; |
65 | | - } |
| 88 | + foreach ( $form['fields'] as $field ) { |
| 89 | + if ( $field->get_input_type() !== 'form' ) { |
| 90 | + continue; |
| 91 | + } |
66 | 92 |
|
67 | | - $attachment_urls = $upload_field->multipleFiles ? json_decode( $attachment_urls, true ) : array( $attachment_urls ); |
| 93 | + if ( $this->child_form_field_ids && ! in_array( $field['id'], $this->child_form_field_ids ) ) { |
| 94 | + continue; |
| 95 | + } |
| 96 | + |
| 97 | + $upload_fields = GFCommon::get_fields_by_type( GFAPI::get_form( $field->gpnfForm ), array( 'fileupload' ) ); |
| 98 | + $child_entries = $parent_entry->get_child_entries( $field->id ); |
| 99 | + $upload_root = GFFormsModel::get_upload_root(); |
| 100 | + |
| 101 | + foreach ( $child_entries as $child_entry ) { |
| 102 | + foreach ( $upload_fields as $upload_field ) { |
| 103 | + if ( ! $this->is_applicable_upload_field( $upload_field ) ) { |
| 104 | + continue; |
| 105 | + } |
68 | 106 |
|
69 | | - foreach ( $attachment_urls as $attachment_url ) { |
70 | | - $attachment_url = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $attachment_url ); |
71 | | - $attachments[] = $attachment_url; |
| 107 | + $attachment_urls = rgar( $child_entry, $upload_field->id ); |
| 108 | + if ( empty( $attachment_urls ) ) { |
| 109 | + continue; |
| 110 | + } |
| 111 | + |
| 112 | + $attachment_urls = $upload_field->multipleFiles ? json_decode( $attachment_urls, true ) : array( $attachment_urls ); |
| 113 | + |
| 114 | + foreach ( $attachment_urls as $attachment_url ) { |
| 115 | + $attachment_url = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $attachment_url ); |
| 116 | + $attachments[] = $attachment_url; |
| 117 | + } |
72 | 118 | } |
73 | 119 | } |
74 | 120 | } |
| 121 | + |
| 122 | + return $notification; |
| 123 | + } |
| 124 | + |
| 125 | + function is_applicable_notification( $notification ) { |
| 126 | + return empty( $this->notification_ids ) || in_array( $notification['id'], $this->notification_ids ); |
| 127 | + } |
| 128 | + |
| 129 | + function is_applicable_upload_field( $upload_field ) { |
| 130 | + return empty( $this->child_form_upload_field_ids ) || in_array( $upload_field->id, $this->child_form_upload_field_ids ); |
75 | 131 | } |
| 132 | +} |
| 133 | + |
| 134 | +/** |
| 135 | + * Notes: |
| 136 | + * - If notifications IDs are specified this snippet will only work for those specific forms. |
| 137 | + * - If child form upload field IDs are specified, only files from those fields will be attached. |
| 138 | + * - If the parent form has upload fields, they will be attached instead of the child form's fields. |
| 139 | + * - If none of these conditions are present, child form uploads will be attached by default. |
| 140 | + * |
| 141 | + * Configuration: |
| 142 | + * |
| 143 | + * 1) Ensure that the parent form's notification has the "Attach uploaded fields to notification" checked. |
| 144 | + * 2) [Optional] Modify one ore more of the following examples to limit the snippet to specific notification IDs or child form upload field IDs. |
| 145 | + * |
| 146 | + * 💡 Notification IDs can be retrieved from the nid parameter in the URL when editing a notification. |
| 147 | +*/ |
| 148 | + |
| 149 | +# ------------------------------------------------- |
| 150 | +# Examples |
| 151 | +# ------------------------------------------------- |
| 152 | + |
| 153 | +# Attach all child form uploads to all notifications. |
| 154 | +// new GPNF_Auto_Attach_Child_Files(); |
| 155 | + |
| 156 | +# Attach all uploads from child forms to *only* the notification with id "63331f3fcf7f0". |
| 157 | +// new GPNF_Auto_Attach_Child_Files( array( |
| 158 | +// 'notification_ids' => array( '63331f3fcf7f0' ), |
| 159 | +// ) ); |
| 160 | + |
| 161 | +# Attach child form upload from field with ID 3 to all notifications. |
| 162 | +// new GPNF_Auto_Attach_Child_Files( array( |
| 163 | +// 'child_form_upload_field_ids' => array( 3 ), |
| 164 | +// ) ); |
| 165 | + |
| 166 | + |
| 167 | +# Attach child form upload from field with ID 3 to only notification with id "63331f3fcf7f0". |
| 168 | +// new GPNF_Auto_Attach_Child_Files( array( |
| 169 | +// 'notification_ids' => array( '63331f3fcf7f0' ), |
| 170 | +// 'child_form_upload_field_ids' => array( 3 ), |
| 171 | +// ) ); |
76 | 172 |
|
77 | | - return $notification; |
78 | | -}, 10, 3 ); |
| 173 | +# Attach all child form uploads only on the child form with field ID of 2 on the parent form with ID of 1 |
| 174 | +// new GPNF_Auto_Attach_Child_Files( array( |
| 175 | +// 'parent_form_id' => 1, |
| 176 | +// 'child_form_field_ids' => array( 2 ), |
| 177 | +// ) ); |
0 commit comments