PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.0
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.0, at includes/Core/Integration/MailChimp/MailChimpHandler.php

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