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 +428 -285 1.92.15.3 View file →
@@ -2,339 +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\Admin\Form\Helpers;
6 7 use BitCode\BitForm\Core\Database\FormEntryLogModel;
7 8 use BitCode\BitForm\Core\Database\FormEntryModel;
8 9 use BitCode\BitForm\Core\Database\IntegrationModel;
9 10 use BitCode\BitForm\Core\Util\FieldValueHandler;
10 11 use BitCode\BitForm\Core\Util\MailNotifier;
12 +use BitCode\BitForm\Frontend\Form\FrontendFormManager;
11 13
12 -final class IntegrationHandler {
13 - private static $_formID;
14 - private static $_integrationModel;
15 - private $_user_details;
14 +final class IntegrationHandler
15 +{
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 - 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
31 - public function getAIntegration($integrationID, $integrationCategory = null, $integrationType = null) {
32 - $conditions = [
33 - 'form_id' => static::$_formID,
34 - 'id' => $integrationID,
35 - ];
36 - if (!is_null($integrationType)) {
37 - $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
38 - }
39 - if (!is_null($integrationCategory)) {
40 - $conditions = array_merge($conditions, ['category' => $integrationCategory]);
41 - }
42 - return static::$_integrationModel->get(
43 - [
44 - 'id',
45 - 'integration_name',
46 - 'integration_type',
47 - 'integration_details',
48 - 'form_id',
49 - ],
50 - $conditions
51 - );
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]);
52 66 }
53 67
54 - public function getAllIntegration($integrationCategory = null, $integrationType = null, $status = null, $id = null) {
55 - $conditions = [
56 - 'form_id' => static::$_formID,
57 - ];
58 - if (!is_null($integrationType)) {
59 - $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
60 - }
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 + }
61 89
62 - if (!is_null($integrationCategory)) {
63 - $conditions = array_merge($conditions, ['category' => $integrationCategory]);
64 - }
65 - if (!is_null($status)) {
66 - $conditions = array_merge($conditions, ['status' => 1]);
67 - }
68 - if (!is_null($id)) {
69 - $conditions = array_merge($conditions, ['id' => $id]);
70 - }
71 - return static::$_integrationModel->get(
72 - [
73 - 'id',
74 - 'form_id',
75 - 'integration_name',
76 - 'integration_type',
77 - 'integration_details',
78 - 'status',
79 - ],
80 - $conditions
81 - );
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]);
82 95 }
83 96
84 - public function getIntegrationWithoutFormId($integrationCategory = null, $integrationType = null, $status = null, $id = null) {
85 - $conditions = [];
86 - if (!is_null($integrationType)) {
87 - $conditions = array_merge($conditions, ['integration_type' => $integrationType]);
88 - }
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 + }
89 118
90 - if (!is_null($integrationCategory)) {
91 - $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);
92 206 }
93 - if (!is_null($status)) {
94 - $conditions = array_merge($conditions, ['status' => 1]);
95 - }
96 - if (!is_null($id)) {
97 - $conditions = array_merge($conditions, ['id' => $id]);
98 - }
99 - return static::$_integrationModel->get(
100 - [
101 - 'id',
102 - 'form_id',
103 - 'integration_name',
104 - 'integration_type',
105 - 'integration_details',
106 - 'status',
107 - ],
108 - $conditions
109 - );
207 + }
110 208 }
209 + return $dataToReplaceField;
210 + }
111 211
112 - public function saveIntegration($integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) {
113 - if ($status == null) {
114 - $status = 1;
115 - }
116 - return static::$_integrationModel->insert(
117 - [
118 - 'integration_name' => $integrationName,
119 - 'integration_type' => $integrationType,
120 - 'integration_details' => $integrationDetails,
121 - 'category' => $integrationCategory,
122 - 'form_id' => static::$_formID,
123 - 'user_id' => $this->_user_details['id'],
124 - 'user_ip' => $this->_user_details['ip'],
125 - 'status' => $status,
126 - 'created_at' => $this->_user_details['time'],
127 - 'updated_at' => $this->_user_details['time'],
128 - ]
129 - );
212 + private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues)
213 + {
214 + if (empty($stringToReplaceField)) {
215 + return $stringToReplaceField;
130 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);
131 222
132 - public function updateIntegration($integrationID, $integrationName, $integrationType, $integrationDetails, $integrationCategory, $status = null) {
133 - if ($status === null) {
134 - $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 + }
135 237 }
136 - return static::$_integrationModel->update(
137 - [
138 - 'integration_name' => $integrationName,
139 - 'integration_type' => $integrationType,
140 - 'integration_details' => $integrationDetails,
141 - 'category' => $integrationCategory,
142 - 'form_id' => static::$_formID,
143 - 'user_id' => $this->_user_details['id'],
144 - 'user_ip' => $this->_user_details['ip'],
145 - 'status' => $status,
146 - 'updated_at' => $this->_user_details['time'],
147 - ],
148 - [
149 - 'id' => $integrationID,
150 - ]
151 - );
238 +
239 + $stringToReplaceField = $repeaterValues;
240 + }
152 241 }
242 + return $stringToReplaceField;
243 + }
153 244
154 - public function duplicateAllInAForm($oldFormId) {
155 - $integCols = ['integration_name', 'integration_type', 'integration_details', 'category', 'form_id', 'user_id', 'user_ip', 'status', 'created_at', 'updated_at'];
156 - $integDupData = [
157 - 'integration_name',
158 - 'integration_type',
159 - 'integration_details',
160 - 'category',
161 - static::$_formID,
162 - $this->_user_details['id'],
163 - $this->_user_details['ip'],
164 - 'status',
165 - $this->_user_details['time'],
166 - $this->_user_details['time'],
167 - ];
168 - 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 + ];
169 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 + }
170 269
171 - public function deleteIntegration($integrationID) {
172 - return static::$_integrationModel->delete(
173 - [
174 - 'id' => $integrationID,
175 - 'form_id' => static::$_formID,
176 - ]
177 - );
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 + }
178 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 + }
179 293
180 - public static function replaceFieldWithValue($dataToReplaceField, $fieldValues) {
181 - if (empty($dataToReplaceField)) {
182 - return false;
183 - }
184 - if (is_string($dataToReplaceField)) {
185 - $dataToReplaceField = static::replaceFieldWithValueHelper($dataToReplaceField, $fieldValues);
186 - } elseif (is_array($dataToReplaceField)) {
187 - foreach ($dataToReplaceField as $field => $value) {
188 - if (is_array($value) && count($value) === 1) {
189 - $dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value[0], $fieldValues);
190 - } elseif (is_array($value)) {
191 - $dataToReplaceField[$field] = static::replaceFieldWithValue($value, $fieldValues);
192 - } else {
193 - $dataToReplaceField[$field] = static::replaceFieldWithValueHelper($value, $fieldValues);
194 - }
195 - }
196 - }
197 - 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']);
198 300 }
199 301
200 - private static function replaceFieldWithValueHelper($stringToReplaceField, $fieldValues) {
201 - if (empty($stringToReplaceField)) {
202 - 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 + ];
203 320 }
204 - $fieldPattern = '/\${\w[^ ${}]*}/';
205 - preg_match_all($fieldPattern, $stringToReplaceField, $matchedField);
206 - $uniqueFieldsInStr = array_unique($matchedField[0]);
207 - foreach ($uniqueFieldsInStr as $key => $value) {
208 - $fieldName = substr($value, 2, strlen($value) - 3);
209 - if (!empty($fieldValues[$fieldName])) {
210 - $stringToReplaceField = is_string($fieldValues[$fieldName]) ? str_replace($value, $fieldValues[$fieldName], $stringToReplaceField) :
211 - wp_json_encode($fieldValues[$fieldName]);
212 - }
213 - }
214 - return $stringToReplaceField;
215 - }
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();
216 327
217 - public static function maybeSetCronForIntegration($workFlowReturnedData, $opType) {
218 - $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 + );
219 339
220 - if (isset($workFlowReturnedData['message'])) {
221 - $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 + }
222 349 }
223 - if (isset($workFlowReturnedData['redirectPage'])) {
224 - $responseData['redirectPage'] = $workFlowReturnedData['redirectPage'];
350 + if (function_exists('fastcgi_finish_request')) {
351 + fastcgi_finish_request();
225 352 }
226 - if ($opType === 'update' && isset($workFlowReturnedData['updatedData'])) {
227 - $responseData['updatedData'] = $workFlowReturnedData['updatedData'];
353 +
354 + if (defined('REST_REQUEST') || wp_doing_ajax()) {
355 + die;
228 356 }
229 - if (!isset($workFlowReturnedData['triggerData'])) {
230 - 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']);
231 367 }
368 + }
369 + do_action('bitforms_exec_integrations', $triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']);
232 370
233 - $triggerData = $workFlowReturnedData['triggerData'];
371 + if (function_exists('fastcgi_finish_request')) {
372 + fastcgi_finish_request();
373 + }
234 374
235 - if (function_exists('fastcgi_finish_request') || !wp_doing_ajax()) {
236 - if (!headers_sent()) {
237 - header('Connection: close');
238 - $contentType = wp_doing_ajax() || defined('REST_REQUEST') ? 'application/json' : 'text/html';
239 - header('Content-Type: ' . $contentType . '; charset=' . get_option('blog_charset'));
240 - status_header(200);
241 - $response = [
242 - 'success' => true,
243 - 'data' => $responseData,
244 - ];
245 - }
246 - ob_start();
247 - echo wp_doing_ajax() || defined('REST_REQUEST') ? wp_json_encode($response) : $workFlowReturnedData['message'];
248 - ob_end_flush();
249 - flush();
250 - session_write_close();
375 + if (defined('REST_REQUEST') || wp_doing_ajax()) {
376 + die;
377 + }
378 + return $workFlowReturnedData;
379 + }
251 380
252 - if (isset($workFlowReturnedData['integrationRun']) && !$workFlowReturnedData['integrationRun']) {
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 + }
253 413
254 - $entryModel = new FormEntryModel();
255 - $updatedStatus = $entryModel->update(
256 - array(
257 - "status" => 2,
258 - ),
259 - array(
260 - 'form_id' => $triggerData['formID'],
261 - 'id' => $triggerData['entryID'],
262 - )
263 - );
414 + public static function repeaterFieldValueFormatter($fieldValues)
415 + {
416 + $formattedArray = [];
264 417
265 - if (!is_wp_error($updatedStatus)) {
266 - if ($workFlowReturnedData['dflt_template']) {
267 - $triggerData['fields'] = $workFlowReturnedData['fields'];
268 - do_action('bf_double_optin_confirmation', $workFlowReturnedData['integrationDetails'], $triggerData);
269 - } elseif (isset($triggerData['dblOptin'])) {
270 - foreach ($triggerData['dblOptin'] as $value) {
271 - MailNotifier::notify($value, $triggerData['formID'], $triggerData['fields'], $triggerData['entryID'], true, $triggerData['logID']);
272 - }
273 - }
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;
424 + }
425 + } else {
426 + $formattedArray[$key][] = $v;
427 + }
428 + }
429 + } else {
430 + $formattedArray[$key] = $value;
431 + }
432 + }
274 433
275 - }
276 - if (function_exists('fastcgi_finish_request')) {
277 - fastcgi_finish_request();
278 - }
434 + return $formattedArray;
435 + }
279 436
280 - if (defined('REST_REQUEST') || wp_doing_ajax()) {
281 - die;
282 - }
283 - return $workFlowReturnedData;
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 + }
284 453
285 - }
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];
286 458
287 - if (isset($triggerData['mail'])) {
288 - $formManager = new AdminFormManager($triggerData['formID']);
289 - $formContent = $formManager->getFormContent();
290 - $submitted_fields = $formContent->fields;
291 - $fieldValueForMail = FieldValueHandler::formatFieldValueForMail($submitted_fields, $workFlowReturnedData['fields']);
292 - foreach ($triggerData['mail'] as $value) {
293 - MailNotifier::notify($value, $triggerData['formID'], $fieldValueForMail, $triggerData['entryID']);
294 - }
295 - }
296 - do_action('bitforms_exec_integrations', $triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']);
459 + if (!is_array($repeatValues)) {
460 + continue;
461 + }
297 462
298 - if (function_exists('fastcgi_finish_request')) {
299 - fastcgi_finish_request();
300 - }
301 -
302 - if (defined('REST_REQUEST') || wp_doing_ajax()) {
303 - die;
304 - }
305 - return $workFlowReturnedData;
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 + }
306 471 }
472 + }
473 + }
307 474
308 - $entryID = $triggerData['entryID'];
309 - if (isset($workFlowReturnedData['cron']) && $workFlowReturnedData['cron'] && !isset($triggerData['mail']) && isset($workFlowReturnedData['integrationRun']) && $workFlowReturnedData['integrationRun']) {
310 - $eventScheuled = wp_schedule_single_event(time(), "bitforms_exec_integrations", array($triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']));
311 - $scheduleTime = wp_next_scheduled("bitforms_exec_integrations", array($triggerData['integrations'], $workFlowReturnedData['fields'], $triggerData['formID'], $triggerData['entryID'], $triggerData['logID']));
312 - $responseData['cron'] = get_site_url(null, "/wp-cron.php?doing_wp_cron&{$scheduleTime}");
313 - } else if (!empty($triggerData['integrations']) || !empty($triggerData['mail']) || isset($workFlowReturnedData['integrationRun']) && !$workFlowReturnedData['integrationRun']) {
314 - $triggerData['fields'] = $workFlowReturnedData['fields'];
315 - if (!$workFlowReturnedData['integrationRun'] && isset($workFlowReturnedData['integrationDetails'])) {
316 - $triggerData['dbl_opt_donf'] = $workFlowReturnedData['integrationDetails'];
317 - $triggerData['dbl_opt_dflt_template'] = $workFlowReturnedData['dflt_template'];
318 - $triggerData['integrationRun'] = $workFlowReturnedData['integrationRun'];
319 - }
320 - set_transient("bitform_trigger_transient_{$entryID}", $triggerData, HOUR_IN_SECONDS);
321 - $entryLog = new FormEntryLogModel();
322 - $queueuEntry = $entryLog->log_history_insert(
323 - array(
324 - "log_id" => $triggerData['logID'],
325 - "integration_id" => 0,
326 - "api_type" => wp_json_encode(["type" => "trigger", "type_name" => "Workflow", "on" => $opType]),
327 - "response_type" => "queued",
328 - "response_obj" => json_encode(['status' => 'queued']),
329 - "created_at" => current_time("mysql"),
330 - )
331 - );
332 - $responseData['cronNotOk'] = [
333 - $triggerData['entryID'],
334 - $triggerData['logID'],
335 - $queueuEntry,
336 - ];
337 - }
338 - 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 + }
339 480 }
481 + return $fieldValues;
482 + }
340 483 }