| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBoards\App\Hooks\Handlers; |
| 4 |
|
| 5 |
use FluentBoards\App\Models\Task; |
| 6 |
use FluentBoards\App\Models\TaskMeta; |
| 7 |
use FluentBoards\App\Services\Constant; |
| 8 |
|
| 9 |
class UpdateHandler |
| 10 |
{ |
| 11 |
public static function maybeSubtaskGroupSync() |
| 12 |
{ |
| 13 |
$lastDbVersion = get_option('_fluent_boards_db_version'); |
| 14 |
if (!$lastDbVersion || version_compare($lastDbVersion, FLUENT_BOARDS_DB_VERSION, '<')) { |
| 15 |
// the last updated version is less than the current version |
| 16 |
// self::adjustOldSubtasksToSubtaskGroup(); |
| 17 |
update_option('_fluent_boards_db_version', FLUENT_BOARDS_DB_VERSION); |
| 18 |
} |
| 19 |
|
| 20 |
} |
| 21 |
public static function maybeUpdateDbTables() |
| 22 |
{ |
| 23 |
$lastDbVersion = get_option('_fluent_boards_db_version'); |
| 24 |
if (!$lastDbVersion || version_compare($lastDbVersion, FLUENT_BOARDS_DB_VERSION, '<')) { |
| 25 |
// last updated version is less than current version |
| 26 |
self::updateDatabase(); |
| 27 |
update_option('_fluent_boards_db_version', FLUENT_BOARDS_DB_VERSION); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
private static function adjustOldSubtasksToSubtaskGroup() |
| 32 |
{ |
| 33 |
$allSubtasks = Task::whereNotNull('parent_id')->get(); |
| 34 |
foreach ($allSubtasks as $subtask) { |
| 35 |
//need to check if that subtask is in a subtaskGroup or not |
| 36 |
$isInGroup = TaskMeta::where('task_id', $subtask->id) |
| 37 |
->where('key', Constant::SUBTASK_GROUP_CHILD) |
| 38 |
->exists(); |
| 39 |
if ($isInGroup) { |
| 40 |
continue; |
| 41 |
} |
| 42 |
|
| 43 |
//check if any subtask group created for the parent task of the current subtask |
| 44 |
$group = TaskMeta::where('task_id', $subtask->parent_id) |
| 45 |
->where('key', Constant::SUBTASK_GROUP_NAME) |
| 46 |
->first(); |
| 47 |
|
| 48 |
if ($group) { |
| 49 |
TaskMeta::create([ |
| 50 |
'task_id' => $subtask->id, |
| 51 |
'key' => Constant::SUBTASK_GROUP_CHILD, |
| 52 |
'value' => $group->id |
| 53 |
]); |
| 54 |
} else { |
| 55 |
$newSubtaskGroup = TaskMeta::create([ |
| 56 |
'task_id' => $subtask->parent_id, |
| 57 |
'key' => Constant::SUBTASK_GROUP_NAME, |
| 58 |
'value' => 'Subtask Group One' |
| 59 |
]); |
| 60 |
|
| 61 |
TaskMeta::create([ |
| 62 |
'task_id' => $subtask->id, |
| 63 |
'key' => Constant::SUBTASK_GROUP_CHILD, |
| 64 |
'value' => $newSubtaskGroup->id |
| 65 |
]); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
} |
| 70 |
|
| 71 |
private static function updateDatabase() |
| 72 |
{ |
| 73 |
global $wpdb; |
| 74 |
|
| 75 |
$table = $wpdb->prefix . 'fbs_board_terms'; |
| 76 |
|
| 77 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Schema check query, caching not applicable |
| 78 |
if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table)) != $table) { |
| 79 |
return; |
| 80 |
} else { |
| 81 |
// change column type from int to decimal - for already installed sites |
| 82 |
$column_name = 'position'; |
| 83 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name and column name cannot be prepared |
| 84 |
$preparedQuery = $wpdb->prepare("DESCRIBE $table %s", $column_name); |
| 85 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Variable contains prepared query, schema check, caching not applicable |
| 86 |
$dataType = $wpdb->get_row($preparedQuery); |
| 87 |
if (strpos($dataType->Type, 'int') !== false) { |
| 88 |
$sql = $wpdb->prepare( |
| 89 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name and column name cannot be prepared |
| 90 |
"ALTER TABLE $table MODIFY $column_name decimal(10,2) NOT NULL DEFAULT '1' COMMENT 'Position: 1 = top/first, 2 = second/second in top, etc.';" |
| 91 |
); |
| 92 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Variable contains prepared query, schema modification, caching not applicable |
| 93 |
$wpdb->query($sql); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$table = $wpdb->prefix . 'fbs_comments'; |
| 98 |
|
| 99 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Schema check query, caching not applicable |
| 100 |
if ($wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table)) != $table) { |
| 101 |
return; |
| 102 |
} else { |
| 103 |
$column_name = 'settings'; |
| 104 |
// Check if the column already exists |
| 105 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- schema check, caching not applicable |
| 106 |
$column_exists = $wpdb->get_var($wpdb->prepare( |
| 107 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name cannot be prepared, schema check, caching not applicable |
| 108 |
"SHOW COLUMNS FROM $table LIKE %s", $column_name |
| 109 |
)); |
| 110 |
|
| 111 |
if (!$column_exists) { |
| 112 |
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name and column name cannot be prepared |
| 113 |
$sql = "ALTER TABLE $table ADD $column_name TEXT NULL COMMENT 'serialize array' AFTER `created_by`"; |
| 114 |
// phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Variable contains ALTER TABLE query, schema modification, caching not applicable |
| 115 |
$wpdb->query($sql); |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
} |
| 120 |
|
| 121 |
} |
| 122 |
|