PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.60
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.60
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / Integrations / MailChimp / MailChimpIntegration.php

MailChimpIntegration.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.60, at app/Services/Integrations/MailChimp/MailChimpIntegration.php

456 lines 17.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services\Integrations\MailChimp;
4
5 use FluentForm\App\Services\Integrations\IntegrationManager;
6 use FluentForm\App\Services\Integrations\MailChimp\MailChimpSubscriber as Subscriber;
7 use FluentForm\Framework\Foundation\Application;
8 use FluentForm\Framework\Helpers\ArrayHelper;
9
10 class MailChimpIntegration extends IntegrationManager
11 {
12 /**
13 * MailChimp Subscriber that handles & process all the subscribing logics.
14 */
15 use Subscriber;
16
17 public function __construct(Application $application)
18 {
19 parent::__construct(
20 $application,
21 'MailChimp',
22 'mailchimp',
23 '_fluentform_mailchimp_details',
24 'mailchimp_feeds',
25 12
26 );
27
28 $this->description = 'WP Fluent Forms Mailchimp module allows you to create Mailchimp newsletter signup forms in WordPress';
29
30 $this->logo = $this->app->url('public/img/integrations/mailchimp.png');
31 $this->registerAdminHooks();
32
33 add_action('wp_ajax_fluentform_mailchimp_interest_groups', array($this, 'fetchInterestGroups'));
34
35 // add_filter('fluentform_notifying_async_mailchimp', '__return_false');
36 }
37
38 public function getGlobalFields($fields)
39 {
40 return [
41 'logo' => $this->logo,
42 'menu_title' => __('Mailchimp Settings', 'fluentform'),
43 'menu_description' => __('Mailchimp is a marketing platform for small businesses. Send beautiful emails, connect your e-commerce store, advertise, and build your brand. Use Fluent Form to collect customer information and automatically add it to your Mailchimp campaign list. If you don\'t have a Mailchimp account, you can <a href="http://www.mailchimp.com/" target="_blank">sign up for one here.</a>', 'fluentform'),
44 'valid_message' => __('Your Mailchimp API Key is valid', 'fluentform'),
45 'invalid_message' => __('Your Mailchimp API Key is not valid', 'fluentform'),
46 'save_button_text' => __('Save Settings', 'fluentform'),
47 'fields' => [
48 'apiKey' => [
49 'type' => 'text',
50 'label_tips' => __("Enter your Mailchimp API Key, if you do not have <br>Please login to your MailChimp account and go to<br>Profile -> Extras -> Api Keys", 'fluentform'),
51 'label' => __('Mailchimp API Key', 'fluentform'),
52 ]
53 ],
54 'hide_on_valid' => true,
55 'discard_settings' => [
56 'section_description' => 'Your Mailchimp API integration is up and running',
57 'button_text' => 'Disconnect Mailchimp',
58 'data' => [
59 'apiKey' => ''
60 ],
61 'show_verify' => true
62 ]
63 ];
64 }
65
66 public function getGlobalSettings($settings)
67 {
68 $globalSettings = get_option($this->optionKey);
69 if (!$globalSettings) {
70 $globalSettings = [];
71 }
72 $defaults = [
73 'apiKey' => '',
74 'status' => ''
75 ];
76
77 return wp_parse_args($globalSettings, $defaults);
78 }
79
80 public function saveGlobalSettings($mailChimp)
81 {
82 if (!$mailChimp['apiKey']) {
83 $mailChimpSettings = [
84 'apiKey' => '',
85 'status' => false
86 ];
87 // Update the reCaptcha details with siteKey & secretKey.
88 update_option($this->optionKey, $mailChimpSettings, 'no');
89 wp_send_json_success([
90 'message' => __('Your settings has been updated and disconnected', 'fluentform'),
91 'status' => false
92 ], 200);
93 }
94
95 // Verify API key now
96 try {
97 $MailChimp = new MailChimp($mailChimp['apiKey']);
98 $result = $MailChimp->get('lists');
99 if (!$MailChimp->success()) {
100 throw new \Exception($MailChimp->getLastError());
101 }
102 } catch (\Exception $exception) {
103 wp_send_json_error([
104 'message' => $exception->getMessage()
105 ], 400);
106 }
107
108 // MailChimp key is verified now, Proceed now
109
110 $mailChimpSettings = [
111 'apiKey' => sanitize_text_field($mailChimp['apiKey']),
112 'status' => true
113 ];
114
115 // Update the reCaptcha details with siteKey & secretKey.
116 update_option($this->optionKey, $mailChimpSettings, 'no`');
117
118 wp_send_json_success([
119 'message' => __('Your mailchimp api key has been verfied and successfully set', 'fluentform'),
120 'status' => true
121 ], 200);
122 }
123
124 public function pushIntegration($integrations, $formId)
125 {
126 $integrations['mailchimp'] = [
127 'title' => __('Mailchimp Feed', 'fluentform'),
128 'logo' => $this->logo,
129 'is_active' => $this->isConfigured(),
130 'configure_title' => __('Configuration required!', 'fluentform'),
131 'global_configure_url' => admin_url('admin.php?page=fluent_forms_settings#mailchimp'),
132 'configure_message' => __('Mailchimp is not configured yet! Please configure your mailchimp api first', 'fluentform'),
133 'configure_button_text' => __('Set Mailchimp API', 'fluentform')
134 ];
135
136 return $integrations;
137 }
138
139 public function getIntegrationDefaults($settings, $formId)
140 {
141 $settings = [
142 'conditionals' => [
143 'conditions' => [],
144 'status' => false,
145 'type' => 'all'
146 ],
147 'enabled' => true,
148 'list_id' => '',
149 'list_name' => '',
150 'name' => '',
151 'merge_fields' => (object)[],
152 'tags' => '',
153 'tag_routers' => [],
154 'tag_ids_selection_type' => 'simple',
155 'markAsVIP' => false,
156 'fieldEmailAddress' => '',
157 'doubleOptIn' => false,
158 'note' => ''
159 ];
160
161 return $settings;
162 }
163
164 public function getSettingsFields($settings, $formId)
165 {
166 return [
167 'fields' => [
168 [
169 'key' => 'name',
170 'label' => 'Name',
171 'required' => true,
172 'placeholder' => 'Your Feed Name',
173 'component' => 'text'
174 ],
175 [
176 'key' => 'list_id',
177 'label' => 'Mailchimp List',
178 'placeholder' => 'Select Mailchimp List',
179 'tips' => 'Select the Mailchimp list you would like to add your contacts to.',
180 'component' => 'list_ajax_options',
181 'options' => $this->getLists(),
182 ],
183 [
184 'key' => 'merge_fields',
185 'require_list' => true,
186 'label' => 'Map Fields',
187 'tips' => 'Associate your Mailchimp merge tags to the appropriate Fluent Form fields by selecting the appropriate form field from the list.',
188 'component' => 'map_fields',
189 'field_label_remote' => 'Mailchimp Field',
190 'field_label_local' => 'Form Field',
191 'primary_fileds' => [
192 [
193 'key' => 'fieldEmailAddress',
194 'label' => 'Email Address',
195 'required' => true,
196 'input_options' => 'emails'
197 ]
198 ]
199 ],
200 [
201 'key' => 'interest_group',
202 'require_list' => true,
203 'label' => 'Interest Group',
204 'tips' => 'You can map your mailchimp interest group for this contact',
205 'component' => 'chained_fields',
206 'sub_type' => 'radio',
207 'category_label' => 'Select Interest Category',
208 'subcategory_label' => 'Select Interest',
209 'remote_url' => admin_url('admin-ajax.php?action=fluentform_mailchimp_interest_groups'),
210 'inline_tip' => 'Select the mailchimp interest category and interest'
211 ],
212 [
213 'key' => 'tags',
214 'require_list' => true,
215 'label' => 'Tags',
216 'tips' => 'Associate tags to your MailChimp contacts with a comma separated list (e.g. new lead, FluentForms, web source). Commas within a merge tag value will be created as a single tag.',
217 'component' => 'selection_routing',
218 'simple_component' => 'value_text',
219 'routing_input_type' => 'text',
220 'routing_key' => 'tag_ids_selection_type',
221 'settings_key' => 'tag_routers',
222 'labels' => [
223 'choice_label' => 'Enable Dynamic Tag Input',
224 'input_label' => '',
225 'input_placeholder' => 'Tag'
226 ],
227 'inline_tip' => 'Please provide each tag by comma separated value, You can use dynamic smart codes'
228 ],
229 [
230 'key' => 'note',
231 'require_list' => true,
232 'label' => 'Note',
233 'tips' => 'You can write a note for this contact',
234 'component' => 'value_textarea'
235 ],
236 [
237 'key' => 'doubleOptIn',
238 'require_list' => true,
239 'label' => 'Double Opt-in',
240 'tips' => 'When the double opt-in option is enabled,<br />Mailchimp will send a confirmation email<br />to the user and will only add them to your <br /Mailchimp list upon confirmation.',
241 'component' => 'checkbox-single',
242 'checkbox_label' => 'Enable Double Opt-in'
243 ],
244 [
245 'key' => 'markAsVIP',
246 'require_list' => true,
247 'label' => 'VIP',
248 'tips' => 'When enabled,<br /> This contact will be marked as VIP.',
249 'component' => 'checkbox-single',
250 'checkbox_label' => 'Mark as VIP Contact'
251 ],
252 [
253 'require_list' => true,
254 'key' => 'conditionals',
255 'label' => 'Conditional Logics',
256 'tips' => 'Allow mailchimp integration conditionally based on your submission values',
257 'component' => 'conditional_block'
258 ],
259 [
260 'require_list' => true,
261 'key' => 'enabled',
262 'label' => 'Status',
263 'component' => 'checkbox-single',
264 'checkbox_label' => 'Enable This feed'
265 ]
266 ],
267 'button_require_list' => true,
268 'integration_title' => 'Mailchimp'
269 ];
270 }
271
272 public function setFeedAtributes($feed, $formId)
273 {
274 $feed['provider'] = 'mailchimp';
275 $feed['provider_logo'] = $this->logo;
276 return $feed;
277 }
278
279 public function prepareIntegrationFeed($setting, $feed, $formId)
280 {
281 $defaults = $this->getIntegrationDefaults([], $formId);
282
283 foreach ($setting as $settingKey => $settingValue) {
284 if ($settingValue == 'true') {
285 $setting[$settingKey] = true;
286 } else if ($settingValue == 'false') {
287 $setting[$settingKey] = false;
288 } else if ($settingKey == 'conditionals') {
289 if ($settingValue['status'] == 'true') {
290 $settingValue['status'] = true;
291 } else if ($settingValue['status'] == 'false') {
292 $settingValue['status'] = false;
293 }
294 $setting['conditionals'] = $settingValue;
295 }
296 }
297
298 if (!empty($setting['list_id'])) {
299 $setting['list_id'] = (string)$setting['list_id'];
300 }
301
302 $settings['markAsVIP'] = ArrayHelper::isTrue($setting, 'markAsVIP');
303 $settings['doubleOptIn'] = ArrayHelper::isTrue($setting, 'doubleOptIn');
304
305 return wp_parse_args($setting, $defaults);
306 }
307
308 private function getLists()
309 {
310 $settings = get_option('_fluentform_mailchimp_details');
311 try {
312 $MailChimp = new MailChimp($settings['apiKey']);
313 $lists = $MailChimp->get('lists', array('count' => 9999));
314 if (!$MailChimp->success()) {
315 return [];
316 }
317 } catch (\Exception $exception) {
318 return [];
319 }
320
321 $formattedLists = [];
322 foreach ($lists['lists'] as $list) {
323 $formattedLists[$list['id']] = $list['name'];
324 }
325
326 return $formattedLists;
327 }
328
329 public function getMergeFields($list, $listId, $formId)
330 {
331
332 if (!$this->isConfigured()) {
333 return false;
334 }
335 $settings = get_option('_fluentform_mailchimp_details');
336
337 try {
338 $MailChimp = new MailChimp($settings['apiKey']);
339 $list = $MailChimp->get('lists/' . $listId . '/merge-fields', array('count' => 9999));
340 if (!$MailChimp->success()) {
341 return false;
342 }
343 } catch (\Exception $exception) {
344 return false;
345 }
346
347 $mergedFields = $list['merge_fields'];
348 $fields = array();
349
350 foreach ($mergedFields as $merged_field) {
351 $fields[$merged_field['tag']] = $merged_field['name'];
352 }
353
354 return $fields;
355 }
356
357 public function fetchInterestGroups()
358 {
359 $settings = $_REQUEST['settings'];
360
361 $listId = ArrayHelper::get($settings, 'list_id');
362 if(!$listId) {
363 wp_send_json_success([
364 'categories' => [],
365 'subcategories' => [],
366 'reset_values' => true
367 ]);
368 }
369
370 $categoryId = ArrayHelper::get($settings, 'interest_group.category');
371 $categories = $this->getInterestCategories($listId);
372
373 $subCategories = [];
374 if($categoryId) {
375 $subCategories = $this->getInterestSubCategories($listId, $categoryId);
376 }
377
378 wp_send_json_success([
379 'categories' => $categories,
380 'subcategories' => $subCategories,
381 'reset_values' => !$categories && !$subCategories
382 ]);
383 }
384
385 private function getInterestCategories($listId)
386 {
387 $settings = get_option('_fluentform_mailchimp_details');
388 try {
389 $MailChimp = new MailChimp($settings['apiKey']);
390 $categories = $MailChimp->get('/lists/'.$listId.'/interest-categories', array(
391 'count' => 9999,
392 'fields' => 'categories.id,categories.title'
393 ));
394 if (!$MailChimp->success()) {
395 return [];
396 }
397 } catch (\Exception $exception) {
398 return [];
399 }
400 $categories = ArrayHelper::get($categories, 'categories', []);
401 $formattedLists = [];
402 foreach ($categories as $list) {
403 $formattedLists[] = [
404 'value' => $list['id'],
405 'label' => $list['title']
406 ];
407 }
408 return $formattedLists;
409 }
410
411 private function getInterestSubCategories($listId, $categoryId)
412 {
413 $settings = get_option('_fluentform_mailchimp_details');
414 try {
415 $MailChimp = new MailChimp($settings['apiKey']);
416 $categories = $MailChimp->get('/lists/'.$listId.'/interest-categories/'.$categoryId.'/interests', array(
417 'count' => 9999,
418 'fields' => 'interests.id,interests.name'
419 ));
420 if (!$MailChimp->success()) {
421 return [];
422 }
423 } catch (\Exception $exception) {
424 return [];
425 }
426 $categories = ArrayHelper::get($categories, 'interests', []);
427 $formattedLists = [];
428 foreach ($categories as $list) {
429 $formattedLists[] = [
430 'value' => $list['id'],
431 'label' => $list['name']
432 ];
433 }
434 return $formattedLists;
435 }
436
437
438 /*
439 * For Handling Notifications broadcast
440 */
441 public function notify($feed, $formData, $entry, $form)
442 {
443 $response = $this->subscribe($feed, $formData, $entry, $form);
444
445 if ($response == true) {
446 do_action('ff_integration_action_result', $feed, 'success', 'MailChimp feed has been successfully initialed and pushed data');
447 } else {
448 $message = 'Mailchimp feed has been failed to deliver feed';
449 if (is_wp_error($response)) {
450 $message = $response->get_error_message();
451 }
452 do_action('ff_integration_action_result', $feed, 'failed', $message);
453 }
454 }
455 }
456