| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPIDE\App\Classes; |
| 4 |
|
| 5 |
use WPIDE\App\App; |
| 6 |
use const WPIDE\Constants\DIR; |
| 7 |
use const WPIDE\Constants\VERSION; |
| 8 |
|
| 9 |
class Migrations |
| 10 |
{ |
| 11 |
|
| 12 |
public static $version_key; |
| 13 |
public static $installed_time_key; |
| 14 |
public static $changelog_viewed_key; |
| 15 |
|
| 16 |
public static $new_version; |
| 17 |
public static $old_version; |
| 18 |
public static $installed_time; |
| 19 |
public static $changelog_viewed; |
| 20 |
|
| 21 |
public static $migrations = array(); |
| 22 |
|
| 23 |
public static function init() |
| 24 |
{ |
| 25 |
|
| 26 |
self::$version_key = self::get_version_key(); |
| 27 |
self::$installed_time_key = self::get_installed_time_key(); |
| 28 |
self::$changelog_viewed_key = self::get_changelog_viewed_key(); |
| 29 |
self::$new_version = VERSION; |
| 30 |
|
| 31 |
self::$changelog_viewed = (bool)get_option(self::$changelog_viewed_key); |
| 32 |
|
| 33 |
self::$old_version = get_option(self::$version_key); |
| 34 |
|
| 35 |
if(defined('WPIDE_DEBUG_MIGRATION_OLD_VERSION')) { |
| 36 |
self::$old_version = WPIDE_DEBUG_MIGRATION_OLD_VERSION; |
| 37 |
} |
| 38 |
|
| 39 |
self::$installed_time = intval(get_option(self::$installed_time_key)); |
| 40 |
|
| 41 |
if (empty(self::$installed_time)) { |
| 42 |
self::$installed_time = time(); |
| 43 |
update_option(self::$installed_time_key, self::$installed_time); |
| 44 |
} |
| 45 |
|
| 46 |
add_action('init', [ __CLASS__, 'upgrade'], 10); |
| 47 |
} |
| 48 |
|
| 49 |
public static function get_version_key(): string |
| 50 |
{ |
| 51 |
|
| 52 |
return App::instance()->prefix('version'); |
| 53 |
} |
| 54 |
|
| 55 |
public static function get_installed_time_key(): string |
| 56 |
{ |
| 57 |
|
| 58 |
return App::instance()->prefix('installed_time'); |
| 59 |
} |
| 60 |
|
| 61 |
public static function get_changelog_viewed_key(): string |
| 62 |
{ |
| 63 |
|
| 64 |
return App::instance()->prefix('changelog_viewed'); |
| 65 |
} |
| 66 |
|
| 67 |
public static function get_migrations(): array |
| 68 |
{ |
| 69 |
|
| 70 |
$files = glob(DIR.'migrations/migration-*.php'); |
| 71 |
|
| 72 |
$migrations = array(); |
| 73 |
|
| 74 |
foreach ($files as $file) { |
| 75 |
|
| 76 |
preg_match('/migration\-(.+?)\.php/', $file, $matches); |
| 77 |
$migrations[] = $matches[1]; |
| 78 |
} |
| 79 |
|
| 80 |
return $migrations; |
| 81 |
} |
| 82 |
|
| 83 |
public static function upgrade() |
| 84 |
{ |
| 85 |
|
| 86 |
if (self::$new_version !== self::$old_version) { |
| 87 |
|
| 88 |
$migrations = self::get_migrations(); |
| 89 |
|
| 90 |
foreach ($migrations as $migration) { |
| 91 |
|
| 92 |
if (self::$old_version < $migration) { |
| 93 |
|
| 94 |
self::migrate($migration); |
| 95 |
} |
| 96 |
} |
| 97 |
// End Migrations |
| 98 |
|
| 99 |
update_option(self::$version_key, self::$new_version); |
| 100 |
|
| 101 |
self::after_upgrade(); |
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
public static function migrate($version) |
| 107 |
{ |
| 108 |
|
| 109 |
$path = DIR.'migrations/migration-' . $version . '.php'; |
| 110 |
|
| 111 |
if (file_exists($path)) { |
| 112 |
|
| 113 |
require_once $path; |
| 114 |
} |
| 115 |
|
| 116 |
} |
| 117 |
|
| 118 |
public static function after_upgrade() |
| 119 |
{ |
| 120 |
|
| 121 |
self::set_changelog_viewed(false); |
| 122 |
|
| 123 |
do_action(App::instance()->prefix('migration_complete')); |
| 124 |
} |
| 125 |
|
| 126 |
public static function has_unviewed_changelog(): bool |
| 127 |
{ |
| 128 |
|
| 129 |
return !self::$changelog_viewed; |
| 130 |
} |
| 131 |
|
| 132 |
public static function set_changelog_viewed($viewed = true) |
| 133 |
{ |
| 134 |
|
| 135 |
self::$changelog_viewed = $viewed; |
| 136 |
update_option(self::$changelog_viewed_key, intval($viewed)); |
| 137 |
} |
| 138 |
|
| 139 |
} |
| 140 |
|