PluginProbe
Redirection / 4.1
Redirection v4.1
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / database / database.php

database.php in Redirection 4.1, at database/database.php

160 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 include_once dirname( __FILE__ ) . '/database-status.php';
4 include_once dirname( __FILE__ ) . '/database-upgrader.php';
5
6 class Red_Database {
7 /**
8 * Get all upgrades for a database version
9 *
10 * @return array Array of versions from self::get_upgrades()
11 */
12 public function get_upgrades_for_version( $current_version, $current_stage ) {
13 if ( $current_version === '' ) {
14 return [
15 [
16 'version' => REDIRECTION_DB_VERSION,
17 'file' => 'latest.php',
18 'class' => 'Red_Latest_Database',
19 ],
20 ];
21 }
22
23 $upgraders = [];
24 $found = false;
25
26 foreach ( $this->get_upgrades() as $upgrade ) {
27 if ( ! $found ) {
28 $upgrader = Red_Database_Upgrader::get( $upgrade );
29
30 $stage_present = in_array( $current_stage, array_keys( $upgrader->get_stages() ), true );
31 $same_version = $current_stage === false && version_compare( $upgrade['version'], $current_version, 'g' );
32
33 if ( $stage_present || $same_version ) {
34 $found = true;
35 }
36 }
37
38 if ( $found ) {
39 $upgraders[] = $upgrade;
40 }
41 }
42
43 return $upgraders;
44 }
45
46 /**
47 * Apply a particular upgrade stage
48 *
49 * @return mixed Result for upgrade
50 */
51 public function apply_upgrade( Red_Database_Status $status ) {
52 $upgraders = $this->get_upgrades_for_version( $status->get_current_version(), $status->get_current_stage() );
53
54 if ( count( $upgraders ) === 0 ) {
55 $status->set_error( 'No upgrades found for version ' . $status->get_current_version() );
56 return;
57 }
58
59 if ( $status->get_current_stage() === false ) {
60 if ( $status->needs_installing() ) {
61 $status->start_install( $upgraders );
62 } else {
63 $status->start_upgrade( $upgraders );
64 }
65 }
66
67 // Look at first upgrade
68 $upgrader = Red_Database_Upgrader::get( $upgraders[0] );
69
70 // Perform the upgrade
71 $upgrader->perform_stage( $status );
72
73 if ( ! $status->is_error() ) {
74 $status->set_next_stage();
75 }
76 }
77
78 public static function apply_to_sites( $callback ) {
79 if ( is_multisite() && ( is_network_admin() || defined( 'WP_CLI' ) && WP_CLI ) ) {
80 array_map( function( $site ) use ( $callback ) {
81 switch_to_blog( $site->blog_id );
82
83 $callback();
84
85 restore_current_blog();
86 }, get_sites() );
87
88 return;
89 }
90
91 $callback();
92 }
93
94 /**
95 * Get latest database installer
96 *
97 * @return object Red_Latest_Database
98 */
99 public static function get_latest_database() {
100 include_once dirname( __FILE__ ) . '/schema/latest.php';
101
102 return new Red_Latest_Database();
103 }
104
105 /**
106 * List of all upgrades and their associated file
107 *
108 * @return array Database upgrade array
109 */
110 public function get_upgrades() {
111 return [
112 [
113 'version' => '2.0.1',
114 'file' => '201.php',
115 'class' => 'Red_Database_201',
116 ],
117 [
118 'version' => '2.1.16',
119 'file' => '216.php',
120 'class' => 'Red_Database_216',
121 ],
122 [
123 'version' => '2.2',
124 'file' => '220.php',
125 'class' => 'Red_Database_220',
126 ],
127 [
128 'version' => '2.3.1',
129 'file' => '231.php',
130 'class' => 'Red_Database_231',
131 ],
132 [
133 'version' => '2.3.2',
134 'file' => '232.php',
135 'class' => 'Red_Database_232',
136 ],
137 [
138 'version' => '2.3.3',
139 'file' => '233.php',
140 'class' => 'Red_Database_233',
141 ],
142 [
143 'version' => '2.4',
144 'file' => '240.php',
145 'class' => 'Red_Database_240',
146 ],
147 [
148 'version' => '4.0',
149 'file' => '400.php',
150 'class' => 'Red_Database_400',
151 ],
152 [
153 'version' => '4.1',
154 'file' => '410.php',
155 'class' => 'Red_Database_410',
156 ],
157 ];
158 }
159 }
160