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 / ActiveCampaign / ActiveCampaignHandler.php

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

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