PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.17.6
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.17.6
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
← All changes | includes/Core/Integration/IntegrationHandler.php +440 -245 1.42.17.6 View file →
@@ -1,291 +1,486 @@
1 1 <?php
2 2
3 3 namespace BitCode\BitForm\Core\Integration;
4 4
5 +use BitCode\BitForm\Admin\Form\AdminFormManager;
6 +use BitCode\BitForm\Admin\Form\Helpers;
7 +use BitCode\BitForm\Core\Database\FormEntryLogModel;
8 +use BitCode\BitForm\Core\Database\FormEntryModel;
9 +use BitCode\BitForm\Core\Database\IntegrationModel;
10 +use BitCode\BitForm\Core\Util\FieldValueHandler;
5 11 use BitCode\BitForm\Core\Util\MailNotifier;
6 -use BitCode\BitForm\Core\Database\IntegrationModel;
7 -use BitCode\BitForm\Core\Database\FormEntryLogModel;
12 +use BitCode\BitForm\Frontend\Form\FrontendFormManager;
8 13
9 -
10 14 final class IntegrationHandler
11 15 {
12 - private static $_formID;
13 - private static $_integrationModel;
14 - private $_user_details;
16 + private static $_formID;
17 + private static $_integrationModel;
18 + private $_user_details;
15 19
16 - /**
17 - * Constructor of Integration Handler
18 - *
19 - * @param Integer $formID If Integration is accessible globally then
20 - * $formID will be 0 and category app
21 - * @param Array $user_details Details of user accessing data
22 - * @return void
23 - */
24 - public function __construct($formID, $user_details = null)
25 - {
26 - static::$_formID = $formID;
27 - static::$_integrationModel = new IntegrationModel();
28 - $this->_user_details = $user_details;
20 + /**
21 + * Constructor of Integration Handler
22 + *
23 + * @param Integer $formID If Integration is accessible globally then
24 + * $formID will be 0 and category app
25 + * @param Array $user_details Details of user accessing data
26 + * @return void
27 + */
28 + public function __construct($formID, $user_details = null)
29 + {
30 + static::$_formID = $formID;
31 + static::$_integrationModel = new IntegrationModel();
32 + $this->_user_details = $user_details;
33 + }
34 +
35 + public function getAIntegration($integrationID, $integrationCategory = null, $integrationType = null)
36 + {
37 + $conditions = [
38 + 'form_id' => static::$_formID,
39 + 'id' => $integrationID,
40 + ];
41 + if (!is_null($integrationType)) {
42 + $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
29 43 }
44 + if (!is_null($integrationCategory)) {
45 + $conditions = array_merge($conditions, ['category' => $integrationCategory]);
46 + }
47 + return static::$_integrationModel->get(
48 + [
49 + 'id',
50 + 'integration_name',
51 + 'integration_type',
52 + 'integration_details',
53 + 'form_id',
54 + ],
55 + $conditions
56 + );
57 + }
30 58
59 + public function getAllIntegration($integrationCategory = null, $integrationType = null, $status = null, $id = null)
60 + {
61 + $conditions = [
62 + 'form_id' => static::$_formID,
63 + ];
64 + if (!is_null($integrationType)) {
65 + $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
66 + }
31 67
32 - public function getAIntegration($integrationID, $integrationCategory = null, $integrationType = null)
33 - {
34 - $conditions = array(
35 - 'form_id' => static::$_formID,
36 - 'id' => $integrationID,
37 - );
38 - if (!is_null($integrationType)) {
39 - $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
40 - }
41 - if (!is_null($integrationCategory)) {
42 - $conditions = array_merge($conditions, ['category' => $integrationCategory]);
43 - }
44 - return static::$_integrationModel->get(
45 - array(
46 - 'id',
47 - 'integration_name',
48 - 'integration_type',
49 - 'integration_details',
50 - 'form_id'
51 - ),
52 - $conditions
53 - );
68 + if (!is_null($integrationCategory)) {
69 + $conditions = array_merge($conditions, ['category' => $integrationCategory]);
54 70 }
71 + if (!is_null($status)) {
72 + $conditions = array_merge($conditions, ['status' => 1]);
73 + }
74 + if (!is_null($id)) {
75 + $conditions = array_merge($conditions, ['id' => $id]);
76 + }
77 + return static::$_integrationModel->get(
78 + [
79 + 'id',
80 + 'form_id',
81 + 'integration_name',
82 + 'integration_type',
83 + 'integration_details',
84 + 'status',
85 + ],
86 + $conditions
87 + );
88 + }
55 89
56 - public function getAllIntegration($integrationCategory = null, $integrationType = null, $status = null)
57 - {
58 - $conditions = array(
59 - 'form_id' => static::$_formID
60 - );
61 - if (!is_null($integrationType)) {
62 - $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
63 - }
90 + public function getIntegrationWithoutFormId($integrationCategory = null, $integrationType = null, $status = null, $id = null)
91 + {
92 + $conditions = [];
93 + if (!is_null($integrationType)) {
94 + $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
95 + }
64 96
65 - if (!is_null($integrationCategory)) {
66 - $conditions = array_merge($conditions, ['category' => $integrationCategory]);
67 - }
68 - if (!is_null($status)) {
69 - $conditions = array_merge($conditions, ['status' => 1]);
70 - }
71 - return static::$_integrationModel->get(
72 - array(
73 - 'id',
74 - 'form_id',
75 - 'integration_name',
76 - 'integration_type',
77 - 'integration_details',
78 - 'status'
79 - ),
80 - $conditions
81 - );
97 + if (!is_null($integrationCategory)) {
98 + $conditions = array_merge($conditions, ['category' => $integrationCategory]);
82 99 }
100 + if (!is_null($status)) {
101 + $conditions = array_merge($conditions, ['status' => 1]);
102 + }
103 + if (!is_null($id)) {
104 + $conditions = array_merge($conditions, ['id' => $id]);
105 + }
106 + return static::$_integrationModel->get(
107 + [
108 + 'id',
109 + 'form_id',
110 + 'integration_name',
111 + 'integration_type',
112 + 'integration_details',
113 + 'status',
114 + ],
115 + $conditions
116 + );
117 + }
83 118
84 - public function singleIntegration($integrationCategory = null, $integrationType = null, $status = null){
85 -
86 - $conditions='';
87 - if (!is_null($integrationType)) {
88 - $conditions .= "WHERE `integration_type`='$integrationType'";
119 + public function saveIntegration($integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null)
120 + {
121 + if (null === $status) {
122 + $status = 1;
123 + }
124 + return static::$_integrationModel->insert(
125 + [
126 + 'integration_name' => $integrationName,
127 + 'integration_type' => $integrationType,
128 + 'integration_details' => $integrationDetails,
129 + 'category' => $integrationCategory,
130 + 'form_id' => static::$_formID,
131 + 'user_id' => $this->_user_details['id'],
132 + 'user_ip' => $this->_user_details['ip'],
133 + 'status' => $status,
134 + 'created_at' => $this->_user_details['time'],
135 + 'updated_at' => $this->_user_details['time'],
136 + ]
137 + );
138 + }
139 +
140 + public function updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null)
141 + {
142 + if (null === $status) {
143 + $status = 1;
144 + }
145 + return static::$_integrationModel->update(
146 + [
147 + 'integration_name' => $integrationName,
148 + 'integration_type' => $integrationType,
149 + 'integration_details' => $integrationDetails,
150 + 'category' => $integrationCategory,
151 + 'form_id' => static::$_formID,
152 + 'user_id' => $this->_user_details['id'],
153 + 'user_ip' => $this->_user_details['ip'],
154 + 'status' => $status,
155 + 'updated_at' => $this->_user_details['time'],
156 + ],
157 + [
158 + 'id' => $integrationID,
159 + ]
160 + );
161 + }
162 +
163 + public function duplicateAllInAForm($oldFormId)
164 + {
165 + $integCols = ['integration_name', 'integration_type', 'integration_details', 'category', 'form_id', 'user_id', 'user_ip', 'status', 'created_at', 'updated_at'];
166 + $integDupData = [
167 + 'integration_name',
168 + 'integration_type',
169 + 'integration_details',
170 + 'category',
171 + static::$_formID,
172 + $this->_user_details['id'],
173 + $this->_user_details['ip'],
174 + 'status',
175 + $this->_user_details['time'],
176 + $this->_user_details['time'],
177 + ];
178 + return static::$_integrationModel->duplicate($integCols, $integDupData, ['form_id' => $oldFormId]);
179 + }
180 +
181 + public function deleteIntegration($integrationID)
182 + {
183 + return static::$_integrationModel->delete(
184 + [
185 + 'id' => $integrationID,
186 + 'form_id' => static::$_formID,
187 + ]
188 + );
189 + }
190 +
191 + public static function replaceFieldWithValue($dataToReplaceField, $fieldValues)
192 + {
193 + if (empty($dataToReplaceField)) {
194 + return false;
195 + }
196 + if (is_string($dataToReplaceField)) {
197 + $dataToReplaceField = static::replaceFieldWithValueHelper($dataToReplaceField, $fieldValues);
198 + } elseif (is_array($dataToReplaceField)) {
199 + foreach ($dataToReplaceField as $field => $value) {
200 + if (is_array($value) && 1 === count($value)) {
201 + $dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value[0], $fieldValues);
202 + } elseif (is_array($value)) {
203 + $dataToReplaceField[$field] = static::replaceFieldWithValue($value, $fieldValues);
204 + } else {
205 + $dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value, $fieldValues);
89 206 }
207 + }
208 + }
209 + return $dataToReplaceField;
210 + }
90 211
91 - if (!is_null($integrationCategory)) {
92 - $conditions .= "AND `category`='$integrationCategory'";
93 - }
94 - if (!is_null($status)) {
95 - $conditions .= "AND status=1";
96 - }
97 - return static::$_integrationModel->single_row(
98 - $conditions,
99 - array('id')
100 - );
101 -
212 + private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues)
213 + {
214 + if (empty($stringToReplaceField)) {
215 + return $stringToReplaceField;
102 216 }
217 + $fieldPattern = '/\${\w[^ ${}]*}/';
218 + preg_match_all($fieldPattern, $stringToReplaceField, $matchedField);
219 + $uniqueFieldsInStr = array_unique($matchedField[0]);
220 + foreach ($uniqueFieldsInStr as $key => $value) {
221 + $fieldName = substr($value, 2, strlen($value) - 3);
103 222
104 - public function saveIntegration($integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null)
105 - {
106 - if ($status == null) {
107 - $status = 1;
223 + $repeaterArr = explode('.', $fieldName);
224 +
225 + if (isset($fieldValues[$fieldName])) {
226 + $stringToReplaceField = is_string($fieldValues[$fieldName])
227 + ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField)
228 + : wp_json_encode($fieldValues[$fieldName]);
229 + } elseif (2 === count($repeaterArr) && isset($fieldValues[$repeaterArr[0]])) {
230 + $repeaterValues = [];
231 + $repeaterFieldValues = $fieldValues[$repeaterArr[0]];
232 +
233 + foreach ($repeaterFieldValues as $value) {
234 + if (isset($value[$repeaterArr[1]])) {
235 + $repeaterValues[] = $value[$repeaterArr[1]];
236 + }
108 237 }
109 - return static::$_integrationModel->insert(
110 - array(
111 - "integration_name" => $integrationName,
112 - "integration_type" => $integrationType,
113 - "integration_details" => $integrationDetails,
114 - 'category' => $integrationCategory,
115 - 'form_id' => static::$_formID,
116 - "user_id" => $this->_user_details['id'],
117 - "user_ip" => $this->_user_details['ip'],
118 - "status" => $status,
119 - "created_at" => $this->_user_details['time'],
120 - "updated_at" => $this->_user_details['time']
121 - )
122 - );
238 +
239 + $stringToReplaceField = $repeaterValues;
240 + }
123 241 }
242 + return $stringToReplaceField;
243 + }
124 244
125 - public function updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null)
126 - {
127 - if ($status === null) {
128 - $status = 1;
129 - }
130 - return static::$_integrationModel->update(
131 - array(
132 - "integration_name" => $integrationName,
133 - "integration_type" => $integrationType,
134 - "integration_details" => $integrationDetails,
135 - 'category' => $integrationCategory,
136 - 'form_id' => static::$_formID,
137 - "user_id" => $this->_user_details['id'],
138 - "user_ip" => $this->_user_details['ip'],
139 - "status" => $status,
140 - "updated_at" => $this->_user_details['time']
141 - ),
142 - array(
143 - "id" => $integrationID
144 - )
145 - );
245 + public static function updatedHiddenFieldValue($formID)
246 + {
247 + $hiddenFields = [];
248 + $frontendFormManger = new FrontendFormManager($formID);
249 +
250 + if ($frontendFormManger->isHoneypotActive()) {
251 + $str = '_bitform' . '_' . $formID . '_' . time() . '_';
252 + $honpotToken = Helpers::honeypotEncryptedToken($str);
253 + $hiddenFields[] = [
254 + 'name' => 'b_h_t',
255 + 'value' => $honpotToken
256 + ];
146 257 }
258 + $csrfTokens = Helpers::csrfEecrypted();
259 + $hiddenFields[] = [
260 + 'name' => 't_identity',
261 + 'value' => $csrfTokens['t_identity'],
262 + ];
263 + $hiddenFields[] = [
264 + 'name' => 'csrf',
265 + 'value' => $csrfTokens['csrf']
266 + ];
267 + return $hiddenFields;
268 + }
147 269
148 - public function duplicateAllInAForm($oldFormId)
149 - {
150 - $integCols = ["integration_name", "integration_type", "integration_details", 'category', 'form_id', "user_id", "user_ip", "status", "created_at", "updated_at"];
151 - $integDupData = array(
152 - "integration_name",
153 - "integration_type",
154 - "integration_details",
155 - 'category',
156 - static::$_formID,
157 - $this->_user_details['id'],
158 - $this->_user_details['ip'],
159 - "status",
160 - $this->_user_details['time'],
161 - $this->_user_details['time']
162 - );
163 - return static::$_integrationModel->duplicate($integCols, $integDupData, ['form_id' => $oldFormId]);
270 + public static function maybeSetCronForIntegration($workFlowReturnedData, $opType, $isFormRequest = true)
271 + {
272 + $responseData = [];
273 + $entryId = $workFlowReturnedData['triggerData']['entryID'];
274 + $responseData['entry_id'] = $entryId;
275 + if (isset($workFlowReturnedData['message'])) {
276 + $formID = $workFlowReturnedData['triggerData']['formID'];
277 + $responseData['message'] = $workFlowReturnedData['message'];
278 + $responseData['hidden_fields'] = self::updatedHiddenFieldValue($formID);
279 + $responseData['msg_id'] = isset($workFlowReturnedData['msg_id']) ? $workFlowReturnedData['msg_id'] : 0;
280 + if (isset($workFlowReturnedData['msg_duration'])) {
281 + $responseData['msg_duration'] = $workFlowReturnedData['msg_duration'];
282 + }
164 283 }
284 + if (isset($workFlowReturnedData['redirectPage'])) {
285 + $responseData['redirectPage'] = $workFlowReturnedData['redirectPage'];
286 + }
287 + if ('update' === $opType && isset($workFlowReturnedData['updatedData'])) {
288 + $responseData['updatedData'] = $workFlowReturnedData['updatedData'];
289 + }
290 + if (!isset($workFlowReturnedData['triggerData'])) {
291 + return $responseData;
292 + }
165 293
166 - public function deleteIntegration($integrationID)
167 - {
168 - return static::$_integrationModel->delete(
169 - array(
170 - 'id' => $integrationID,
171 - 'form_id' => static::$_formID,
172 - )
173 - );
294 + $triggerData = $workFlowReturnedData['triggerData'];
295 +
296 + $trnasientData = get_transient("bitform_trigger_transient_{$entryId}");
297 + $trnasientData = is_string($trnasientData) ? json_decode($trnasientData) : $trnasientData;
298 + if (!empty($trnasientData['fields'])) {
299 + $workFlowReturnedData['fields'] = array_merge($workFlowReturnedData['fields'], $trnasientData['fields']);
174 300 }
175 301
176 - public static function replaceFieldWithValue($dataToReplaceField, $fieldValues)
177 - {
178 - if (empty($dataToReplaceField)) {
179 - return false;
302 + if (function_exists('fastcgi_finish_request') || !wp_doing_ajax()) {
303 + if (!headers_sent()) {
304 + header('Connection: close');
305 + $contentType = wp_doing_ajax() || defined('REST_REQUEST') ? 'application/json' : 'text/html';
306 + header('Content-Type: ' . $contentType . '; charset=' . get_option('blog_charset'));
307 + status_header(200);
308 + if (!$isFormRequest) {
309 + $response = [
310 + 'status' => 200,
311 + 'code' => 4000,
312 + 'message' => 'Data Added Successfully!!',
313 + 'success' => true
314 + ];
315 + } else {
316 + $response = [
317 + 'success' => true,
318 + 'data' => $responseData,
319 + ];
180 320 }
181 - if (is_string($dataToReplaceField)) {
182 - $dataToReplaceField = static::replaceFieldWithValueHelper($dataToReplaceField, $fieldValues);
183 - } elseif (is_array($dataToReplaceField)) {
184 - foreach ($dataToReplaceField as $field => $value) {
185 - if (is_array($value) && count($value) === 1) {
186 - $dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value[0], $fieldValues);
187 - } elseif (is_array($value)) {
188 - $dataToReplaceField[$field] = static::replaceFieldWithValue($value, $fieldValues);
189 - } else {
190 - $dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value, $fieldValues);
191 - }
321 + }
322 + ob_start();
323 + echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : $workFlowReturnedData['message'];
324 + ob_end_flush();
325 + flush();
326 + session_write_close();
327 +
328 + if (isset($workFlowReturnedData['integrationRun']) && !$workFlowReturnedData['integrationRun']) {
329 + $entryModel = new FormEntryModel();
330 + $updatedStatus = $entryModel->update(
331 + [
332 + 'status' => 2,
333 + ],
334 + [
335 + 'form_id' => $triggerData['formID'],
336 + 'id' => $triggerData['entryID'],
337 + ]
338 + );
339 +
340 + if (!is_wp_error($updatedStatus)) {
341 + if ($workFlowReturnedData['dflt_template']) {
342 + $triggerData['fields'] = $workFlowReturnedData['fields'];
343 + do_action('bf_double_optin_confirmation', $workFlowReturnedData['integrationDetails'], $triggerData);
344 + } elseif (isset($triggerData['dblOptin'])) {
345 + foreach ($triggerData['dblOptin'] as $value) {
346 + MailNotifier::notify($value, $triggerData['formID'], $triggerData['fields'], $triggerData['entryID'], true, $triggerData['logID']);
192 347 }
348 + }
193 349 }
194 - return $dataToReplaceField;
195 - }
350 + if (function_exists('fastcgi_finish_request')) {
351 + fastcgi_finish_request();
352 + }
196 353
197 - private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues)
198 - {
199 - if (empty($stringToReplaceField)) {
200 - return $stringToReplaceField;
354 + if (defined('REST_REQUEST') || wp_doing_ajax()) {
355 + die;
201 356 }
202 - $fieldPattern = '/\${\w[^ ${}]*}/';
203 - preg_match_all($fieldPattern, $stringToReplaceField, $matchedField);
204 - $uniqueFieldsInStr = array_unique($matchedField[0]);
205 - foreach ($uniqueFieldsInStr as $key => $value) {
206 - $fieldName = substr($value, 2, strlen($value) - 3);
207 - if (!empty($fieldValues[$fieldName])) {
208 - $stringToReplaceField = is_string($fieldValues[$fieldName]) ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField) :
209 - wp_json_encode($fieldValues[$fieldName]);
210 - }
357 + return $workFlowReturnedData;
358 + }
359 +
360 + if (isset($triggerData['mail'])) {
361 + $formManager = new AdminFormManager($triggerData['formID']);
362 + $formContent = $formManager->getFormContent();
363 + $submitted_fields = $formContent->fields;
364 + $fieldValueForMail = FieldValueHandler::formatFieldValueForMail($submitted_fields, $workFlowReturnedData['fields']);
365 + foreach ($triggerData['mail'] as $value) {
366 + MailNotifier::notify($value, $triggerData['formID'], $fieldValueForMail, $triggerData['entryID']);
211 367 }
212 - return $stringToReplaceField;
368 + }
369 + do_action('bitforms_exec_integrations', $triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']);
370 +
371 + if (function_exists('fastcgi_finish_request')) {
372 + fastcgi_finish_request();
373 + }
374 +
375 + if (defined('REST_REQUEST') || wp_doing_ajax()) {
376 + die;
377 + }
378 + return $workFlowReturnedData;
213 379 }
214 380
215 - public static function maybeSetCronForIntegration($workFlowReturnedData, $opType)
216 - {
217 - $responseData = [];
218 - if (isset($workFlowReturnedData['message'])) {
219 - $responseData['message'] = $workFlowReturnedData['message'];
220 - }
221 - if (isset($workFlowReturnedData['redirectPage'])) {
222 - $responseData['redirectPage'] = $workFlowReturnedData['redirectPage'];
223 - }
224 - if ($opType === 'update' && isset($workFlowReturnedData['updatedData'])) {
225 - $responseData['updatedData'] = $workFlowReturnedData['updatedData'];
226 - }
227 - if (!isset($workFlowReturnedData['triggerData'])) {
228 - return $responseData;
229 - }
230 - $triggerData = $workFlowReturnedData['triggerData'];
231 - if (function_exists('fastcgi_finish_request') || !wp_doing_ajax()) {
232 - if (!headers_sent()) {
233 - header("Connection: close");
234 - $contentType = wp_doing_ajax() || defined('REST_REQUEST') ? "application/json" : "text/html";
235 - header('Content-Type: '. $contentType .'; charset=' . get_option('blog_charset'));
236 - status_header(200);
237 - $response = array(
238 - 'success' => true,
239 - 'data' => $responseData,
240 - );
241 - }
242 - ob_start();
243 - echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : $workFlowReturnedData['message'];
244 - ob_end_flush();
245 - flush();
246 - session_write_close();
247 - if (function_exists('fastcgi_finish_request')) {
248 - fastcgi_finish_request();
249 - }
381 + $entryID = $triggerData['entryID'];
382 + if (isset($workFlowReturnedData['cron']) && $workFlowReturnedData['cron'] && !isset($triggerData['mail']) && isset($workFlowReturnedData['integrationRun']) && $workFlowReturnedData['integrationRun']) {
383 + $eventScheuled = wp_schedule_single_event(time(), 'bitforms_exec_integrations', [$triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']]);
384 + $scheduleTime = wp_next_scheduled('bitforms_exec_integrations', [$triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']]);
385 + $responseData['cron'] = get_site_url(null, "/wp-cron.php?doing_wp_cron&{$scheduleTime}");
386 + } elseif (!empty($triggerData['integrations']) || !empty($triggerData['mail']) || isset($workFlowReturnedData['integrationRun']) && !$workFlowReturnedData['integrationRun']) {
387 + $triggerData['fields'] = $workFlowReturnedData['fields'];
388 + if (!$workFlowReturnedData['integrationRun'] && isset($workFlowReturnedData['integrationDetails'])) {
389 + $triggerData['dbl_opt_donf'] = $workFlowReturnedData['integrationDetails'];
390 + $triggerData['dbl_opt_dflt_template'] = $workFlowReturnedData['dflt_template'];
391 + $triggerData['integrationRun'] = $workFlowReturnedData['integrationRun'];
392 + }
393 + set_transient("bitform_trigger_transient_{$entryID}", $triggerData, HOUR_IN_SECONDS);
394 + $entryLog = new FormEntryLogModel();
395 + $queueuEntry = $entryLog->log_history_insert(
396 + [
397 + 'log_id' => $triggerData['logID'],
398 + 'integration_id' => 0,
399 + 'api_type' => wp_json_encode(['type' => 'trigger', 'type_name' => 'Workflow', 'on' => $opType]),
400 + 'response_type' => 'queued',
401 + 'response_obj' => wp_json_encode(['status' => 'queued']),
402 + 'created_at' => current_time('mysql'),
403 + ]
404 + );
405 + $responseData['cronNotOk'] = [
406 + $triggerData['entryID'],
407 + $triggerData['logID'],
408 + $queueuEntry,
409 + ];
410 + }
411 + return $responseData;
412 + }
250 413
251 - if (isset($triggerData['mail'])) {
252 - foreach ($triggerData['mail'] as $value) {
253 - MailNotifier::notify($value, $triggerData['formID'], $workFlowReturnedData['fields'], $triggerData['entryID']);
254 - }
414 + public static function repeaterFieldValueFormatter($fieldValues)
415 + {
416 + $formattedArray = [];
417 +
418 + foreach ($fieldValues as $key => $value) {
419 + if (is_array($value)) {
420 + foreach ($value as $index => $v) {
421 + if (is_array($v)) {
422 + foreach ($v as $k1 => $v1) {
423 + $formattedArray[$k1][] = $v1;
255 424 }
256 - do_action("bitforms_exec_integrations", $triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']);
257 - if (defined('REST_REQUEST') || wp_doing_ajax()) {
258 - die;
259 - }
260 - return $workFlowReturnedData;
425 + } else {
426 + $formattedArray[$key][] = $v;
427 + }
261 428 }
429 + } else {
430 + $formattedArray[$key] = $value;
431 + }
432 + }
262 433
263 - $entryID = $triggerData['entryID'];
264 - if (isset($workFlowReturnedData['cron']) && $workFlowReturnedData['cron'] && !isset($triggerData['mail'])) {
265 - $eventScheuled = wp_schedule_single_event(time(), "bitforms_exec_integrations", array($triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']));
266 - $scheduleTime = wp_next_scheduled("bitforms_exec_integrations", array($triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']));
267 - $responseData['cron'] = get_site_url(null, "/wp-cron.php?doing_wp_cron&{$scheduleTime}");
268 - } else if (!empty($triggerData['integrations']) || !empty($triggerData['mail'])) {
269 - $triggerData['fields'] = $workFlowReturnedData['fields'];
270 - set_transient("bitform_trigger_transient_{$entryID}", $triggerData, HOUR_IN_SECONDS);
271 - $entryLog = new FormEntryLogModel();
272 - $queueuEntry = $entryLog->log_history_insert(
273 - array(
274 - "log_id" => $triggerData['logID'],
275 - "integration_id" => 0,
276 - "api_type" => wp_json_encode(["type" => "trigger", "type_name" => "Workflow", "on" => $opType]),
277 - "response_type" => "queued",
278 - "response_obj" => json_encode(['status' => 'queued']),
279 - "created_at" => current_time("mysql"),
280 - )
281 - );
282 - $responseData['cronNotOk'] = [
283 - $triggerData['entryID'],
284 - $triggerData['logID'],
285 - $queueuEntry
286 - ];
434 + return $formattedArray;
435 + }
436 +
437 + /**
438 + *
439 + * @param mixed $fieldValues
440 + * @param mixed $returnRepeaterValue default is array
441 + * @return array and repeater field value as array or string
442 + */
443 + public static function formattedRepeaterValue($fieldValues, $returnRepeaterValue = '')
444 + {
445 + // get the repeater field and store it in an array
446 + $repeaterField = [];
447 + foreach ($fieldValues as $key => $value) {
448 + if (!preg_match('/\./', $key)) {
449 + continue;
450 + }
451 + $repeaterField[] = $key;
452 + }
453 +
454 + // put the value of the child key to the parent repeater field\
455 + foreach ($repeaterField as $key) {
456 + list($parentKey, $childKey) = explode('.', $key);
457 + $repeatValues = isset($fieldValues[$parentKey]) ? $fieldValues[$parentKey] : $key;
458 +
459 + if (!is_array($repeatValues)) {
460 + $fieldValues[$key] = $fieldValues[$childKey];
461 + continue;
462 + }
463 +
464 + foreach ($repeatValues as $value) {
465 + if (isset($value[$childKey])) {
466 + if (!isset($fieldValues[$key]) || !is_array($fieldValues[$key])) {
467 + $fieldValues[$key] = [];
468 + }
469 + if (!empty($value[$childKey])) {
470 + $fieldValues[$key][] = $value[$childKey];
471 + }
287 472 }
473 + }
474 + }
288 475
289 - return $responseData;
476 + // convert the array to string repeater field value
477 + if ('string' === $returnRepeaterValue) {
478 + foreach ($repeaterField as $key) {
479 + if (isset($fieldValues[$key]) && is_array($fieldValues[$key])) {
480 + $fieldValues[$key] = implode(', ', $fieldValues[$key]);
481 + }
482 + }
290 483 }
484 + return $fieldValues;
485 + }
291 486 }