| 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 |
public function __construct($formID, $user_details = null) |
| 20 |
{ |
| 21 |
static::$_formID = $formID; |
| 22 |
static::$_workFlowModel = new WorkFlowModel(); |
| 23 |
$this->_user_details = $user_details; |
| 24 |
$this->_actions = new Actions($formID); |
| 25 |
} |
| 26 |
|
| 27 |
public function getWorkFlow($workFlowRun, $workFlowType, $workFlowIds = null, $orderColumn = 'id') |
| 28 |
{ |
| 29 |
if (null === $workFlowIds) { |
| 30 |
$condition = [ |
| 31 |
'form_id' => static::$_formID, |
| 32 |
]; |
| 33 |
} else { |
| 34 |
$condition = [ |
| 35 |
'id' => [$workFlowIds], |
| 36 |
]; |
| 37 |
} |
| 38 |
if (!empty($workFlowRun)) { |
| 39 |
$condition = \array_merge( |
| 40 |
$condition, |
| 41 |
[ |
| 42 |
'workflow_run' => $workFlowRun, |
| 43 |
] |
| 44 |
); |
| 45 |
} |
| 46 |
if (!empty($workFlowType)) { |
| 47 |
if (!is_array($workFlowType)) { |
| 48 |
$workFlowType = [$workFlowType]; |
| 49 |
} |
| 50 |
$condition = \array_merge( |
| 51 |
$condition, |
| 52 |
[ |
| 53 |
'workflow_type' => $workFlowType, |
| 54 |
] |
| 55 |
); |
| 56 |
} |
| 57 |
$workFlows = static::$_workFlowModel->get( |
| 58 |
[ |
| 59 |
'id', |
| 60 |
'workflow_type', |
| 61 |
'workflow_run', |
| 62 |
'workflow_behaviour', |
| 63 |
'workflow_condition', |
| 64 |
'workflow_name', |
| 65 |
'workflow_order', |
| 66 |
], |
| 67 |
$condition, |
| 68 |
null, |
| 69 |
null, |
| 70 |
$orderColumn, |
| 71 |
'desc' |
| 72 |
); |
| 73 |
if (empty($workFlows) || is_wp_error($workFlows)) { |
| 74 |
return []; |
| 75 |
} |
| 76 |
|
| 77 |
return $workFlows; |
| 78 |
} |
| 79 |
|
| 80 |
public function executeOnLoad($workFlowRun, $fields) |
| 81 |
{ |
| 82 |
$workFlows = $this->getWorkFlow(['create_edit', $workFlowRun], ['always','onload'], null, 'workflow_order'); |
| 83 |
$workFlowReturnable = []; |
| 84 |
if (empty($workFlows) || is_wp_error($workFlows)) { |
| 85 |
return []; |
| 86 |
} |
| 87 |
$fieldData = Helper::getFieldData($fields); |
| 88 |
foreach ($workFlows as $workFlow) { |
| 89 |
$conditions = json_decode($workFlow->workflow_condition); |
| 90 |
$conditionBehaviour = $workFlow->workflow_behaviour; |
| 91 |
if ('cond' === $conditionBehaviour) { |
| 92 |
foreach ($conditions as $condition) { |
| 93 |
$type = $condition->cond_type; |
| 94 |
if (!empty($condition->actions->fields)) { |
| 95 |
$conditionStatus = false; |
| 96 |
|
| 97 |
if (!empty($condition->logics)) { |
| 98 |
$logics = $condition->logics; |
| 99 |
$fieldData = Helper::smartFldMargeFormFld($logics, $fieldData); |
| 100 |
$conditionalLogic = new ConditionalLogic($logics, $fieldData); |
| 101 |
$conditionStatus = $conditionalLogic->getConditionStatus(); |
| 102 |
} |
| 103 |
if (($conditionStatus && in_array($type, ['if', 'else-if']) || 'else' === $type)) { |
| 104 |
$data = $this->_actions->setFieldProperty($condition->actions->fields, $fieldData, $fields); |
| 105 |
$fields = $data[0]; |
| 106 |
break; |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
} elseif ('always' === $conditionBehaviour) { |
| 111 |
if (!empty($conditions[0]->actions->fields)) { |
| 112 |
$data = $this->_actions->setFieldProperty($conditions[0]->actions->fields, $fieldData, $fields); |
| 113 |
$fields = $data[0]; |
| 114 |
$fieldData = $data[1]; |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
$workFlowReturnable['fields'] = $fields; |
| 119 |
|
| 120 |
return $workFlowReturnable; |
| 121 |
} |
| 122 |
|
| 123 |
public function executeOnUserInput($workFlowRun) |
| 124 |
{ |
| 125 |
$workFlows = $this->getWorkFlow(['create_edit', $workFlowRun], ['always','oninput'], null, 'workflow_order'); |
| 126 |
$workFlowReturnable = []; |
| 127 |
$data = SmartTags::getPostUserData(); |
| 128 |
$workFlowReturnable['smart_tags'] = SmartTags::smartTags($data); |
| 129 |
if (empty($workFlows) || is_wp_error($workFlows)) { |
| 130 |
return $workFlowReturnable; |
| 131 |
} |
| 132 |
$onUserInputRun = []; |
| 133 |
foreach ($workFlows as $index => $value) { |
| 134 |
$conditions = json_decode($value->workflow_condition); |
| 135 |
$onUserInputRun['event_type'] = 'on_input'; |
| 136 |
$onUserInputRun['conditions'] = $conditions; |
| 137 |
|
| 138 |
$workFlowReturnable['onfield_input_conditions'][$index] = $onUserInputRun; |
| 139 |
} |
| 140 |
return $workFlowReturnable; |
| 141 |
} |
| 142 |
|
| 143 |
public function executeOnValidate($workFlowRun, $fieldData, $fieldValue) |
| 144 |
{ |
| 145 |
$workFlows = $this->getWorkFlow(['create_edit', $workFlowRun], 'onvalidate', null, 'workflow_order'); |
| 146 |
$workFlowReturnable = []; |
| 147 |
if (empty($workFlows) || is_wp_error($workFlows)) { |
| 148 |
return []; |
| 149 |
} |
| 150 |
|
| 151 |
foreach ($workFlows as $workFlow) { |
| 152 |
$conditions = json_decode($workFlow->workflow_condition); |
| 153 |
$conditionBehaviour = $workFlow->workflow_behaviour; |
| 154 |
if ('cond' === $conditionBehaviour) { |
| 155 |
foreach ($conditions as $condition) { |
| 156 |
$type = $condition->cond_type; |
| 157 |
if (!empty($condition->actions->failure)) { |
| 158 |
$validateMsg = $condition->actions->failure; |
| 159 |
} else { |
| 160 |
$validateMsg = '{"id":"0"}'; |
| 161 |
} |
| 162 |
$conditionStatus = false; |
| 163 |
if (!empty($condition->logics)) { |
| 164 |
$logics = $condition->logics; |
| 165 |
$fieldData = Helper::smartFldMargeFormFld($logics, $fieldData); |
| 166 |
$conditionalLogic = new ConditionalLogic($logics, $fieldData); |
| 167 |
$conditionStatus = $conditionalLogic->getConditionStatus(); |
| 168 |
} |
| 169 |
if (($conditionStatus && in_array($type, ['if', 'else-if'])) || 'else' === $type) { |
| 170 |
$workFlowReturnable = $this->_actions->confirmationMessage($workFlowReturnable, $validateMsg, $fieldValue); |
| 171 |
if (empty($workFlowReturnable)) { |
| 172 |
$workFlowReturnable['message'] = '<p>Something error in Form Validation</p>'; |
| 173 |
} |
| 174 |
break; |
| 175 |
} |
| 176 |
} |
| 177 |
} |
| 178 |
} |
| 179 |
return $workFlowReturnable; |
| 180 |
} |
| 181 |
|
| 182 |
public function isExistDoubleOptin($data) |
| 183 |
{ |
| 184 |
$data['integrationRun'] = true; |
| 185 |
if (has_action('bf_double_optin_confirmation')) { |
| 186 |
$activeDoubleOpt = ( new IntegrationHandler(static::$_formID) )->getAllIntegration('double-opt-in', 'double-opt-in', 1); |
| 187 |
|
| 188 |
if (!is_wp_error($activeDoubleOpt) && count($activeDoubleOpt) > 0) { |
| 189 |
$dplOptinDetails = json_decode($activeDoubleOpt[0]->integration_details); |
| 190 |
if (isset($dplOptinDetails->disable_loggin_user) && !is_user_logged_in() || !isset($dplOptinDetails->disable_loggin_user)) { |
| 191 |
$data['integrationRun'] = false; |
| 192 |
$data['integrationDetails'] = $activeDoubleOpt[0]; |
| 193 |
$data['dflt_template'] = isset($dplOptinDetails->dflt_temp) ? true : false; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
return $data; |
| 198 |
} |
| 199 |
|
| 200 |
public function executeOnSubmit($workFlowRun, $fields, $fieldValue, $entryID, $logID, $workflowsIds = null) |
| 201 |
{ |
| 202 |
$workFlows = $this->getWorkFlow(['create_edit', $workFlowRun], ['onsubmit'], $workflowsIds, 'workflow_order'); |
| 203 |
$workFlowReturnable = []; |
| 204 |
$workFlowReturnable = $this->isExistDoubleOptin($workFlowReturnable); |
| 205 |
if (empty($workFlows) || is_wp_error($workFlows)) { |
| 206 |
$defaultConfimation = Helper::setDefaultSubmitConfirmation('successMsg', $fieldValue, static::$_formID, 0); |
| 207 |
if (empty($defaultConfimation['confirmation'])) { |
| 208 |
$workFlowReturnable['message'] = 'edit' !== $workFlowRun ? __('Form Submitted Successfully', 'bit-form') |
| 209 |
: __('Entry Updated Successfully', 'bit-form'); |
| 210 |
} else { |
| 211 |
$workFlowReturnable['message'] = $defaultConfimation['confirmation']; |
| 212 |
} |
| 213 |
$workFlowReturnable['msg_id'] = 0; |
| 214 |
$workFlowReturnable['dflt_message'] = true; |
| 215 |
$defaultConfimation = Helper::setDefaultSubmitConfirmation('redirectPage', $fieldValue, static::$_formID, 0); |
| 216 |
|
| 217 |
$workFlowReturnable['redirectPage'] = $defaultConfimation['confirmation']; |
| 218 |
$data = [ |
| 219 |
'integrations' => [Helper::setDefaultSubmitConfirmation('webHooks', $fieldValue, static::$_formID, $logID)], |
| 220 |
'entryID' => $entryID, |
| 221 |
'logID' => $logID, |
| 222 |
'formID' => static::$_formID, |
| 223 |
]; |
| 224 |
$workFlowReturnable['triggerData'] = $data; |
| 225 |
$workFlowReturnable['cron'] = false; |
| 226 |
return $workFlowReturnable; |
| 227 |
} |
| 228 |
|
| 229 |
$fieldData = Helper::getFieldData($fields); |
| 230 |
|
| 231 |
$isCronOK = !defined('DOING_CRON') && wp_doing_ajax() && (!defined('DISABLE_WP_CRON') || (defined('DISABLE_WP_CRON') && DISABLE_WP_CRON)); |
| 232 |
if ($isCronOK) { |
| 233 |
// From wp spawn_cron() |
| 234 |
$gmt_time = microtime(true); |
| 235 |
$lock = get_transient('doing_cron'); |
| 236 |
if ($lock > $gmt_time + 10 * MINUTE_IN_SECONDS) { |
| 237 |
$lock = 0; |
| 238 |
} |
| 239 |
if ($lock + WP_CRON_LOCK_TIMEOUT > $gmt_time) { |
| 240 |
$isCronOK = false; |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
$onFormSuccessActionDefault = [ |
| 245 |
'workFlowReturnable' => $workFlowReturnable, |
| 246 |
'mailData' => null, |
| 247 |
'dblOptin' => null, |
| 248 |
'integrationsToExc' => [], |
| 249 |
'isWebHookQueued' => false, |
| 250 |
]; |
| 251 |
|
| 252 |
foreach ($workFlows as $workFlow) { |
| 253 |
$conditions = json_decode($workFlow->workflow_condition); |
| 254 |
|
| 255 |
$conditionBehaviour = $workFlow->workflow_behaviour; |
| 256 |
if ('cond' === $conditionBehaviour) { |
| 257 |
foreach ($conditions as $condition) { |
| 258 |
$type = $condition->cond_type; |
| 259 |
if (!empty($condition->actions)) { |
| 260 |
$actions = $condition->actions; |
| 261 |
$conditionStatus = false; |
| 262 |
if (!empty($condition->logics)) { |
| 263 |
$logics = $condition->logics; |
| 264 |
$fieldData = Helper::smartFldMargeFormFld($logics, $fieldData); |
| 265 |
$conditionalLogic = new ConditionalLogic($logics, $fieldData); |
| 266 |
$conditionStatus = $conditionalLogic->getConditionStatus(); |
| 267 |
} |
| 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 |
|