-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathpage-optimize.php
More file actions
313 lines (257 loc) · 8.94 KB
/
page-optimize.php
File metadata and controls
313 lines (257 loc) · 8.94 KB
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
<?php
/*
Plugin Name: Page Optimize
Plugin URI: https://wordpress.org/plugins/page-optimize/
Description: Optimizes JS and CSS for faster page load and render in the browser.
Author: Automattic
Version: 0.6.3
Author URI: http://automattic.com/
*/
// Default cache directory
if ( ! defined( 'PAGE_OPTIMIZE_CACHE_DIR' ) ) {
define( 'PAGE_OPTIMIZE_CACHE_DIR', WP_CONTENT_DIR . '/cache/page_optimize' );
}
if ( ! defined( 'PAGE_OPTIMIZE_ABSPATH' ) ) {
define( 'PAGE_OPTIMIZE_ABSPATH', ABSPATH );
}
if ( ! defined( 'PAGE_OPTIMIZE_CSS_MINIFY' ) ) {
define( 'PAGE_OPTIMIZE_CSS_MINIFY', false );
}
define( 'PAGE_OPTIMIZE_CRON_CACHE_CLEANUP_JOB', 'page_optimize_cron_cache_cleanup' );
// TODO: Copy tests from nginx-http-concat and/or write them
// TODO: Make concat URL dir configurable
if ( isset( $_SERVER['REQUEST_URI'] ) && '/_static/' === substr( $_SERVER['REQUEST_URI'], 0, 9 ) ) {
require_once __DIR__ . '/service.php';
exit;
}
function page_optimize_cache_cleanup( $cache_folder = false, $file_age = DAY_IN_SECONDS ) {
if ( ! is_dir( $cache_folder ) ) {
return;
}
// If cache is disabled when the cleanup runs, purge it
$using_cache = defined( 'PAGE_OPTIMIZE_CACHE_DIR' ) && ! empty( PAGE_OPTIMIZE_CACHE_DIR );
if ( ! $using_cache ) {
$file_age = 0;
}
// If the cache folder changed since queueing, purge it
if ( $using_cache && $cache_folder !== PAGE_OPTIMIZE_CACHE_DIR ) {
$file_age = 0;
}
// Grab all files in the cache directory
$cache_files = glob( $cache_folder . '/page-optimize-cache-*' );
// Cleanup all files older than $file_age
foreach ( $cache_files as $cache_file ) {
if ( ! is_file( $cache_file ) ) {
continue;
}
if ( ( time() - $file_age ) > filemtime( $cache_file ) ) {
unlink( $cache_file );
}
}
}
add_action( PAGE_OPTIMIZE_CRON_CACHE_CLEANUP_JOB, 'page_optimize_cache_cleanup' );
// Unschedule cache cleanup, and purge cache directory
function page_optimize_deactivate() {
$cache_folder = false;
if ( defined( 'PAGE_OPTIMIZE_CACHE_DIR' ) && ! empty( PAGE_OPTIMIZE_CACHE_DIR ) ) {
$cache_folder = PAGE_OPTIMIZE_CACHE_DIR;
}
page_optimize_cache_cleanup( $cache_folder, 0 /* max file age in seconds */ );
wp_clear_scheduled_hook( PAGE_OPTIMIZE_CRON_CACHE_CLEANUP_JOB, [ $cache_folder ] );
}
register_deactivation_hook( __FILE__, 'page_optimize_deactivate' );
function page_optimize_uninstall() {
// Run cleanup on uninstall. You can uninstall an active plugin w/o deactivation.
page_optimize_deactivate();
// JS
delete_option( 'page_optimize-js' );
delete_option( 'page_optimize-load-mode' );
delete_option( 'page_optimize-js-exclude' );
// CSS
delete_option( 'page_optimize-css' );
delete_option( 'page_optimize-css-exclude' );
}
register_uninstall_hook( __FILE__, 'page_optimize_uninstall' );
function page_optimize_get_text_domain() {
return 'page-optimize';
}
function page_optimize_should_concat_js() {
// Support query param for easy testing
if ( isset( $_GET['concat-js'] ) ) {
return $_GET['concat-js'] !== '0';
}
return !! get_option( 'page_optimize-js', page_optimize_js_default() );
}
// TODO: Support JS load mode regardless of whether concat is enabled
function page_optimize_load_mode_js() {
// Support query param for easy testing
if ( ! empty( $_GET['load-mode-js'] ) ) {
$load_mode = page_optimize_sanitize_js_load_mode( $_GET['load-mode-js'] );
} else {
$load_mode = page_optimize_sanitize_js_load_mode( get_option( 'page_optimize-load-mode', page_optimize_js_load_mode_default() ) );
}
return $load_mode;
}
function page_optimize_should_concat_css() {
// Support query param for easy testing
if ( isset( $_GET['concat-css'] ) ) {
return $_GET['concat-css'] !== '0';
}
return !! get_option( 'page_optimize-css', page_optimize_css_default() );
}
function page_optimize_js_default() {
return true;
}
function page_optimize_css_default() {
return true;
}
function page_optimize_js_load_mode_default() {
return '';
}
function page_optimize_js_exclude_list() {
$exclude_list = get_option( 'page_optimize-js-exclude' );
if ( false === $exclude_list ) {
// Use the default since the option is not set
return page_optimize_js_exclude_list_default();
}
if ( '' === $exclude_list ) {
return [];
}
return explode( ',', $exclude_list );
}
function page_optimize_js_exclude_list_default() {
// WordPress core stuff, a lot of other plugins depend on it.
return [ 'jquery', 'jquery-core', 'underscore', 'backbone' ];
}
function page_optimize_css_exclude_list() {
$exclude_list = get_option( 'page_optimize-css-exclude' );
if ( false === $exclude_list ) {
// Use the default since the option is not set
return page_optimize_css_exclude_list_default();
}
if ( '' === $exclude_list ) {
return [];
}
return explode( ',', $exclude_list );
}
function page_optimize_css_exclude_list_default() {
// WordPress Core and known conflicting plugins
return [ 'admin-bar', 'dashicons', 'elementor-app' ];
}
function page_optimize_sanitize_js_load_mode( $value ) {
switch ( $value ) {
case 'async':
case 'defer':
break;
default:
$value = '';
break;
}
return $value;
}
function page_optimize_sanitize_exclude_field( $value ) {
if ( empty( $value ) ) {
return '';
}
$excluded_strings = explode( ',', sanitize_text_field( $value ) );
$sanitized_values = [];
foreach ( $excluded_strings as $excluded_string ) {
if ( ! empty( $excluded_string ) ) {
$sanitized_values[] = trim( $excluded_string );
}
}
return implode( ',', $sanitized_values );
}
/**
* Determines whether a string starts with another string.
*/
function page_optimize_starts_with( $prefix, $str ) {
$prefix_length = strlen( $prefix );
if ( strlen( $str ) < $prefix_length ) {
return false;
}
return substr( $str, 0, $prefix_length ) === $prefix;
}
/**
* Answers whether the plugin should provide concat resource URIs
* that are relative to a common ancestor directory. Assuming a common ancestor
* allows us to skip resolving resource URIs to filesystem paths later on.
*/
function page_optimize_use_concat_base_dir() {
return defined( 'PAGE_OPTIMIZE_CONCAT_BASE_DIR' ) && file_exists( PAGE_OPTIMIZE_CONCAT_BASE_DIR );
}
/**
* Get a filesystem path relative to a configured base path for resources
* that will be concatenated. Assuming a common ancestor allows us to skip
* resolving resource URIs to filesystem paths later on.
*/
function page_optimize_remove_concat_base_prefix( $original_fs_path ) {
// Always check longer path first
if ( strlen( PAGE_OPTIMIZE_ABSPATH ) > strlen( PAGE_OPTIMIZE_CONCAT_BASE_DIR ) ) {
$longer_path = PAGE_OPTIMIZE_ABSPATH;
$shorter_path = PAGE_OPTIMIZE_CONCAT_BASE_DIR;
} else {
$longer_path = PAGE_OPTIMIZE_CONCAT_BASE_DIR;
$shorter_path = PAGE_OPTIMIZE_ABSPATH;
}
$prefix_abspath = trailingslashit( $longer_path );
if ( page_optimize_starts_with( $prefix_abspath, $original_fs_path ) ) {
return substr( $original_fs_path, strlen( $prefix_abspath ) );
}
$prefix_basedir = trailingslashit( $shorter_path );
if ( page_optimize_starts_with( $prefix_basedir, $original_fs_path ) ) {
return substr( $original_fs_path, strlen( $prefix_basedir ) );
}
// If we end up here, this is a resource we shouldn't have tried to concat in the first place
return '/page-optimize-resource-outside-base-path/' . basename( $original_fs_path );
}
function page_optimize_schedule_cache_cleanup() {
$cache_folder = false;
if ( defined( 'PAGE_OPTIMIZE_CACHE_DIR' ) && ! empty( PAGE_OPTIMIZE_CACHE_DIR ) ) {
$cache_folder = PAGE_OPTIMIZE_CACHE_DIR;
}
$args = [ $cache_folder ];
// If caching is on, and job isn't queued for current cache folder
if( false !== $cache_folder && false === wp_next_scheduled( PAGE_OPTIMIZE_CRON_CACHE_CLEANUP_JOB, $args ) ) {
wp_schedule_event( time(), 'daily', PAGE_OPTIMIZE_CRON_CACHE_CLEANUP_JOB, $args );
}
}
// Cases when we don't want to concat
function page_optimize_bail() {
// Bail if we're in customizer
global $wp_customize;
if ( isset( $wp_customize ) ) {
return true;
}
// Bail if we're in any of the excluded pages.
global $pagenow;
$excluded_pages = array(
'post.php',
'post-new.php',
'site-editor.php',
);
if ( isset( $pagenow ) && in_array( $pagenow, $excluded_pages ) ) {
return true;
}
// Bail if Divi theme is active, and we're in the Divi Front End Builder
if ( ! empty( $_GET['et_fb'] ) && 'Divi' === wp_get_theme()->get_template() ) {
return true;
}
// Bail if we're editing pages in Brizy Editor
if ( class_exists( 'Brizy_Editor' ) && method_exists( 'Brizy_Editor', 'prefix' ) && ( isset( $_GET[ Brizy_Editor::prefix( '-edit-iframe' ) ] ) || isset( $_GET[ Brizy_Editor::prefix( '-edit' ) ] ) ) ) {
return true;
}
return false;
}
function page_optimize_init() {
if ( page_optimize_bail() ) {
return;
}
page_optimize_schedule_cache_cleanup();
require_once __DIR__ . '/settings.php';
require_once __DIR__ . '/concat-css.php';
require_once __DIR__ . '/concat-js.php';
// Disable Jetpack photon-cdn for static JS/CSS
add_filter( 'jetpack_force_disable_site_accelerator', '__return_true' );
}
add_action( 'plugins_loaded', 'page_optimize_init' );