PluginProbe
WP-Stateless – Google Cloud Storage / 3.0
WP-Stateless – Google Cloud Storage v3.0
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / class-upgrader.php

class-upgrader.php in WP-Stateless – Google Cloud Storage 3.0, at lib/classes/class-upgrader.php

238 lines 7.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 $version = get_site_option( 'wp_sm_version', false );
19 if(!$version){
20 self::fresh_install();
21 }
22
23 /* Maybe upgrade blog ( single site ) */
24 self::upgrade();
25
26 /* Maybe upgrade network options */
27 if( ud_get_stateless_media()->is_network_detected(true) ) {
28 self::upgrade_network();
29 }
30
31 }
32
33 /**
34 * Upgrade current blog ( or single site )
35 *
36 */
37 private static function upgrade() {
38 global $wpdb;
39
40 $version = get_option( 'wp_sm_version', false );
41
42 /**
43 * Upgrade to v.1.2.0 requirements
44 */
45 if ( !$version || version_compare( $version, '1.2.0', '<' ) ) {
46
47 if( $v = get_option( 'sm.mode' ) ) {
48 update_option( 'sm_mode', $v );
49 delete_option( 'sm.mode' );
50 }
51
52 $v = get_option( 'sm_mode' );
53 if( $v == '0' ) update_option( 'sm_mode', 'disabled' );
54 elseif( $v == '1' ) update_option( 'sm_mode', 'backup' );
55 elseif( $v == '2' ) update_option( 'sm_mode', 'cdn' );
56
57 if( $v = get_option( 'sm.service_account_name' ) ) {
58 update_option( 'sm_service_account_name', $v );
59 delete_option( 'sm.service_account_name' );
60 }
61
62 if( $v = get_option( 'sm.key_file_path' ) ) {
63 update_option( 'sm_key_file_path', $v );
64 delete_option( 'sm.key_file_path' );
65 }
66
67 if( $v = get_option( 'sm.bucket' ) ) {
68 update_option( 'sm_bucket', $v );
69 delete_option( 'sm.bucket' );
70 }
71
72 delete_option( 'sm.app_name' );
73 delete_option( 'sm.body_rewrite' );
74 delete_option( 'sm.bucket_url_path' );
75 delete_option( 'sm.post_content_rewrite' );
76
77 }
78
79 update_option('dismissed_notice_stateless_cache_busting', true);
80 if ( !$version || version_compare( $version, '2.1.7', '<' ) ){
81 $sm_mode = get_option('sm_mode', null);
82 $hashify_file_name = get_option('sm_hashify_file_name', null);
83 if($version && $sm_mode == 'stateless' && $hashify_file_name == 'true'){
84 delete_option('dismissed_notice_stateless_cache_busting');
85 }
86 }
87
88 if ( !$version || version_compare( $version, '2.2.0', '<' ) ){
89 $sm_synced_files = get_option('sm_synced_files', array());
90 if(!empty($sm_synced_files) && is_array($sm_synced_files)){
91 $table_name = $wpdb->prefix . SyncNonMedia::table;
92 // Backing up the old data with autoload false.
93 // @todo delete in future release.
94 add_option('__sm_synced_files', $sm_synced_files, null, false);
95
96 $files = array();
97 $place_holders = array();
98 $query = "INSERT INTO $table_name (file, status) VALUES ";
99
100 $sm_synced_files = array_unique($sm_synced_files);
101 foreach ($sm_synced_files as $key => $file) {
102 array_push($files, $file, 'synced');
103 $place_holders[] = "('%s', '%s')"; /* In my case, i know they will always be integers */
104 }
105 $query .= implode(', ', $place_holders);
106 $query = $wpdb->prepare("$query ", $files);
107 $wpdb->query( $query );
108
109 delete_option('sm_synced_files');
110 }
111 }
112
113 if ( !is_multisite() && $version && version_compare( $version, '3.0', '<' ) ){
114 self::migrate_root_dir();
115 //updating mode name `stateless` to `ephemeral`
116 $sm_mode = get_option( 'sm_mode' );
117 if ($sm_mode == 'stateless') update_option( 'sm_mode', 'ephemeral' );
118 }
119
120 update_option( 'wp_sm_version', ud_get_stateless_media()->args[ 'version' ] );
121
122 }
123
124 /**
125 * Upgrade Network Enabled
126 *
127 */
128 private static function upgrade_network() {
129
130 $version = get_site_option( 'wp_sm_version', false );
131
132 /**
133 * Upgrade to v.1.2.0 requirements
134 */
135 if ( !$version || version_compare( $version, '1.2.0', '<' ) ) {
136
137 if( $v = get_site_option( 'sm.key_file_path' ) ) {
138 update_site_option( 'sm_key_file_path', $v );
139 delete_site_option( 'sm.key_file_path' );
140 }
141
142 if( $v = get_option( 'sm.service_account_name' ) ) {
143 update_site_option( 'sm_service_account_name', $v );
144 delete_site_option( 'sm.service_account_name' );
145 }
146
147 }
148
149 /**
150 * Upgrade to v.3.0 requirements
151 */
152 if ( is_multisite() && $version && version_compare( $version, '3.0', '<' ) ){
153 $sites = get_sites();
154 foreach ( $sites as $site ) {
155 switch_to_blog( $site->blog_id );
156 self::migrate_root_dir(true);
157 //updating mode name `stateless` to `ephemeral`
158 $sm_mode = get_option( 'sm_mode' );
159 if ($sm_mode == 'stateless') update_option( 'sm_mode', 'ephemeral' );
160 restore_current_blog();
161 }
162
163 //removing network option, because sites could uses different setting for Orginize media
164 update_network_option( 1, 'sm_root_dir', '' );
165
166 //forcing `Cache-Busting` for multisite to prevent replacing media with same filenames
167 update_network_option( 1, 'sm_hashify_file_name', 'true' );
168
169
170
171 $sm_mode_site = get_site_option( 'sm_mode' );
172 if ($sm_mode_site == 'stateless') update_site_option( 'sm_mode', 'ephemeral' );
173
174
175 }
176
177 update_site_option( 'wp_sm_version', ud_get_stateless_media()->args[ 'version' ] );
178
179 }
180
181 /**
182 * Fresh install
183 */
184 private static function fresh_install(){
185 if ( is_multisite() ) {
186 $sites = get_sites();
187 foreach ( $sites as $site ) {
188 switch_to_blog( $site->blog_id );
189 self::migrate_root_dir(true, true);
190 restore_current_blog();
191 }
192 }
193 }
194
195 /**
196 *
197 * @param bool $multisite pass true to use multisite prefix
198 * @param bool $fresh_install for first install
199 * @return string `sm_root_dir` value
200 */
201 private static function migrate_root_dir($multisite = false, $fresh_install = false){
202 $network_root_dir = get_network_option(1, 'sm_root_dir');
203 $sm_root_dir = $network_root_dir ? $network_root_dir : get_option('sm_root_dir', '');
204 $organize_media = get_option('uploads_use_yearmonth_folders');
205
206 if( $organize_media == '1' && empty( $sm_root_dir ) ){
207 $sm_root_dir = '/%date_year/date_month%/';
208 } elseif ( !empty( $sm_root_dir ) && $organize_media == '1' ) {
209 $sm_root_dir = trim($sm_root_dir, ' /') . '/%date_year/date_month%/';
210 } elseif ( !empty( $sm_root_dir ) ) {
211 // $sm_root_dir = $sm_root_dir;
212 } elseif ( $organize_media != '1' ) {
213 $sm_root_dir = '';
214 }
215
216 if($multisite && $fresh_install){
217 if ( empty( $sm_root_dir ) || $sm_root_dir[0] !== '/' ) {
218 $sm_root_dir = "/" . $sm_root_dir;
219 }
220 if(false === strpos($sm_root_dir, '/sites/%site_id%')){
221 $sm_root_dir = '/sites/%site_id%' . $sm_root_dir;
222 }
223 }
224
225 update_option( 'sm_root_dir', $sm_root_dir );
226
227 if( $multisite ) {
228 //forcing `Cache-Busting` for multisite to prevent replacing media with same filenames
229 update_option( 'sm_hashify_file_name', 'true' );
230 }
231
232 return $sm_root_dir;
233 }
234 }
235
236 }
237
238 }