PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.15.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.15.3
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
← All changes | includes/Core/Integration/IntegrationHandler.php +433 -267 1.5.22.15.3 View file →
@@ -2,316 +2,482 @@
2 2
3 3 namespace BitCode\BitForm\Core\Integration;
4 4
5 5 use BitCode\BitForm\Admin\Form\AdminFormManager;
6 -use BitCode\BitForm\Core\Util\MailNotifier;
6 +use BitCode\BitForm\Admin\Form\Helpers;
7 +use BitCode\BitForm\Core\Database\FormEntryLogModel;
8 +use BitCode\BitForm\Core\Database\FormEntryModel;
7 9 use BitCode\BitForm\Core\Database\IntegrationModel;
8 -use BitCode\BitForm\Core\Database\FormEntryLogModel;
9 10 use BitCode\BitForm\Core\Util\FieldValueHandler;
11 +use BitCode\BitForm\Core\Util\MailNotifier;
12 +use BitCode\BitForm\Frontend\Form\FrontendFormManager;
10 13
11 14 final class IntegrationHandler
12 15 {
13 - private static $_formID;
14 - private static $_integrationModel;
15 - private $_user_details;
16 + private static $_formID;
17 + private static $_integrationModel;
18 + private $_user_details;
16 19
17 - /**
18 - * Constructor of Integration Handler
19 - *
20 - * @param Integer $formID If Integration is accessible globally then
21 - * $formID will be 0 and category app
22 - * @param Array $user_details Details of user accessing data
23 - * @return void
24 - */
25 - public function __construct($formID, $user_details = null)
26 - {
27 - static::$_formID = $formID;
28 - static::$_integrationModel = new IntegrationModel();
29 - $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]);
30 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 + }
31 58
32 - public function getAIntegration($integrationID, $integrationCategory = null, $integrationType = null)
33 - {
34 - $conditions = [
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 - [
46 - 'id',
47 - 'integration_name',
48 - 'integration_type',
49 - 'integration_details',
50 - 'form_id'
51 - ],
52 - $conditions
53 - );
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]);
54 66 }
55 67
56 - public function getAllIntegration($integrationCategory = null, $integrationType = null, $status = null, $id = null)
57 - {
58 - $conditions = [
59 - 'form_id' => static::$_formID
60 - ];
61 - if (!is_null($integrationType)) {
62 - $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
63 - }
68 + if (!is_null($integrationCategory)) {
69 + $conditions = array_merge($conditions, ['category' => $integrationCategory]);
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 + }
64 89
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 - if (!is_null($id)) {
72 - $conditions = array_merge($conditions, ['id' => $id]);
73 - }
74 - return static::$_integrationModel->get(
75 - [
76 - 'id',
77 - 'form_id',
78 - 'integration_name',
79 - 'integration_type',
80 - 'integration_details',
81 - 'status'
82 - ],
83 - $conditions
84 - );
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]);
85 95 }
86 96
87 - public function getIntegrationWithoutFormId($integrationCategory = null, $integrationType = null, $status = null, $id = null)
88 - {
89 - $conditions = [];
90 - if (!is_null($integrationType)) {
91 - $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
92 - }
97 + if (!is_null($integrationCategory)) {
98 + $conditions = array_merge($conditions, ['category' => $integrationCategory]);
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 + }
93 118
94 - if (!is_null($integrationCategory)) {
95 - $conditions = array_merge($conditions, ['category' => $integrationCategory]);
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);
96 206 }
97 - if (!is_null($status)) {
98 - $conditions = array_merge($conditions, ['status' => 1]);
99 - }
100 - if (!is_null($id)) {
101 - $conditions = array_merge($conditions, ['id' => $id]);
102 - }
103 - return static::$_integrationModel->get(
104 - [
105 - 'id',
106 - 'form_id',
107 - 'integration_name',
108 - 'integration_type',
109 - 'integration_details',
110 - 'status'
111 - ],
112 - $conditions
113 - );
207 + }
114 208 }
209 + return $dataToReplaceField;
210 + }
115 211
116 - public function saveIntegration($integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null)
117 - {
118 - if ($status == null) {
119 - $status = 1;
120 - }
121 - return static::$_integrationModel->insert(
122 - [
123 - 'integration_name' => $integrationName,
124 - 'integration_type' => $integrationType,
125 - 'integration_details' => $integrationDetails,
126 - 'category' => $integrationCategory,
127 - 'form_id' => static::$_formID,
128 - 'user_id' => $this->_user_details['id'],
129 - 'user_ip' => $this->_user_details['ip'],
130 - 'status' => $status,
131 - 'created_at' => $this->_user_details['time'],
132 - 'updated_at' => $this->_user_details['time']
133 - ]
134 - );
212 + private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues)
213 + {
214 + if (empty($stringToReplaceField)) {
215 + return $stringToReplaceField;
135 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);
136 222
137 - public function updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null)
138 - {
139 - if ($status === null) {
140 - $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 + }
141 237 }
142 - return static::$_integrationModel->update(
143 - [
144 - 'integration_name' => $integrationName,
145 - 'integration_type' => $integrationType,
146 - 'integration_details' => $integrationDetails,
147 - 'category' => $integrationCategory,
148 - 'form_id' => static::$_formID,
149 - 'user_id' => $this->_user_details['id'],
150 - 'user_ip' => $this->_user_details['ip'],
151 - 'status' => $status,
152 - 'updated_at' => $this->_user_details['time']
153 - ],
154 - [
155 - 'id' => $integrationID
156 - ]
157 - );
238 +
239 + $stringToReplaceField = $repeaterValues;
240 + }
158 241 }
242 + return $stringToReplaceField;
243 + }
159 244
160 - public function duplicateAllInAForm($oldFormId)
161 - {
162 - $integCols = ['integration_name', 'integration_type', 'integration_details', 'category', 'form_id', 'user_id', 'user_ip', 'status', 'created_at', 'updated_at'];
163 - $integDupData = [
164 - 'integration_name',
165 - 'integration_type',
166 - 'integration_details',
167 - 'category',
168 - static::$_formID,
169 - $this->_user_details['id'],
170 - $this->_user_details['ip'],
171 - 'status',
172 - $this->_user_details['time'],
173 - $this->_user_details['time']
174 - ];
175 - return static::$_integrationModel->duplicate($integCols, $integDupData, ['form_id' => $oldFormId]);
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 + ];
176 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 + }
177 269
178 - public function deleteIntegration($integrationID)
179 - {
180 - return static::$_integrationModel->delete(
181 - [
182 - 'id' => $integrationID,
183 - 'form_id' => static::$_formID,
184 - ]
185 - );
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 + }
186 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 + }
187 293
188 - public static function replaceFieldWithValue($dataToReplaceField, $fieldValues)
189 - {
190 - if (empty($dataToReplaceField)) {
191 - return false;
192 - }
193 - if (is_string($dataToReplaceField)) {
194 - $dataToReplaceField = static::replaceFieldWithValueHelper($dataToReplaceField, $fieldValues);
195 - } elseif (is_array($dataToReplaceField)) {
196 - foreach ($dataToReplaceField as $field => $value) {
197 - if (is_array($value) && count($value) === 1) {
198 - $dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value[0], $fieldValues);
199 - } elseif (is_array($value)) {
200 - $dataToReplaceField[$field] = static::replaceFieldWithValue($value, $fieldValues);
201 - } else {
202 - $dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value, $fieldValues);
203 - }
204 - }
205 - }
206 - return $dataToReplaceField;
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']);
207 300 }
208 301
209 - private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues)
210 - {
211 - if (empty($stringToReplaceField)) {
212 - return $stringToReplaceField;
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 + ];
213 320 }
214 - $fieldPattern = '/\${\w[^ ${}]*}/';
215 - preg_match_all($fieldPattern, $stringToReplaceField, $matchedField);
216 - $uniqueFieldsInStr = array_unique($matchedField[0]);
217 - foreach ($uniqueFieldsInStr as $key => $value) {
218 - $fieldName = substr($value, 2, strlen($value) - 3);
219 - if (!empty($fieldValues[$fieldName])) {
220 - $stringToReplaceField = is_string($fieldValues[$fieldName]) ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField) :
221 - wp_json_encode($fieldValues[$fieldName]);
222 - }
223 - }
224 - return $stringToReplaceField;
225 - }
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();
226 327
227 - public static function maybeSetCronForIntegration($workFlowReturnedData, $opType)
228 - {
229 - $responseData = [];
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 + );
230 339
231 - if (isset($workFlowReturnedData['message'])) {
232 - $responseData['message'] = $workFlowReturnedData['message'];
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']);
347 + }
348 + }
233 349 }
234 - if (isset($workFlowReturnedData['redirectPage'])) {
235 - $responseData['redirectPage'] = $workFlowReturnedData['redirectPage'];
350 + if (function_exists('fastcgi_finish_request')) {
351 + fastcgi_finish_request();
236 352 }
237 - if ($opType === 'update' && isset($workFlowReturnedData['updatedData'])) {
238 - $responseData['updatedData'] = $workFlowReturnedData['updatedData'];
353 +
354 + if (defined('REST_REQUEST') || wp_doing_ajax()) {
355 + die;
239 356 }
240 - if (!isset($workFlowReturnedData['triggerData'])) {
241 - return $responseData;
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']);
242 367 }
243 - $triggerData = $workFlowReturnedData['triggerData'];
244 - // if (function_exists('fastcgi_finish_request')) {
245 - // var_dump($triggerData['integrations'], 'beforeif');
246 - // }
368 + }
369 + do_action('bitforms_exec_integrations', $triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']);
247 370
248 - if (function_exists('fastcgi_finish_request') || !wp_doing_ajax()) {
249 - if (!headers_sent()) {
250 - header('Connection: close');
251 - $contentType = wp_doing_ajax() || defined('REST_REQUEST') ? 'application/json' : 'text/html';
252 - header('Content-Type: ' . $contentType . '; charset=' . get_option('blog_charset'));
253 - status_header(200);
254 - $response = [
255 - 'success' => true,
256 - 'data' => $responseData,
257 - ];
258 - }
259 - ob_start();
260 - echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : $workFlowReturnedData['message'];
261 - ob_end_flush();
262 - flush();
263 - session_write_close();
264 - // if (function_exists('fastcgi_finish_request')) {
265 - // fastcgi_finish_request();
266 - // }
267 - //var_dump('after');
268 - if (isset($triggerData['mail'])) {
269 - $formManager = new AdminFormManager($triggerData['formID']);
270 - $formContent = $formManager->getFormContent();
271 - $submitted_fields = $formContent->fields;
272 - $fieldValueForMail = FieldValueHandler::formatFieldValueForMail($submitted_fields, $workFlowReturnedData['fields']);
273 - foreach ($triggerData['mail'] as $value) {
274 - MailNotifier::notify($value, $triggerData['formID'], $fieldValueForMail, $triggerData['entryID']);
275 - }
276 - }
277 - do_action('bitforms_exec_integrations', $triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']);
371 + if (function_exists('fastcgi_finish_request')) {
372 + fastcgi_finish_request();
373 + }
278 374
279 - if (function_exists('fastcgi_finish_request')) {
280 - fastcgi_finish_request();
281 - }
375 + if (defined('REST_REQUEST') || wp_doing_ajax()) {
376 + die;
377 + }
378 + return $workFlowReturnedData;
379 + }
282 380
283 - if (defined('REST_REQUEST') || wp_doing_ajax()) {
284 - die;
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 + }
413 +
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;
285 424 }
286 - return $workFlowReturnedData;
425 + } else {
426 + $formattedArray[$key][] = $v;
427 + }
287 428 }
429 + } else {
430 + $formattedArray[$key] = $value;
431 + }
432 + }
288 433
289 - $entryID = $triggerData['entryID'];
290 - if (isset($workFlowReturnedData['cron']) && $workFlowReturnedData['cron'] && !isset($triggerData['mail'])) {
291 - $eventScheuled = wp_schedule_single_event(time(), 'bitforms_exec_integrations', [$triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']]);
292 - $scheduleTime = wp_next_scheduled('bitforms_exec_integrations', [$triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']]);
293 - $responseData['cron'] = get_site_url(null, "/wp-cron.php?doing_wp_cron&{$scheduleTime}");
294 - } elseif (!empty($triggerData['integrations']) || !empty($triggerData['mail'])) {
295 - $triggerData['fields'] = $workFlowReturnedData['fields'];
296 - set_transient("bitform_trigger_transient_{$entryID}", $triggerData, HOUR_IN_SECONDS);
297 - $entryLog = new FormEntryLogModel();
298 - $queueuEntry = $entryLog->log_history_insert(
299 - [
300 - 'log_id' => $triggerData['logID'],
301 - 'integration_id' => 0,
302 - 'api_type' => wp_json_encode(['type' => 'trigger', 'type_name' => 'Workflow', 'on' => $opType]),
303 - 'response_type' => 'queued',
304 - 'response_obj' => json_encode(['status' => 'queued']),
305 - 'created_at' => current_time('mysql'),
306 - ]
307 - );
308 - $responseData['cronNotOk'] = [
309 - $triggerData['entryID'],
310 - $triggerData['logID'],
311 - $queueuEntry
312 - ];
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 = $fieldValues[$parentKey];
458 +
459 + if (!is_array($repeatValues)) {
460 + continue;
461 + }
462 +
463 + foreach ($repeatValues as $value) {
464 + if (isset($value[$childKey])) {
465 + if (!isset($fieldValues[$key]) || !is_array($fieldValues[$key])) {
466 + $fieldValues[$key] = [];
467 + }
468 + if (!empty($value[$childKey])) {
469 + $fieldValues[$key][] = $value[$childKey];
470 + }
313 471 }
472 + }
473 + }
314 474
315 - return $responseData;
475 + // convert the array to string repeater field value
476 + if ('string' === $returnRepeaterValue) {
477 + foreach ($repeaterField as $key) {
478 + $fieldValues[$key] = implode(', ', $fieldValues[$key]);
479 + }
316 480 }
481 + return $fieldValues;
482 + }
317 483 }