PluginProbe
WPIDE – File Manager & Code Editor / 3.5.5
WPIDE – File Manager & Code Editor v3.5.5
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.5.5, at App/Classes/Migrations.php

140 lines 3.1 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
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