PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.9.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.9.1
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / Integration / MailChimp / MailChimpHandler.php

MailChimpHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.9.1, at includes/Core/Integration/MailChimp/MailChimpHandler.php

326 lines 9.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * MailChimp Integration
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\MailChimp;
9
10 use BitCode\BitForm\Core\Integration\IntegrationHandler;
11 use BitCode\BitForm\Core\Util\HttpHelper;
12 use WP_Error;
13
14 /**
15 * Provide functionality for MailChimp integration
16 */
17 class MailChimpHandler
18 {
19 private $_integrationID;
20
21 public function __construct($integrationID, $fromID)
22 {
23 $this->_integrationID = $integrationID;
24 }
25
26 /**
27 * MailChimp API Endpoint
28 */
29 public static function apiEndPoint($dc)
30 {
31 return "https://$dc.api.mailchimp.com/3.0";
32 }
33
34 /**
35 * Helps to register ajax function's with wp
36 *
37 * @return null
38 */
39 public static function registerAjax()
40 {
41 add_action('wp_ajax_bitforms_mChimp_generate_token', [__CLASS__, 'generateTokens']);
42 add_action('wp_ajax_bitforms_mChimp_refresh_audience', [__CLASS__, 'refreshAudienceAjaxHelper']);
43 add_action('wp_ajax_bitforms_mChimp_refresh_fields', [__CLASS__, 'refreshAudienceFields']);
44 add_action('wp_ajax_bitforms_mChimp_refresh_tags', [__CLASS__, 'refreshTagsAjaxHelper']);
45 }
46
47 /**
48 * Process ajax request for generate_token
49 *
50 * @return JSON zoho crm api response and status
51 */
52 public static function generateTokens()
53 {
54 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
55 $authorizationHeader = null;
56 $inputJSON = file_get_contents('php://input');
57 $requestsParams = json_decode($inputJSON);
58 if (
59 empty($requestsParams->clientId)
60 || empty($requestsParams->clientSecret)
61 || empty($requestsParams->redirectURI)
62 || empty($requestsParams->code)
63 ) {
64 wp_send_json_error(
65 __(
66 'Requested parameter is empty',
67 'bit-form'
68 ),
69 400
70 );
71 }
72
73 $apiEndpoint = 'https://login.mailchimp.com/oauth2/token';
74 $authorizationHeader['Content-Type'] = 'application/x-www-form-urlencoded';
75 $requestParams = [
76 'code' => $requestsParams->code,
77 'client_id' => $requestsParams->clientId,
78 'client_secret' => $requestsParams->clientSecret,
79 'redirect_uri' => $requestsParams->redirectURI,
80 'grant_type' => 'authorization_code'
81 ];
82 $apiResponse = HttpHelper::post($apiEndpoint, $requestParams, $authorizationHeader);
83
84 $metaDataEndPoint = 'https://login.mailchimp.com/oauth2/metadata';
85
86 $authorizationHeader['Authorization'] = "Bearer {$apiResponse->access_token}";
87 $metaData = HttpHelper::post($metaDataEndPoint, null, $authorizationHeader);
88
89 $apiResponse->dc = $metaData->dc;
90
91 if (is_wp_error($apiResponse) || !empty($apiResponse->error)) {
92 wp_send_json_error(
93 empty($apiResponse->error) ? 'Unknown' : $apiResponse->error,
94 400
95 );
96 }
97 $apiResponse->generates_on = \time();
98 wp_send_json_success($apiResponse, 200);
99 } else {
100 wp_send_json_error(
101 __(
102 'Token expired',
103 'bit-form'
104 ),
105 401
106 );
107 }
108 }
109
110 /**
111 * Process ajax request for refresh MailChimp Audience list
112 *
113 * @return JSON MailChimp data
114 */
115 public static function refreshAudienceAjaxHelper()
116 {
117 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
118 $authorizationHeader = null;
119 $inputJSON = file_get_contents('php://input');
120 $queryParams = json_decode($inputJSON);
121
122 if (
123 empty($queryParams->tokenDetails)
124 || empty($queryParams->clientId)
125 || empty($queryParams->clientSecret)
126 || empty($queryParams->tokenDetails->dc)
127 ) {
128 wp_send_json_error(
129 __(
130 'Requested parameter is empty',
131 'bit-form'
132 ),
133 400
134 );
135 }
136 $response = [];
137 $apiEndpoint = self::apiEndPoint($queryParams->tokenDetails->dc) . '/lists';
138
139 $authorizationHeader['Authorization'] = "Bearer {$queryParams->tokenDetails->access_token}";
140 $audienceResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
141
142 $allList = [];
143 if (!is_wp_error($audienceResponse) && empty($audienceResponse->response->error)) {
144 $audienceLists = $audienceResponse->lists;
145 // wp_send_json_success($audienceLists);
146 foreach ($audienceLists as $audienceList) {
147 $allList[$audienceList->name] = (object) [
148 'listId' => $audienceList->id,
149 'listName' => $audienceList->name
150 ];
151 }
152 uksort($allList, 'strnatcasecmp');
153
154 $response['audiencelist'] = $allList;
155 // wp_send_json_success($response, 200);
156 } else {
157 wp_send_json_error(
158 $audienceResponse->response->error->message,
159 400
160 );
161 }
162 wp_send_json_success($response, 200);
163 } else {
164 wp_send_json_error(
165 __(
166 'Token expired',
167 'bit-form'
168 ),
169 401
170 );
171 }
172 }
173
174 /**
175 * Process ajax request for refresh MailChimp Audince Fields
176 * @return JSON MailChimp Audience fields
177 */
178 public static function refreshAudienceFields()
179 {
180 $authorizationHeader = null;
181 $response = null;
182 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
183 $inputJSON = file_get_contents('php://input');
184 $queryParams = json_decode($inputJSON);
185 // wp_send_json_success($queryParams);
186 if (
187 empty($queryParams->tokenDetails)
188 || empty($queryParams->listId)
189 || empty($queryParams->tokenDetails->dc)
190 ) {
191 wp_send_json_error(
192 __(
193 'Requested parameter is empty',
194 'bit-form'
195 ),
196 400
197 );
198 }
199 $apiEndpoint = self::apiEndPoint($queryParams->tokenDetails->dc) . "/lists/$queryParams->listId/merge-fields";
200 $authorizationHeader['Authorization'] = "Bearer {$queryParams->tokenDetails->access_token}";
201 $mergeFieldResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
202 // wp_send_json_success($mergeFieldResponse);
203 $fields = [];
204 if (!is_wp_error($mergeFieldResponse)) {
205 $allFields = $mergeFieldResponse->merge_fields;
206 foreach ($allFields as $field) {
207 if ('Address' === $field->name) {
208 continue;
209 }
210 $fields[$field->name] = (object) [
211 'tag' => $field->tag,
212 'name' => $field->name
213 ];
214 }
215 $fields['Email'] = (object) ['tag' => 'email_address', 'name' => 'Email'];
216 $response['audienceField'] = $fields;
217 wp_send_json_success($response);
218 }
219 } else {
220 wp_send_json_error(
221 __(
222 'Token expired',
223 'bit-form'
224 ),
225 401
226 );
227 }
228 }
229
230 /**
231 * Process ajax request for refresh MailChimp Tags
232 * @return JSON MailChimp Tags
233 */
234 public static function refreshTagsAjaxHelper()
235 {
236 $authorizationHeader = null;
237 $response = null;
238 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce($_REQUEST['_ajax_nonce'], 'bitforms_save')) {
239 $inputJSON = file_get_contents('php://input');
240 $queryParams = json_decode($inputJSON);
241 // wp_send_json_success($queryParams);
242 if (
243 empty($queryParams->tokenDetails)
244 || empty($queryParams->listId)
245 || empty($queryParams->tokenDetails->dc)
246 ) {
247 wp_send_json_error(
248 __(
249 'Requested parameter is empty',
250 'bit-form'
251 ),
252 400
253 );
254 }
255 $apiEndpoint = self::apiEndPoint($queryParams->tokenDetails->dc) . "/lists/$queryParams->listId/segments?count=1000";
256 $authorizationHeader['Authorization'] = "Bearer {$queryParams->tokenDetails->access_token}";
257 $tagsList = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
258 // wp_send_json_success($tagsList->segments);
259 $allList = [];
260 foreach ($tagsList->segments as $tag) {
261 $allList[$tag->name] = (object) [
262 'tagId' => $tag->id,
263 'tagName' => $tag->name
264 ];
265 }
266 uksort($allList, 'strnatcasecmp');
267 $response['audienceTags'] = $allList;
268 wp_send_json_success($response);
269 } else {
270 wp_send_json_error(
271 __(
272 'Token expired',
273 'bit-form'
274 ),
275 401
276 );
277 }
278 }
279
280 /**
281 * Save updated access_token to avoid unnecessary token generation
282 *
283 * @param Integer $fromID ID of Integration related form
284 * @param Integer $integrationID ID of Mail Chimp Integration
285 * @param Obeject $tokenDetails refreshed token info
286 *
287 * @return null
288 */
289 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
290 {
291 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
292
293 $tokenDetails = $integrationDetails->tokenDetails;
294 $listId = $integrationDetails->listId;
295 $tags = $integrationDetails->tags;
296 $fieldMap = $integrationDetails->field_map;
297 $actions = $integrationDetails->actions;
298 $defaultDataConf = $integrationDetails->default;
299 $addressFields = $integrationDetails->address_field;
300
301 if (
302 empty($tokenDetails)
303 || empty($listId)
304 || empty($fieldMap)
305 || empty($defaultDataConf)
306 ) {
307 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Mail Chimp api', 'bit-form'));
308 }
309 $recordApiHelper = new RecordApiHelper($tokenDetails, $this->_integrationID, $logID, $entryID);
310 $mChimpApiResponse = $recordApiHelper->executeRecordApi(
311 $listId,
312 $tags,
313 $defaultDataConf,
314 $fieldValues,
315 $fieldMap,
316 $actions,
317 $addressFields
318 );
319
320 if (is_wp_error($mChimpApiResponse)) {
321 return $mChimpApiResponse;
322 }
323 return $mChimpApiResponse;
324 }
325 }
326