PluginProbe
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder / 3.1.2
Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder v3.1.2
V-3.3.0 3.2.2 3.2.1 3.2.0 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 V3.0.3 V3.0.2 -3.0.1 V_3.0.0 1.1.1 1.1.8 1.2 1.3 1.4 1.4.18 1.5.2 1.9 2.0 2.10.0 2.10.1 2.10.2 All 137 releases
bit-form / includes / Core / WorkFlow / WorkFlow.php

WorkFlow.php in Bit Form – Contact Form, Payment Forms, Multi Step Forms, Calculator & Custom Form Builder 3.1.2, at includes/Core/WorkFlow/WorkFlow.php

450 lines 17.2 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\Util\MailNotifier;
11
12 final class WorkFlow
13 {
14 private static $_formID;
15 private static $_workFlowModel;
16 private $_actions;
17
18 private $_user_details;
19
20 public function __construct($formID, $user_details = null)
21 {
22 static::$_formID = $formID;
23 static::$_workFlowModel = new WorkFlowModel();
24 $this->_user_details = $user_details;
25 $this->_actions = new Actions($formID);
26 }
27
28 public function getWorkFlow($workFlowRun, $workFlowType, $workFlowIds = null, $orderColumn = 'id', $workFlowStatus = 1)
29 {
30 if (null === $workFlowIds) {
31 $condition = [
32 'form_id' => static::$_formID,
33 ];
34 } else {
35 $condition = [
36 'id' => $workFlowIds,
37 ];
38 }
39 if (!empty($workFlowRun)) {
40 $condition = \array_merge(
41 $condition,
42 [
43 'workflow_run' => $workFlowRun,
44 ]
45 );
46 }
47 if (!empty($workFlowType)) {
48 if (!is_array($workFlowType)) {
49 $workFlowType = [$workFlowType];
50 }
51 $condition = \array_merge(
52 $condition,
53 [
54 'workflow_type' => $workFlowType,
55 ]
56 );
57 }
58 if (!empty($workFlowStatus)) {
59 $condition = \array_merge(
60 $condition,
61 [
62 'workflow_status' => $workFlowStatus,
63 ]
64 );
65 }
66 $workFlows = static::$_workFlowModel->get(
67 [
68 'id',
69 'workflow_type',
70 'workflow_run',
71 'workflow_behaviour',
72 'workflow_condition',
73 'workflow_info',
74 'workflow_name',
75 'workflow_order',
76 'workflow_status',
77 ],
78 $condition,
79 null,
80 null,
81 $orderColumn,
82 'desc'
83 );
84 if (empty($workFlows) || is_wp_error($workFlows)) {
85 return [];
86 }
87
88 return $workFlows;
89 }
90
91 public function executeOnLoad($workFlowRun, $fields)
92 {
93 $workFlows = $this->getWorkFlow(['create_edit', $workFlowRun], ['always', 'onload'], null, 'workflow_order');
94 $workFlowReturnable = [];
95 if (empty($workFlows) || is_wp_error($workFlows)) {
96 return [];
97 }
98 $workFlows = array_reverse($workFlows);
99 $fieldData = Helper::getFieldData($fields);
100 foreach ($workFlows as $workFlow) {
101 $conditions = json_decode($workFlow->workflow_condition);
102 $conditionBehaviour = $workFlow->workflow_behaviour;
103 if ('cond' === $conditionBehaviour) {
104 foreach ($conditions as $condition) {
105 $type = $condition->cond_type;
106 if (!empty($condition->actions->fields)) {
107 $conditionStatus = false;
108
109 if (!empty($condition->logics)) {
110 $logics = $condition->logics;
111 $fieldData = Helper::smartFldMargeFormFld($logics, $fieldData);
112 $conditionalLogic = new ConditionalLogic($logics, $fieldData);
113 $conditionStatus = $conditionalLogic->getConditionStatus();
114 }
115 if (($conditionStatus && in_array($type, ['if', 'else-if']) || 'else' === $type)) {
116 $data = $this->_actions->setFieldProperty($condition->actions->fields, $fieldData, $fields);
117 $fields = $data[0];
118 $fieldData = $data[1];
119 break;
120 }
121 }
122 }
123 } elseif ('always' === $conditionBehaviour) {
124 if (!empty($conditions[0]->actions->fields)) {
125 $data = $this->_actions->setFieldProperty($conditions[0]->actions->fields, $fieldData, $fields);
126 $fields = $data[0];
127 $fieldData = $data[1];
128 }
129 }
130 }
131 $workFlowReturnable['fields'] = $fields;
132
133 return $workFlowReturnable;
134 }
135
136 public function executeOnUserInput($workFlowRun)
137 {
138 $workFlows = $this->getWorkFlow(['create_edit', $workFlowRun], ['always', 'oninput'], null, 'workflow_order');
139 $workFlowReturnable = [];
140 if (empty($workFlows) || is_wp_error($workFlows)) {
141 return $workFlowReturnable;
142 }
143 $onUserInputRun = [];
144 foreach ($workFlows as $index => $value) {
145 $conditions = json_decode($value->workflow_condition);
146 $onUserInputRun['event_type'] = 'on_input';
147 $onUserInputRun['conditions'] = $conditions;
148
149 $workFlowReturnable['onfield_input_conditions'][$index] = $onUserInputRun;
150 }
151 return $workFlowReturnable;
152 }
153
154 public function executeOnValidate($workFlowRun, $fieldData, $fieldValue)
155 {
156 $workFlows = $this->getWorkFlow(['create_edit', $workFlowRun], 'onvalidate', null, 'workflow_order');
157 $workFlowReturnable = [];
158 if (empty($workFlows) || is_wp_error($workFlows)) {
159 return [];
160 }
161
162 foreach ($workFlows as $workFlow) {
163 $conditions = json_decode($workFlow->workflow_condition);
164 $conditionBehaviour = $workFlow->workflow_behaviour;
165 if ('cond' === $conditionBehaviour) {
166 foreach ($conditions as $condition) {
167 $type = $condition->cond_type;
168 if (!empty($condition->actions->failure)) {
169 $validateMsg = $condition->actions->failure;
170 } else {
171 $validateMsg = '{"id":"0"}';
172 }
173 $conditionStatus = false;
174 if (!empty($condition->logics)) {
175 $logics = $condition->logics;
176 $fieldData = Helper::smartFldMargeFormFld($logics, $fieldData);
177 $conditionalLogic = new ConditionalLogic($logics, $fieldData);
178 $conditionStatus = $conditionalLogic->getConditionStatus();
179 }
180 if (($conditionStatus && in_array($type, ['if', 'else-if'])) || 'else' === $type) {
181 $workFlowReturnable = $this->_actions->confirmationMessage($workFlowReturnable, $validateMsg, $fieldValue);
182 if (empty($workFlowReturnable)) {
183 $workFlowReturnable['message'] = '<p>Something error in Form Validation</p>';
184 }
185 break;
186 }
187 }
188 }
189 }
190 return $workFlowReturnable;
191 }
192
193 public function isExistDoubleOptin($data)
194 {
195 $data['integrationRun'] = true;
196 if (has_action('bitform_double_optin_confirmation')) {
197 $activeDoubleOpt = (new IntegrationHandler(static::$_formID))->getAllIntegration('double-opt-in', 'double-opt-in', 1);
198
199 if (!is_wp_error($activeDoubleOpt) && count($activeDoubleOpt) > 0) {
200 $dplOptinDetails = json_decode($activeDoubleOpt[0]->integration_details);
201 if (isset($dplOptinDetails->disable_loggin_user) && !is_user_logged_in() || !isset($dplOptinDetails->disable_loggin_user)) {
202 $data['integrationRun'] = false;
203 $data['integrationDetails'] = $activeDoubleOpt[0];
204 $data['dflt_template'] = isset($dplOptinDetails->dflt_temp) ? true : false;
205 }
206 }
207 }
208 return $data;
209 }
210
211 public function executeOnSubmit($workFlowRun, $fields, $fieldValue, $entryID, $logID, $workflowsIds = null)
212 {
213 $workFlows = $this->getWorkFlow(['create_edit', $workFlowRun], ['onsubmit'], $workflowsIds, 'workflow_order');
214 $workFlowReturnable = [];
215 $workFlowReturnable = $this->isExistDoubleOptin($workFlowReturnable);
216 if (empty($workFlows) || is_wp_error($workFlows)) {
217 $defaultConfimation = Helper::setDefaultSubmitConfirmation('successMsg', $fieldValue, static::$_formID, 0);
218 if (empty($defaultConfimation['confirmation'])) {
219 $workFlowReturnable['message'] = 'edit' !== $workFlowRun ? __('Form Submitted Successfully', 'bit-form')
220 : __('Entry Updated Successfully', 'bit-form');
221 } else {
222 $workFlowReturnable['message'] = $defaultConfimation['confirmation'];
223 }
224 $workFlowReturnable['msg_id'] = 0;
225 $workFlowReturnable['dflt_message'] = true;
226 $defaultConfimation = Helper::setDefaultSubmitConfirmation('redirectPage', $fieldValue, static::$_formID, 0);
227
228 $workFlowReturnable['redirectPage'] = $defaultConfimation['confirmation'];
229 $data = [
230 'integrations' => [Helper::setDefaultSubmitConfirmation('webHooks', $fieldValue, static::$_formID, $logID)],
231 'entryID' => $entryID,
232 'logID' => $logID,
233 'formID' => static::$_formID,
234 ];
235 $workFlowReturnable['triggerData'] = $data;
236 $workFlowReturnable['cron'] = false;
237 return $workFlowReturnable;
238 }
239
240 $fieldData = Helper::getFieldData($fields);
241
242 $isCronOK = !defined('DOING_CRON') && wp_doing_ajax() && (!defined('DISABLE_WP_CRON') || (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON));
243 if ($isCronOK) {
244 // From wp spawn_cron()
245 $gmt_time = microtime(true);
246 $lock = get_transient('doing_cron');
247 if ($lock > $gmt_time + 10 * MINUTE_IN_SECONDS) {
248 $lock = 0;
249 }
250 if ($lock + WP_CRON_LOCK_TIMEOUT > $gmt_time) {
251 $isCronOK = false;
252 }
253 }
254
255 $onFormSuccessActionDefault = [
256 'workFlowReturnable' => $workFlowReturnable,
257 'mailData' => [],
258 'dblOptin' => [],
259 'integrationsToExc' => [],
260 'isWebHookQueued' => false,
261 ];
262
263 foreach ($workFlows as $workFlow) {
264 $conditions = json_decode($workFlow->workflow_condition);
265
266 $conditionBehaviour = $workFlow->workflow_behaviour;
267 if ('cond' === $conditionBehaviour) {
268 foreach ($conditions as $condition) {
269 $type = $condition->cond_type;
270 if (!empty($condition->actions)) {
271 $actions = $condition->actions;
272 $conditionStatus = false;
273 if (!empty($condition->logics)) {
274 $logics = $condition->logics;
275 $fieldData = Helper::smartFldMargeFormFld($logics, $fieldData);
276 $conditionalLogic = new ConditionalLogic($logics, $fieldData);
277 $conditionStatus = $conditionalLogic->getConditionStatus();
278 }
279 $conditionStatus = apply_filters('bitform_filter_workflow_condition_status', $conditionStatus, $condition, $fieldData, $this::$_formID);
280
281 if (($conditionStatus && in_array($type, ['if', 'else-if'])) || 'else' === $type) {
282 $fieldValue = $this->_actions->setOnSubmitSetFieldValue($actions->fields, $fieldValue);
283 $onFormSuccessActionDefault = $this->_actions->setOnFormSuccess($onFormSuccessActionDefault, $actions->success, $fieldValue, $entryID);
284 $onFormSuccessActionDefault['workFlowReturnable']['fields'] = $fieldValue;
285 break;
286 }
287 }
288 }
289 } elseif ('always' === $conditionBehaviour) {
290 $actions = $conditions[0]->actions;
291 $fieldValue = $this->_actions->setOnSubmitSetFieldValue($actions->fields, $fieldValue);
292 $onFormSuccessActionDefault = $this->_actions->setOnFormSuccess($onFormSuccessActionDefault, $actions->success, $fieldValue, $entryID);
293 $onFormSuccessActionDefault['workFlowReturnable']['fields'] = $fieldValue;
294 }
295 }
296 $workFlowReturnable = $onFormSuccessActionDefault['workFlowReturnable'];
297 $integrationsToExc = $onFormSuccessActionDefault['integrationsToExc'];
298 if (empty($workFlowReturnable['message'])) {
299 $defaultConfimation = Helper::setDefaultSubmitConfirmation('successMsg', $fieldValue, static::$_formID, $logID);
300 $workFlowReturnable['message'] = $defaultConfimation['confirmation'];
301 $workFlowReturnable['msg_id'] = 0;
302
303 if (empty($defaultConfimation['confirmation'])) {
304 $workFlowReturnable['message'] = 'edit' !== $workFlowRun ? __('Form Submitted Successfully', 'bit-form')
305 : __('Entry Updated Successfully', 'bit-form');
306 }
307 $workFlowReturnable['dflt_message'] = true;
308 }
309 if (empty($workFlowReturnable['redirectPage'])) {
310 $defaultConfimation = Helper::setDefaultSubmitConfirmation('redirectPage', $fieldValue, static::$_formID, $logID);
311
312 $workFlowReturnable['redirectPage'] = $defaultConfimation['confirmation'];
313 }
314 if (!$onFormSuccessActionDefault['isWebHookQueued']) {
315 //checked korte hobe
316 $webWooks = Helper::setDefaultSubmitConfirmation('webHooks', $fieldValue, 1, static::$_formID);
317 if (!empty($webWooks['confirmation'])) {
318 $integrationsToExc[] = $webWooks['confirmation'];
319 }
320 }
321
322 // if (!empty($integrationsToExc)) {
323 $data = [
324 'mail' => $onFormSuccessActionDefault['mailData'],
325 'dblOptin' => $onFormSuccessActionDefault['dblOptin'],
326 'integrations' => $integrationsToExc,
327 'entryID' => $entryID,
328 'logID' => $logID,
329 'formID' => static::$_formID,
330 ];
331 $workFlowReturnable['triggerData'] = $data;
332 if ($isCronOK) {
333 $workFlowReturnable['cron'] = true;
334 } else {
335 $workFlowReturnable['cron'] = false;
336 }
337 // }
338 return $workFlowReturnable;
339 }
340
341 public function executeOnDelete(AdminFormManager $formManager, $formID, $deletedIds)
342 {
343 $workFlows = $this->getWorkFlow(['delete'], 'delete');
344 $workFlowReturnable = [];
345 if (empty($workFlows) || is_wp_error($workFlows) || empty($deletedIds)) {
346 return [];
347 }
348 if (!$formManager instanceof AdminFormManager) {
349 $formManager = new AdminFormManager($formID);
350 }
351 $returnableEntries = $deletedIds;
352 $formFields = $formManager->getFieldLabel();
353 $entryMeta = new FormEntryMetaModel();
354 $entries = $entryMeta->getEntryMeta(
355 $formFields,
356 $deletedIds
357 );
358 if (is_wp_error($entries) || empty($entries['entries'])) {
359 return $entries;
360 }
361 foreach ($entries['entries'] as $entry) {
362 $fieldValue = (array) $entry;
363 unset($fieldValue['entry_id']);
364 $fields = $formManager->getFormContentWithValue($fieldValue)->fields;
365 $fieldData = Helper::getFieldData($fields);
366
367 foreach ($workFlows as $workFlow) {
368 $workFlowBlock = json_decode($workFlow->workflow_condition);
369 // $conditions = $workFlowBlock->logics;
370 // $actions = $workFlowBlock->actions;
371 $conditionBehaviour = $workFlow->workflow_behaviour;
372
373 if ('cond' === $conditionBehaviour) {
374 foreach ($workFlowBlock as $condition) {
375 $actions = $condition->actions;
376 $type = $condition->cond_type;
377 $logics = $condition->logics;
378 $fieldData = Helper::smartFldMargeFormFld($logics, $fieldData);
379 $conditionalLogic = new ConditionalLogic($logics, $fieldData);
380 $conditionStatus = $conditionalLogic->getConditionStatus();
381
382 $conditionStatus = apply_filters('bitform_filter_workflow_condition_status', $conditionStatus, $condition, $fieldData, $this::$_formID);
383
384 if (($conditionStatus && in_array($type, ['if', 'else-if'])) || 'else' === $type) {
385 $isExists = \array_search($entry->entry_id, $returnableEntries);
386 if (!empty($actions->avoid_delete) && false !== $isExists) {
387 unset($returnableEntries[$isExists]);
388 $returnableEntries = array_values($returnableEntries);
389 } elseif (false === $isExists) {
390 $returnableEntries[] = $entry->entry_id;
391 }
392 if (!empty($actions->success)) {
393 foreach ($actions->success as $successActionDetail) {
394 switch ($successActionDetail->type) {
395 case 'webHooks':
396 if (!empty($successActionDetail->details->id)) {
397 $webHooks = $successActionDetail->details->id;
398 Integrations::executeIntegrations($webHooks, $fieldValue, static::$_formID);
399 }
400 break;
401 case 'mailNotify':
402 if (!empty($successActionDetail->details->id)) {
403 MailNotifier::notify($successActionDetail->details, static::$_formID, $fieldValue, $entry->entry_id);
404 }
405 break;
406 default:
407 break;
408 }
409 }
410 }
411 break;
412 }
413 }
414 } elseif ('always' === $conditionBehaviour) {
415 $actions = $workFlowBlock[0]->actions;
416 $isExists = \array_search($entry->entry_id, $returnableEntries);
417 if (!empty($actions->avoid_delete) && false !== $isExists) {
418 unset($returnableEntries[$isExists]);
419 $returnableEntries = array_values($returnableEntries);
420 } elseif (false === $isExists) {
421 $returnableEntries[] = $entry->entry_id;
422 }
423 if (!empty($actions->success)) {
424 foreach ($actions->success as $successActionDetail) {
425 switch ($successActionDetail->type) {
426 case 'webHooks':
427 if (!empty($successActionDetail->details->id)) {
428 $webHooks = $successActionDetail->details->id;
429 Integrations::executeIntegrations($webHooks, $fieldValue, static::$_formID);
430 }
431 break;
432 case 'mailNotify':
433 if (!empty($successActionDetail->details->id)) {
434 MailNotifier::notify($successActionDetail->details, static::$_formID, $fieldValue, $entry->entry_id);
435 }
436 break;
437 default:
438 break;
439 }
440 }
441 }
442 }
443 }
444 }
445 // }
446 $workFlowReturnable['entries'] = array_values($returnableEntries);
447 return $workFlowReturnable;
448 }
449 }
450