PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.13
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.13
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 / ActiveCampaign / ActiveCampaignHandler.php

ActiveCampaignHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.21.13, at includes/Core/Integration/ActiveCampaign/ActiveCampaignHandler.php

325 lines 8.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Active Campaign Integration
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\ActiveCampaign;
9
10 if (!defined('ABSPATH')) {
11 exit;
12 }
13
14 use BitCode\BitForm\Core\Integration\IntegrationHandler;
15 use BitCode\BitForm\Core\Util\HttpHelper;
16 use BitCode\BitForm\GlobalHelper;
17 use WP_Error;
18
19 /**
20 * Provide functionality for ZohoCrm integration
21 */
22 class ActiveCampaignHandler
23 {
24 private $_formID;
25 private $_integrationID;
26
27 public function __construct($integrationID, $fromID)
28 {
29 $this->_formID = $fromID;
30 $this->_integrationID = $integrationID;
31 }
32
33 /**
34 * Helps to register ajax function's with wp
35 *
36 * @return null
37 */
38 public static function registerAjax()
39 {
40 add_action('wp_ajax_bitforms_aCampaign_authorize', [__CLASS__, 'activeCampaignAuthorize']);
41 add_action('wp_ajax_bitforms_aCampaign_headers', [__CLASS__, 'activeCampaignHeaders']);
42 add_action('wp_ajax_bitforms_aCampaign_lists', [__CLASS__, 'activeCampaignLists']);
43 add_action('wp_ajax_bitforms_aCampaign_tags', [__CLASS__, 'activeCampaignTags']);
44 }
45
46 public static function _apiEndpoint($api_url, $method)
47 {
48 return "{$api_url}/api/3/{$method}/";
49 }
50
51 /**
52 * Process ajax request
53 *
54 * @return string Active Campaign api response and status
55 */
56 public static function activeCampaignAuthorize()
57 {
58 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
59
60 GlobalHelper::requirePostMethod();
61
62 try {
63 $requestsParams = GlobalHelper::formatRequestData();
64 } catch (\InvalidArgumentException $e) {
65 wp_send_json_error($e->getMessage(), 400);
66 }
67
68 if (
69 empty($requestsParams->api_key)
70 || empty($requestsParams->api_url)
71 ) {
72 wp_send_json_error(
73 __(
74 'Requested parameter is empty',
75 'bit-form'
76 ),
77 400
78 );
79 }
80
81 $apiEndpoint = self::_apiEndpoint($requestsParams->api_url, 'accounts');
82 $authorizationHeader = [];
83 $authorizationHeader['Api-Token'] = $requestsParams->api_key;
84 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
85
86 if (is_wp_error($apiResponse) || empty($apiResponse)) {
87 wp_send_json_error(
88 empty($apiResponse) ? 'Unknown' : $apiResponse,
89 400
90 );
91 }
92
93 wp_send_json_success(true);
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 lists
107 *
108 * @return JSON active campaign list data
109 */
110 public static function activeCampaignLists()
111 {
112 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
113
114
115 GlobalHelper::requirePostMethod();
116 try {
117 $queryParams = GlobalHelper::formatRequestData();
118 } catch (\InvalidArgumentException $e) {
119 wp_send_json_error($e->getMessage(), 400);
120 }
121 if (
122 empty($queryParams->api_key)
123 || empty($queryParams->api_url)
124 ) {
125 wp_send_json_error(
126 __(
127 'Requested parameter is empty',
128 'bit-form'
129 ),
130 400
131 );
132 }
133
134 $apiEndpoint = self::_apiEndpoint($queryParams->api_url, 'lists');
135 $authorizationHeader = [];
136 $authorizationHeader['Api-Token'] = $queryParams->api_key;
137 $aCampaignResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
138
139 $lists = [];
140 if (!is_wp_error($aCampaignResponse)) {
141 $allLists = $aCampaignResponse->lists;
142
143 foreach ($allLists as $list) {
144 $lists[$list->name] = (object) [
145 'listId' => $list->id,
146 'listName' => $list->name,
147 ];
148 }
149 $response = [];
150 $response['activeCampaignLists'] = $lists;
151 wp_send_json_success($response);
152 }
153 } else {
154 wp_send_json_error(
155 __(
156 'Token expired',
157 'bit-form'
158 ),
159 401
160 );
161 }
162 }
163
164 /**
165 * Process ajax request for refresh Tags
166 *
167 * @return JSON active campaign tags data
168 */
169 public static function activeCampaignTags()
170 {
171 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
172
173
174 GlobalHelper::requirePostMethod();
175
176 try {
177 $queryParams = GlobalHelper::formatRequestData();
178 } catch (\InvalidArgumentException $e) {
179 wp_send_json_error($e->getMessage(), 400);
180 }
181
182 if (
183 empty($queryParams->api_key)
184 || empty($queryParams->api_url)
185 ) {
186 wp_send_json_error(
187 __(
188 'Requested parameter is empty',
189 'bit-form'
190 ),
191 400
192 );
193 }
194
195 $apiEndpoint = self::_apiEndpoint($queryParams->api_url, 'tags?limit=500');
196 $authorizationHeader = [];
197 $authorizationHeader['Api-Token'] = $queryParams->api_key;
198 $aCampaignResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
199
200 $tags = [];
201 if (!is_wp_error($aCampaignResponse)) {
202 $allTags = $aCampaignResponse->tags;
203
204 foreach ($allTags as $tag) {
205 $tags[$tag->tag] = (object) [
206 'tagId' => $tag->id,
207 'tagName' => $tag->tag,
208 ];
209 }
210 $response = [];
211 $response['activeCampaignTags'] = $tags;
212 wp_send_json_success($response);
213 }
214 } else {
215 wp_send_json_error(
216 __(
217 'Token expired',
218 'bit-form'
219 ),
220 401
221 );
222 }
223 }
224
225 /**
226 * Process ajax request for refresh crm modules
227 *
228 * @return JSON crm module data
229 */
230 public static function activeCampaignHeaders()
231 {
232 $authorizationHeader = null;
233 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
234
235
236 GlobalHelper::requirePostMethod();
237
238 try {
239 $queryParams = GlobalHelper::formatRequestData();
240 } catch (\InvalidArgumentException $e) {
241 wp_send_json_error($e->getMessage(), 400);
242 }
243
244 if (
245 empty($queryParams->api_key)
246 || empty($queryParams->api_url)
247 ) {
248 wp_send_json_error(
249 __(
250 'Requested parameter is empty',
251 'bit-form'
252 ),
253 400
254 );
255 }
256
257 // $apiEndpoint = "{$queryParams->api_url}/api/3/fields";
258 $apiEndpoint = self::_apiEndpoint($queryParams->api_url, 'fields');
259 $$authorizationHeader = [];
260 $authorizationHeader['Api-Token'] = $queryParams->api_key;
261 $aCampaignResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
262
263 $fields = [];
264 if (!is_wp_error($aCampaignResponse)) {
265 $allFields = $aCampaignResponse->fields;
266 foreach ($allFields as $field) {
267 $fields[$field->title] = (object) [
268 'fieldId' => $field->id,
269 'fieldName' => $field->title,
270 'required' => '0' === $field->isrequired ? false : true
271 ];
272 }
273 $fields['FirstName'] = (object) ['fieldId' => 'firstName', 'fieldName' => 'First Name', 'required' => false];
274 $fields['LastName'] = (object) ['fieldId' => 'lastName', 'fieldName' => 'Last Name', 'required' => false];
275 $fields['Email'] = (object) ['fieldId' => 'email', 'fieldName' => 'Email', 'required' => true];
276 $fields['Phone'] = (object) ['fieldId' => 'phone', 'fieldName' => 'Phone', 'required' => false];
277 $response = [];
278 $response['activeCampaignField'] = $fields;
279 wp_send_json_success($response);
280 }
281 } else {
282 wp_send_json_error(
283 __(
284 'Token expired',
285 'bit-form'
286 ),
287 401
288 );
289 }
290 }
291
292 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
293 {
294 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
295 $api_key = $integrationDetails->api_key;
296 $api_url = $integrationDetails->api_url;
297 $fieldMap = $integrationDetails->field_map;
298 $actions = $integrationDetails->actions;
299 $listId = $integrationDetails->listId;
300 $tags = $integrationDetails->tagIds;
301
302 if (
303 empty($api_key)
304 || empty($api_url)
305 || empty($fieldMap)
306 ) {
307 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Sendinblue api', 'bit-form'));
308 }
309 $recordApiHelper = new RecordApiHelper($api_key, $api_url, $this->_integrationID, $logID, $entryID);
310 $activeCampaignApiResponse = $recordApiHelper->executeRecordApi(
311 $fieldValues,
312 $fieldMap,
313 $actions,
314 $listId,
315 $tags,
316 $this->_formID
317 );
318
319 if (is_wp_error($activeCampaignApiResponse)) {
320 return $activeCampaignApiResponse;
321 }
322 return $activeCampaignApiResponse;
323 }
324 }
325