PluginProbe
WPIDE – File Manager & Code Editor / 3.1
WPIDE – File Manager & Code Editor v3.1
3.5.8 3.5.7 2.0.14 2.0.15 2.0.16 2.0.2 2.0.4 2.0.5 2.0.6 2.0.7 2.0.8 2.0.9 2.1 2.2 2.3 2.3.1 2.3.2 2.4.0 2.5 2.6 3.0 3.1 3.2 3.3 3.4 All 54 releases
wpide / App / Classes / Migrations.php

Migrations.php in WPIDE – File Manager & Code Editor 3.1, at App/Classes/Migrations.php

135 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 self::$installed_time = intval(get_option(self::$installed_time_key));
35
36 if (empty(self::$installed_time)) {
37 self::$installed_time = time();
38 update_option(self::$installed_time_key, self::$installed_time);
39 }
40
41 add_action('init', [ __CLASS__, 'upgrade'], 10);
42 }
43
44 public static function get_version_key(): string
45 {
46
47 return App::instance()->prefix('version');
48 }
49
50 public static function get_installed_time_key(): string
51 {
52
53 return App::instance()->prefix('installed_time');
54 }
55
56 public static function get_changelog_viewed_key(): string
57 {
58
59 return App::instance()->prefix('changelog_viewed');
60 }
61
62 public static function get_migrations(): array
63 {
64
65 $files = glob(DIR.'migrations/migration-*.php');
66
67 $migrations = array();
68
69 foreach ($files as $file) {
70
71 preg_match('/migration\-(.+?)\.php/', $file, $matches);
72 $migrations[] = $matches[1];
73 }
74
75 return $migrations;
76 }
77
78 public static function upgrade()
79 {
80
81 if (self::$new_version !== self::$old_version) {
82
83 $migrations = self::get_migrations();
84
85 foreach ($migrations as $migration) {
86
87 if (self::$old_version < $migration) {
88
89 self::migrate($migration);
90 }
91 }
92 // End Migrations
93
94 update_option(self::$version_key, self::$new_version);
95
96 self::after_upgrade();
97 }
98
99 }
100
101 public static function migrate($version)
102 {
103
104 $path = DIR.'migrations/migration-' . $version . '.php';
105
106 if (file_exists($path)) {
107
108 require_once $path;
109 }
110
111 }
112
113 public static function after_upgrade()
114 {
115
116 self::set_changelog_viewed(false);
117
118 do_action(App::instance()->prefix('migration_complete'));
119 }
120
121 public static function has_unviewed_changelog(): bool
122 {
123
124 return !self::$changelog_viewed;
125 }
126
127 public static function set_changelog_viewed($viewed = true)
128 {
129
130 self::$changelog_viewed = $viewed;
131 update_option(self::$changelog_viewed_key, intval($viewed));
132 }
133
134 }
135