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

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

249 lines 7.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Groundhogg Record Api
5 */
6
7 namespace BitCode\BitForm\Core\Integration\Groundhogg;
8
9 use BitCode\BitForm\Core\Util\ApiResponse;
10 use BitCode\BitForm\Core\Util\HttpHelper;
11
12 /**
13 * Provide functionality for Record insert, upsert
14 */
15 class RecordApiHelper
16 {
17 private $integrationID;
18 private $logID;
19
20 private $_logResponse;
21
22 private $entryID;
23
24 public function __construct($integId, $logID, $entryID)
25 {
26 $this->integrationID = $integId;
27 $this->_logResponse = new ApiResponse();
28 $this->logID = $logID;
29 $this->entryID = $entryID;
30 }
31
32 public function generateReqDataFromFieldMap($data, $fieldMap)
33 {
34 $dataFinal = [];
35 foreach ($fieldMap as $key => $value) {
36 $triggerValue = $value->formField;
37 $actionValue = $value->GroundhoggMapField;
38 if ('custom' === $triggerValue) {
39 $dataFinal[$actionValue] = $value->customValue;
40 } elseif (!is_null($data[$triggerValue])) {
41 $dataFinal[$actionValue] = $data[$triggerValue];
42 }
43 }
44 return $dataFinal;
45 }
46
47 public function generateMetaDataFromFieldMap($data, $fieldMap)
48 {
49 $dataFinal = [];
50 foreach ($fieldMap as $value) {
51 $triggerValue = $value->formField;
52 $actionValue = $value->GroundhoggMetaMapField;
53
54 if ('custom' === $triggerValue) {
55 $triggerValue = $value->customMetaFormValue;
56 }
57
58 if ('custom' === $actionValue) {
59 $actionValue = $value->customMetaGroundValue;
60 }
61
62 if (!is_null($data[$triggerValue])) {
63 $dataFinal[$actionValue] = $data[$triggerValue];
64 } else {
65 $dataFinal[$actionValue] = $triggerValue;
66 }
67 }
68 return $dataFinal;
69 }
70
71 public static function createContact($finalData, $finalReorganizedTags, $integrationDetails)
72 {
73 if (empty($integrationDetails->token) || empty($integrationDetails->public_key) || empty($integrationDetails->domainName)) {
74 wp_send_json_error(
75 __(
76 'Request parameter is empty',
77 'bit-form'
78 ),
79 400
80 );
81 }
82
83 $authorizationHeader = [
84 'gh-token' => $integrationDetails->token,
85 'gh-public-key' => $integrationDetails->public_key
86 ];
87
88 $apiEndpoint = $integrationDetails->domainName . '/wp-json/gh/v3/contacts';
89 return HttpHelper::post($apiEndpoint, $finalData, $authorizationHeader);
90 }
91
92 public static function createTag($diffTags, $integrationDetails)
93 {
94 if (empty($integrationDetails->token) || empty($integrationDetails->public_key) || empty($integrationDetails->domainName)) {
95 wp_send_json_error(
96 __(
97 'Request parameter is empty',
98 'bit-form'
99 ),
100 400
101 );
102 }
103
104 $authorizationHeader = [
105 'gh-token' => $integrationDetails->token,
106 'gh-public-key' => $integrationDetails->public_key
107 ];
108
109 $apiEndpoint = $integrationDetails->domainName . '/wp-json/gh/v3/tags';
110 return HttpHelper::post($apiEndpoint, $diffTags, $authorizationHeader);
111 }
112
113 public static function checkExitsTagsOrCreate($integrationDetails, $finalReorganizedTags = [])
114 {
115 $authorizationParams = [
116 'gh-token' => $integrationDetails->token,
117 'gh-public-key' => $integrationDetails->public_key
118 ];
119 $exitsTags = [];
120
121 $apiEndpoint = $integrationDetails->domainName . '/wp-json/gh/v3/tags';
122 $apiResponse = HttpHelper::get($apiEndpoint, null, $authorizationParams);
123 if ('success' === $apiResponse->status) {
124 $tags = $apiResponse->tags;
125 foreach ($tags as $tag) {
126 array_push($exitsTags, $tag->tag_name);
127 }
128 } else {
129 return null;
130 };
131 $diffTags['tags'] = array_diff($finalReorganizedTags, $exitsTags);
132 if ($diffTags) {
133 self::createTag($diffTags, $integrationDetails);
134 }
135 }
136
137 public static function addTagsToExitsUser($addTagsToUser, $integrationDetails, $addTagToEmail)
138 {
139 $authorizationParams = [
140 'gh-token' => $integrationDetails->token,
141 'gh-public-key' => $integrationDetails->public_key
142 ];
143 $prePraperData = [
144 'id_or_email' => $addTagToEmail,
145 'tags' => $addTagsToUser,
146 ];
147 $apiEndpoint = $integrationDetails->domainName . '/wp-json/gh/v3/contacts/apply_tags';
148 return HttpHelper::request($apiEndpoint, 'PUT', $prePraperData, $authorizationParams);
149 }
150
151 public function execute(
152 $mainAction,
153 $defaultDataConf,
154 $fieldValues,
155 $fieldMap,
156 $public_key,
157 $token,
158 $actions,
159 $integrationDetails,
160 $formId
161 ) {
162 $mainAction = $integrationDetails->mainAction;
163 $fieldData = [];
164 $finalData = $this->generateReqDataFromFieldMap($fieldValues, $fieldMap);
165 // 1 = create contact with tag
166 $apiResponse = null;
167 $finalReorganizedTags = null;
168 if ('1' === $mainAction) {
169 if ($integrationDetails->showMeta) {
170 $fieldMapMeta = $integrationDetails->field_map_meta;
171 $metaData = $this->generateMetaDataFromFieldMap($fieldValues, $fieldMapMeta);
172 $finalData['meta'] = $metaData;
173 }
174 if (!empty($actions->tags)) {
175 $finalReorganizedTags = [];
176 $tags = explode(',', $actions->tags);
177 foreach ($tags as $tag) {
178 if (isset($fieldValues[$tag])) {
179 $finalReorganizedTags[] = $fieldValues[$tag];
180 } else {
181 $sanitize = ltrim($tag, 'ground-');
182 $finalReorganizedTags[] = $sanitize;
183 }
184 };
185 $finalData['tags'] = $finalReorganizedTags;
186 }
187 $this->checkExitsTagsOrCreate($integrationDetails, $finalReorganizedTags);
188 $apiResponse = $this->createContact($finalData, $finalReorganizedTags, $integrationDetails);
189 }
190 // 2 = add tag to contact
191 $apiResponseError = null;
192 $apiResponseSuccess = null;
193 if ('2' === $mainAction) {
194 $addTagsToUser = [];
195 $addTagToEmails = [];
196 $allSelectedEmails = explode(',', $integrationDetails->emailAddress);
197 foreach ($allSelectedEmails as $emailAddress) {
198 // $addTagToEmails[] = $fieldValues[$emailAddress];
199 array_push($addTagToEmails, $fieldValues[$emailAddress]);
200 }
201
202 if ($integrationDetails->addTagToUser) {
203 $tags = explode(',', $integrationDetails->addTagToUser);
204 foreach ($tags as $tag) {
205 if (isset($fieldValues[$tag])) {
206 $addTagsToUser[] = $fieldValues[$tag];
207 } else {
208 $sanitize = ltrim($tag, 'ground-');
209 $addTagsToUser[] = $sanitize;
210 }
211 };
212 $finalData['tags'] = $addTagsToUser;
213 }
214
215 $this->checkExitsTagsOrCreate($integrationDetails, $addTagsToUser);
216 foreach ($addTagToEmails as $addTagToEmail) {
217 $apiResponse = $this->addTagsToExitsUser($addTagsToUser, $integrationDetails, $addTagToEmail);
218 if (property_exists($apiResponse, 'code')) {
219 $apiResponseError[$addTagToEmail] = $apiResponse;
220 } else {
221 $apiResponseSuccess[$addTagToEmail] = $apiResponse;
222 }
223 }
224 }
225
226 $entryDetails = [
227 'formId' => $formId,
228 'entryId' => $this->entryID,
229 'fieldValues' => $fieldValues
230 ];
231 if ('1' === $mainAction) {
232 if (property_exists($apiResponse, 'errors')) {
233 $this->_logResponse->apiResponse($this->logID, $this->integrationID, ['type' => 'record', 'type_name' => 'Add Contact'], 'errors', $apiResponse, $entryDetails);
234 } else {
235 $this->_logResponse->apiResponse($this->logID, $this->integrationID, ['type' => 'record', 'type_name' => 'Add Contact'], 'success', $apiResponse, $entryDetails);
236 }
237 }
238 if ('2' === $mainAction) {
239 if (!empty($apiResponseError)) {
240 $this->_logResponse->apiResponse($this->logID, $this->integrationID, ['type' => 'record', 'type_name' => 'Add Tags'], 'error', $apiResponse, $entryDetails);
241 }
242 if (!empty($apiResponseSuccess)) {
243 $this->_logResponse->apiResponse($this->logID, $this->integrationID, ['type' => 'record', 'type_name' => 'Add Tags'], 'success', $apiResponse, $entryDetails);
244 }
245 }
246 return $apiResponse;
247 }
248 }
249