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

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