· 5 years ago · May 14, 2020, 12:16 PM
1<?php
2
3if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5}
6
7use FluentForm\App\Services\Integrations\IntegrationManager;
8use FluentForm\Framework\Foundation\Application;
9use FluentForm\Framework\Helpers\ArrayHelper;
10
11class WPF_FluentForms extends IntegrationManager {
12
13 public $slug;
14
15 public function __construct() {
16 parent::__construct(
17 false,
18 'WP Fusion',
19 'wpfusion',
20 '_fluentform_wpfusion_settings',
21 'fluentform_wpfusion_feed',
22 16
23 );
24
25 $this->logo = WPF_DIR_URL . 'assets/img/logo-wide-color.png';
26
27 $this->description = sprintf( __( 'WP Fusion syncs your Fluent Forms entries to %s.', 'wp-fusion' ), wp_fusion()->crm->name );
28
29 $this->registerAdminHooks();
30
31 $this->slug = 'fluent-forms';
32 wp_fusion()->integrations->{'fluent-forms'} = $this;
33
34 // add_filter('fluentform_notifying_async_wpfusion', '__return_false');
35 }
36
37 public function getGlobalFields( $fields ) {
38 return [
39 'logo' => $this->logo,
40 'menu_title' => __( 'WP Fusion Settings', 'wp-fusion' ),
41 'menu_description' => sprintf( __( 'Fluent Forms is already connected to %s by WP Fusion, there\'s nothing to configure here. You can set up WP Fusion your individual forms under Settings » Marketing & CRM Integrations. For more information <a href="https://wpfusion.com/documentation/lead-generation/fluent-forms/" target="_blank">see the documentation</a>.', 'wp-fusion' ), wp_fusion()->crm->name ),
42 'valid_message' => __( 'Your Mailchimp API Key is valid', 'fluentform' ),
43 'invalid_message' => ' ',
44 'save_button_text' => ' ',
45 ];
46 }
47
48 /**
49 * Set integration to configured
50 *
51 * @access public
52 * @return bool Configured
53 */
54
55 public function isConfigured() {
56 return true;
57 }
58
59 /**
60 * Register the integration
61 *
62 * @access public
63 * @return array Integrations
64 */
65
66 public function pushIntegration( $integrations, $form_id ) {
67
68 $integrations[ $this->integrationKey ] = [
69 'title' => $this->title . ' Integration',
70 'logo' => $this->logo,
71 'is_active' => true,
72 'configure_title' => 'Configration required!',
73 'global_configure_url' => admin_url( 'admin.php?page=fluent_forms_settings#general-wpfusion-settings' ),
74 'configure_message' => 'WP Fusion is not configured yet! Please configure your WP Fusion API first',
75 'configure_button_text' => 'Set WP Fusion API',
76 ];
77
78 return $integrations;
79
80 }
81
82 /**
83 * Get integration defaults
84 *
85 * @access public
86 * @return array Defaults
87 */
88
89 public function getIntegrationDefaults( $settings, $form_id ) {
90
91 return [
92 'name' => '',
93 'fieldEmailAddress' => '',
94 'custom_field_mappings' => (object) [],
95 'default_fields' => (object) [],
96 'note' => '',
97 'tags' => '',
98 'conditionals' => [
99 'conditions' => [],
100 'status' => false,
101 'type' => 'all',
102 ],
103 'instant_responders' => false,
104 'last_broadcast_campaign' => false,
105 'enabled' => true,
106 ];
107 }
108
109 /**
110 * Get settings fields
111 *
112 * @access public
113 * @return array Settings
114 */
115
116 public function getSettingsFields( $settings, $form_id ) {
117 return [
118 'fields' => [
119 [
120 'key' => 'name',
121 'label' => 'Name',
122 'required' => true,
123 'placeholder' => 'Your Feed Name',
124 'component' => 'text',
125 ],
126 [
127 'key' => 'custom_field_mappings',
128 'require_list' => false,
129 'label' => 'Map Fields',
130 'tips' => 'Select which Fluent Form fields pair with their respective ' . wp_fusion()->crm->name . ' fields.',
131 'component' => 'map_fields',
132 'field_label_remote' => wp_fusion()->crm->name . ' Field',
133 'field_label_local' => 'Form Field',
134 'default_fields' => $this->getMergeFields( false, false, $form_id ),
135 ],
136 [
137 'key' => 'tags',
138 'require_list' => false,
139 'label' => __( 'Tags', 'wp-fusion' ),
140 'tips' => __( 'Associate tags to your contacts with a comma separated list (e.g. new lead, FluentForms, web source).', 'wp-fusion' ),
141 'component' => 'value_text',
142 'inline_tip' => __( 'Enter tag names or tag IDs, separated by commas', 'wp-fusion' ),
143 ],
144 [
145 'require_list' => false,
146 'key' => 'conditionals',
147 'label' => 'Conditional Logics',
148 'tips' => __( 'Allow WP Fusion integration conditionally based on your submission values', 'wp-fusion' ),
149 'component' => 'conditional_block',
150 ],
151 [
152 'require_list' => false,
153 'key' => 'enabled',
154 'label' => 'Status',
155 'component' => 'checkbox-single',
156 'checkobox_label' => 'Enable This feed',
157 ],
158 ],
159 'button_require_list' => false,
160 'integration_title' => $this->title,
161 ];
162 }
163
164 /**
165 * Get CRM fields
166 *
167 * @access public
168 * @return array Fields
169 */
170
171 public function getMergeFields( $list, $list_id, $form_id ) {
172
173 $fields = array();
174
175 $available_fields = wp_fusion()->settings->get( 'crm_fields', array() );
176
177 if ( isset( $available_fields['Standard Fields'] ) ) {
178
179 $available_fields = array_merge( $available_fields['Standard Fields'], $available_fields['Custom Fields'] );
180
181 }
182
183 asort( $available_fields );
184
185 foreach ( $available_fields as $field_id => $field_label ) {
186
187 $remote_required = false;
188
189 if ( $field_label == 'Email' ) {
190 $remote_required = true;
191 }
192
193 $fields[] = array(
194 'name' => $field_id,
195 'label' => $field_label,
196 'required' => $remote_required,
197 );
198
199 }
200
201 return $fields;
202
203 }
204
205
206 /**
207 * Handle form submission
208 *
209 * @access public
210 * @return void
211 */
212
213 public function notify( $feed, $form_data, $entry, $form ) {
214
215 $email_address = false;
216
217 $update_data = $feed['processedValues']['default_fields'];
218
219 foreach ( $update_data as $field => $value ) {
220
221 $update_data[ $field ] = apply_filters( 'wpf_format_field_value', $value, 'text', $field );
222
223 if ( $email_address == false && is_email( $value ) ) {
224 $email_address = $value;
225 }
226 }
227
228 $input_tags = explode( ',', $feed['processedValues']['tags'] );
229
230 $apply_tags = array();
231
232 // Get tags to apply
233 foreach ( $input_tags as $tag ) {
234
235 $tag_id = wp_fusion()->user->get_tag_id( $tag );
236
237 if ( false === $tag_id ) {
238
239 wpf_log( 'notice', 0, 'Warning: ' . $tag . ' is not a valid tag name or ID.' );
240 continue;
241
242 }
243
244 $apply_tags[] = $tag_id;
245
246 }
247
248 $args = array(
249 'email_address' => $email_address,
250 'update_data' => $update_data,
251 'apply_tags' => $apply_tags,
252 'add_only' => false,
253 'integration_slug' => 'fluent_forms',
254 'integration_name' => 'Fluent Forms',
255 'form_id' => $form->id,
256 'form_title' => $form->title,
257 'form_edit_link' => admin_url( 'admin.php?page=fluent_forms&route=editor&form_id=' . $form->id ),
258 );
259
260 require_once WPF_DIR_PATH . 'includes/integrations/class-forms-helper.php';
261
262 $contact_id = WPF_Forms_Helper::process_form_data( $args );
263
264 if ( is_wp_error( $contact_id ) ) {
265 do_action( 'ff_integration_action_result', $feed, 'failed', $contact_id->get_error_message() );
266 } else {
267 do_action( 'ff_integration_action_result', $feed, 'success', 'Entry synced to ' . wp_fusion()->crm->name . ' (contact ID ' . $contact_id . ')' );
268 }
269
270 }
271
272}
273
274new WPF_FluentForms();