PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 3.7.2
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v3.7.2
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / classes / Install.php

Install.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 3.7.2, at includes/classes/Install.php

425 lines 12.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Installation functionalities
4 *
5 * @package PoweredCache
6 */
7
8 namespace PoweredCache;
9
10 use const PoweredCache\Constants\DB_VERSION_OPTION_NAME;
11 use const PoweredCache\Constants\SETTING_OPTION;
12
13 /**
14 * Class Install
15 */
16 class Install {
17 /**
18 * Placeholder constructor.
19 */
20 public function __construct() {
21 }
22
23 /**
24 * Return an instance of the current class
25 *
26 * @since 2.0
27 */
28 public static function factory() {
29 static $instance = false;
30
31 if ( ! $instance ) {
32 $instance = new self();
33 $instance->setup();
34 }
35
36 return $instance;
37 }
38
39 /**
40 * Setup Hooks
41 */
42 public function setup() {
43 add_action( 'init', [ $this, 'check_version' ], 5 );
44 }
45
46 /**
47 * Check DB version and run the updater is required.
48 */
49 public function check_version() {
50 if ( defined( 'IFRAME_REQUEST' ) && IFRAME_REQUEST ) {
51 return;
52 }
53
54 if ( version_compare( get_option( DB_VERSION_OPTION_NAME ), POWERED_CACHE_DB_VERSION, '<' ) ) {
55 $this->install();
56 /**
57 * Fires after plugin update.
58 *
59 * @hook powered_cache_updated
60 *
61 * @since 2.0
62 */
63 do_action( 'powered_cache_updated' );
64 }
65 }
66
67 /**
68 * Perform Installation
69 */
70 public function install() {
71 if ( ! is_blog_installed() ) {
72 return;
73 }
74
75 $lock_key = 'powered_cache_installing';
76 // Check if we are not already running
77 if ( $this->has_lock( $lock_key ) ) {
78 return;
79 }
80
81 // lets set the transient now.
82 $this->set_lock( $lock_key );
83
84 if ( POWERED_CACHE_IS_NETWORK ) {
85 $this->maybe_upgrade_network_wide();
86 } else {
87 $this->maybe_upgrade();
88 }
89
90 $this->remove_lock( $lock_key );
91 }
92
93 /**
94 * Upgrade routine for network wide activation
95 */
96 public function maybe_upgrade_network_wide() {
97 if ( version_compare( get_site_option( DB_VERSION_OPTION_NAME ), POWERED_CACHE_DB_VERSION, '<' ) ) {
98 $this->upgrade_30( true );
99 $this->upgrade_32( true );
100 $this->upgrade_33( true );
101 $this->upgrade_34( true );
102
103 \PoweredCache\Utils\log( sprintf( '[Networkwide] Upgrade DB version: %s', POWERED_CACHE_DB_VERSION ) );
104 update_site_option( DB_VERSION_OPTION_NAME, POWERED_CACHE_DB_VERSION );
105 }
106 }
107
108 /**
109 * Upgrade routine
110 */
111 public function maybe_upgrade() {
112 if ( version_compare( get_option( DB_VERSION_OPTION_NAME ), POWERED_CACHE_DB_VERSION, '<' ) ) {
113 \PoweredCache\Utils\log( sprintf( 'Upgrade DB version: %s', POWERED_CACHE_DB_VERSION ) );
114 $this->maybe_migrate_from_1x();
115 $this->upgrade_30();
116 $this->upgrade_32();
117 $this->upgrade_33();
118 $this->upgrade_34();
119
120 update_option( DB_VERSION_OPTION_NAME, POWERED_CACHE_DB_VERSION );
121 }
122 }
123
124 /**
125 * Changing "accepted query string" as "ignored query string" with version 3.x
126 *
127 * @param bool $network_wide whether plugin activated network-wide or not
128 *
129 * @return void
130 * @since 3.0
131 */
132 public function upgrade_30( $network_wide = false ) {
133 $current_version = $network_wide ? get_site_option( DB_VERSION_OPTION_NAME ) : get_option( DB_VERSION_OPTION_NAME );
134 if ( ! version_compare( $current_version, '3.0', '<' ) ) {
135 return;
136 }
137
138 $settings = \PoweredCache\Utils\get_settings( $network_wide );
139
140 if ( empty( $settings['accepted_query_strings'] ) ) {
141 return;
142 }
143
144 $settings['ignored_query_strings'] = $settings['accepted_query_strings'];
145 unset( $settings['accepted_query_strings'] );
146
147 if ( $network_wide ) {
148 update_site_option( SETTING_OPTION, $settings );
149 } else {
150 update_option( SETTING_OPTION, $settings );
151 }
152
153 Config::factory()->save_configuration( $settings, $network_wide );
154 \PoweredCache\Utils\log( 'Upgraded to version 3.0' );
155 }
156
157 /**
158 * Changing js execution method
159 *
160 * @param bool $network_wide whether plugin activated network-wide or not
161 *
162 * @return void
163 * @since 3.2
164 */
165 public function upgrade_32( $network_wide = false ) {
166 $current_version = $network_wide ? get_site_option( DB_VERSION_OPTION_NAME ) : get_option( DB_VERSION_OPTION_NAME );
167 if ( ! version_compare( $current_version, '3.2', '<' ) ) {
168 return;
169 }
170
171 $settings = \PoweredCache\Utils\get_settings( $network_wide );
172
173 if ( ! empty( $settings['js_execution_method'] ) ) {
174 if ( in_array( $settings['js_execution_method'], [ 'async', 'defer' ], true ) ) {
175 $settings['js_defer'] = true;
176 }
177
178 if ( 'delayed' === $settings['js_execution_method'] ) {
179 $settings['js_delay'] = true;
180 $settings['combine_js'] = false;
181 }
182
183 unset( $settings['js_execution_method'] );
184 unset( $settings['js_execution_optimized_only'] );
185 }
186
187 if ( $network_wide ) {
188 update_site_option( SETTING_OPTION, $settings );
189 } else {
190 update_option( SETTING_OPTION, $settings );
191 }
192
193 Config::factory()->save_configuration( $settings, $network_wide );
194 \PoweredCache\Utils\log( 'Upgraded to version 3.2' );
195 }
196
197 /**
198 * Upgrade routine for version 3.3
199 *
200 * @param bool $network_wide whether plugin activated network-wide or not
201 *
202 * @return void
203 */
204 public function upgrade_33( $network_wide = false ) {
205 $current_version = $network_wide ? get_site_option( DB_VERSION_OPTION_NAME ) : get_option( DB_VERSION_OPTION_NAME );
206 if ( ! version_compare( $current_version, '3.3', '<' ) ) {
207 return;
208 }
209
210 $settings = \PoweredCache\Utils\get_settings( $network_wide );
211
212 // disable rewrite file optimizer if auto configure htaccess is disabled
213 if ( ! $settings['auto_configure_htaccess'] && $settings['rewrite_file_optimizer'] ) {
214 $settings['rewrite_file_optimizer'] = false;
215 }
216
217 if ( $network_wide ) {
218 update_site_option( SETTING_OPTION, $settings );
219 } else {
220 update_option( SETTING_OPTION, $settings );
221 }
222
223 Config::factory()->save_configuration( $settings, $network_wide );
224
225 if ( $settings['auto_configure_htaccess'] && $settings['rewrite_file_optimizer'] ) {
226 if ( $network_wide ) {
227 \PoweredCache\Utils\clean_page_cache_dir();
228 } else {
229 \PoweredCache\Utils\clean_site_cache_dir();
230 }
231 }
232
233 \PoweredCache\Utils\log( 'Upgraded to version 3.3' );
234 }
235
236 /**
237 * Upgrade routine for version 3.4
238 *
239 * @param bool $network_wide whether plugin activated network-wide or not
240 * *
241 * @return void
242 */
243 public function upgrade_34( $network_wide = false ) {
244 $current_version = $network_wide ? get_site_option( DB_VERSION_OPTION_NAME ) : get_option( DB_VERSION_OPTION_NAME );
245 if ( ! version_compare( $current_version, '3.4', '<' ) ) {
246 return;
247 }
248
249 $settings = \PoweredCache\Utils\get_settings( $network_wide );
250
251 $encryption = new Encryption();
252
253 if ( ! empty( $settings['cloudflare_api_key'] ) && false === $encryption->decrypt( $settings['cloudflare_api_key'] ) ) {
254 $settings['cloudflare_api_key'] = $encryption->encrypt( $settings['cloudflare_api_key'] );
255 }
256
257 if ( ! empty( $settings['cloudflare_api_token'] ) && false === $encryption->decrypt( $settings['cloudflare_api_token'] ) ) {
258 $settings['cloudflare_api_token'] = $encryption->encrypt( $settings['cloudflare_api_token'] );
259 }
260
261 if ( $network_wide ) {
262 update_site_option( SETTING_OPTION, $settings );
263 } else {
264 update_option( SETTING_OPTION, $settings );
265 }
266
267 Config::factory()->save_configuration( $settings, $network_wide );
268 \PoweredCache\Utils\log( 'Upgraded to version 3.4' );
269 }
270
271 /**
272 * Check if a lock exists of the upgrade routine
273 *
274 * @param string $lock_name transient name
275 *
276 * @return bool
277 */
278 private function has_lock( $lock_name ) {
279 if ( POWERED_CACHE_IS_NETWORK ) {
280 if ( 'yes' === get_site_transient( $lock_name ) ) {
281 return true;
282 }
283
284 return false;
285 }
286
287 if ( 'yes' === get_transient( $lock_name ) ) {
288 return true;
289 }
290
291 return false;
292 }
293
294 /**
295 * Set the lock
296 *
297 * @param string $lock_name transient name for the lock
298 *
299 * @return bool
300 */
301 private function set_lock( $lock_name ) {
302 if ( POWERED_CACHE_IS_NETWORK ) {
303 return set_site_transient( $lock_name, 'yes', MINUTE_IN_SECONDS );
304 }
305
306 return set_transient( $lock_name, 'yes', MINUTE_IN_SECONDS );
307 }
308
309 /**
310 * Remove lock
311 *
312 * @param string $lock_name transient name for the lock
313 *
314 * @return bool
315 */
316 private function remove_lock( $lock_name ) {
317 if ( POWERED_CACHE_IS_NETWORK ) {
318 return delete_site_transient( $lock_name );
319 }
320
321 return delete_transient( $lock_name );
322 }
323
324
325 /**
326 * Migrate existing options and extension settings from 1.x
327 */
328 public function maybe_migrate_from_1x() {
329 if ( POWERED_CACHE_IS_NETWORK ) { // 1x wasn't support network wide activation
330 return;
331 }
332
333 $db_version = get_option( DB_VERSION_OPTION_NAME );
334 $old_options = get_option( SETTING_OPTION );
335 $default_options = \PoweredCache\Utils\get_settings();
336
337 if ( empty( $db_version ) && ! empty( $old_options ) ) {
338 \PoweredCache\Utils\log( 'Upgrading from version 1.x' );
339 $migrated_options = [];
340
341 // migrate the settings with same key
342 foreach ( $default_options as $key => $default_value ) {
343 if ( isset( $old_options[ $key ] ) ) {
344 $migrated_options[ $key ] = $old_options[ $key ];
345 } else {
346 $migrated_options[ $key ] = $default_value;
347 }
348 }
349
350 $changed_keys = [
351 'enable_page_caching' => 'enable_page_cache',
352 'configure_htaccess' => 'auto_configure_htaccess',
353 'cdn_status' => 'enable_cdn',
354 'show_cache_message' => 'cache_footprint',
355 ];
356
357 foreach ( $changed_keys as $old_key => $new_key ) {
358 if ( isset( $old_options[ $old_key ] ) ) {
359 $migrated_options[ $new_key ] = $old_options[ $old_key ];
360 }
361 }
362
363 $active_extensions = (array) $old_options['active_extensions'];
364 $extension_settings = (array) $old_options['extension_settings'];
365
366 $migrated_options['enable_cloudflare'] = in_array( 'cloudflare', $active_extensions, true );
367 if ( ! empty( $extension_settings['cloudflare'] ) ) {
368 $migrated_options['cloudflare_email'] = $extension_settings['cloudflare']['email'];
369 $migrated_options['cloudflare_api_key'] = $extension_settings['cloudflare']['api_key'];
370 $migrated_options['cloudflare_zone'] = $extension_settings['cloudflare']['zone'];
371 }
372
373 $migrated_options['enable_lazy_load'] = in_array( 'lazy-load', $active_extensions, true );
374
375 if ( ! empty( $extension_settings['lazyload'] ) ) {
376 $migrated_options['lazy_load_post_content'] = $extension_settings['lazyload']['post_content'];
377 $migrated_options['lazy_load_images'] = $extension_settings['lazyload']['image'];
378 $migrated_options['lazy_load_iframes'] = $extension_settings['lazyload']['iframe'];
379 $migrated_options['lazy_load_widgets'] = $extension_settings['lazyload']['widget_text'];
380 $migrated_options['lazy_load_post_thumbnail'] = $extension_settings['lazyload']['post_thumbnail'];
381 $migrated_options['lazy_load_avatars'] = $extension_settings['lazyload']['avatar'];
382 }
383
384 $migrated_options['enable_cache_preload'] = in_array( 'preload', $active_extensions, true );
385
386 if ( ! empty( $extension_settings['preload'] ) ) {
387 $migrated_options['preload_homepage'] = (bool) $extension_settings['preload']['homepage'];
388 $migrated_options['preload_public_posts'] = (bool) $extension_settings['preload']['post_count'];
389 $migrated_options['preload_public_tax'] = (bool) $extension_settings['preload']['taxonomies'];
390 $migrated_options['enable_sitemap_preload'] = (bool) $extension_settings['preload']['sitemap_integration'];
391 $migrated_options['preload_sitemap'] = $extension_settings['preload']['sitemaps'];
392 }
393
394 $migrated_options['enable_varnish'] = in_array( 'varnish', $active_extensions, true );
395 if ( ! empty( $extension_settings['varnish'] ) ) {
396 $migrated_options['varnish_ip'] = $extension_settings['varnish']['varnish_ip'];
397 }
398
399 if ( in_array( 'minifier', $active_extensions, true ) && ! empty( $extension_settings['minifier'] ) ) {
400 $migrated_options['minify_html'] = $extension_settings['minifier']['minify_html'];
401 $migrated_options['minify_css'] = $extension_settings['minifier']['minify_css'];
402 $migrated_options['combine_css'] = $extension_settings['minifier']['concat_css'];
403 $migrated_options['minify_js'] = $extension_settings['minifier']['minify_js'];
404 $migrated_options['combine_js'] = $extension_settings['minifier']['concat_js'];
405 $migrated_options['excluded_css_files'] = $extension_settings['minifier']['excluded_css'];
406 $migrated_options['excluded_js_files'] = $extension_settings['minifier']['excluded_js'];
407 $migrated_options['js_execution_method'] = $extension_settings['minifier']['js_execution'];
408 }
409
410 update_option( SETTING_OPTION, $migrated_options );
411 Config::factory()->save_configuration( $migrated_options ); // make it current
412
413 \PoweredCache\Utils\log( 'Upgraded from version 1.x' );
414
415 // remove old crons
416 wp_clear_scheduled_hook( 'powered_cache_preload_hook' );
417 wp_clear_scheduled_hook( 'powered_cache_preload_child_process' );
418 wp_clear_scheduled_hook( 'powered_cache_purge_cache' );
419 }
420 }
421
422
423 }
424
425