| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentPlayer\Database; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) exit; |
| 6 |
|
| 7 |
use FluentPlayer\App\Services\PresetService; |
| 8 |
use FluentPlayer\Database\Migrations\DurationBackfillMigrator; |
| 9 |
use FluentPlayer\Database\Migrations\EmailCollectionsMigrator; |
| 10 |
use FluentPlayer\Database\Migrations\ResumePlaybackResetMigrator; |
| 11 |
|
| 12 |
class DBMigrator |
| 13 |
{ |
| 14 |
public static function run($networkWide = false) |
| 15 |
{ |
| 16 |
if ($networkWide && is_multisite()) { |
| 17 |
$sites = get_sites(); |
| 18 |
foreach ($sites as $site) { |
| 19 |
switch_to_blog($site->blog_id); |
| 20 |
self::migrate(); |
| 21 |
restore_current_blog(); |
| 22 |
} |
| 23 |
} else { |
| 24 |
self::migrate(); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
private static function migrate() |
| 29 |
{ |
| 30 |
require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 31 |
self::maybeRenameTables(); |
| 32 |
self::migratePresetsToOptions(); |
| 33 |
EmailCollectionsMigrator::migrate(); |
| 34 |
DurationBackfillMigrator::migrate(); |
| 35 |
ResumePlaybackResetMigrator::migrate(); |
| 36 |
} |
| 37 |
|
| 38 |
private static function maybeRenameTables() |
| 39 |
{ |
| 40 |
global $wpdb; |
| 41 |
|
| 42 |
$renames = [ |
| 43 |
'fluent_player_presets' => 'flp_presets', |
| 44 |
'fluent_player_email_collections' => 'flp_email_collections', |
| 45 |
'fluent_player_visits' => 'flp_visits', |
| 46 |
]; |
| 47 |
|
| 48 |
foreach ($renames as $oldSuffix => $newSuffix) { |
| 49 |
$oldTable = $wpdb->prefix . $oldSuffix; |
| 50 |
$newTable = $wpdb->prefix . $newSuffix; |
| 51 |
|
| 52 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery -- One-time migration check |
| 53 |
$oldExists = $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $oldTable)) === $oldTable; |
| 54 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery -- One-time migration check |
| 55 |
$newExists = $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $newTable)) === $newTable; |
| 56 |
|
| 57 |
if ($oldExists && !$newExists) { |
| 58 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table names from hardcoded strings |
| 59 |
$wpdb->query("RENAME TABLE `{$oldTable}` TO `{$newTable}`"); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
foreach (['fluent_player_play_resumes', 'flp_play_resumes'] as $suffix) { |
| 64 |
$table = $wpdb->prefix . $suffix; |
| 65 |
// phpcs:ignore WordPress.DB.DirectDatabaseQuery,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name from hardcoded string |
| 66 |
$wpdb->query("DROP TABLE IF EXISTS `{$table}`"); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
private static function migratePresetsToOptions() |
| 71 |
{ |
| 72 |
PresetService::maybeMigrateFromTable(); |
| 73 |
PresetService::maybeCreateDefaults(); |
| 74 |
PresetService::syncBuiltInControls(); |
| 75 |
} |
| 76 |
} |
| 77 |
|