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

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