| 1 |
<?php |
| 2 |
/** |
| 3 |
* Upgrader |
| 4 |
* |
| 5 |
* @since 1.2.0 |
| 6 |
*/ |
| 7 |
namespace wpCloud\StatelessMedia { |
| 8 |
|
| 9 |
if( !class_exists( 'wpCloud\StatelessMedia\Upgrader' ) ) { |
| 10 |
|
| 11 |
final class Upgrader { |
| 12 |
|
| 13 |
/** |
| 14 |
* Upgrades data if needed. |
| 15 |
* |
| 16 |
*/ |
| 17 |
public static function call() { |
| 18 |
/* Maybe upgrade blog ( single site ) */ |
| 19 |
self::upgrade(); |
| 20 |
/* Maybe upgrade network options */ |
| 21 |
if( ud_get_stateless_media()->is_network_detected() ) { |
| 22 |
self::upgrade_network(); |
| 23 |
} |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Upgrade current blog ( or single site ) |
| 29 |
* |
| 30 |
*/ |
| 31 |
private static function upgrade() { |
| 32 |
global $wpdb; |
| 33 |
|
| 34 |
$version = get_option( 'wp_sm_version', false ); |
| 35 |
|
| 36 |
/** |
| 37 |
* Upgrade to v.1.2.0 requirements |
| 38 |
*/ |
| 39 |
if ( !$version || version_compare( $version, '1.2.0', '<' ) ) { |
| 40 |
|
| 41 |
if( $v = get_option( 'sm.mode' ) ) { |
| 42 |
update_option( 'sm_mode', $v ); |
| 43 |
delete_option( 'sm.mode' ); |
| 44 |
} |
| 45 |
|
| 46 |
$v = get_option( 'sm_mode' ); |
| 47 |
if( $v == '0' ) update_option( 'sm_mode', 'disabled' ); |
| 48 |
elseif( $v == '1' ) update_option( 'sm_mode', 'backup' ); |
| 49 |
elseif( $v == '2' ) update_option( 'sm_mode', 'cdn' ); |
| 50 |
|
| 51 |
if( $v = get_option( 'sm.service_account_name' ) ) { |
| 52 |
update_option( 'sm_service_account_name', $v ); |
| 53 |
delete_option( 'sm.service_account_name' ); |
| 54 |
} |
| 55 |
|
| 56 |
if( $v = get_option( 'sm.key_file_path' ) ) { |
| 57 |
update_option( 'sm_key_file_path', $v ); |
| 58 |
delete_option( 'sm.key_file_path' ); |
| 59 |
} |
| 60 |
|
| 61 |
if( $v = get_option( 'sm.bucket' ) ) { |
| 62 |
update_option( 'sm_bucket', $v ); |
| 63 |
delete_option( 'sm.bucket' ); |
| 64 |
} |
| 65 |
|
| 66 |
delete_option( 'sm.app_name' ); |
| 67 |
delete_option( 'sm.body_rewrite' ); |
| 68 |
delete_option( 'sm.bucket_url_path' ); |
| 69 |
delete_option( 'sm.post_content_rewrite' ); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
update_option('dismissed_notice_stateless_cache_busting', true); |
| 74 |
if ( !$version || version_compare( $version, '2.1.7', '<' ) ){ |
| 75 |
$sm_mode = get_option('sm_mode', null); |
| 76 |
$hashify_file_name = get_option('sm_hashify_file_name', null); |
| 77 |
if($version && $sm_mode == 'stateless' && $hashify_file_name == 'true'){ |
| 78 |
delete_option('dismissed_notice_stateless_cache_busting'); |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
if ( !$version || version_compare( $version, '2.2.0', '<' ) ){ |
| 83 |
$sm_synced_files = get_option('sm_synced_files', array()); |
| 84 |
if(!empty($sm_synced_files) && is_array($sm_synced_files)){ |
| 85 |
$table_name = $wpdb->prefix . SyncNonMedia::table; |
| 86 |
// Backing up the old data with autoload false. |
| 87 |
// @todo delete in future release. |
| 88 |
add_option('__sm_synced_files', $sm_synced_files, null, false); |
| 89 |
|
| 90 |
$files = array(); |
| 91 |
$place_holders = array(); |
| 92 |
$query = "INSERT INTO $table_name (file, status) VALUES "; |
| 93 |
|
| 94 |
$sm_synced_files = array_unique($sm_synced_files); |
| 95 |
foreach ($sm_synced_files as $key => $file) { |
| 96 |
array_push($files, $file, 'synced'); |
| 97 |
$place_holders[] = "('%s', '%s')"; /* In my case, i know they will always be integers */ |
| 98 |
} |
| 99 |
$query .= implode(', ', $place_holders); |
| 100 |
$query = $wpdb->prepare("$query ", $files); |
| 101 |
$wpdb->query( $query ); |
| 102 |
|
| 103 |
delete_option('sm_synced_files'); |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
update_option( 'wp_sm_version', ud_get_stateless_media()->args[ 'version' ] ); |
| 108 |
|
| 109 |
} |
| 110 |
|
| 111 |
/** |
| 112 |
* Upgrade Network Enabled |
| 113 |
* |
| 114 |
*/ |
| 115 |
private static function upgrade_network() { |
| 116 |
|
| 117 |
$version = get_site_option( 'wp_sm_version', false ); |
| 118 |
|
| 119 |
/** |
| 120 |
* Upgrade to v.1.2.0 requirements |
| 121 |
*/ |
| 122 |
if ( !$version || version_compare( $version, '1.2.0', '<' ) ) { |
| 123 |
|
| 124 |
if( $v = get_site_option( 'sm.key_file_path' ) ) { |
| 125 |
update_site_option( 'sm_key_file_path', $v ); |
| 126 |
delete_site_option( 'sm.key_file_path' ); |
| 127 |
} |
| 128 |
|
| 129 |
if( $v = get_option( 'sm.service_account_name' ) ) { |
| 130 |
update_site_option( 'sm_service_account_name', $v ); |
| 131 |
delete_site_option( 'sm.service_account_name' ); |
| 132 |
} |
| 133 |
|
| 134 |
} |
| 135 |
|
| 136 |
update_site_option( 'wp_sm_version', ud_get_stateless_media()->args[ 'version' ] ); |
| 137 |
|
| 138 |
} |
| 139 |
|
| 140 |
} |
| 141 |
|
| 142 |
} |
| 143 |
|
| 144 |
} |