PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.2
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 / WorkFlowRunHelper.php

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

980 lines 47.8 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\WorkFlowModel;
7 use BitCode\BitForm\Core\Integration\Integrations;
8 use BitCode\BitForm\Core\Database\FormEntryMetaModel;
9 use BitCode\BitForm\Core\Messages\EmailTemplateHandler;
10 use BitCode\BitForm\Core\Messages\SuccessMessageHandler;
11 use BitCode\BitForm\Core\Integration\IntegrationHandler;
12 use BitCode\BitForm\Core\Util\FieldValueHandler;
13 use BitCode\BitForm\Core\Util\MailConfig;
14
15 final class WorkFlowRunHelper
16 {
17 private static $_formID;
18 private static $_workFlowModel;
19 private $_user_details;
20
21 public function __construct($formID, $user_details = null)
22 {
23 static::$_formID = $formID;
24 static::$_workFlowModel = new WorkFlowModel();
25 $this->_user_details = $user_details;
26 }
27
28 public function getWorkFlow($workFlowRun, $workFlowType, $workFlowIds = null )
29 {
30 if($workFlowIds==null){
31 $condition = array(
32 'form_id' => static::$_formID,
33 );
34 }else{
35 $condition = array(
36 'id' => array($workFlowIds),
37 );
38 }
39 if (!empty($workFlowRun)) {
40 $condition = \array_merge(
41 $condition,
42 array(
43 'workflow_run' => $workFlowRun
44 )
45 );
46 }
47 if (!empty($workFlowType)) {
48 $condition = \array_merge(
49 $condition,
50 array(
51 'workflow_type' => $workFlowType
52 )
53 );
54 }
55 $workFlows = static::$_workFlowModel->get(
56 array(
57 'id',
58 'workflow_name',
59 'workflow_type',
60 'workflow_run',
61 'workflow_behaviour',
62 'workflow_condition',
63 'workflow_action'
64 ),
65 $condition,
66 null,
67 null,
68 'id'
69 );
70 if (empty($workFlows) || is_wp_error($workFlows)) {
71 return [];
72 }
73 return $workFlows;
74 }
75 public function executeOnLoad($workFlowRun, $fields)
76 {
77 $workFlows = $this->getWorkFlow(array("create_edit", $workFlowRun), array('onload'));
78 $workFlowReturnable = array();
79 if (empty($workFlows) || is_wp_error($workFlows)) {
80 return [];
81 }
82 $fieldData = array();
83 foreach ($fields as $fieldKey => $fieldDetail) {
84 // $fieldLabel = !empty($fieldDetail->lbl) ? preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\!]/', '_', $fieldDetail->lbl) : null;
85 $fieldData[$fieldKey] = array(
86 'key' => $fieldKey,
87 'value' => empty($fieldDetail->val) ? '' : $fieldDetail->val,
88 'type' => $fieldDetail->typ,
89 );
90 if (isset($fieldDetail->mul)) {
91 $fieldData[$fieldKey] =
92 array_merge(
93 $fieldData[$fieldKey],
94 array(
95 'mul' => true
96 )
97 );
98 }
99 }
100
101 foreach ($workFlows as $key => $value) {
102 $allAction = json_decode($value->workflow_action);
103 if ($value->workflow_behaviour === 'cond' && !$this->getConditionStatus(json_decode($value->workflow_condition), $fieldData)) {
104 continue;
105 }
106 foreach ($allAction->action as $actionKey => $actionDetail) {
107 if (!empty($actionDetail->action) && !empty($actionDetail->field)) {
108 switch ($actionDetail->action) {
109 case 'value':
110 if (!empty($actionDetail->val) && !empty($fieldData[$actionDetail->field]['key'])) {
111 $actionValue = $this->replaceFieldWithValue($actionDetail->val, $fieldData);
112 $fields->{$fieldData[$actionDetail->field]['key']}->val = $actionValue;
113 $fieldData[$actionDetail->field]['value'] = $actionValue;
114 }
115 break;
116
117 case 'hide':
118 if (!empty($fieldData[$actionDetail->field]['key'])) {
119 $fields->{$fieldData[$actionDetail->field]['key']}->valid->hide = true;
120 }
121 break;
122
123 case 'disable':
124 if (!empty($fieldData[$actionDetail->field]['key'])) {
125 $fields->{$fieldData[$actionDetail->field]['key']}->valid->disabled = true;
126 }
127 break;
128
129 case 'enable':
130 if (!empty($fieldData[$actionDetail->field]['key'])) {
131 $fields->{$fieldData[$actionDetail->field]['key']}->valid->disabled = false;
132 }
133 break;
134
135 case 'show':
136 if (!empty($fieldData[$actionDetail->field]['key'])) {
137 $fields->{$fieldData[$actionDetail->field]['key']}->valid->hide = false;
138 if ($fields->{$fieldData[$actionDetail->field]['key']}->typ === 'hidden') {
139 $fields->{$fieldData[$actionDetail->field]['key']}->typ = 'text';
140 }
141 }
142 break;
143 }
144 }
145 }
146 }
147 $workFlowReturnable['fields'] = $fields;
148 return $workFlowReturnable;
149 }
150 public function executeOnUserInput($workFlowRun, $fields)
151 {
152 $workFlows = $this->getWorkFlow(array("create_edit", $workFlowRun), array('oninput'));
153 $workFlowReturnable = array();
154 if (empty($workFlows) || is_wp_error($workFlows)) {
155 return [];
156 }
157
158 $fieldData = array();
159 foreach ($fields as $fieldKey => $fieldDetail) {
160 // if (!empty($fieldDetail->lbl)) {
161 // $fieldData[$fieldKey . preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\!]/', '_', $fieldDetail->lbl)] = $fieldKey;
162 // } else {
163 // $fieldData[$fieldKey] = $fieldKey;
164 // }
165 $fieldData[$fieldKey] = $fieldKey;
166 }
167 foreach ($workFlows as $key => $value) {
168 $allAction = json_decode($value->workflow_action);
169 $logics = json_decode($value->workflow_condition);
170 $onUserInputWorkFlow = array(
171 'logics' => $logics,
172 'actions' => $allAction->action
173 );
174 foreach ($fieldData as $fieldfName => $fieldKey) {
175 if ((strpos(wp_json_encode($logics), $fieldfName)) !== false) {
176 if (isset($workFlowReturnable['fieldToCheck'][$fieldfName])) {
177 $workFlowReturnable['fieldToCheck'][$fieldfName] = array_merge($workFlowReturnable['fieldToCheck'][$fieldfName], [$key]);
178 } else {
179 $workFlowReturnable['fieldToCheck'][$fieldfName] = [$key];
180 }
181 }
182 }
183
184 foreach ($allAction->action as $logic) {
185 if (!is_null($logic->field) && isset($fieldData[$logic->field])) {
186 $workFlowReturnable['fieldToChange'][$logic->field] = $fieldData[$logic->field];
187 }
188 }
189 $workFlowReturnable['conditional'][] = $onUserInputWorkFlow;
190 }
191 return $workFlowReturnable;
192 }
193 public function executeOnValidate($workFlowRun, $fieldData, $fieldValue)
194 {
195 $workFlows = $this->getWorkFlow(array("create_edit", $workFlowRun), 'onvalidate');
196 $workFlowReturnable = array();
197 if (empty($workFlows) || is_wp_error($workFlows)) {
198 return [];
199 }
200
201 foreach ($workFlows as $key => $value) {
202 $allAction = json_decode($value->workflow_action);
203 if ($value->workflow_behaviour === 'cond' && !$this->getConditionStatus(json_decode($value->workflow_condition), $fieldData)) {
204 continue;
205 }
206 if (is_array($allAction->validateMsg)) {
207 foreach ($allAction->validateMsg as $validateMsg) {
208 if (!empty($validateMsg)) {
209 $id = json_decode($validateMsg)->id;
210 $successMessageHandler
211 = new SuccessMessageHandler(static::$_formID);
212 $successMessage = $successMessageHandler->getAMessage($id);
213 if (!is_wp_error($successMessage) && !empty($successMessage)) {
214 $workFlowReturnable[] = $this->replaceFieldWithValue($successMessage[0]->message_content, $fieldValue);
215 }
216 }
217 }
218 } else {
219 if (!empty($allAction->validateMsg)) {
220 $id = json_decode($allAction->validateMsg)->id;
221 $successMessageHandler
222 = new SuccessMessageHandler(static::$_formID);
223 $successMessage = $successMessageHandler->getAMessage($id);
224 if (!is_wp_error($successMessage) && !empty($successMessage)) {
225 $workFlowReturnable[] = $this->replaceFieldWithValue($successMessage[0]->message_content, $fieldValue);
226 }
227 }
228 }
229 }
230 return $workFlowReturnable;
231 }
232 public function executeOnSubmit($workFlowRun, $fields, $fieldValue, $entryID, $logID, $workflowsIds = null)
233 {
234 $workFlows = $this->getWorkFlow(array("create_edit", $workFlowRun), array('onsubmit'),$workflowsIds);
235 $workFlowReturnable = array();
236 if (empty($workFlows) || is_wp_error($workFlows)) {
237 $workFlowReturnable['message'] = $this->setDefaultSubmitConfirmation('successMsg', $fieldValue);
238 if (empty($workFlowReturnable['message'])) {
239 $workFlowReturnable['message'] = $workFlowRun !== 'edit' ? __('Form Submitted Successfully', 'bitform')
240 : __('Entry Updated Successfully', 'bitform');
241 }
242 $workFlowReturnable['redirectPage'] = $this->setDefaultSubmitConfirmation('redirectPage', $fieldValue);
243 $this->setDefaultSubmitConfirmation('webHooks', $fieldValue, $logID);
244
245 return $workFlowReturnable;
246 }
247
248 $fieldData = array();
249 foreach ($fields as $fieldKey => $fieldDetail) {
250 // $fieldLabel = !empty($fieldDetail->lbl) ? preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\!]/', '_', $fieldDetail->lbl) : null;
251 $fieldData[$fieldKey] = array(
252 'key' => $fieldKey,
253 'value' => empty($fieldDetail->val) ? '' : $fieldDetail->val,
254 'type' => $fieldDetail->typ,
255 );
256 if (isset($fieldDetail->mul)) {
257 $fieldData[$fieldKey] =
258 array_merge(
259 $fieldData[$fieldKey],
260 array(
261 'mul' => true
262 )
263 );
264 }
265 }
266 $isWebHookExecuted = false;
267 $isCronOK = !defined('DOING_CRON') && wp_doing_ajax() && (!defined('DISABLE_WP_CRON') || (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON));
268 if ($isCronOK) {
269 // From wp spawn_cron()
270 $gmt_time = microtime(true);
271 $lock = get_transient('doing_cron');
272 if ($lock > $gmt_time + 10 * MINUTE_IN_SECONDS) {
273 $lock = 0;
274 }
275 if ($lock + WP_CRON_LOCK_TIMEOUT > $gmt_time) {
276 $isCronOK = false;
277 }
278 }
279 if (!$isCronOK) {
280 ignore_user_abort(true);
281 if (!headers_sent()) {
282 header("Connection: close");
283 header('Content-Type: application/json; charset=' . get_option('blog_charset'));
284 status_header(200);
285 }
286 $message = $this->setDefaultSubmitConfirmation('successMsg', $fieldValue);
287 if (empty($message)) {
288 $message = $workFlowRun !== 'edit' ? __('Form Submitted Successfully', 'bitform')
289 : __('Entry Updated Successfully', 'bitform');
290 }
291 $response = array(
292 'success' => true,
293 'data' => ['message' => $message],
294 );
295 if ($workFlowRun !== 'edit') {
296 if( $workflowsIds == null){
297 ob_start();
298 echo wp_doing_ajax() ? wp_json_encode($response) : $message;
299 ob_end_flush();
300 \flush();
301 }
302 }
303 if (function_exists('fastcgi_finish_request')) {
304 fastcgi_finish_request();
305 }
306 }
307 $integrationsToExc = [];
308 foreach ($workFlows as $key => $value) {
309 $allAction = json_decode($value->workflow_action);
310 if ($value->workflow_behaviour === 'cond' && !$this->getConditionStatus(json_decode($value->workflow_condition), $fieldData)) {
311 continue;
312 }
313 if (!empty($allAction->action)) {
314 foreach ($allAction->action as $actionKey => $actionDetail) {
315 if (!empty($actionDetail->action) && !empty($actionDetail->field)) {
316 switch ($actionDetail->action) {
317 case 'value':
318 if (!empty($actionDetail->val)) {
319 $actionValue = $this->replaceFieldWithValue($actionDetail->val, $fieldData);
320 $fieldValue[$actionDetail->field] = $actionValue;
321 }
322 break;
323 }
324 }
325 }
326 }
327 if (!empty($allAction->successAction)) {
328 foreach ($allAction->successAction as $successActionIndex => $successActionDetail) {
329 switch ($successActionDetail->type) {
330 case 'successMsg':
331 if (!empty($successActionDetail->details->id)) {
332 $id = json_decode($successActionDetail->details->id)->id;
333 $successMessageHandler
334 = new SuccessMessageHandler(static::$_formID);
335 $successMessage = $successMessageHandler->getAMessage($id);
336 if (!is_wp_error($successMessage) && !empty($successMessage)) {
337 $workFlowReturnable['message'] = $this->replaceFieldWithValue($successMessage[0]->message_content, $fieldValue);
338 }
339 }
340 break;
341 case 'redirectPage':
342 if (!empty($successActionDetail->details->id)) {
343 $id = json_decode($successActionDetail->details->id)->id;
344 $integrationHandler = new IntegrationHandler(static::$_formID);
345 $redirectPage = $integrationHandler->getAIntegration($id, 'form', 'redirectPage');
346 if (!is_wp_error($redirectPage) && !empty($redirectPage)) {
347 $url = json_decode($redirectPage[0]->integration_details)->url;
348 if (!empty($url)) {
349 $url = $this->replaceFieldWithValue($url, $fieldValue);
350 }
351 $workFlowReturnable['redirectPage'] = empty($url) ? false : esc_url_raw($url);
352 }
353 }
354 break;
355 case 'webHooks':
356 if (!empty($successActionDetail->details->id)) {
357 $webHooks = $successActionDetail->details->id;
358 if ($isCronOK) {
359 $integrationsToExc[] = $webHooks;
360 } else {
361 Integrations::executeIntegrations($webHooks, $fieldValue, static::$_formID, 0, $logID);
362 }
363 }
364 break;
365 case 'mailNotify':
366 if (!empty($successActionDetail->details->id)) {
367 $emailTemplateHandler
368 = new EmailTemplateHandler(static::$_formID);
369 if (is_string($successActionDetail->details->id)) {
370 $mailTemplateID = json_decode($successActionDetail->details->id)->id;
371 $mailTemplate = $emailTemplateHandler->getATemplate($mailTemplateID);
372 if (!is_wp_error($mailTemplate)) {
373 $mailTo = FieldValueHandler::validateMailArry($successActionDetail->details->to, $fieldValue);
374 if (!empty($mailTo)) {
375 (new MailConfig())->sendMail();
376 $mailSubject = $this->replaceFieldWithValue($mailTemplate[0]->sub, $fieldValue);
377 $mailBody = $this->replaceFieldWithValue($mailTemplate[0]->body, $fieldValue);
378
379 $mailHeaders = array(
380 // "Content-Type: text/html; charset=UTF-8",
381 );
382 if (!empty($successActionDetail->details->bcc)) {
383 $mailBCC = FieldValueHandler::validateMailArry($successActionDetail->details->bcc, $fieldValue);
384 if (is_array($mailBCC)) {
385 foreach ($mailBCC as $key => $emailAddress) {
386 $mailHeaders[] = "Bcc: " . trim($emailAddress);
387 }
388 } else {
389 $mailHeaders[] = "Bcc: " . trim($mailBCC);
390 }
391 }
392 if (!empty($successActionDetail->details->cc)) {
393 $mailCC = FieldValueHandler::validateMailArry($successActionDetail->details->cc, $fieldValue);
394 if (is_array($mailCC)) {
395 foreach ($mailCC as $key => $emailAddress) {
396 $mailHeaders[] = "Cc: " . trim($emailAddress);
397 }
398 } else {
399 $mailHeaders[] = "Cc: " . trim($mailCC);
400 }
401 }
402 add_filter('wp_mail_content_type', [$this, 'filterMailContentType']);
403 $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders);
404 remove_filter('wp_mail_content_type', [$this, 'filterMailContentType']);
405 }
406 }
407 }
408 }
409 break;
410
411 case 'integ':
412
413 if (!empty($successActionDetail->details->id)) {
414 $integrations = $successActionDetail->details->id;
415 if ($isCronOK) {
416 $integrationsToExc[] = $integrations;
417 } else {
418 Integrations::executeIntegrations($integrations, $fieldValue, static::$_formID, $entryID, $logID);
419 }
420 }
421 break;
422
423 default:
424 break;
425 }
426 }
427 }
428 $workFlowReturnable['fields'] = $fieldValue;
429 }
430
431 if (empty($workFlowReturnable['message'])) {
432 $workFlowReturnable['message'] = $this->setDefaultSubmitConfirmation('successMsg', $fieldValue);
433 if ($workFlowRun === 'create' && empty($workFlowReturnable['message'])) {
434 $workFlowReturnable['message'] = $workFlowRun !== 'edit' ? __('Form Submitted Successfully', 'bitform')
435 : __('Entry Updated Successfully', 'bitform');
436 }
437 }
438 if (empty($workFlowReturnable['redirectPage'])) {
439 $workFlowReturnable['redirectPage'] = $this->setDefaultSubmitConfirmation('redirectPage', $fieldValue);
440 }
441 if (!$isWebHookExecuted) {
442 $this->setDefaultSubmitConfirmation('webHooks', $fieldValue, 1);
443 }
444
445 if (!empty($integrationsToExc)) {
446 $eventScheuled = wp_schedule_single_event(time(), "bitforms_exec_integrations", array($integrationsToExc, $fieldValue, static::$_formID, $entryID, $logID));
447 $workFlowReturnable['cron'] = wp_next_scheduled("bitforms_exec_integrations", array($integrationsToExc, $fieldValue, static::$_formID, $entryID, $logID));
448 }
449
450 if (!$isCronOK && $workFlowRun !== 'edit') {
451 $workFlowReturnable['cronNotOk'] = true;
452 }
453 return $workFlowReturnable;
454 }
455 public function executeOnDelete(AdminFormManager $formManager, $formID, $entries)
456 {
457 $workFlows = $this->getWorkFlow(array("delete"), 'delete');
458 $workFlowReturnable = array();
459 if (empty($workFlows) || is_wp_error($workFlows) || empty($entries)) {
460 return [];
461 }
462 if (!$formManager instanceof AdminFormManager) {
463 $formManager = new AdminFormManager($formID);
464 }
465 $returnableEntries = $entries;
466 $formFields = $formManager->getFieldLabel();
467 $entryMeta = new FormEntryMetaModel();
468 $entryDetails = new \stdClass;
469 foreach ($entries as $key => $entryID) {
470 $entryDetails->id = $entryID;
471 $entryValues = $entryMeta->getEntryMeta(
472 $formFields,
473 [$entryDetails]
474 );
475 if (!empty($entryValues['entries'][0])) {
476 $fieldValue = (array)$entryValues['entries'][0];
477 unset($fieldValue['entry_id']);
478 $fields = $formManager->getFormContentWithValue($fieldValue)->fields;
479 $fieldData = array();
480 foreach ($fields as $fieldKey => $fieldDetail) {
481 // $fieldLabel = !empty($fieldDetail->lbl) ? preg_replace('/[\`\~\!\@\#\$\'\.\s\?\+\-\*\&\|\/\\!]/', '_', $fieldDetail->lbl) : null;
482 $fieldData[$fieldKey] = array(
483 'key' => $fieldKey,
484 'value' => empty($fieldDetail->val) ? '' : $fieldDetail->val,
485 'type' => $fieldDetail->typ,
486 );
487 if (isset($fieldDetail->mul)) {
488 $fieldData[$fieldKey] =
489 array_merge(
490 $fieldData[$fieldKey],
491 array(
492 'mul' => true
493 )
494 );
495 }
496 }
497
498 foreach ($workFlows as $key => $value) {
499 $allAction = json_decode($value->workflow_action);
500 if ($value->workflow_behaviour === 'cond' && !$this->getConditionStatus(json_decode($value->workflow_condition), $fieldData)) {
501 continue;
502 }
503 $isExists = \array_search($entryID, $returnableEntries);
504 if (!empty($allAction->avoid_delete) && $isExists !== false) {
505 unset($returnableEntries[$isExists]);
506 $returnableEntries = array_values($returnableEntries);
507 } elseif ($isExists === false) {
508 $returnableEntries[] = $entryID;
509 }
510 if (!empty($allAction->successAction)) {
511 foreach ($allAction->successAction as $successActionIndex => $successActionDetail) {
512 switch ($successActionDetail->type) {
513 case 'webHooks':
514 if (!empty($successActionDetail->details->id)) {
515 $webHooks = $successActionDetail->details->id;
516 Integrations::executeIntegrations($webHooks, $fieldValue, static::$_formID);
517 }
518 break;
519 case 'mailNotify':
520 if (!empty($successActionDetail->details->id)) {
521 $emailTemplateHandler
522 = new EmailTemplateHandler(static::$_formID);
523 if (is_string($successActionDetail->details->id)) {
524 $mailTemplateID = json_decode($successActionDetail->details->id)->id;
525 $mailTemplate = $emailTemplateHandler->getATemplate($mailTemplateID);
526 if (!is_wp_error($mailTemplate)) {
527 $mailTo = FieldValueHandler::validateMailArry($successActionDetail->details->to, $fieldValue);
528 if (!empty($mailTo)) {
529 (new MailConfig())->sendMail();
530 $mailSubject = $this->replaceFieldWithValue($mailTemplate[0]->sub, $fieldValue);
531 $mailBody = $this->replaceFieldWithValue($mailTemplate[0]->body, $fieldValue);
532
533 $mailHeaders = array();
534
535 if (!empty($successActionDetail->details->bcc)) {
536 $mailBCC = FieldValueHandler::validateMailArry($successActionDetail->details->bcc, $fieldValue);
537 if (is_array($mailBCC)) {
538 foreach ($mailBCC as $key => $emailAddress) {
539 $mailHeaders[] = "Bcc: " . trim($emailAddress);
540 }
541 } else {
542 $mailHeaders[] = "Bcc: " . trim($mailBCC);
543 }
544 }
545 if (!empty($successActionDetail->details->cc)) {
546 $mailCC = FieldValueHandler::validateMailArry($successActionDetail->details->cc, $fieldValue);
547 if (is_array($mailCC)) {
548 foreach ($mailCC as $key => $emailAddress) {
549 $mailHeaders[] = "Cc: " . trim($emailAddress);
550 }
551 } else {
552 $mailHeaders[] = "Cc: " . trim($mailCC);
553 }
554 }
555 add_filter('wp_mail_content_type', [$this, 'filterMailContentType']);
556 $status = wp_mail($mailTo, $mailSubject, $mailBody, $mailHeaders);
557 remove_filter('wp_mail_content_type', [$this, 'filterMailContentType']);
558 }
559 }
560 }
561 }
562 break;
563 default:
564 break;
565 }
566 }
567 }
568 }
569 }
570 }
571 $workFlowReturnable['entries'] = array_values($returnableEntries);
572 return $workFlowReturnable;
573 }
574
575 private function getConditionStatus($workFlowCondition, $data)
576 {
577 if (is_array($workFlowCondition)) {
578 foreach ($workFlowCondition as $sskey => $ssvalue) {
579 if (!is_string($ssvalue)) {
580 $isCondition = $this->getConditionStatus($ssvalue, $data);
581 if ($sskey === 0) {
582 $conditionSatus = $isCondition;
583 }
584 if ($sskey - 1 >= 0 && is_string($workFlowCondition[$sskey - 1])) {
585 switch (strtolower($workFlowCondition[$sskey - 1])) {
586 case 'or':
587 $conditionSatus = $conditionSatus || $isCondition;
588 break;
589
590 case 'and':
591 $conditionSatus = $conditionSatus && $isCondition;
592 break;
593
594 default:
595 break;
596 }
597 }
598 }
599 }
600 return (bool) $conditionSatus;
601 } else {
602 $workFlowCondition->val = $this->replaceFieldWithValue($workFlowCondition->val, $data);
603 switch ($workFlowCondition->logic) {
604 case 'equal':
605 if ((isset($data[$workFlowCondition->field]['mul']) && $data[$workFlowCondition->field]['mul'])
606 || $data[$workFlowCondition->field]['type'] === 'check'
607 ) {
608 $fieldValue = !empty($data[$workFlowCondition->field]['value']) ? $data[$workFlowCondition->field]['value'] : [];
609 if (is_string($fieldValue)) {
610 if ($fieldValue[0] === '[' && $fieldValue[strlen($fieldValue) - 1] === ']') {
611 $fieldValue = json_decode($fieldValue);
612 } else {
613 $fieldValue = explode(',', $fieldValue);
614 }
615 }
616 $valueToCheck = \explode(',', $workFlowCondition->val);
617 if (count($valueToCheck) !== count($fieldValue)) {
618 return false;
619 }
620 $checker = 0;
621 foreach ($valueToCheck as $key => $value) {
622 if (!empty($fieldValue) && \in_array($value, $fieldValue)) {
623 $checker = $checker + 1;
624 }
625 }
626 if ($checker === count($valueToCheck) && count($valueToCheck) === count($fieldValue)) {
627 return true;
628 }
629 return false;
630 }
631 return $data[$workFlowCondition->field]['value'] === $workFlowCondition->val;
632
633 case 'not_equal':
634 if ((isset($data[$workFlowCondition->field]['mul']) && $data[$workFlowCondition->field]['mul'])
635 || $data[$workFlowCondition->field]['type'] === 'check'
636 ) {
637 $fieldValue = !empty($data[$workFlowCondition->field]['value']) ? $data[$workFlowCondition->field]['value'] : [];
638 if (is_string($fieldValue)) {
639 if ($fieldValue[0] === '[' && $fieldValue[strlen($fieldValue) - 1] === ']') {
640 $fieldValue = json_decode($fieldValue);
641 } else {
642 $fieldValue = explode(',', $fieldValue);
643 }
644 }
645 $valueToCheck = \explode(',', $workFlowCondition->val);
646 $valueToCheckLenght = count($valueToCheck);
647 if ($valueToCheckLenght !== count($fieldValue)) {
648 return true;
649 }
650 $checker = 0;
651 foreach ($valueToCheck as $key => $value) {
652 if (!in_array($value, $fieldValue)) {
653 $checker += 1;
654 // var_dump($checker, $valueToCheckLenght, $fieldValue);
655 }
656 }
657 return $valueToCheckLenght === $checker;
658 }
659 return $data[$workFlowCondition->field]['value'] !== $workFlowCondition->val;
660
661 case 'null':
662 // var_dump('null',empty($data[$workFlowCondition->field]['value']));
663 return empty($data[$workFlowCondition->field]['value']);
664
665 case 'not_null':
666 // var_dump('!null',$data[$workFlowCondition->field]['value']);
667 return !empty($data[$workFlowCondition->field]['value']);
668
669 case 'contain':
670 if (!isset($data[$workFlowCondition->field]['value'])) {
671 return false;
672 }
673 if ((isset($data[$workFlowCondition->field]['mul']) && $data[$workFlowCondition->field]['mul'])
674 || $data[$workFlowCondition->field]['type'] === 'check'
675 ) {
676 $fieldValue = !empty($data[$workFlowCondition->field]['value']) ? $data[$workFlowCondition->field]['value'] : [];
677 if (is_string($fieldValue)) {
678 if ($fieldValue[0] === '[' && $fieldValue[strlen($fieldValue) - 1] === ']') {
679 $fieldValue = json_decode($fieldValue);
680 } else {
681 $fieldValue = explode(',', $fieldValue);
682 }
683 }
684 $valueToCheck = \explode(',', $workFlowCondition->val);
685 $checker = 0;
686 foreach ($valueToCheck as $key => $value) {
687 if (\in_array($value, $fieldValue)) {
688 $checker = $checker + 1;
689 }
690 }
691 if ($checker > 0) {
692 return true;
693 }
694 return false;
695 }
696 return isset($data[$workFlowCondition->field]['value']) && stripos($data[$workFlowCondition->field]['value'], $workFlowCondition->val) !== false;
697
698 case 'contain_all':
699 if (!isset($data[$workFlowCondition->field]['value'])) {
700 return false;
701 }
702 if ((isset($data[$workFlowCondition->field]['mul']) && $data[$workFlowCondition->field]['mul'])
703 || $data[$workFlowCondition->field]['type'] === 'check'
704 ) {
705 $fieldValue = !empty($data[$workFlowCondition->field]['value']) ? $data[$workFlowCondition->field]['value'] : [];
706 if (is_string($fieldValue)) {
707 if ($fieldValue[0] === '[' && $fieldValue[strlen($fieldValue) - 1] === ']') {
708 $fieldValue = json_decode($fieldValue);
709 } else {
710 $fieldValue = explode(',', $fieldValue);
711 }
712 }
713 $valueToCheck = \explode(',', $workFlowCondition->val);
714 $checker = 0;
715 foreach ($valueToCheck as $key => $value) {
716 if (\in_array($value, $fieldValue)) {
717 $checker = $checker + 1;
718 }
719 }
720 if ($checker >= count($valueToCheck)) {
721 return true;
722 }
723 return false;
724 }
725 return isset($data[$workFlowCondition->field]['value']) && stripos($data[$workFlowCondition->field]['value'], $workFlowCondition->val) !== false;
726
727 case 'not_contain':
728 if (!isset($data[$workFlowCondition->field]['value'])) {
729 return false;
730 }
731 if ((isset($data[$workFlowCondition->field]['mul']) && $data[$workFlowCondition->field]['mul'])
732 || $data[$workFlowCondition->field]['type'] === 'check'
733 ) {
734 $fieldValue = !empty($data[$workFlowCondition->field]['value']) ? $data[$workFlowCondition->field]['value'] : [];
735 if (is_string($fieldValue)) {
736 if ($fieldValue[0] === '[' && $fieldValue[strlen($fieldValue) - 1] === ']') {
737 $fieldValue = json_decode($fieldValue);
738 } else {
739 $fieldValue = explode(',', $fieldValue);
740 }
741 }
742 $valueToCheck = \explode(',', $workFlowCondition->val);
743 $checker = 0;
744 foreach ($valueToCheck as $key => $value) {
745 if (!in_array($value, $fieldValue)) {
746 $checker = $checker + 1;
747 }
748 }
749 if ($checker === count($valueToCheck)) {
750 return true;
751 }
752 return false;
753 }
754 return stripos($data[$workFlowCondition->field]['value'], $workFlowCondition->val) === false;
755
756 case 'greater':
757 if (!isset($data[$workFlowCondition->field]['value'])) {
758 return false;
759 }
760 return isset($data[$workFlowCondition->field]['value']) && $data[$workFlowCondition->field]['value'] > $workFlowCondition->val;
761
762 case 'less':
763 if (!isset($data[$workFlowCondition->field]['value'])) {
764 return false;
765 }
766 return isset($data[$workFlowCondition->field]['value']) && $data[$workFlowCondition->field]['value'] < $workFlowCondition->val;
767
768 case 'greater_or_equal':
769 if (!isset($data[$workFlowCondition->field]['value'])) {
770 return false;
771 }
772 return isset($data[$workFlowCondition->field]['value']) && $data[$workFlowCondition->field]['value'] >= $workFlowCondition->val;
773
774 case 'less_or_equal':
775 if (!isset($data[$workFlowCondition->field]['value'])) {
776 return false;
777 }
778 return isset($data[$workFlowCondition->field]['value']) && $data[$workFlowCondition->field]['value'] <= $workFlowCondition->val;
779
780 case 'start_with':
781 if (!isset($data[$workFlowCondition->field]['value'])) {
782 return false;
783 }
784 return isset($data[$workFlowCondition->field]['value']) && stripos($data[$workFlowCondition->field]['value'], $workFlowCondition->val) === 0;
785
786 case 'end_with':
787 if (!isset($data[$workFlowCondition->field]['value'])) {
788 return false;
789 }
790 $fieldValue = $data[$workFlowCondition->field]['value'];
791 $fieldValueLength = strlen($data[$workFlowCondition->field]['value']);
792 $compareValue = strtolower($workFlowCondition->val);
793 $compareValueLength = strlen($workFlowCondition->val);
794 $fieldValueEnds = strtolower(substr($fieldValue, $fieldValueLength - $compareValueLength, $fieldValueLength));
795 return $compareValue === $fieldValueEnds;
796
797
798 default:
799 return false;
800 }
801 }
802 }
803
804 private function replaceFieldWithValue($stringToReplaceField, $fieldValues)
805 {
806 $stringToReplaceField = FieldValueHandler::replaceFieldWithValue($stringToReplaceField, $fieldValues);
807 return $this->evalMathExpression($stringToReplaceField);
808 }
809
810 private function evalMathExpression($stringWithFieldValue)
811 {
812 $mathExpr = $stringWithFieldValue;
813 if (empty($mathExpr)) {
814 return $stringWithFieldValue;
815 }
816 preg_match_all('/[\+\-\*\/\s]+/', $mathExpr, $isMathExpr);
817 if (empty($isMathExpr[0])) {
818 return $stringWithFieldValue;
819 }
820 preg_match_all('/\w+/', $mathExpr, $exprValues);
821 if (empty($exprValues[0])) {
822 return $stringWithFieldValue;
823 }
824
825 foreach ($exprValues[0] as $opreands) {
826 if (!is_numeric($opreands)) {
827 return $stringWithFieldValue;
828 }
829 }
830 $validOperator = ['+', '-', '*', '^', '/'];
831 foreach ($isMathExpr[0] as $key => $value) {
832 if (!in_array(trim($value), $validOperator)) {
833 return $stringWithFieldValue;
834 }
835 }
836 $mathExpr = str_replace(' ', null, $mathExpr);
837 $mathExpr = preg_replace('/\{|\[|\(/', '(', $mathExpr);
838 $mathExpr = preg_replace('/\}|\]/', ')', $mathExpr);
839 $calculated = $this->infixToPostfixEvalute($mathExpr);
840 if (!is_null($calculated)) {
841 return $calculated[0];
842 }
843 return $stringWithFieldValue;
844 }
845
846 private function setDefaultSubmitConfirmation($confirmationType, $fieldValue, $logID = 0)
847 {
848 $returnableData = null;
849 switch ($confirmationType) {
850 case 'successMsg':
851 $successMessageHandler
852 = new SuccessMessageHandler(static::$_formID);
853 $successMessage = $successMessageHandler->getAllMessage();
854 if (!is_wp_error($successMessage) && !empty($successMessage) && count($successMessage) > 0) {
855 $returnableData = $this->replaceFieldWithValue($successMessage[0]->message_content, $fieldValue);
856 }
857 break;
858 case 'redirectPage':
859 $integrationHandler = new IntegrationHandler(static::$_formID);
860 $redirectPage = $integrationHandler->getAllIntegration('form', 'redirectPage');
861 if (!is_wp_error($redirectPage) && !empty($redirectPage) && count($redirectPage) > 0) {
862 $url = json_decode($redirectPage[0]->integration_details)->url;
863 if (!empty($url)) {
864 $url = $this->replaceFieldWithValue($url, $fieldValue);
865 }
866 $returnableData = empty($url) ? '' : esc_url_raw($url);
867 }
868 break;
869 case 'webHooks':
870 $integrationHandler = new IntegrationHandler(static::$_formID);
871 $webHooks = $integrationHandler->getAllIntegration('form', 'webHooks');
872 if (!is_wp_error($webHooks) && !empty($webHooks) && count($webHooks) > 0) {
873 Integrations::executeIntegrations(["{\"id\":{$webHooks[0]->id}}"], $fieldValue, static::$_formID, 0, $logID);
874 }
875 break;
876 default:
877 break;
878 }
879
880 return $returnableData;
881 }
882
883 public function infixToPostfixEvalute($expression)
884 {
885 $operatorStack = [];
886 $outputQueue = [];
887 $numTemp = null;
888 for ($strIndex = 0; $strIndex < strlen($expression); $strIndex++) {
889 $token = $expression[$strIndex];
890 if ($token === '+' || $token === '-' || $token === '*' || $token === '/' || $token === '^' || $token === '(' || $token === ')') {
891 if (!is_null($numTemp)) {
892 $outputQueue[] = $numTemp;
893 $numTemp = null;
894 }
895 $stackSize = count($operatorStack);
896 if ($stackSize) {
897 $stackTop = $operatorStack[$stackSize - 1];
898 }
899 if ($token === '(') {
900 $operatorStack[] = $token;
901 } elseif ($token === ')') {
902 while ($operatorStack[count($operatorStack) - 1] !== '(') {
903 $outputQueue[] = array_pop($operatorStack);
904 if ($operatorStack[count($operatorStack) - 1] === '(') {
905 array_pop($operatorStack);
906 break;
907 }
908 }
909 } elseif (isset($stackTop) && $this->operatorPrecedence($token) > $this->operatorPrecedence($stackTop)) {
910 $operatorStack[] = $token;
911 } elseif ($token !== '^' && $stackSize) {
912 $operatorStack[$stackSize - 1] = $token;
913 $outputQueue[] = $stackTop;
914 } else {
915 $operatorStack[] = $token;
916 }
917 continue;
918 }
919 $numTemp .= $token;
920 if ($strIndex === strlen($expression) - 1 && !is_null($numTemp)) {
921 $outputQueue[] = $numTemp;
922 }
923 }
924
925 if (!is_null($operatorStack)) {
926 $outputQueue = array_merge($outputQueue, array_reverse($operatorStack));
927 }
928 // print_r($outputQueue);
929 // print_r($operatorStack);
930 $resultStack = [];
931 foreach ($outputQueue as $value) {
932 if (is_numeric($value)) {
933 $resultStack[] = $value;
934 continue;
935 }
936 $secondOperand = array_pop($resultStack);
937 $firstOperand = array_pop($resultStack);
938 $resultStack[] = $this->calculte($firstOperand, $secondOperand, $value);
939 }
940 return $resultStack;
941 }
942
943 public function operatorPrecedence($operator)
944 {
945 switch ($operator) {
946 case '^':
947 return 4;
948 case '*':
949 case '/':
950 return 3;
951 case '+':
952 case '-':
953 return 2;
954 default:
955 return 0;
956 }
957 }
958
959 public function calculte($firstOperand, $secondOperand, $operator)
960 {
961 switch ($operator) {
962 case '+':
963 return $firstOperand + $secondOperand;
964 case '-':
965 return $firstOperand - $secondOperand;
966 case '*':
967 return $firstOperand * $secondOperand;
968 case '/':
969 return $firstOperand / $secondOperand;
970 case '^':
971 return $firstOperand ** $secondOperand;
972 }
973 }
974
975 public function filterMailContentType()
976 {
977 return 'text/html';
978 }
979 }
980