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