PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.7
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.7
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 6.2.7, at app/Services/Integrations/MailChimp/MailChimpIntegration.php

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