|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Gravity Perks // Nested Forms // Auto-attach Uploaded Files from Parent to Child Notifications |
| 4 | + * http://gravitywiz.com/documentation/gravity-forms-nested-forms/ |
| 5 | + * |
| 6 | + * This snippet automatically attaches files uploaded in the parent form to child form notifications. |
| 7 | + * If child forms have any File Upload fields, this snippet will attach files from specified parent upload fields |
| 8 | + * or all parent upload fields to notifications sent by the child form. |
| 9 | + * |
| 10 | + * Plugin Name: GP Nested Forms - Auto-attach Uploaded Files from Parent to Child Notifications |
| 11 | + * Plugin URI: http://gravitywiz.com/documentation/gravity-forms-nested-forms/ |
| 12 | + * Description: Auto-attach Uploaded Files from Parent to Child Notifications |
| 13 | + * Author: Gravity Wiz |
| 14 | + * Version: 0.1 |
| 15 | + * Author URI: https://gravitywiz.com/ |
| 16 | + */ |
| 17 | + |
| 18 | +class GPNF_Auto_Attach_Parent_Files { |
| 19 | + /** |
| 20 | + * The ID of the child form to apply this to (optional). |
| 21 | + */ |
| 22 | + private $child_form_id = null; |
| 23 | + |
| 24 | + /** |
| 25 | + * The IDs of the parent form fields containing files to be attached. |
| 26 | + */ |
| 27 | + private $parent_form_file_field_ids = array(); |
| 28 | + |
| 29 | + /** |
| 30 | + * The IDs of the child form notifications to which parent uploads should be attached (optional). |
| 31 | + */ |
| 32 | + private $child_notification_ids = array(); |
| 33 | + |
| 34 | + function __construct( $config = array() ) { |
| 35 | + $this->set_config_data( $config ); |
| 36 | + $this->init_hooks(); |
| 37 | + } |
| 38 | + |
| 39 | + function init_hooks() { |
| 40 | + add_filter( 'gform_notification', array( $this, 'attach_parent_files_to_child_notifications' ), 10, 3 ); |
| 41 | + } |
| 42 | + |
| 43 | + function set_config_data( $config ) { |
| 44 | + if ( ! empty( $config['child_form_id'] ) ) { |
| 45 | + $this->child_form_id = $config['child_form_id']; |
| 46 | + } |
| 47 | + |
| 48 | + if ( ! empty( $config['parent_form_file_field_ids'] ) ) { |
| 49 | + $this->parent_form_file_field_ids = $config['parent_form_file_field_ids']; |
| 50 | + } |
| 51 | + |
| 52 | + if ( ! empty( $config['child_notification_ids'] ) ) { |
| 53 | + $this->child_notification_ids = $config['child_notification_ids']; |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + function attach_parent_files_to_child_notifications( $notification, $form, $entry ) { |
| 58 | + if ( ! class_exists( 'GPNF_Entry' ) ) { |
| 59 | + return $notification; |
| 60 | + } |
| 61 | + |
| 62 | + if ( $this->child_form_id && $form['id'] != $this->child_form_id ) { |
| 63 | + return $notification; |
| 64 | + } |
| 65 | + |
| 66 | + if ( ! $this->is_applicable_notification( $notification ) ) { |
| 67 | + return $notification; |
| 68 | + } |
| 69 | + |
| 70 | + $attachments =& $notification['attachments']; |
| 71 | + |
| 72 | + $parent_entry_id = rgar( $entry, 'gpnf_entry_parent' ); |
| 73 | + if ( ! $parent_entry_id ) { |
| 74 | + return $notification; |
| 75 | + } |
| 76 | + |
| 77 | + $parent_entry = GFAPI::get_entry( $parent_entry_id ); |
| 78 | + |
| 79 | + if ( ! $parent_entry ) { |
| 80 | + return $notification; |
| 81 | + } |
| 82 | + |
| 83 | + foreach ( $this->parent_form_file_field_ids as $field_id ) { |
| 84 | + $uploaded_files = rgar( $parent_entry, $field_id ); |
| 85 | + |
| 86 | + if ( empty( $uploaded_files ) ) { |
| 87 | + continue; |
| 88 | + } |
| 89 | + |
| 90 | + $upload_root = GFFormsModel::get_upload_root(); |
| 91 | + $upload_field = GFAPI::get_field( $parent_entry['form_id'], $field_id ); |
| 92 | + $is_multiple_files = $upload_field->multipleFiles; |
| 93 | + |
| 94 | + $files = $is_multiple_files ? json_decode( $uploaded_files, true ) : array( $uploaded_files ); |
| 95 | + |
| 96 | + foreach ( $files as $file ) { |
| 97 | + $attachments[] = preg_replace( '|^(.*?)/gravity_forms/|', $upload_root, $file ); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return $notification; |
| 102 | + } |
| 103 | + |
| 104 | + function is_applicable_notification( $notification ) { |
| 105 | + return empty( $this->child_notification_ids ) || in_array( $notification['id'], $this->child_notification_ids ); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +/** |
| 110 | + * Configuration Examples |
| 111 | + * |
| 112 | + * Notes: |
| 113 | + * - Specify `child_form_id` to limit this to a specific child form. |
| 114 | + * - Specify `parent_form_file_field_ids` to limit which parent fields' files are attached. |
| 115 | + * - Specify `child_notification_ids` to limit this to specific child notifications. |
| 116 | + */ |
| 117 | + |
| 118 | +# Attach files from all parent file upload fields to all child notifications. |
| 119 | +// new GPNF_Auto_Attach_Parent_Files(); |
| 120 | + |
| 121 | +# Attach files from parent file upload field with ID 5 to all child notifications. |
| 122 | +new GPNF_Auto_Attach_Parent_Files( array( |
| 123 | + 'parent_form_file_field_ids' => array( 2 ), |
| 124 | +) ); |
| 125 | + |
| 126 | +# Attach files from parent file upload field with ID 5 to a specific child notification (ID '123abc'). |
| 127 | +// new GPNF_Auto_Attach_Parent_Files( array( |
| 128 | +// 'parent_form_file_field_ids' => array( 5 ), |
| 129 | +// 'child_notification_ids' => array( '123abc' ), |
| 130 | +// ) ); |
| 131 | + |
| 132 | +# Attach files from all parent file upload fields to a specific child form (ID 10). |
| 133 | +// new GPNF_Auto_Attach_Parent_Files( array( |
| 134 | +// 'child_form_id' => 10, |
| 135 | +// ) ); |
0 commit comments