PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 1.2.4
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v1.2.4
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 3.6.66 All 195 releases
fluentform / app / Modules / Integration / MailChimpSubscriber.php

MailChimpSubscriber.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 1.2.4, at app/Modules/Integration/MailChimpSubscriber.php

97 lines 2.9 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\Modules\Integration;
4
5 use FluentForm\App\Services\ConditionAssesor;
6 use FluentForm\Framework\Helpers\ArrayHelper;
7 use FluentForm\App\Services\Integrations\MailChimp;
8
9 trait MailChimpSubscriber
10 {
11 /**
12 * Enabled MailChimp feed settings.
13 *
14 * @var array $feeds
15 */
16 protected $feeds = [];
17
18 /**
19 * Form input data.
20 *
21 * @param array $formData
22 */
23 public function setApplicableFeeds($formData)
24 {
25 $feeds = $this->getAll();
26
27 foreach ($feeds as $feed) {
28 if ($this->isApplicable($feed, $formData)) {
29 $email = ArrayHelper::get($formData, ArrayHelper::get($feed->formattedValue, 'fieldEmailAddress'));
30
31 if (is_string($email) && is_email($email)) {
32 $feed->formattedValue['fieldEmailAddress'] = $email;
33
34 $this->feeds[] = $feed;
35 }
36 }
37 }
38 }
39
40 /**
41 * Determine if the feed is eligible to be applied.
42 *
43 * @param $feed
44 * @param $formData
45 *
46 * @return bool
47 */
48 public function isApplicable(&$feed, &$formData)
49 {
50 return ArrayHelper::get($feed->formattedValue, 'enabled') &&
51 ArrayHelper::get($feed->formattedValue, 'list_id') &&
52 ConditionAssesor::evaluate($feed->formattedValue, $formData);
53 }
54
55 /**
56 * Subscribe a user to the list on form submission.
57 *
58 * @param $formData
59 */
60 public function subscribe($formData)
61 {
62 if ($this->isConfigured()) {
63
64 // Prepare applicable feeds.
65 $this->setApplicableFeeds($formData);
66
67 foreach ($this->feeds as $feed) {
68 $mergeFields = [];
69
70 foreach (ArrayHelper::get($feed->formattedValue, 'merge_fields', []) as $field => $getter) {
71 $value = ArrayHelper::get($formData, $getter, '');
72
73 $mergeFields[$field] = is_array($value) ? implode(' ', $value) : $value;
74 }
75
76 $status = $feed->formattedValue['doubleOptIn'] ? 'pending' : 'subscribed';
77
78 $arguments = [
79 'email_address' => $feed->formattedValue['fieldEmailAddress'],
80 'status' => $status,
81 'merge_fields' => (object) $mergeFields,
82 'double_optin' => $feed->formattedValue['doubleOptIn'],
83 'vip' => $feed->formattedValue['markAsVIP'],
84 ];
85
86 $settings = get_option('_fluentform_mailchimp_details');
87
88 $MailChimp = new MailChimp($settings['apiKey']);
89
90 $endPoint = 'lists/'.$feed->formattedValue['list_id'].'/members/'
91 .md5(strtolower($feed->formattedValue['fieldEmailAddress']));
92
93 $MailChimp->put($endPoint, $arguments);
94 }
95 }
96 }
97 }