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

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