| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Upgrader |
| 5 |
* |
| 6 |
* @since 1.2.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace wpCloud\StatelessMedia { |
| 10 |
|
| 11 |
if (!class_exists('wpCloud\StatelessMedia\Upgrader')) { |
| 12 |
|
| 13 |
final class Upgrader { |
| 14 |
|
| 15 |
/** |
| 16 |
* Upgrades data if needed. |
| 17 |
* |
| 18 |
*/ |
| 19 |
public static function call() { |
| 20 |
$version = get_site_option('wp_sm_version', false); |
| 21 |
|
| 22 |
if (!$version && is_multisite()) { |
| 23 |
$sites = get_sites(); |
| 24 |
foreach ($sites as $site) { |
| 25 |
switch_to_blog($site->blog_id); |
| 26 |
$version = get_option('wp_sm_version'); |
| 27 |
if ($version) { |
| 28 |
restore_current_blog(); |
| 29 |
break; |
| 30 |
} |
| 31 |
restore_current_blog(); |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
if (!$version) { |
| 36 |
self::fresh_install(); |
| 37 |
} |
| 38 |
|
| 39 |
/* Maybe upgrade blog ( single site ) */ |
| 40 |
self::upgrade(); |
| 41 |
|
| 42 |
/* Maybe upgrade network options */ |
| 43 |
if (ud_get_stateless_media()->is_network_detected(true)) { |
| 44 |
self::upgrade_network($version); |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Upgrade current blog ( or single site ) |
| 50 |
* |
| 51 |
*/ |
| 52 |
private static function upgrade() { |
| 53 |
global $wpdb; |
| 54 |
|
| 55 |
$version = get_option('wp_sm_version', false); |
| 56 |
|
| 57 |
/** |
| 58 |
* Upgrade to v.1.2.0 requirements |
| 59 |
*/ |
| 60 |
if (!$version || version_compare($version, '1.2.0', '<')) { |
| 61 |
|
| 62 |
if ($v = get_option('sm.mode')) { |
| 63 |
update_option('sm_mode', $v); |
| 64 |
delete_option('sm.mode'); |
| 65 |
} |
| 66 |
|
| 67 |
$v = get_option('sm_mode'); |
| 68 |
if ($v == '0') update_option('sm_mode', 'disabled'); |
| 69 |
elseif ($v == '1') update_option('sm_mode', 'backup'); |
| 70 |
elseif ($v == '2') update_option('sm_mode', 'cdn'); |
| 71 |
|
| 72 |
if ($v = get_option('sm.service_account_name')) { |
| 73 |
update_option('sm_service_account_name', $v); |
| 74 |
delete_option('sm.service_account_name'); |
| 75 |
} |
| 76 |
|
| 77 |
if ($v = get_option('sm.key_file_path')) { |
| 78 |
update_option('sm_key_file_path', $v); |
| 79 |
delete_option('sm.key_file_path'); |
| 80 |
} |
| 81 |
|
| 82 |
if ($v = get_option('sm.bucket')) { |
| 83 |
update_option('sm_bucket', $v); |
| 84 |
delete_option('sm.bucket'); |
| 85 |
} |
| 86 |
|
| 87 |
delete_option('sm.app_name'); |
| 88 |
delete_option('sm.body_rewrite'); |
| 89 |
delete_option('sm.bucket_url_path'); |
| 90 |
delete_option('sm.post_content_rewrite'); |
| 91 |
} |
| 92 |
|
| 93 |
update_option('dismissed_notice_stateless_cache_busting', true); |
| 94 |
if (!$version || version_compare($version, '2.1.7', '<')) { |
| 95 |
$sm_mode = get_option('sm_mode', null); |
| 96 |
$hashify_file_name = get_option('sm_hashify_file_name', null); |
| 97 |
if ($version && $sm_mode == 'stateless' && $hashify_file_name == 'true') { |
| 98 |
delete_option('dismissed_notice_stateless_cache_busting'); |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
if (!is_multisite() && $version && version_compare($version, '3.0', '<')) { |
| 103 |
self::migrate_root_dir(); |
| 104 |
//updating mode name `stateless` to `ephemeral` |
| 105 |
$sm_mode = get_option('sm_mode'); |
| 106 |
if ($sm_mode == 'stateless') update_option('sm_mode', 'ephemeral'); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Upgrade to v4.0 |
| 111 |
*/ |
| 112 |
if ( $version && version_compare($version, '4.0', '<') ) { |
| 113 |
$modules = get_option('stateless-modules', []); |
| 114 |
|
| 115 |
if ( is_array($modules) && isset($modules['dynamic-image-support']) ) { |
| 116 |
update_option('sm_dynamic_image_support', $modules['dynamic-image-support']); |
| 117 |
|
| 118 |
unset($modules['dynamic-image-support']); |
| 119 |
update_option('stateless-modules', $modules); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
update_option('wp_sm_version', ud_get_stateless_media()->args['version']); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Upgrade Network Enabled |
| 128 |
* |
| 129 |
*/ |
| 130 |
private static function upgrade_network($version) { |
| 131 |
/** |
| 132 |
* Upgrade to v.1.2.0 requirements |
| 133 |
*/ |
| 134 |
if (!$version || version_compare($version, '1.2.0', '<')) { |
| 135 |
|
| 136 |
if ($v = get_site_option('sm.key_file_path')) { |
| 137 |
update_site_option('sm_key_file_path', $v); |
| 138 |
delete_site_option('sm.key_file_path'); |
| 139 |
} |
| 140 |
|
| 141 |
if ($v = get_option('sm.service_account_name')) { |
| 142 |
update_site_option('sm_service_account_name', $v); |
| 143 |
delete_site_option('sm.service_account_name'); |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Upgrade to v.3.0 requirements |
| 149 |
*/ |
| 150 |
if (is_multisite() && $version && version_compare($version, '3.0', '<')) { |
| 151 |
$sites = get_sites(); |
| 152 |
foreach ($sites as $site) { |
| 153 |
switch_to_blog($site->blog_id); |
| 154 |
self::migrate_root_dir(true); |
| 155 |
//updating mode name `stateless` to `ephemeral` |
| 156 |
$sm_mode = get_option('sm_mode'); |
| 157 |
if ($sm_mode == 'stateless') update_option('sm_mode', 'ephemeral'); |
| 158 |
restore_current_blog(); |
| 159 |
} |
| 160 |
|
| 161 |
//removing network option, because sites could uses different setting for Orginize media |
| 162 |
update_network_option(1, 'sm_root_dir', ''); |
| 163 |
|
| 164 |
//forcing `Cache-Busting` for multisite to prevent replacing media with same filenames |
| 165 |
update_network_option(1, 'sm_hashify_file_name', 'true'); |
| 166 |
|
| 167 |
$sm_mode_site = get_site_option('sm_mode'); |
| 168 |
if ($sm_mode_site == 'stateless') update_site_option('sm_mode', 'ephemeral'); |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* Upgrade to v4.0 |
| 173 |
*/ |
| 174 |
if ( $version && version_compare($version, '4.0', '<') ) { |
| 175 |
$modules = get_site_option('stateless-modules', []); |
| 176 |
|
| 177 |
if ( is_array($modules) && isset($modules['dynamic-image-support']) ) { |
| 178 |
update_site_option('sm_dynamic_image_support', $modules['dynamic-image-support']); |
| 179 |
|
| 180 |
unset($modules['dynamic-image-support']); |
| 181 |
update_site_option('stateless-modules', $modules); |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
update_site_option('wp_sm_version', ud_get_stateless_media()->args['version']); |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Fresh install |
| 190 |
*/ |
| 191 |
private static function fresh_install() { |
| 192 |
if (is_multisite()) { |
| 193 |
$sites = get_sites(); |
| 194 |
foreach ($sites as $site) { |
| 195 |
switch_to_blog($site->blog_id); |
| 196 |
self::migrate_root_dir(true, true); |
| 197 |
restore_current_blog(); |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
/** |
| 203 |
* |
| 204 |
* @param bool $multisite pass true to use multisite prefix |
| 205 |
* @param bool $fresh_install for first install |
| 206 |
* @return string `sm_root_dir` value |
| 207 |
*/ |
| 208 |
private static function migrate_root_dir($multisite = false, $fresh_install = false) { |
| 209 |
$network_root_dir = get_network_option(1, 'sm_root_dir'); |
| 210 |
$sm_root_dir = $network_root_dir ? $network_root_dir : get_option('sm_root_dir', ''); |
| 211 |
$organize_media = get_option('uploads_use_yearmonth_folders'); |
| 212 |
|
| 213 |
if ($organize_media == '1' && empty($sm_root_dir)) { |
| 214 |
$sm_root_dir = '/%date_year/date_month%/'; |
| 215 |
} elseif (!empty($sm_root_dir) && $organize_media == '1') { |
| 216 |
$sm_root_dir = trim($sm_root_dir, ' /') . '/%date_year/date_month%/'; |
| 217 |
} elseif (!empty($sm_root_dir)) { |
| 218 |
// $sm_root_dir = $sm_root_dir; |
| 219 |
} elseif ($organize_media != '1') { |
| 220 |
$sm_root_dir = ''; |
| 221 |
} |
| 222 |
|
| 223 |
if ($multisite && $fresh_install) { |
| 224 |
if (empty($sm_root_dir) || $sm_root_dir[0] !== '/') { |
| 225 |
$sm_root_dir = "/" . $sm_root_dir; |
| 226 |
} |
| 227 |
if (false === strpos($sm_root_dir, '/sites/%site_id%')) { |
| 228 |
$sm_root_dir = '/sites/%site_id%' . $sm_root_dir; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
update_option('sm_root_dir', $sm_root_dir); |
| 233 |
|
| 234 |
if ($multisite) { |
| 235 |
//forcing `Cache-Busting` for multisite to prevent replacing media with same filenames |
| 236 |
update_option('sm_hashify_file_name', 'true'); |
| 237 |
} |
| 238 |
|
| 239 |
return $sm_root_dir; |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* Upgrade database, perform tasks that dbDelta can't do |
| 244 |
* |
| 245 |
* @param string $new_version |
| 246 |
* @param string $old_version |
| 247 |
*/ |
| 248 |
public static function upgrade_db($new_version, $old_version) { |
| 249 |
global $wpdb; |
| 250 |
|
| 251 |
if ( !empty($old_version) && version_compare($old_version, '1.1', '<') ) { |
| 252 |
try { |
| 253 |
// Remove UNIQUE indexes, which will be recreated later using dbDelta as non-unique |
| 254 |
$wpdb->query('ALTER TABLE ' . ud_stateless_db()->files . ' DROP INDEX post_id'); |
| 255 |
|
| 256 |
} catch (\Throwable $e) { |
| 257 |
Helper::log($e->getMessage()); |
| 258 |
} |
| 259 |
} |
| 260 |
|
| 261 |
if ( !empty($old_version) && version_compare($old_version, '1.2', '<') ) { |
| 262 |
try { |
| 263 |
// Remove UNIQUE indexes, which will be recreated later using dbDelta as non-unique |
| 264 |
$wpdb->query('ALTER TABLE ' . ud_stateless_db()->files . ' DROP INDEX name'); |
| 265 |
|
| 266 |
} catch (\Throwable $e) { |
| 267 |
Helper::log($e->getMessage()); |
| 268 |
} |
| 269 |
} |
| 270 |
} |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
|