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

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