PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.10.0
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.10.0
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 2.10.0, at includes/Core/Integration/Groundhogg/RecordApiHelper.php

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