PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.1.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.1.1
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 / SendinBlue / SendinBlueHandler.php

SendinBlueHandler.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.1.1, at includes/Core/Integration/SendinBlue/SendinBlueHandler.php

315 lines 8.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ZohoSheet Integration
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\SendinBlue;
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 SendinBlue (Brevo) integration
21 */
22 class SendinBlueHandler
23 {
24 private $_integrationID;
25 private const BREVO_API_ENDPOINT = 'https://api.brevo.com/v3';
26
27 private $formId;
28
29 public function __construct($integrationID, $fromID)
30 {
31 $this->_integrationID = $integrationID;
32
33 $this->formId = $fromID;
34 }
35
36 /**
37 * Helps to register ajax function's with wp
38 *
39 * @return null
40 */
41 public static function registerAjax()
42 {
43 add_action('wp_ajax_bitforms_sblue_authorize', [__CLASS__, 'sendinBlueAuthorize']);
44 add_action('wp_ajax_bitforms_sblue_refresh_lists', [__CLASS__, 'refreshlists']);
45 add_action('wp_ajax_bitforms_sblue_headers', [__CLASS__, 'sendinblueHeaders']);
46 add_action('wp_ajax_bitforms_sblue_refresh_template', [__CLASS__, 'refreshTemplate']);
47 }
48
49 /**
50 * Process ajax request for generate_token
51 *
52 * @return JSON zoho crm api response and status
53 */
54 public static function sendinBlueAuthorize()
55 {
56 $authorizationHeader = null;
57 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
58 GlobalHelper::requirePostMethod();
59
60 try {
61 $requestsParams = GlobalHelper::formatRequestData();
62 } catch (\InvalidArgumentException $e) {
63 wp_send_json_error($e->getMessage(), 400);
64 }
65 if (
66 empty($requestsParams->api_key)
67 ) {
68 wp_send_json_error(
69 __(
70 'Requested parameter is empty',
71 'bit-form'
72 ),
73 400
74 );
75 }
76
77 $apiEndpoint = self::BREVO_API_ENDPOINT . '/account';
78 $authorizationHeader['Accept'] = 'application/json';
79 $authorizationHeader['api-key'] = $requestsParams->api_key;
80 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
81
82 if (is_wp_error($apiResponse) || (!empty($apiResponse->code) && 'unauthorized' === $apiResponse->code)) {
83 wp_send_json_error(
84 empty($apiResponse->code) ? 'Unknown' : $apiResponse->message,
85 400
86 );
87 }
88
89 wp_send_json_success(true);
90 } else {
91 wp_send_json_error(
92 __(
93 'Token expired',
94 'bit-form'
95 ),
96 401
97 );
98 }
99 }
100
101 /**
102 * Process ajax request for refresh crm modules
103 *
104 * @return JSON crm module data
105 */
106 public static function refreshlists()
107 {
108 $authorizationHeader = null;
109 $response = null;
110 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
111 GlobalHelper::requirePostMethod();
112
113 try {
114 $requestsParams = GlobalHelper::formatRequestData();
115 } catch (\InvalidArgumentException $e) {
116 wp_send_json_error($e->getMessage(), 400);
117 }
118
119 if (
120 empty($requestsParams->api_key)
121 ) {
122 wp_send_json_error(
123 __(
124 'Requested parameter is empty',
125 'bit-form'
126 ),
127 400
128 );
129 }
130 $apiEndpoint = self::BREVO_API_ENDPOINT . '/contacts/lists?limit=50&offset=0&sort=desc';
131 $authorizationHeader['Accept'] = 'application/json';
132 $authorizationHeader['api-key'] = $requestsParams->api_key;
133 $sblueResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
134
135 $allList = [];
136 if (!is_wp_error($sblueResponse) && empty($sblueResponse->code)) {
137 $sblueList = $sblueResponse->lists;
138
139 foreach ($sblueList as $list) {
140 $allList[$list->name] = (object) [
141 'id' => $list->id,
142 'name' => $list->name
143 ];
144 }
145 uksort($allList, 'strnatcasecmp');
146
147 $response['sblueList'] = $allList;
148 } else {
149 wp_send_json_error(
150 $sblueResponse->message,
151 400
152 );
153 }
154 wp_send_json_success($response, 200);
155 } else {
156 wp_send_json_error(
157 __(
158 'Token expired',
159 'bit-form'
160 ),
161 401
162 );
163 }
164 }
165
166 public static function refreshTemplate()
167 {
168 $authorizationHeader = null;
169 $response = null;
170 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
171 GlobalHelper::requirePostMethod();
172
173 try {
174 $requestsParams = GlobalHelper::formatRequestData();
175 } catch (\InvalidArgumentException $e) {
176 wp_send_json_error($e->getMessage(), 400);
177 }
178 if (
179 empty($requestsParams->api_key)
180 ) {
181 wp_send_json_error(
182 __(
183 'Requested parameter is empty',
184 'bit-form'
185 ),
186 400
187 );
188 }
189 $apiEndpoint = self::BREVO_API_ENDPOINT . '/smtp/templates';
190 $authorizationHeader['Accept'] = 'application/json';
191 $authorizationHeader['api-key'] = $requestsParams->api_key;
192 $sblueResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
193
194 $allList = [];
195 if (!is_wp_error($sblueResponse) && $sblueResponse->templates) {
196 $sblueTemplates = $sblueResponse->templates;
197
198 foreach ($sblueTemplates as $list) {
199 $allList[$list->name] = (object) [
200 'id' => $list->id,
201 'name' => ucfirst($list->name)
202 ];
203 }
204
205 uksort($allList, 'strnatcasecmp');
206
207 $response['sblueTemplates'] = $allList;
208 } else {
209 wp_send_json_error(
210 $sblueResponse->message,
211 400
212 );
213 }
214 wp_send_json_success($response, 200);
215 } else {
216 wp_send_json_error(
217 __(
218 'Token expired',
219 'bit-form'
220 ),
221 401
222 );
223 }
224 }
225
226 public static function sendinblueHeaders()
227 {
228 $authorizationHeader = null;
229 $response = null;
230 if (isset($_REQUEST['_ajax_nonce']) && wp_verify_nonce(sanitize_text_field(wp_unslash($_REQUEST['_ajax_nonce'])), 'bitforms_save')) {
231 GlobalHelper::requirePostMethod();
232
233 try {
234 $queryParams = GlobalHelper::formatRequestData();
235 } catch (\InvalidArgumentException $e) {
236 wp_send_json_error($e->getMessage(), 400);
237 }
238
239 if (
240 empty($queryParams->api_key)
241 ) {
242 wp_send_json_error(
243 __(
244 'Requested parameter is empty',
245 'bit-form'
246 ),
247 400
248 );
249 }
250 $apiEndpoint = self::BREVO_API_ENDPOINT . '/contacts/attributes';
251 $authorizationHeader['Accept'] = 'application/json';
252 $authorizationHeader['api-key'] = $queryParams->api_key;
253 $sblueResponse = HttpHelper::get($apiEndpoint, null, $authorizationHeader);
254 $fields = [];
255 if (!is_wp_error($sblueResponse)) {
256 $allFields = $sblueResponse->attributes;
257 // wp_send_json_success($allFields);
258 foreach ($allFields as $field) {
259 if (!empty($field->type) && 'float' !== $field->type) {
260 $fields[$field->name] = (object) [
261 'fieldId' => $field->name,
262 'fieldName' => $field->name
263 ];
264 }
265 }
266 $fields['Email'] = (object) ['fieldId' => 'email', 'fieldName' => 'Email', 'required' => true];
267 $response['sendinBlueField'] = $fields;
268 wp_send_json_success($response);
269 }
270 } else {
271 wp_send_json_error(
272 __(
273 'Token expired',
274 'bit-form'
275 ),
276 401
277 );
278 }
279 }
280
281 public function execute(IntegrationHandler $integrationHandler, $integrationData, $fieldValues, $entryID, $logID)
282 {
283 $integrationDetails = is_string($integrationData->integration_details) ? json_decode($integrationData->integration_details) : $integrationData->integration_details;
284
285 $api_key = $integrationDetails->api_key;
286 $lists = $integrationDetails->lists;
287 $fieldMap = $integrationDetails->field_map;
288 $actions = $integrationDetails->actions;
289 $defaultDataConf = $integrationDetails->default;
290
291 if (
292 empty($api_key)
293 || empty($lists)
294 || empty($fieldMap)
295 || empty($defaultDataConf)
296 ) {
297 return new WP_Error('REQ_FIELD_EMPTY', __('module, fields are required for Sendinblue api', 'bit-form'));
298 }
299 $recordApiHelper = new RecordApiHelper($api_key, $this->_integrationID, $logID, $entryID);
300 $sendinBlueApiResponse = $recordApiHelper->executeRecordApi(
301 $lists,
302 $defaultDataConf,
303 $fieldValues,
304 $fieldMap,
305 $actions,
306 $this->formId
307 );
308
309 if (is_wp_error($sendinBlueApiResponse)) {
310 return $sendinBlueApiResponse;
311 }
312 return $sendinBlueApiResponse;
313 }
314 }
315