PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / -3.0.1
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v-3.0.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 / RecordApiHelper.php

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

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