PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 1.3
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v1.3
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.3, at includes/Core/WorkFlow/WorkFlowRunHelper.php

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