PluginProbe
Redirection / 3.7.3
Redirection v3.7.3
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 3.7.3, at database/database.php

149 lines 3.2 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 return new WP_Error( 'redirect', 'No upgrades found for version ' . $status->get_current_version() );
56 }
57
58 if ( $status->get_current_stage() === false ) {
59 if ( $status->needs_installing() ) {
60 $status->start_install( $upgraders );
61 } else {
62 $status->start_upgrade( $upgraders );
63 }
64 }
65
66 // Look at first upgrade
67 $upgrader = Red_Database_Upgrader::get( $upgraders[0] );
68
69 // Perform the upgrade
70 $upgrader->perform_stage( $status );
71
72 if ( ! $status->is_error() ) {
73 $status->set_next_stage();
74 }
75 }
76
77 public static function apply_to_sites( $callback ) {
78 if ( is_network_admin() ) {
79 array_map( function( $site ) use ( $callback ) {
80 switch_to_blog( $site->blog_id );
81
82 $callback();
83
84 restore_current_blog();
85 }, get_sites() );
86
87 return;
88 }
89
90 $callback();
91 }
92
93 /**
94 * Get latest database installer
95 *
96 * @return object Red_Latest_Database
97 */
98 public static function get_latest_database() {
99 include_once dirname( __FILE__ ) . '/schema/latest.php';
100
101 return new Red_Latest_Database();
102 }
103
104 /**
105 * List of all upgrades and their associated file
106 *
107 * @return array Database upgrade array
108 */
109 public function get_upgrades() {
110 return [
111 [
112 'version' => '2.0.1',
113 'file' => '201.php',
114 'class' => 'Red_Database_201',
115 ],
116 [
117 'version' => '2.1.16',
118 'file' => '216.php',
119 'class' => 'Red_Database_216',
120 ],
121 [
122 'version' => '2.2',
123 'file' => '220.php',
124 'class' => 'Red_Database_220',
125 ],
126 [
127 'version' => '2.3.1',
128 'file' => '231.php',
129 'class' => 'Red_Database_231',
130 ],
131 [
132 'version' => '2.3.2',
133 'file' => '232.php',
134 'class' => 'Red_Database_232',
135 ],
136 [
137 'version' => '2.3.3',
138 'file' => '233.php',
139 'class' => 'Red_Database_233',
140 ],
141 [
142 'version' => '2.4',
143 'file' => '240.php',
144 'class' => 'Red_Database_240',
145 ],
146 ];
147 }
148 }
149