| 1 |
<?php |
| 2 |
|
| 3 |
namespace BitCode\BitForm\Core\Fallback; |
| 4 |
|
| 5 |
use BitCode\BitForm\Core\Database\DB; |
| 6 |
use BitCode\BitForm\Core\Database\WorkFlowModel; |
| 7 |
use BitCode\BitForm\Core\Util\IpTool; |
| 8 |
|
| 9 |
class WorkFlow |
| 10 |
{ |
| 11 |
/** |
| 12 |
* Free/Pro conditional-logic separation: backfill workflow_category on existing rows. |
| 13 |
* |
| 14 |
* The DB migration adds the column with DEFAULT 'classic', so every pre-existing row is already |
| 15 |
* classic (= keeps executing in Free exactly as before). Here we only retag rows that the basic |
| 16 |
* field show/hide UI marked as info.type === 'basic'. Existing rows predate the advanced (Pro) |
| 17 |
* feature, so nothing legitimately maps to 'advanced' during this one-time upgrade. |
| 18 |
*/ |
| 19 |
public function normalizeCategory() |
| 20 |
{ |
| 21 |
// Runs on `init` (any request), but the `workflow_category` column is added by a migration gated |
| 22 |
// behind an admin request. Ensure the column exists first (self-heal) so a non-admin/cron first |
| 23 |
// request does not silently no-op and leave rows uncategorized. |
| 24 |
global $wpdb; |
| 25 |
$table = $wpdb->prefix . 'bitforms_workflows'; |
| 26 |
if (null === $wpdb->get_var("SHOW COLUMNS FROM `{$table}` LIKE 'workflow_category'")) { |
| 27 |
DB::migrate(); |
| 28 |
if (null === $wpdb->get_var("SHOW COLUMNS FROM `{$table}` LIKE 'workflow_category'")) { |
| 29 |
return ''; |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
$workFlowModel = new WorkFlowModel(); |
| 34 |
$workFlows = $workFlowModel->get( |
| 35 |
['id', 'workflow_info', 'workflow_category'], |
| 36 |
[], |
| 37 |
null, |
| 38 |
null, |
| 39 |
'id' |
| 40 |
); |
| 41 |
if (is_wp_error($workFlows) || count($workFlows) <= 0) { |
| 42 |
return ''; |
| 43 |
} |
| 44 |
foreach ($workFlows as $workflow) { |
| 45 |
$info = !empty($workflow->workflow_info) ? \json_decode($workflow->workflow_info) : null; |
| 46 |
$category = (!empty($info) && isset($info->type) && 'basic' === $info->type) ? 'basic' : 'classic'; |
| 47 |
if ($category !== $workflow->workflow_category) { |
| 48 |
$workFlowModel->update( |
| 49 |
['workflow_category' => $category], |
| 50 |
['id' => $workflow->id] |
| 51 |
); |
| 52 |
} |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
public function conditionalLogic() |
| 57 |
{ |
| 58 |
$workFlowUpdatedSatus = true; |
| 59 |
$workFlowModel = new WorkFlowModel(); |
| 60 |
$workFlows = $workFlowModel->get( |
| 61 |
[ |
| 62 |
'id', |
| 63 |
'workflow_behaviour', |
| 64 |
'workflow_condition', |
| 65 |
'workflow_action', |
| 66 |
'workflow_status', |
| 67 |
], |
| 68 |
[], |
| 69 |
null, |
| 70 |
null, |
| 71 |
'id' |
| 72 |
); |
| 73 |
if (is_wp_error($workFlows) || count($workFlows) <= 0) { |
| 74 |
return ''; |
| 75 |
} |
| 76 |
$ipTool = new IpTool(); |
| 77 |
$user_details = $ipTool->getUserDetail(); |
| 78 |
|
| 79 |
foreach ($workFlows as $index => $workflow) { |
| 80 |
$decode = \json_decode($workflow->workflow_action); |
| 81 |
$logic = \json_decode($workflow->workflow_condition); |
| 82 |
$beheviour = $workflow->workflow_behaviour; |
| 83 |
$cond = 'cond' === $beheviour ? 'if' : 'always'; |
| 84 |
|
| 85 |
$workFlows[$index]->workflow_condition = [ |
| 86 |
'cond_type' => $cond, |
| 87 |
'actions' => [ |
| 88 |
'fields' => $decode->action ? $decode->action : [], |
| 89 |
'success' => $decode->successAction ? $decode->successAction : [], |
| 90 |
'failure' => $decode->validateMsg ? $decode->validateMsg : '', |
| 91 |
], |
| 92 |
]; |
| 93 |
if ('cond' === $beheviour) { |
| 94 |
$workFlows[$index]->workflow_condition['logics'] = $logic; |
| 95 |
} |
| 96 |
$updatedWorkFlow = wp_json_encode([$workFlows[$index]->workflow_condition]); |
| 97 |
$updated = $workFlowModel->update( |
| 98 |
[ |
| 99 |
'workflow_condition' => $updatedWorkFlow, |
| 100 |
'updated_at' => $user_details['time'], |
| 101 |
], |
| 102 |
[ |
| 103 |
'id' => $workflow->id, |
| 104 |
] |
| 105 |
); |
| 106 |
if (is_wp_error($updated)) { |
| 107 |
$workFlowUpdatedSatus = false; |
| 108 |
break; |
| 109 |
} |
| 110 |
} |
| 111 |
if ($workFlowUpdatedSatus) { |
| 112 |
global $wpdb; |
| 113 |
// Schema migration: table name interpolation only. $wpdb->prepare() cannot parameterize DDL statements. |
| 114 |
$wpdb->query( |
| 115 |
"ALTER TABLE `{$wpdb->prefix}bitforms_workflows` DROP `workflow_action`" |
| 116 |
); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|