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