PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.31
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.31
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 / MailChimpSubscriber.php

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

216 lines 6.6 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\ConditionAssesor;
6 use FluentForm\App\Services\Integrations\LogResponseTrait;
7 use FluentForm\Framework\Helpers\ArrayHelper;
8
9 trait MailChimpSubscriber
10 {
11 use LogResponseTrait;
12
13 /**
14 * Enabled MailChimp feed settings.
15 *
16 * @var array $feeds
17 */
18 protected $feeds = [];
19
20 /**
21 * Required for api response logging
22 * @var string
23 */
24 protected $metaKey = 'fluentform_mailchimp_feed';
25
26 /**
27 * Form input data.
28 *
29 * @param array $formData
30 */
31 public function setApplicableFeeds($formData)
32 {
33 $feeds = $this->getAll();
34
35 foreach ($feeds as $feed) {
36 if ($this->isApplicable($feed, $formData)) {
37 $email = ArrayHelper::get(
38 $formData, ArrayHelper::get($feed->formattedValue, 'fieldEmailAddress')
39 );
40
41 if (is_string($email) && is_email($email)) {
42 $feed->formattedValue['fieldEmailAddress'] = $email;
43
44 $this->feeds[] = $feed;
45 }
46 }
47 }
48 }
49
50 /**
51 * Determine if the feed is eligible to be applied.
52 *
53 * @param $feed
54 * @param $formData
55 *
56 * @return bool
57 */
58 public function isApplicable(&$feed, &$formData)
59 {
60 return ArrayHelper::get($feed->formattedValue, 'enabled') &&
61 ArrayHelper::get($feed->formattedValue, 'list_id') &&
62 ConditionAssesor::evaluate($feed->formattedValue, $formData);
63 }
64
65 /**
66 * Subscribe a user to the list on form submission.
67 *
68 * @param $feed
69 * @param $formData
70 * @param $entry
71 * @param $form
72 * @return array|bool|false
73 * @throws \Exception
74 */
75 public function subscribe($feed, $formData, $entry, $form)
76 {
77
78 $feedData = $feed['processedValues'];
79
80
81 if (!is_email($feedData['fieldEmailAddress'])) {
82 $feedData['fieldEmailAddress'] = ArrayHelper::get($formData, $feedData['fieldEmailAddress']);
83 }
84
85 if (!is_email($feedData['fieldEmailAddress'])) {
86 return false;
87 }
88
89 $mergeFields = array_filter(ArrayHelper::get($feedData, 'merge_fields'));
90 $status = ArrayHelper::isTrue($feedData, 'doubleOptIn') ? 'pending' : 'subscribed';
91
92 $listId = $feedData['list_id'];
93
94 $arguments = [
95 'email_address' => $feedData['fieldEmailAddress'],
96 'status_if_new' => $status,
97 'double_optin' => ArrayHelper::isTrue($feedData, 'doubleOptIn'),
98 'vip' => ArrayHelper::isTrue($feedData, 'markAsVIP'),
99 ];
100
101 if ($mergeFields) {
102 $arguments['merge_fields'] = (object)$mergeFields;
103 }
104
105 if ($entry->ip) {
106 $arguments['ip_signup'] = $entry->ip;
107 }
108
109 if ($feedData['tags']) {
110 $providedTags = explode(',', $feedData['tags']);
111 $arguments['tags'] = array_map('trim', $providedTags);
112 }
113
114 $note = '';
115 if ($feedData['note']) {
116 $note = esc_attr($feedData['note']);
117 }
118
119 $arguments['interests'] = [];
120
121 $contactHash = md5(strtolower($arguments['email_address']));
122
123 $existingMember = $this->getMemberByEmail($listId, $arguments['email_address']);
124
125 $isNew = true;
126 if (!empty($existingMember['id'])) {
127 $isNew = false;
128 // We have members so we can merge the values
129 if (apply_filters('fluentform_mailchimp_keep_existing_interests', true, $form->id)) {
130 $arguments['interests'] = ArrayHelper::get($existingMember, 'interests', []);
131 }
132
133 if ($arguments['tags']) {
134 if (apply_filters('fluentform_mailchimp_keep_existing_tags', true, $form->id)) {
135 $tags = ArrayHelper::get($existingMember, 'tags', []);
136 $tagNames = [];
137 foreach ($tags as $tag) {
138 $tagNames[] = $tag['name'];
139 }
140 $allTags = wp_parse_args($arguments['tags'], $tagNames);
141 $arguments['tags'] = array_unique($allTags);
142 }
143 }
144 }
145
146 if (
147 ArrayHelper::get($feedData, 'interest_group.sub_category') &&
148 ArrayHelper::get($feedData, 'interest_group.category')
149 ) {
150 $interestGroup = ArrayHelper::get($feedData, 'interest_group.sub_category');
151 $arguments['interests'][$interestGroup] = true;
152 }
153
154 $arguments = array_filter($arguments);
155 $settings = get_option('_fluentform_mailchimp_details');
156 $MailChimp = new MailChimp($settings['apiKey']);
157 $endPoint = 'lists/' . $listId . '/members/' . $contactHash;
158
159
160 $result = $MailChimp->put($endPoint, $arguments);
161
162 if ($result && !is_wp_error($result) && isset($result['id'])) {
163 $noteEnpoint = 'lists/' . $listId . '/members/' . $contactHash . '/notes';
164 if ($note) {
165 $MailChimp->post($noteEnpoint, [
166 'note' => $note
167 ]);
168 }
169
170 // Let's sync the tags
171 if (!$isNew && $arguments['tags']) {
172 $currentTags = [];
173 foreach ($result['tags'] as $tag) {
174 $currentTags[] = $tag['name'];
175 }
176 $newTags = $arguments['tags'];
177 sort($newTags);
178 sort($currentTags);
179
180 if ($newTags != $currentTags) {
181 $tagEnpoint = 'lists/' . $listId . '/members/' . $contactHash . '/tags';
182 if ($newTags) {
183 $formattedtags = [];
184 foreach ($newTags as $tag) {
185 $formattedtags[] = [
186 'name' => $tag,
187 'status' => 'active'
188 ];
189 }
190 $MailChimp->post($tagEnpoint, [
191 'tags' => $formattedtags
192 ]);
193 }
194 }
195 }
196
197 return true;
198 }
199
200 return $result;
201 }
202
203 /**
204 * Get a specific MailChimp list member.
205 *
206 */
207 public function getMemberByEmail($list_id, $email_address)
208 {
209 $settings = get_option('_fluentform_mailchimp_details');
210 $MailChimp = new MailChimp($settings['apiKey']);
211 // Prepare subscriber hash.
212 $subscriber_hash = md5(strtolower($email_address));
213 return $MailChimp->get('lists/' . $list_id . '/members/' . $subscriber_hash);
214 }
215 }
216