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

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

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