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 / SendinBlue / RecordApiHelper.php

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

112 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * ZohoRecruit Record Api
5 *
6 */
7
8 namespace BitCode\BitForm\Core\Integration\SendinBlue;
9
10 use BitCode\BitForm\Core\Database\FormEntryLogModel;
11 use BitCode\BitForm\Core\Util\ApiResponse as UtilApiResponse;
12 use BitCode\BitForm\Core\Util\HttpHelper;
13
14 /**
15 * Provide functionality for Record insert,upsert
16 */
17 class RecordApiHelper {
18 private $_defaultHeader;
19 private $_integrationID;
20 private $_logID;
21 private $_logResponse;
22 private $_entryID;
23
24 public function __construct($api_key, $integId, $logID, $entryID) {
25 // wp_send_json_success($tokenDetails);
26 $this->_defaultHeader['Content-Type'] = 'application/json';
27 $this->_defaultHeader['api-key'] = $api_key;
28 $this->_integrationID = $integId;
29 $this->_logID = $logID;
30 $this->_logResponse = new UtilApiResponse();
31 $this->_entryID = $entryID;
32 }
33
34 public function insertRecord($data) {
35 $insertRecordEndpoint = 'https://api.sendinblue.com/v3/contacts';
36 return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader);
37 }
38
39 public function updateRecord($id, $data) {
40 $updateRecordEndpoint = "https://api.sendinblue.com/v3/contacts/{$id}";
41 return HttpHelper::request($updateRecordEndpoint, 'PUT', $data, $this->_defaultHeader);
42 }
43
44 public function executeRecordApi($lists, $defaultDataConf, $fieldValues, $fieldMap, $actions) {
45 $fieldData = [];
46 $attributes = [];
47 foreach ($fieldMap as $fieldPair) {
48 if (!empty($fieldPair->sendinBlueField)) {
49 if ('email' === $fieldPair->sendinBlueField) {
50 $fieldData['email'] = $fieldValues[$fieldPair->formField];
51 } elseif ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
52 $attributes[$fieldPair->sendinBlueField] = $fieldPair->customValue;
53 } else {
54 $attributes[$fieldPair->sendinBlueField] = $fieldValues[$fieldPair->formField];
55 }
56 }
57 }
58 $arrLists = array_map(function ($val) {
59 return (int) $val;
60 }, $lists);
61
62 $fieldData['attributes'] = (object) $attributes;
63 $fieldData['listIds'] = $arrLists;
64 $model = new FormEntryLogModel();
65
66 $recordApiResponse = null;
67 $type = null;
68 if ($this->_entryID) {
69 $result = $model->entryLogCheck($this->_entryID, $this->_integrationID);
70 if (!count($result) || isset($result->errors['result_empty'])) {
71 $recordApiResponse = $this->insertRecord(wp_json_encode($fieldData));
72 $type = 'insert';
73
74 if (!empty($actions->update) && !empty($recordApiResponse->message) && 'Contact already exist' === $recordApiResponse->message) {
75 $contactEmail = $fieldData['email'];
76 $recordApiResponse = $this->updateRecord($contactEmail, wp_json_encode($fieldData));
77 if (empty($recordApiResponse)) {
78 $recordApiResponse = ['success' => true, 'id' => $fieldData['email']];
79 }
80 $type = 'update';
81 }
82 } else {
83 $contactId = json_decode($result[0]->response_obj);
84 $recordApiResponse = $this->updateRecord($contactId->id, wp_json_encode($fieldData));
85 if (empty($recordApiResponse)) {
86 $recordApiResponse = ['success' => true, 'id' => $contactId->id];
87 }
88 $type = 'update';
89 }
90 }
91
92 if ($recordApiResponse && isset($recordApiResponse->code)) {
93 $this->_logResponse->apiResponse(
94 $this->_logID,
95 $this->_integrationID,
96 ['type' => 'record', 'type_name' => $type],
97 'error',
98 $recordApiResponse
99 );
100 } else {
101 $this->_logResponse->apiResponse(
102 $this->_logID,
103 $this->_integrationID,
104 ['type' => 'record', 'type_name' => $type],
105 'success',
106 $recordApiResponse
107 );
108 }
109 return $recordApiResponse;
110 }
111 }
112