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 / WorkFlow.php

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

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