PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 2.1
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v2.1
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 2.1, at includes/classes/Install.php

269 lines 7.8 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 update_site_option( DB_VERSION_OPTION_NAME, POWERED_CACHE_DB_VERSION );
99 }
100 }
101
102 /**
103 * Upgrade routine
104 */
105 public function maybe_upgrade() {
106 if ( version_compare( get_option( DB_VERSION_OPTION_NAME ), POWERED_CACHE_DB_VERSION, '<' ) ) {
107 $this->maybe_migrate_from_1x();
108
109 \PoweredCache\Utils\log( sprintf( 'Upgrade DB version: %d', POWERED_CACHE_DB_VERSION ) );
110 update_option( DB_VERSION_OPTION_NAME, POWERED_CACHE_DB_VERSION );
111 }
112 }
113
114
115 /**
116 * Check if a lock exists of the upgrade routine
117 *
118 * @param string $lock_name transient name
119 *
120 * @return bool
121 */
122 private function has_lock( $lock_name ) {
123 if ( POWERED_CACHE_IS_NETWORK ) {
124 if ( 'yes' === get_site_transient( $lock_name ) ) {
125 return true;
126 }
127
128 return false;
129 }
130
131 if ( 'yes' === get_transient( $lock_name ) ) {
132 return true;
133 }
134
135 return false;
136 }
137
138 /**
139 * Set the lock
140 *
141 * @param string $lock_name transient name for the lock
142 *
143 * @return bool
144 */
145 private function set_lock( $lock_name ) {
146 if ( POWERED_CACHE_IS_NETWORK ) {
147 return set_site_transient( $lock_name, 'yes', MINUTE_IN_SECONDS );
148 }
149
150 return set_transient( $lock_name, 'yes', MINUTE_IN_SECONDS );
151 }
152
153 /**
154 * Remove lock
155 *
156 * @param string $lock_name transient name for the lock
157 *
158 * @return bool
159 */
160 private function remove_lock( $lock_name ) {
161 if ( POWERED_CACHE_IS_NETWORK ) {
162 return delete_site_transient( $lock_name );
163 }
164
165 return delete_transient( $lock_name );
166 }
167
168
169 /**
170 * Migrate existing options and extension settings from 1.x
171 */
172 public function maybe_migrate_from_1x() {
173 if ( POWERED_CACHE_IS_NETWORK ) { // 1x wasn't support network wide activation
174 return;
175 }
176
177 $db_version = get_option( DB_VERSION_OPTION_NAME );
178 $old_options = get_option( SETTING_OPTION );
179 $default_options = \PoweredCache\Utils\get_settings();
180
181 if ( empty( $db_version ) && ! empty( $old_options ) ) {
182 \PoweredCache\Utils\log( 'Upgrading from version 1.x' );
183 $migrated_options = [];
184
185 // migrate the settings with same key
186 foreach ( $default_options as $key => $default_value ) {
187 if ( isset( $old_options[ $key ] ) ) {
188 $migrated_options[ $key ] = $old_options[ $key ];
189 } else {
190 $migrated_options[ $key ] = $default_value;
191 }
192 }
193
194 $changed_keys = [
195 'enable_page_caching' => 'enable_page_cache',
196 'configure_htaccess' => 'auto_configure_htaccess',
197 'cdn_status' => 'enable_cdn',
198 'show_cache_message' => 'cache_footprint',
199 ];
200
201 foreach ( $changed_keys as $old_key => $new_key ) {
202 if ( isset( $old_options[ $old_key ] ) ) {
203 $migrated_options[ $new_key ] = $old_options[ $old_key ];
204 }
205 }
206
207 $active_extensions = (array) $old_options['active_extensions'];
208 $extension_settings = (array) $old_options['extension_settings'];
209
210 $migrated_options['enable_cloudflare'] = in_array( 'cloudflare', $active_extensions, true );
211 if ( ! empty( $extension_settings['cloudflare'] ) ) {
212 $migrated_options['cloudflare_email'] = $extension_settings['cloudflare']['email'];
213 $migrated_options['cloudflare_api_key'] = $extension_settings['cloudflare']['api_key'];
214 $migrated_options['cloudflare_zone'] = $extension_settings['cloudflare']['zone'];
215 }
216
217 $migrated_options['enable_lazy_load'] = in_array( 'lazy-load', $active_extensions, true );
218
219 if ( ! empty( $extension_settings['lazyload'] ) ) {
220 $migrated_options['lazy_load_post_content'] = $extension_settings['lazyload']['post_content'];
221 $migrated_options['lazy_load_images'] = $extension_settings['lazyload']['image'];
222 $migrated_options['lazy_load_iframes'] = $extension_settings['lazyload']['iframe'];
223 $migrated_options['lazy_load_widgets'] = $extension_settings['lazyload']['widget_text'];
224 $migrated_options['lazy_load_post_thumbnail'] = $extension_settings['lazyload']['post_thumbnail'];
225 $migrated_options['lazy_load_avatars'] = $extension_settings['lazyload']['avatar'];
226 }
227
228 $migrated_options['enable_cache_preload'] = in_array( 'preload', $active_extensions, true );
229
230 if ( ! empty( $extension_settings['preload'] ) ) {
231 $migrated_options['preload_homepage'] = (bool) $extension_settings['preload']['homepage'];
232 $migrated_options['preload_public_posts'] = (bool) $extension_settings['preload']['post_count'];
233 $migrated_options['preload_public_tax'] = (bool) $extension_settings['preload']['taxonomies'];
234 $migrated_options['enable_sitemap_preload'] = (bool) $extension_settings['preload']['sitemap_integration'];
235 $migrated_options['preload_sitemap'] = $extension_settings['preload']['sitemaps'];
236 }
237
238 $migrated_options['enable_varnish'] = in_array( 'varnish', $active_extensions, true );
239 if ( ! empty( $extension_settings['varnish'] ) ) {
240 $migrated_options['varnish_ip'] = $extension_settings['varnish']['varnish_ip'];
241 }
242
243 if ( in_array( 'minifier', $active_extensions, true ) && ! empty( $extension_settings['minifier'] ) ) {
244 $migrated_options['minify_html'] = $extension_settings['minifier']['minify_html'];
245 $migrated_options['minify_css'] = $extension_settings['minifier']['minify_css'];
246 $migrated_options['combine_css'] = $extension_settings['minifier']['concat_css'];
247 $migrated_options['minify_js'] = $extension_settings['minifier']['minify_js'];
248 $migrated_options['combine_js'] = $extension_settings['minifier']['concat_js'];
249 $migrated_options['excluded_css_files'] = $extension_settings['minifier']['excluded_css'];
250 $migrated_options['excluded_js_files'] = $extension_settings['minifier']['excluded_js'];
251 $migrated_options['js_execution_method'] = $extension_settings['minifier']['js_execution'];
252 }
253
254 update_option( SETTING_OPTION, $migrated_options );
255 Config::factory()->save_configuration( $migrated_options ); // make it current
256
257 \PoweredCache\Utils\log( 'Upgraded from version 1.x' );
258
259 // remove old crons
260 wp_clear_scheduled_hook( 'powered_cache_preload_hook' );
261 wp_clear_scheduled_hook( 'powered_cache_preload_child_process' );
262 wp_clear_scheduled_hook( 'powered_cache_purge_cache' );
263 }
264 }
265
266
267 }
268
269