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

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

117 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 {
19 private $_defaultHeader;
20 private $_integrationID;
21 private $_logID;
22 private $_logResponse;
23 private $_entryID;
24
25 public function __construct($api_key, $integId, $logID, $entryID)
26 {
27 // wp_send_json_success($tokenDetails);
28 $this->_defaultHeader['Content-Type'] = 'application/json';
29 $this->_defaultHeader['api-key'] = $api_key;
30 $this->_integrationID = $integId;
31 $this->_logID = $logID;
32 $this->_logResponse = new UtilApiResponse();
33 $this->_entryID = $entryID;
34 }
35
36 public function insertRecord($data)
37 {
38 $insertRecordEndpoint = 'https://api.sendinblue.com/v3/contacts';
39 return HttpHelper::post($insertRecordEndpoint, $data, $this->_defaultHeader);
40 }
41
42 public function updateRecord($id, $data)
43 {
44 $updateRecordEndpoint = "https://api.sendinblue.com/v3/contacts/{$id}";
45 return HttpHelper::request($updateRecordEndpoint, 'PUT', $data, $this->_defaultHeader);
46 }
47
48 public function executeRecordApi($lists, $defaultDataConf, $fieldValues, $fieldMap, $actions)
49 {
50 $fieldData = [];
51 $attributes = [];
52 foreach ($fieldMap as $fieldPair) {
53 if (!empty($fieldPair->sendinBlueField)) {
54 if ('email' === $fieldPair->sendinBlueField) {
55 $fieldData['email'] = $fieldValues[$fieldPair->formField];
56 } elseif ('custom' === $fieldPair->formField && isset($fieldPair->customValue)) {
57 $attributes[$fieldPair->sendinBlueField] = $fieldPair->customValue;
58 } else {
59 $attributes[$fieldPair->sendinBlueField] = $fieldValues[$fieldPair->formField];
60 }
61 }
62 }
63 $arrLists = array_map(function ($val) {
64 return (int) $val;
65 }, $lists);
66
67 $fieldData['attributes'] = (object) $attributes;
68 $fieldData['listIds'] = $arrLists;
69 $model = new FormEntryLogModel();
70
71 $recordApiResponse = null;
72 $type = null;
73 if ($this->_entryID) {
74 $result = $model->entryLogCheck($this->_entryID, $this->_integrationID);
75 if (!count($result) || isset($result->errors['result_empty'])) {
76 $recordApiResponse = $this->insertRecord(wp_json_encode($fieldData));
77 $type = 'insert';
78
79 if (!empty($actions->update) && !empty($recordApiResponse->message) && 'Contact already exist' === $recordApiResponse->message) {
80 $contactEmail = $fieldData['email'];
81 $recordApiResponse = $this->updateRecord($contactEmail, wp_json_encode($fieldData));
82 if (empty($recordApiResponse)) {
83 $recordApiResponse = ['success' => true, 'id' => $fieldData['email']];
84 }
85 $type = 'update';
86 }
87 } else {
88 $contactId = json_decode($result[0]->response_obj);
89 $recordApiResponse = $this->updateRecord($contactId->id, wp_json_encode($fieldData));
90 if (empty($recordApiResponse)) {
91 $recordApiResponse = ['success' => true, 'id' => $contactId->id];
92 }
93 $type = 'update';
94 }
95 }
96
97 if ($recordApiResponse && isset($recordApiResponse->code)) {
98 $this->_logResponse->apiResponse(
99 $this->_logID,
100 $this->_integrationID,
101 ['type' => 'record', 'type_name' => $type],
102 'error',
103 $recordApiResponse
104 );
105 } else {
106 $this->_logResponse->apiResponse(
107 $this->_logID,
108 $this->_integrationID,
109 ['type' => 'record', 'type_name' => $type],
110 'success',
111 $recordApiResponse
112 );
113 }
114 return $recordApiResponse;
115 }
116 }
117