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

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

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