diff --git a/gp-nested-forms/gpnf-auto-attach-parent-files.php b/gp-nested-forms/gpnf-auto-attach-parent-files.php new file mode 100644 index 000000000..1569ddd69 --- /dev/null +++ b/gp-nested-forms/gpnf-auto-attach-parent-files.php @@ -0,0 +1,135 @@ +set_config_data( $config ); + $this->init_hooks(); + } + + function init_hooks() { + add_filter( 'gform_notification', array( $this, 'attach_parent_files_to_child_notifications' ), 10, 3 ); + } + + function set_config_data( $config ) { + if ( ! empty( $config['child_form_id'] ) ) { + $this->child_form_id = $config['child_form_id']; + } + + if ( ! empty( $config['parent_form_file_field_ids'] ) ) { + $this->parent_form_file_field_ids = $config['parent_form_file_field_ids']; + } + + if ( ! empty( $config['child_notification_ids'] ) ) { + $this->child_notification_ids = $config['child_notification_ids']; + } + } + + function attach_parent_files_to_child_notifications( $notification, $form, $entry ) { + if ( ! class_exists( 'GPNF_Entry' ) ) { + return $notification; + } + + if ( $this->child_form_id && $form['id'] != $this->child_form_id ) { + return $notification; + } + + if ( ! $this->is_applicable_notification( $notification ) ) { + return $notification; + } + + $attachments =& $notification['attachments']; + + $parent_entry_id = rgar( $entry, 'gpnf_entry_parent' ); + if ( ! $parent_entry_id ) { + return $notification; + } + + $parent_entry = GFAPI::get_entry( $parent_entry_id ); + + if ( ! $parent_entry ) { + return $notification; + } + + foreach ( $this->parent_form_file_field_ids as $field_id ) { + $uploaded_files = rgar( $parent_entry, $field_id ); + + if ( empty( $uploaded_files ) ) { + continue; + } + + $upload_root = GFFormsModel::get_upload_root(); + $upload_field = GFAPI::get_field( $parent_entry['form_id'], $field_id ); + $is_multiple_files = $upload_field->multipleFiles; + + $files = $is_multiple_files ? json_decode( $uploaded_files, true ) : array( $uploaded_files ); + + foreach ( $files as $file ) { + $attachments[] = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $file ); + } + } + + return $notification; + } + + function is_applicable_notification( $notification ) { + return empty( $this->child_notification_ids ) || in_array( $notification['id'], $this->child_notification_ids ); + } +} + +/** + * Configuration Examples + * + * Notes: + * - Specify `child_form_id` to limit this to a specific child form. + * - Specify `parent_form_file_field_ids` to limit which parent fields' files are attached. + * - Specify `child_notification_ids` to limit this to specific child notifications. + */ + +# Attach files from all parent file upload fields to all child notifications. +// new GPNF_Auto_Attach_Parent_Files(); + +# Attach files from parent file upload field with ID 5 to all child notifications. +new GPNF_Auto_Attach_Parent_Files( array( + 'parent_form_file_field_ids' => array( 2 ), +) ); + +# Attach files from parent file upload field with ID 5 to a specific child notification (ID '123abc'). +// new GPNF_Auto_Attach_Parent_Files( array( +// 'parent_form_file_field_ids' => array( 5 ), +// 'child_notification_ids' => array( '123abc' ), +// ) ); + +# Attach files from all parent file upload fields to a specific child form (ID 10). +// new GPNF_Auto_Attach_Parent_Files( array( +// 'child_form_id' => 10, +// ) );