PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / trunk
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score vtrunk
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
← All changes | includes/file-optimizer.php +64 -7 2.2trunk View file →
@@ -60,16 +60,28 @@
60 60 'css' => 'text/css',
61 61 'js' => 'application/javascript',
62 62 );
63 63
64 +$wp_content_base = '/wp-content';
65 +if ( isset( $_SERVER['SCRIPT_NAME'] ) && false === strpos( $_SERVER['SCRIPT_NAME'], '/wp-content' ) ) {
66 + $script_name = explode( '/', ltrim( $_SERVER['SCRIPT_NAME'], '/' ) );
67 + if ( ! empty( $script_name[0] ) ) {
68 + $wp_content_base = '/' . $script_name[0];
69 + }
70 +}
64 71
65 72 $current_dir = file_optimizer_normalize_path( realpath( dirname( __DIR__ ) ) );
66 73
67 74 /* Constants */
68 75 // By default determine the document root from this scripts path in the plugins dir (you can hardcode this define)
69 -define( 'CONCAT_FILES_ROOT', substr( $current_dir, 0, strpos( $current_dir, '/wp-content' ) ) );
70 -define( 'POWERED_CACHE_FO_CACHE_DIR', CONCAT_FILES_ROOT . '/wp-content/cache/min/' );
76 +define( 'CONCAT_FILES_ROOT', substr( $current_dir, 0, strpos( $current_dir, $wp_content_base ) ) );
77 +define( 'POWERED_CACHE_FO_CACHE_DIR', CONCAT_FILES_ROOT . $wp_content_base.'/cache/min/' );
78 +define( 'POWERED_CACHE_FO_DEBUG', false );
71 79
80 +if ( ! defined( 'POWERED_CACHE_FO_DISABLE_CACHE_HEADERS' ) ) {
81 + define( 'POWERED_CACHE_FO_DISABLE_CACHE_HEADERS', false );
82 +}
83 +
72 84 if ( ! file_exists( POWERED_CACHE_FO_CACHE_DIR ) ) {
73 85 mkdir( POWERED_CACHE_FO_CACHE_DIR, 0775, true );
74 86 }
75 87
@@ -117,12 +129,14 @@
117 129 }
118 130
119 131 function concat_get_path( $uri ) {
120 132 if ( ! strlen( $uri ) ) {
133 + maybe_add_debug_log( sprintf( "File Optimizer 400 - could not retrieve file path for uri %s", $uri ) );
121 134 concat_http_status_exit( 400 );
122 135 }
123 136
124 137 if ( false !== strpos( $uri, '..' ) || false !== strpos( $uri, "\0" ) ) {
138 + maybe_add_debug_log( sprintf( "File Optimizer 400 - could not retrieve file path for uri %s", $uri ) );
125 139 concat_http_status_exit( 400 );
126 140 }
127 141
128 142 return CONCAT_FILES_ROOT . ( '/' != $uri[0] ? '/' : '' ) . $uri;
@@ -140,8 +154,9 @@
140 154 }
141 155
142 156 /* Main() */
143 157 if ( ! in_array( $_SERVER['REQUEST_METHOD'], array( 'GET', 'HEAD' ) ) ) {
158 + maybe_add_debug_log( sprintf( "File Optimizer 400 - unsupported request method: %s", $_SERVER['REQUEST_METHOD'] ) );
144 159 concat_http_status_exit( 400 );
145 160 }
146 161
147 162 // /_static/??/foo/bar.css,/foo1/bar/baz.css?m=293847g
@@ -148,15 +163,15 @@
148 163 // or
149 164 // /_static/??-eJzTT8vP109KLNJLLi7W0QdyDEE8IK4CiVjn2hpZGluYmKcDABRMDPM=
150 165 $args = parse_url( $_SERVER['REQUEST_URI'], PHP_URL_QUERY );
151 166 if ( ! $args || false === strpos( $args, '?' ) ) {
167 + maybe_add_debug_log( sprintf( "File Optimizer 400 - empty query arg or not ? exists" ) );
152 168 concat_http_status_exit( 400 );
153 169 }
154 170
155 171 $args = substr( $args, strpos( $args, '?' ) + 1 );
172 +$args = str_replace( [ '&minify=1', '&minify=0' ], '', $args ); // remove minify parameter
156 173
157 -
158 -
159 174 // /foo/bar.css,/foo1/bar/baz.css?m=293847g
160 175 // or
161 176 // -eJzTT8vP109KLNJLLi7W0QdyDEE8IK4CiVjn2hpZGluYmKcDABRMDPM=
162 177 if ( '-' == $args[0] ) {
@@ -163,8 +178,9 @@
163 178 $args = @gzuncompress( base64_decode( substr( $args, 1 ) ) );
164 179
165 180 // Invalid data, abort!
166 181 if ( false === $args ) {
182 + maybe_add_debug_log( sprintf( "File Optimizer 400 - Invalid Data" ) );
167 183 concat_http_status_exit( 400 );
168 184 }
169 185 }
170 186
@@ -176,8 +192,9 @@
176 192
177 193 // /foo/bar.css,/foo1/bar/baz.css
178 194 $args = explode( ',', $args );
179 195 if ( ! $args ) {
196 + maybe_add_debug_log( sprintf( "File Optimizer 400 - Empty args" ) );
180 197 concat_http_status_exit( 400 );
181 198 }
182 199
183 200 // array('/wp-content/foo/bar.css','//cdn.cname.com/wp-content/foo/bar.css')
@@ -189,8 +206,9 @@
189 206 }
190 207
191 208 // array( '/foo/bar.css', '/foo1/bar/baz.css' )
192 209 if ( 0 == count( $args ) || count( $args ) > $concat_max_files ) {
210 + maybe_add_debug_log( sprintf( "File Optimizer 400 - Concatenating too many or zero file: %s files on queue", count( $args ) ) );
193 211 concat_http_status_exit( 400 );
194 212 }
195 213
196 214 // If we're in a subdirectory context, use that as the root.
@@ -206,10 +224,10 @@
206 224 $last_modified = 0;
207 225 $pre_output = '';
208 226 $output = '';
209 227
210 -$do_minify = (bool) stripos( $_SERVER['REQUEST_URI'], 'minify=1' );
211 -$hash = sha1( $_SERVER['REQUEST_URI'] );
228 +$do_minify = (bool) stripos( $_SERVER['REQUEST_URI'], 'minify=1' );
229 +$hash = sha1( $_SERVER['REQUEST_URI'] );
212 230 $latest_file = end( $args );
213 231
214 232 $cache_file_name = POWERED_CACHE_FO_CACHE_DIR . $hash;
215 233 if ( 'application/javascript' == concat_get_mtype( $latest_file ) ) {
@@ -222,8 +240,9 @@
222 240 $buf = @file_get_contents( $cache_file_name );
223 241 if ( $buf ) {
224 242 $stat = stat( $cache_file_name );
225 243
244 + set_cache_headers();
226 245 header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $stat['mtime'] ) . ' GMT' );
227 246 header( 'Content-Length: ' . ( strlen( $pre_output ) + strlen( $buf ) ) );
228 247 header( 'Content-Type: ' . concat_get_mtype( $latest_file ) );
229 248
@@ -236,13 +255,15 @@
236 255
237 256 $fullpath = concat_get_path( $uri );
238 257
239 258 if ( ! file_exists( $fullpath ) ) {
259 + maybe_add_debug_log( sprintf( "File Optimizer 404 - Missing file: %s", $fullpath ) );
240 260 concat_http_status_exit( 404 );
241 261 }
242 262
243 263 $mime_type = concat_get_mtype( $fullpath );
244 264 if ( ! in_array( $mime_type, $concat_types ) ) {
265 + maybe_add_debug_log( sprintf( "File Optimizer 400 - Unsupported mime type: %s", $mime_type ) );
245 266 concat_http_status_exit( 400 );
246 267 }
247 268
248 269 if ( $concat_unique ) {
@@ -250,8 +271,9 @@
250 271 $last_mime_type = $mime_type;
251 272 }
252 273
253 274 if ( $last_mime_type != $mime_type ) {
275 + maybe_add_debug_log( sprintf( "File Optimizer 400 - Different mime type: last mime %s vs current mime: %s", $last_mime_type, $mime_type ) );
254 276 concat_http_status_exit( 400 );
255 277 }
256 278 }
257 279
@@ -256,8 +278,9 @@
256 278 }
257 279
258 280 $stat = stat( $fullpath );
259 281 if ( false === $stat ) {
282 + maybe_add_debug_log( sprintf( "File Optimizer 500 - false stat" ) );
260 283 concat_http_status_exit( 500 );
261 284 }
262 285
263 286 if ( $stat['mtime'] > $last_modified ) {
@@ -265,8 +288,9 @@
265 288 }
266 289
267 290 $buf = file_get_contents( $fullpath );
268 291 if ( false === $buf ) {
292 + maybe_add_debug_log( sprintf( "File Optimizer 500 - false buffer" ) );
269 293 concat_http_status_exit( 500 );
270 294 }
271 295
272 296 if ( 'text/css' == $mime_type ) {
@@ -339,8 +363,9 @@
339 363 $output = $css_minify->minify();
340 364 }
341 365 }
342 366
367 +set_cache_headers();
343 368 header( 'Last-Modified: ' . gmdate( 'D, d M Y H:i:s', $last_modified ) . ' GMT' );
344 369 header( 'Content-Length: ' . ( strlen( $pre_output ) + strlen( $output ) ) );
345 370 header( "Content-Type: $mime_type" );
346 371
@@ -384,5 +409,37 @@
384 409 $path = ucfirst( $path );
385 410 }
386 411
387 412 return $wrapper . $path;
388 -}
413 +}
414 +
415 +/**
416 + * Maybe add debug message
417 + *
418 + * @param string $message Debug message
419 + *
420 + * @since 2.3
421 + */
422 +function maybe_add_debug_log( $message ) {
423 + if ( defined( 'POWERED_CACHE_FO_DEBUG' ) && POWERED_CACHE_FO_DEBUG ) {
424 + error_log( $message );
425 + }
426 +}
427 +
428 +
429 +/**
430 + * Add cache headers
431 + *
432 + * @return void
433 + * @since 2.5.3
434 + */
435 +function set_cache_headers() {
436 + if ( defined( 'POWERED_CACHE_FO_DISABLE_CACHE_HEADERS' ) && POWERED_CACHE_FO_DISABLE_CACHE_HEADERS ) {
437 + return;
438 + }
439 +
440 + $ttl_in_seconds = 31536000; // 365 days
441 + $ttl = gmdate( "D, d M Y H:i:s", time() + $ttl_in_seconds ) . " GMT";
442 +
443 + header( "Expires: $ttl" );
444 + header( "Cache-Control: max-age=$ttl_in_seconds" );
445 +}