-
Notifications
You must be signed in to change notification settings - Fork 210
/
upgrader.php
319 lines (276 loc) · 10.3 KB
/
upgrader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?php
use WP_Rocket\Logger\Logger;
defined( 'ABSPATH' ) || exit;
/**
* Tell WP what to do when admin is loaded aka upgrader
*
* @since 1.0
*/
function rocket_upgrader() {
// Grab some infos.
$actual_version = (string) get_rocket_option( 'version' );
// You can hook the upgrader to trigger any action when WP Rocket is upgraded.
// first install.
if ( ! $actual_version ) {
do_action( 'wp_rocket_first_install' );
}
// already installed but got updated.
elseif ( WP_ROCKET_VERSION !== $actual_version ) {
do_action( 'wp_rocket_upgrade', WP_ROCKET_VERSION, $actual_version );
}
// If any upgrade has been done, we flush and update version number.
if ( did_action( 'wp_rocket_first_install' ) || did_action( 'wp_rocket_upgrade' ) ) {
flush_rocket_htaccess();
rocket_renew_all_boxes( 0, [ 'rocket_warning_plugin_modification' ] );
$options = get_option( WP_ROCKET_SLUG ); // do not use get_rocket_option() here.
$options['version'] = WP_ROCKET_VERSION;
$keys = rocket_check_key();
if ( is_array( $keys ) ) {
$options = array_merge( $keys, $options );
}
update_option( WP_ROCKET_SLUG, $options );
}
$page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
if (
'wprocket' === $page
&&
did_action( 'wp_rocket_upgrade' )
) {
wp_safe_redirect( esc_url_raw( admin_url( 'options-general.php?page=wprocket' ) ) );
exit;
}
if ( ! rocket_valid_key() && current_user_can( 'rocket_manage_options' ) && 'wprocket' === $page ) {
add_action( 'admin_notices', 'rocket_need_api_key' );
}
}
add_action( 'admin_init', 'rocket_upgrader' );
/**
* Keeps this function up to date at each version
*
* @since 1.0
*/
function rocket_first_install() {
// Generate an random key for cache dir of user.
$secret_cache_key = create_rocket_uniqid();
// Generate an random key for minify md5 filename.
$minify_css_key = create_rocket_uniqid();
$minify_js_key = create_rocket_uniqid();
// Create Option.
add_option(
rocket_get_constant( 'WP_ROCKET_SLUG' ),
/**
* Filters the default rocket options array
*
* @since 2.8
*
* @param array Array of default rocket options
*/
apply_filters(
'rocket_first_install_options',
[
'secret_cache_key' => $secret_cache_key,
'cache_mobile' => 1,
'do_caching_mobile_files' => 0,
'cache_webp' => 0,
'cache_logged_user' => 0,
'cache_ssl' => 1,
'emoji' => 1,
'cache_reject_uri' => [],
'cache_reject_cookies' => [],
'cache_reject_ua' => [],
'cache_query_strings' => [],
'cache_purge_pages' => [],
'purge_cron_interval' => 10,
'purge_cron_unit' => 'HOUR_IN_SECONDS',
'exclude_css' => [],
'exclude_js' => [],
'exclude_inline_js' => [],
'defer_all_js' => 0,
'async_css' => 0,
'critical_css' => '',
'lazyload' => 0,
'lazyload_iframes' => 0,
'lazyload_youtube' => 0,
'minify_css' => 0,
'minify_css_key' => $minify_css_key,
'minify_concatenate_css' => 0,
'minify_js' => 0,
'minify_js_key' => $minify_js_key,
'minify_concatenate_js' => 0,
'minify_google_fonts' => 1,
'manual_preload' => 1,
'dns_prefetch' => 0,
'preload_fonts' => [],
'database_revisions' => 0,
'database_auto_drafts' => 0,
'database_trashed_posts' => 0,
'database_spam_comments' => 0,
'database_trashed_comments' => 0,
'database_all_transients' => 0,
'database_optimize_tables' => 0,
'schedule_automatic_cleanup' => 0,
'automatic_cleanup_frequency' => 'daily',
'cdn' => 0,
'cdn_cnames' => [],
'cdn_zone' => [],
'cdn_reject_files' => [],
'do_cloudflare' => 0,
'cloudflare_email' => '',
'cloudflare_api_key' => '',
'cloudflare_zone_id' => '',
'cloudflare_devmode' => 0,
'cloudflare_protocol_rewrite' => 0,
'cloudflare_auto_settings' => 0,
'cloudflare_old_settings' => '',
'control_heartbeat' => 1,
'heartbeat_site_behavior' => 'reduce_periodicity',
'heartbeat_admin_behavior' => 'reduce_periodicity',
'heartbeat_editor_behavior' => 'reduce_periodicity',
'varnish_auto_purge' => 0,
'analytics_enabled' => 0,
'sucury_waf_cache_sync' => 0,
'sucury_waf_api_key' => '',
]
)
);
rocket_dismiss_box( 'rocket_warning_plugin_modification' );
}
add_action( 'wp_rocket_first_install', 'rocket_first_install' );
/**
* What to do when Rocket is updated, depending on versions
*
* @since 1.0
*
* @param string $wp_rocket_version Latest WP Rocket version.
* @param string $actual_version Installed WP Rocket version.
*/
function rocket_new_upgrade( $wp_rocket_version, $actual_version ) {
if ( version_compare( $actual_version, '2.4.1', '<' ) ) {
delete_transient( 'rocket_ask_for_update' );
}
if ( version_compare( $actual_version, '2.8', '<' ) ) {
$options = get_option( WP_ROCKET_SLUG );
$options['manual_preload'] = 1;
$options['automatic_preload'] = 1;
$options['sitemap_preload_url_crawl'] = '500000';
update_option( WP_ROCKET_SLUG, $options );
}
// Deactivate CloudFlare completely if PHP Version is lower than 5.4.
if ( version_compare( $actual_version, '2.8.16', '<' ) ) {
$options = get_option( WP_ROCKET_SLUG );
$options['do_cloudflare'] = 0;
$options['cloudflare_email'] = '';
$options['cloudflare_api_key'] = '';
$options['cloudflare_devmode'] = 0;
$options['cloudflare_protocol_rewrite'] = 0;
$options['cloudflare_auto_settings'] = 0;
$options['cloudflare_old_settings'] = '';
update_option( WP_ROCKET_SLUG, $options );
}
// Disable minification options if they're active in Autoptimize.
if ( version_compare( $actual_version, '2.9.5', '<' ) ) {
if ( is_plugin_active( 'autoptimize/autoptimize.php' ) ) {
if ( 'on' === get_option( 'autoptimize_css' ) ) {
update_rocket_option( 'minify_css', 0 );
}
if ( 'on' === get_option( 'autoptimize_js' ) ) {
update_rocket_option( 'minify_js', 0 );
}
}
}
// Delete old transients.
if ( version_compare( $actual_version, '2.9.7', '<' ) ) {
delete_transient( 'rocket_check_licence_30' );
delete_transient( 'rocket_check_licence_1' );
}
if ( version_compare( $actual_version, '2.11', '<' ) ) {
rocket_clean_minify();
}
if ( version_compare( $actual_version, '3.2', '<' ) ) {
// Default Heartbeat settings.
$options = get_option( WP_ROCKET_SLUG, [] );
$options['heartbeat_site_behavior'] = 'reduce_periodicity';
$options['heartbeat_admin_behavior'] = 'reduce_periodicity';
$options['heartbeat_editor_behavior'] = 'reduce_periodicity';
if ( ! empty( $options['automatic_preload'] ) || ! empty( $options['sitemap_preload'] ) ) {
$options['manual_preload'] = 1;
}
update_option( WP_ROCKET_SLUG, $options );
rocket_generate_config_file();
// Create a .htaccess file in the log folder.
$handler = Logger::get_stream_handler();
if ( method_exists( $handler, 'create_htaccess_file' ) ) {
try {
$success = $handler->create_htaccess_file();
} catch ( \Exception $e ) {
$success = false;
}
if ( ! $success ) {
Logger::delete_log_file();
}
}
}
if ( version_compare( $actual_version, '3.2.0.1', '<' ) ) {
wp_safe_remote_get( esc_url( home_url() ) );
}
if ( version_compare( $actual_version, '3.12.6', '<' ) ) {
do_action( 'rocket_preload_unlock_all_urls' );
}
if ( version_compare( $actual_version, '3.3.6', '<' ) ) {
delete_site_transient( 'update_wprocket' );
delete_site_transient( 'update_wprocket_response' );
if ( get_rocket_option( 'do_cloudflare' ) && get_rocket_option( 'cloudflare_auto_settings' ) ) {
if ( function_exists( 'set_rocket_cloudflare_browser_cache_ttl' ) ) {
set_rocket_cloudflare_browser_cache_ttl( '31536000' );
}
}
}
if ( rocket_is_ssl_website() ) {
if ( 1 !== (int) get_rocket_option( 'cache_ssl' ) ) {
update_rocket_option( 'cache_ssl', 1 );
rocket_generate_config_file();
}
}
if ( version_compare( $actual_version, '3.4', '<' ) ) {
wp_clear_scheduled_hook( 'rocket_purge_time_event' );
}
if ( version_compare( $actual_version, '3.6', '<' ) ) {
rocket_clean_cache_busting();
rocket_clean_domain();
}
if ( version_compare( $actual_version, '3.7', '<' ) ) {
rocket_clean_minify( 'css' );
rocket_generate_advanced_cache_file();
}
if ( version_compare( $actual_version, '3.8.1', '<' ) ) {
$options = get_option( rocket_get_constant( 'WP_ROCKET_SLUG' ) );
unset( $options['dequeue_jquery_migrate'] );
update_option( rocket_get_constant( 'WP_ROCKET_SLUG' ), $options );
}
if ( version_compare( $actual_version, '3.9', '<' ) ) {
$busting_path = rocket_get_constant( 'WP_ROCKET_CACHE_BUSTING_PATH' );
rocket_rrmdir( $busting_path . 'facebook-tracking' );
rocket_rrmdir( $busting_path . 'google-tracking' );
wp_clear_scheduled_hook( 'rocket_facebook_tracking_cache_update' );
wp_clear_scheduled_hook( 'rocket_google_tracking_cache_update' );
}
if ( version_compare( $actual_version, '3.10', '<' ) ) {
$options = get_option( rocket_get_constant( 'WP_ROCKET_SLUG' ) );
if (
isset( $options['async_css'] ) && $options['async_css'] &&
isset( $options['remove_unused_css'] ) && $options['remove_unused_css']
) {
$options['async_css'] = 0;
$cache_path = rocket_get_constant( 'WP_ROCKET_CACHE_ROOT_PATH' );
rocket_rrmdir( $cache_path . 'used-css' );
update_option( rocket_get_constant( 'WP_ROCKET_SLUG' ), $options );
}
}
if ( version_compare( $actual_version, '3.11.1', '<' ) ) {
rocket_generate_config_file();
}
if ( version_compare( $actual_version, '3.12.4', '<' ) ) {
delete_transient( 'wp_rocket_pricing' );
}
}
add_action( 'wp_rocket_upgrade', 'rocket_new_upgrade', 10, 2 );