PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.65
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.65
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.65, at app/Services/Integrations/MailChimp/MailChimpSubscriber.php

221 lines 6.7 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 if (!is_email($feedData['fieldEmailAddress'])) {
81 $feedData['fieldEmailAddress'] = ArrayHelper::get($formData, $feedData['fieldEmailAddress']);
82 }
83
84 if (!is_email($feedData['fieldEmailAddress'])) {
85 return false;
86 }
87
88 $mergeFields = array_filter(ArrayHelper::get($feedData, 'merge_fields'));
89 $status = ArrayHelper::isTrue($feedData, 'doubleOptIn') ? 'pending' : 'subscribed';
90
91 $listId = $feedData['list_id'];
92
93 $arguments = [
94 'email_address' => $feedData['fieldEmailAddress'],
95 'status_if_new' => $status,
96 'double_optin' => ArrayHelper::isTrue($feedData, 'doubleOptIn'),
97 'vip' => ArrayHelper::isTrue($feedData, 'markAsVIP'),
98 ];
99
100 if ($mergeFields) {
101 $arguments['merge_fields'] = (object)$mergeFields;
102 }
103
104 if ($entry->ip) {
105 $arguments['ip_signup'] = $entry->ip;
106 }
107
108 $tags = $this->getSelectedTagIds($feedData, $formData, 'tags');
109 if(!is_array($tags)) {
110 $tags = explode(',', $tags);
111 }
112
113 $tags = array_map('trim', $tags);
114 $tags = array_filter($tags);
115
116 if ($tags) {
117 $arguments['tags'] = $tags;
118 }
119
120 $note = '';
121 if ($feedData['note']) {
122 $note = esc_attr($feedData['note']);
123 }
124
125 $arguments['interests'] = [];
126
127 $contactHash = md5(strtolower($arguments['email_address']));
128
129 $existingMember = $this->getMemberByEmail($listId, $arguments['email_address']);
130
131 $isNew = true;
132 if (!empty($existingMember['id'])) {
133 $isNew = false;
134 // We have members so we can merge the values
135 if (apply_filters('fluentform_mailchimp_keep_existing_interests', true, $form->id)) {
136 $arguments['interests'] = ArrayHelper::get($existingMember, 'interests', []);
137 }
138
139 if ($arguments['tags']) {
140 if (apply_filters('fluentform_mailchimp_keep_existing_tags', true, $form->id)) {
141 $tags = ArrayHelper::get($existingMember, 'tags', []);
142 $tagNames = [];
143 foreach ($tags as $tag) {
144 $tagNames[] = $tag['name'];
145 }
146 $allTags = wp_parse_args($arguments['tags'], $tagNames);
147 $arguments['tags'] = array_unique($allTags);
148 }
149 }
150 }
151
152 if (
153 ArrayHelper::get($feedData, 'interest_group.sub_category') &&
154 ArrayHelper::get($feedData, 'interest_group.category')
155 ) {
156 $interestGroup = ArrayHelper::get($feedData, 'interest_group.sub_category');
157 $arguments['interests'][$interestGroup] = true;
158 }
159
160 $arguments = array_filter($arguments);
161 $settings = get_option('_fluentform_mailchimp_details');
162 $MailChimp = new MailChimp($settings['apiKey']);
163 $endPoint = 'lists/' . $listId . '/members/' . $contactHash;
164
165
166 $result = $MailChimp->put($endPoint, $arguments);
167
168 if ($result && !is_wp_error($result) && isset($result['id'])) {
169 $noteEnpoint = 'lists/' . $listId . '/members/' . $contactHash . '/notes';
170 if ($note) {
171 $MailChimp->post($noteEnpoint, [
172 'note' => $note
173 ]);
174 }
175
176 // Let's sync the tags
177 if (!$isNew && $arguments['tags']) {
178 $currentTags = [];
179 foreach ($result['tags'] as $tag) {
180 $currentTags[] = $tag['name'];
181 }
182 $newTags = $arguments['tags'];
183 sort($newTags);
184 sort($currentTags);
185
186 if ($newTags != $currentTags) {
187 $tagEnpoint = 'lists/' . $listId . '/members/' . $contactHash . '/tags';
188 if ($newTags) {
189 $formattedtags = [];
190 foreach ($newTags as $tag) {
191 $formattedtags[] = [
192 'name' => $tag,
193 'status' => 'active'
194 ];
195 }
196 $MailChimp->post($tagEnpoint, [
197 'tags' => $formattedtags
198 ]);
199 }
200 }
201 }
202 return true;
203 }
204
205 return $result;
206 }
207
208 /**
209 * Get a specific MailChimp list member.
210 *
211 */
212 public function getMemberByEmail($list_id, $email_address)
213 {
214 $settings = get_option('_fluentform_mailchimp_details');
215 $MailChimp = new MailChimp($settings['apiKey']);
216 // Prepare subscriber hash.
217 $subscriber_hash = md5(strtolower($email_address));
218 return $MailChimp->get('lists/' . $list_id . '/members/' . $subscriber_hash);
219 }
220 }
221