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

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