PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 2.21.4
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v2.21.4
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
bit-form / includes / Core / WorkFlow / WorkFlow.php

WorkFlow.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 2.21.4, at includes/Core/WorkFlow/WorkFlow.php

449 lines 17.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace BitCode\BitForm\Core\WorkFlow;
4
5 use BitCode\BitForm\Admin\Form\AdminFormManager;
6 use BitCode\BitForm\Core\Database\FormEntryMetaModel;
7 use BitCode\BitForm\Core\Database\WorkFlowModel;
8 use BitCode\BitForm\Core\Integration\IntegrationHandler;
9 use BitCode\BitForm\Core\Integration\Integrations;
10 use BitCode\BitForm\Core\Util\MailNotifier;
11
12 final class WorkFlow
13 {
14 private static $_formID;
15 private static $_workFlowModel;
16 private $_actions;
17
18 private $_user_details;
19
20 public function __construct($formID, $user_details = null)
21 {
22 static::$_formID = $formID;
23 static::$_workFlowModel = new WorkFlowModel();
24 $this->_user_details = $user_details;
25 $this->_actions = new Actions($formID);
26 }
27
28 public function getWorkFlow($workFlowRun, $workFlowType, $workFlowIds = null, $orderColumn = 'id', $workFlowStatus = 1)
29 {
30 if (null === $workFlowIds) {
31 $condition = [
32 'form_id' => static::$_formID,
33 ];
34 } else {
35 $condition = [
36 'id' => [$workFlowIds],
37 ];
38 }
39 if (!empty($workFlowRun)) {
40 $condition = \array_merge(
41 $condition,
42 [
43 'workflow_run' => $workFlowRun,
44 ]
45 );
46 }
47 if (!empty($workFlowType)) {
48 if (!is_array($workFlowType)) {
49 $workFlowType = [$workFlowType];
50 }
51 $condition = \array_merge(
52 $condition,
53 [
54 'workflow_type' => $workFlowType,
55 ]
56 );
57 }
58 if (!empty($workFlowStatus)) {
59 $condition = \array_merge(
60 $condition,
61 [
62 'workflow_status' => $workFlowStatus,
63 ]
64 );
65 }
66 $workFlows = static::$_workFlowModel->get(
67 [
68 'id',
69 'workflow_type',
70 'workflow_run',
71 'workflow_behaviour',
72 'workflow_condition',
73 'workflow_name',
74 'workflow_order',
75 'workflow_status',
76 ],
77 $condition,
78 null,
79 null,
80 $orderColumn,
81 'desc'
82 );
83 if (empty($workFlows) || is_wp_error($workFlows)) {
84 return [];
85 }
86
87 return $workFlows;
88 }
89
90 public function executeOnLoad($workFlowRun, $fields)
91 {
92 $workFlows = $this->getWorkFlow(['create_edit', $workFlowRun], ['always', 'onload'], null, 'workflow_order');
93 $workFlowReturnable = [];
94 if (empty($workFlows) || is_wp_error($workFlows)) {
95 return [];
96 }
97 $workFlows = array_reverse($workFlows);
98 $fieldData = Helper::getFieldData($fields);
99 foreach ($workFlows as $workFlow) {
100 $conditions = json_decode($workFlow->workflow_condition);
101 $conditionBehaviour = $workFlow->workflow_behaviour;
102 if ('cond' === $conditionBehaviour) {
103 foreach ($conditions as $condition) {
104 $type = $condition->cond_type;
105 if (!empty($condition->actions->fields)) {
106 $conditionStatus = false;
107
108 if (!empty($condition->logics)) {
109 $logics = $condition->logics;
110 $fieldData = Helper::smartFldMargeFormFld($logics, $fieldData);
111 $conditionalLogic = new ConditionalLogic($logics, $fieldData);
112 $conditionStatus = $conditionalLogic->getConditionStatus();
113 }
114 if (($conditionStatus && in_array($type, ['if', 'else-if']) || 'else' === $type)) {
115 $data = $this->_actions->setFieldProperty($condition->actions->fields, $fieldData, $fields);
116 $fields = $data[0];
117 $fieldData = $data[1];
118 break;
119 }
120 }
121 }
122 } elseif ('always' === $conditionBehaviour) {
123 if (!empty($conditions[0]->actions->fields)) {
124 $data = $this->_actions->setFieldProperty($conditions[0]->actions->fields, $fieldData, $fields);
125 $fields = $data[0];
126 $fieldData = $data[1];
127 }
128 }
129 }
130 $workFlowReturnable['fields'] = $fields;
131
132 return $workFlowReturnable;
133 }
134
135 public function executeOnUserInput($workFlowRun)
136 {
137 $workFlows = $this->getWorkFlow(['create_edit', $workFlowRun], ['always', 'oninput'], null, 'workflow_order');
138 $workFlowReturnable = [];
139 if (empty($workFlows) || is_wp_error($workFlows)) {
140 return $workFlowReturnable;
141 }
142 $onUserInputRun = [];
143 foreach ($workFlows as $index => $value) {
144 $conditions = json_decode($value->workflow_condition);
145 $onUserInputRun['event_type'] = 'on_input';
146 $onUserInputRun['conditions'] = $conditions;
147
148 $workFlowReturnable['onfield_input_conditions'][$index] = $onUserInputRun;
149 }
150 return $workFlowReturnable;
151 }
152
153 public function executeOnValidate($workFlowRun, $fieldData, $fieldValue)
154 {
155 $workFlows = $this->getWorkFlow(['create_edit', $workFlowRun], 'onvalidate', null, 'workflow_order');
156 $workFlowReturnable = [];
157 if (empty($workFlows) || is_wp_error($workFlows)) {
158 return [];
159 }
160
161 foreach ($workFlows as $workFlow) {
162 $conditions = json_decode($workFlow->workflow_condition);
163 $conditionBehaviour = $workFlow->workflow_behaviour;
164 if ('cond' === $conditionBehaviour) {
165 foreach ($conditions as $condition) {
166 $type = $condition->cond_type;
167 if (!empty($condition->actions->failure)) {
168 $validateMsg = $condition->actions->failure;
169 } else {
170 $validateMsg = '{"id":"0"}';
171 }
172 $conditionStatus = false;
173 if (!empty($condition->logics)) {
174 $logics = $condition->logics;
175 $fieldData = Helper::smartFldMargeFormFld($logics, $fieldData);
176 $conditionalLogic = new ConditionalLogic($logics, $fieldData);
177 $conditionStatus = $conditionalLogic->getConditionStatus();
178 }
179 if (($conditionStatus && in_array($type, ['if', 'else-if'])) || 'else' === $type) {
180 $workFlowReturnable = $this->_actions->confirmationMessage($workFlowReturnable, $validateMsg, $fieldValue);
181 if (empty($workFlowReturnable)) {
182 $workFlowReturnable['message'] = '<p>Something error in Form Validation</p>';
183 }
184 break;
185 }
186 }
187 }
188 }
189 return $workFlowReturnable;
190 }
191
192 public function isExistDoubleOptin($data)
193 {
194 $data['integrationRun'] = true;
195 if (has_action('bf_double_optin_confirmation')) {
196 $activeDoubleOpt = (new IntegrationHandler(static::$_formID))->getAllIntegration('double-opt-in', 'double-opt-in', 1);
197
198 if (!is_wp_error($activeDoubleOpt) && count($activeDoubleOpt) > 0) {
199 $dplOptinDetails = json_decode($activeDoubleOpt[0]->integration_details);
200 if (isset($dplOptinDetails->disable_loggin_user) && !is_user_logged_in() || !isset($dplOptinDetails->disable_loggin_user)) {
201 $data['integrationRun'] = false;
202 $data['integrationDetails'] = $activeDoubleOpt[0];
203 $data['dflt_template'] = isset($dplOptinDetails->dflt_temp) ? true : false;
204 }
205 }
206 }
207 return $data;
208 }
209
210 public function executeOnSubmit($workFlowRun, $fields, $fieldValue, $entryID, $logID, $workflowsIds = null)
211 {
212 $workFlows = $this->getWorkFlow(['create_edit', $workFlowRun], ['onsubmit'], $workflowsIds, 'workflow_order');
213 $workFlowReturnable = [];
214 $workFlowReturnable = $this->isExistDoubleOptin($workFlowReturnable);
215 if (empty($workFlows) || is_wp_error($workFlows)) {
216 $defaultConfimation = Helper::setDefaultSubmitConfirmation('successMsg', $fieldValue, static::$_formID, 0);
217 if (empty($defaultConfimation['confirmation'])) {
218 $workFlowReturnable['message'] = 'edit' !== $workFlowRun ? __('Form Submitted Successfully', 'bit-form')
219 : __('Entry Updated Successfully', 'bit-form');
220 } else {
221 $workFlowReturnable['message'] = $defaultConfimation['confirmation'];
222 }
223 $workFlowReturnable['msg_id'] = 0;
224 $workFlowReturnable['dflt_message'] = true;
225 $defaultConfimation = Helper::setDefaultSubmitConfirmation('redirectPage', $fieldValue, static::$_formID, 0);
226
227 $workFlowReturnable['redirectPage'] = $defaultConfimation['confirmation'];
228 $data = [
229 'integrations' => [Helper::setDefaultSubmitConfirmation('webHooks', $fieldValue, static::$_formID, $logID)],
230 'entryID' => $entryID,
231 'logID' => $logID,
232 'formID' => static::$_formID,
233 ];
234 $workFlowReturnable['triggerData'] = $data;
235 $workFlowReturnable['cron'] = false;
236 return $workFlowReturnable;
237 }
238
239 $fieldData = Helper::getFieldData($fields);
240
241 $isCronOK = !defined('DOING_CRON') && wp_doing_ajax() && (!defined('DISABLE_WP_CRON') || (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON));
242 if ($isCronOK) {
243 // From wp spawn_cron()
244 $gmt_time = microtime(true);
245 $lock = get_transient('doing_cron');
246 if ($lock > $gmt_time + 10 * MINUTE_IN_SECONDS) {
247 $lock = 0;
248 }
249 if ($lock + WP_CRON_LOCK_TIMEOUT > $gmt_time) {
250 $isCronOK = false;
251 }
252 }
253
254 $onFormSuccessActionDefault = [
255 'workFlowReturnable' => $workFlowReturnable,
256 'mailData' => [],
257 'dblOptin' => [],
258 'integrationsToExc' => [],
259 'isWebHookQueued' => false,
260 ];
261
262 foreach ($workFlows as $workFlow) {
263 $conditions = json_decode($workFlow->workflow_condition);
264
265 $conditionBehaviour = $workFlow->workflow_behaviour;
266 if ('cond' === $conditionBehaviour) {
267 foreach ($conditions as $condition) {
268 $type = $condition->cond_type;
269 if (!empty($condition->actions)) {
270 $actions = $condition->actions;
271 $conditionStatus = false;
272 if (!empty($condition->logics)) {
273 $logics = $condition->logics;
274 $fieldData = Helper::smartFldMargeFormFld($logics, $fieldData);
275 $conditionalLogic = new ConditionalLogic($logics, $fieldData);
276 $conditionStatus = $conditionalLogic->getConditionStatus();
277 }
278 $conditionStatus = apply_filters('bitform_filter_workflow_condition_status', $conditionStatus, $condition, $fieldData, $this::$_formID);
279
280 if (($conditionStatus && in_array($type, ['if', 'else-if'])) || 'else' === $type) {
281 $fieldValue = $this->_actions->setOnSubmitSetFieldValue($actions->fields, $fieldValue);
282 $onFormSuccessActionDefault = $this->_actions->setOnFormSuccess($onFormSuccessActionDefault, $actions->success, $fieldValue, $entryID);
283 $onFormSuccessActionDefault['workFlowReturnable']['fields'] = $fieldValue;
284 break;
285 }
286 }
287 }
288 } elseif ('always' === $conditionBehaviour) {
289 $actions = $conditions[0]->actions;
290 $fieldValue = $this->_actions->setOnSubmitSetFieldValue($actions->fields, $fieldValue);
291 $onFormSuccessActionDefault = $this->_actions->setOnFormSuccess($onFormSuccessActionDefault, $actions->success, $fieldValue, $entryID);
292 $onFormSuccessActionDefault['workFlowReturnable']['fields'] = $fieldValue;
293 }
294 }
295 $workFlowReturnable = $onFormSuccessActionDefault['workFlowReturnable'];
296 $integrationsToExc = $onFormSuccessActionDefault['integrationsToExc'];
297 if (empty($workFlowReturnable['message'])) {
298 $defaultConfimation = Helper::setDefaultSubmitConfirmation('successMsg', $fieldValue, static::$_formID, $logID);
299 $workFlowReturnable['message'] = $defaultConfimation['confirmation'];
300 $workFlowReturnable['msg_id'] = 0;
301
302 if (empty($defaultConfimation['confirmation'])) {
303 $workFlowReturnable['message'] = 'edit' !== $workFlowRun ? __('Form Submitted Successfully', 'bit-form')
304 : __('Entry Updated Successfully', 'bit-form');
305 }
306 $workFlowReturnable['dflt_message'] = true;
307 }
308 if (empty($workFlowReturnable['redirectPage'])) {
309 $defaultConfimation = Helper::setDefaultSubmitConfirmation('redirectPage', $fieldValue, static::$_formID, $logID);
310
311 $workFlowReturnable['redirectPage'] = $defaultConfimation['confirmation'];
312 }
313 if (!$onFormSuccessActionDefault['isWebHookQueued']) {
314 //checked korte hobe
315 $webWooks = Helper::setDefaultSubmitConfirmation('webHooks', $fieldValue, 1, static::$_formID);
316 if (!empty($webWooks['confirmation'])) {
317 $integrationsToExc[] = $webWooks['confirmation'];
318 }
319 }
320
321 // if (!empty($integrationsToExc)) {
322 $data = [
323 'mail' => $onFormSuccessActionDefault['mailData'],
324 'dblOptin' => $onFormSuccessActionDefault['dblOptin'],
325 'integrations' => $integrationsToExc,
326 'entryID' => $entryID,
327 'logID' => $logID,
328 'formID' => static::$_formID,
329 ];
330 $workFlowReturnable['triggerData'] = $data;
331 if ($isCronOK) {
332 $workFlowReturnable['cron'] = true;
333 } else {
334 $workFlowReturnable['cron'] = false;
335 }
336 // }
337 return $workFlowReturnable;
338 }
339
340 public function executeOnDelete(AdminFormManager $formManager, $formID, $deletedIds)
341 {
342 $workFlows = $this->getWorkFlow(['delete'], 'delete');
343 $workFlowReturnable = [];
344 if (empty($workFlows) || is_wp_error($workFlows) || empty($deletedIds)) {
345 return [];
346 }
347 if (!$formManager instanceof AdminFormManager) {
348 $formManager = new AdminFormManager($formID);
349 }
350 $returnableEntries = $deletedIds;
351 $formFields = $formManager->getFieldLabel();
352 $entryMeta = new FormEntryMetaModel();
353 $entries = $entryMeta->getEntryMeta(
354 $formFields,
355 $deletedIds
356 );
357 if (is_wp_error($entries) || empty($entries['entries'])) {
358 return $entries;
359 }
360 foreach ($entries['entries'] as $entry) {
361 $fieldValue = (array) $entry;
362 unset($fieldValue['entry_id']);
363 $fields = $formManager->getFormContentWithValue($fieldValue)->fields;
364 $fieldData = Helper::getFieldData($fields);
365
366 foreach ($workFlows as $workFlow) {
367 $workFlowBlock = json_decode($workFlow->workflow_condition);
368 // $conditions = $workFlowBlock->logics;
369 // $actions = $workFlowBlock->actions;
370 $conditionBehaviour = $workFlow->workflow_behaviour;
371
372 if ('cond' === $conditionBehaviour) {
373 foreach ($workFlowBlock as $condition) {
374 $actions = $condition->actions;
375 $type = $condition->cond_type;
376 $logics = $condition->logics;
377 $fieldData = Helper::smartFldMargeFormFld($logics, $fieldData);
378 $conditionalLogic = new ConditionalLogic($logics, $fieldData);
379 $conditionStatus = $conditionalLogic->getConditionStatus();
380
381 $conditionStatus = apply_filters('bitform_filter_workflow_condition_status', $conditionStatus, $condition, $fieldData, $this::$_formID);
382
383 if (($conditionStatus && in_array($type, ['if', 'else-if'])) || 'else' === $type) {
384 $isExists = \array_search($entry->entry_id, $returnableEntries);
385 if (!empty($actions->avoid_delete) && false !== $isExists) {
386 unset($returnableEntries[$isExists]);
387 $returnableEntries = array_values($returnableEntries);
388 } elseif (false === $isExists) {
389 $returnableEntries[] = $entry->entry_id;
390 }
391 if (!empty($actions->success)) {
392 foreach ($actions->success as $successActionDetail) {
393 switch ($successActionDetail->type) {
394 case 'webHooks':
395 if (!empty($successActionDetail->details->id)) {
396 $webHooks = $successActionDetail->details->id;
397 Integrations::executeIntegrations($webHooks, $fieldValue, static::$_formID);
398 }
399 break;
400 case 'mailNotify':
401 if (!empty($successActionDetail->details->id)) {
402 MailNotifier::notify($successActionDetail->details, static::$_formID, $fieldValue, $entry->entry_id);
403 }
404 break;
405 default:
406 break;
407 }
408 }
409 }
410 break;
411 }
412 }
413 } elseif ('always' === $conditionBehaviour) {
414 $actions = $workFlowBlock[0]->actions;
415 $isExists = \array_search($entry->entry_id, $returnableEntries);
416 if (!empty($actions->avoid_delete) && false !== $isExists) {
417 unset($returnableEntries[$isExists]);
418 $returnableEntries = array_values($returnableEntries);
419 } elseif (false === $isExists) {
420 $returnableEntries[] = $entry->entry_id;
421 }
422 if (!empty($actions->success)) {
423 foreach ($actions->success as $successActionDetail) {
424 switch ($successActionDetail->type) {
425 case 'webHooks':
426 if (!empty($successActionDetail->details->id)) {
427 $webHooks = $successActionDetail->details->id;
428 Integrations::executeIntegrations($webHooks, $fieldValue, static::$_formID);
429 }
430 break;
431 case 'mailNotify':
432 if (!empty($successActionDetail->details->id)) {
433 MailNotifier::notify($successActionDetail->details, static::$_formID, $fieldValue, $entry->entry_id);
434 }
435 break;
436 default:
437 break;
438 }
439 }
440 }
441 }
442 }
443 }
444 // }
445 $workFlowReturnable['entries'] = array_values($returnableEntries);
446 return $workFlowReturnable;
447 }
448 }
449