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

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