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
3.3.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 All 138 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.0, at includes/Core/Integration/ActiveCampaign/ActiveCampaignHandler.php

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