PluginProbe ʕ •ᴥ•ʔ
WP Super Cache / 1.5.3
WP Super Cache v1.5.3
3.1.1 trunk 0.1 0.2 0.3 0.3.1 0.4 0.5 0.5.1 0.5.2 0.5.3 0.6.2 0.6.3 0.6.4 0.6.5 0.6.6 0.6.7 0.6.8 0.7 0.7.1 0.8 0.8.1 0.8.2 0.8.3 0.8.4 0.8.5 0.8.6 0.8.7 0.8.8 0.8.9 0.9 0.9.1 0.9.2 0.9.3 0.9.3.1 0.9.4 0.9.4.1 0.9.4.2 0.9.4.3 0.9.5 0.9.6 0.9.6.1 0.9.7 0.9.8 0.9.9 0.9.9.1 0.9.9.2 0.9.9.3 0.9.9.4 0.9.9.5 0.9.9.6 0.9.9.7 0.9.9.8 0.9.9.9 1.0 1.0.1 1.1 1.1.1 1.10.0 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.12.4 1.2 1.2.1 1.3 1.3.1 1.3.2 1.3.3 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.4.8 1.4.9 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.7.1 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6 1.6.7 1.6.8 1.6.9 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.7.5 1.7.6 1.7.7 1.7.8 1.7.9 1.8 1.9 1.9.1 1.9.2 1.9.3 1.9.4 2.0.0 2.0.1 3.0.0 3.0.1 3.0.2 3.0.3 3.1.0
wp-super-cache / wp-cache-phase2.php
wp-super-cache Last commit date
languages 13 years ago plugins 8 years ago rest 8 years ago advanced-cache.php 9 years ago ossdl-cdn.php 8 years ago readme.txt 8 years ago wp-cache-base.php 8 years ago wp-cache-config-sample.php 8 years ago wp-cache-phase1.php 8 years ago wp-cache-phase2.php 8 years ago wp-cache.php 8 years ago wp-super-cache.pot 9 years ago
wp-cache-phase2.php
1608 lines
1 <?php
2
3 function wp_cache_phase2() {
4 global $wp_cache_gzip_encoding, $super_cache_enabled, $cache_rebuild_files, $cache_enabled, $wp_cache_gmt_offset, $wp_cache_blog_charset;
5
6 if ( $cache_enabled == false ) {
7 wp_cache_debug( "Caching disabled! quiting!", 1 );
8 return false;
9 }
10
11 wp_cache_debug( 'In WP Cache Phase 2', 5 );
12
13 $wp_cache_gmt_offset = get_option( 'gmt_offset' ); // caching for later use when wpdb is gone. http://wordpress.org/support/topic/224349
14 $wp_cache_blog_charset = get_option( 'blog_charset' );
15
16 wp_cache_mutex_init();
17 if(function_exists('add_action') && ( !defined( 'WPLOCKDOWN' ) || ( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) == '0' ) ) ) {
18 wp_cache_debug( 'Setting up WordPress actions', 5 );
19
20 add_action( 'template_redirect', 'wp_super_cache_query_vars' );
21
22 // Post ID is received
23 add_action('wp_trash_post', 'wp_cache_post_edit', 0);
24 add_action('publish_post', 'wp_cache_post_edit', 0);
25 add_action('edit_post', 'wp_cache_post_change', 0); // leaving a comment called edit_post
26 add_action('delete_post', 'wp_cache_post_edit', 0);
27 add_action('publish_phone', 'wp_cache_post_edit', 0);
28 // Coment ID is received
29 add_action('trackback_post', 'wp_cache_get_postid_from_comment', 99);
30 add_action('pingback_post', 'wp_cache_get_postid_from_comment', 99);
31 add_action('comment_post', 'wp_cache_get_postid_from_comment', 99);
32 add_action('edit_comment', 'wp_cache_get_postid_from_comment', 99);
33 add_action('wp_set_comment_status', 'wp_cache_get_postid_from_comment', 99, 2);
34 // No post_id is available
35 add_action('switch_theme', 'wp_cache_no_postid', 99);
36 add_action('edit_user_profile_update', 'wp_cache_no_postid', 99);
37 add_action( 'wp_update_nav_menu', 'wp_cache_clear_cache_on_menu' );
38 add_action('wp_cache_gc','wp_cache_gc_cron');
39 add_action( 'clean_post_cache', 'wp_cache_post_edit' );
40 add_filter( 'supercache_filename_str', 'wp_cache_check_mobile' );
41 add_action( 'wp_cache_gc_watcher', 'wp_cache_gc_watcher' );
42 add_action( 'transition_post_status', 'wpsc_post_transition', 10, 3 );
43
44 do_cacheaction( 'add_cacheaction' );
45 }
46
47 if ( is_admin() ) {
48 wp_cache_debug( 'Not caching wp-admin requests.', 5 );
49 return false;
50 }
51
52 if ( !empty( $_GET ) && !defined( "DOING_CRON" ) ) {
53 wp_cache_debug( 'Supercache caching disabled. Only using wp-cache. Non empty GET request. ' . json_encode( $_GET ), 5 );
54 $super_cache_enabled = false;
55 }
56
57 if($wp_cache_gzip_encoding)
58 header('Vary: Accept-Encoding, Cookie');
59 else
60 header('Vary: Cookie');
61 ob_start( 'wp_cache_ob_callback' );
62 wp_cache_debug( 'Created output buffer', 4 );
63
64 // restore old supercache file temporarily
65 if ( ( $_SERVER["REQUEST_METHOD"] != 'POST' && empty( $_POST ) ) && $super_cache_enabled && $cache_rebuild_files ) {
66 $user_info = wp_cache_get_cookies_values();
67 $do_cache = apply_filters( 'do_createsupercache', $user_info );
68 if( $user_info == '' || $do_cache === true )
69 wpcache_do_rebuild( get_current_url_supercache_dir() );
70 }
71
72 schedule_wp_gc();
73 }
74
75 function wpcache_do_rebuild( $dir ) {
76 global $do_rebuild_list, $cache_path, $wpsc_file_mtimes;
77 wp_cache_debug( "wpcache_do_rebuild: doing rebuild for $dir" );
78
79 if ( !is_dir( $dir ) ) {
80 wp_cache_debug( "wpcache_do_rebuild: exiting as directory is not a directory: $dir" );
81 return false;
82 }
83
84 $dir = wpsc_get_realpath( $dir );
85
86 if ( isset( $do_rebuild_list[ $dir ] ) ) {
87 wp_cache_debug( "wpcache_do_rebuild: directory already rebuilt: $dir" );
88 return false;
89 }
90
91 $protected = wpsc_get_protected_directories();
92 foreach( $protected as $id => $directory ) {
93 $protected[ $id ] = wpsc_get_realpath( $directory );
94 }
95
96 if ( ! wpsc_is_in_cache_directory( $dir ) ) {
97 wp_cache_debug( "wpcache_do_rebuild: exiting as directory not in cache_path: $dir" );
98 return false;
99 }
100
101 if ( in_array( $dir, $protected ) ) {
102 wp_cache_debug( "wpcache_do_rebuild: exiting as directory is protected: $dir" );
103 return false;
104 }
105
106 if ( !is_dir( $dir ) ) {
107 wp_cache_debug( "wpcache_do_rebuild: exiting as directory is not a directory: $dir" );
108 return false;
109 }
110
111 $dh = @opendir( $dir );
112 if ( false == $dh ) {
113 wp_cache_debug( "wpcache_do_rebuild: exiting as could not open directory for reading: $dir" );
114 return false;
115 }
116
117 $dir = trailingslashit( $dir );
118 $wpsc_file_mtimes = array();
119 while ( ( $file = readdir( $dh ) ) !== false ) {
120 if ( $file == '.' || $file == '..' || false == is_file( $dir . $file ) ) {
121 continue;
122 }
123
124 $cache_file = $dir . $file;
125 // if the file is index.html.needs-rebuild and index.html doesn't exist and
126 // if the rebuild file is less than 10 seconds old then remove the ".needs-rebuild"
127 // extension so index.html can be served to other visitors temporarily
128 // until index.html is generated again at the end of this page.
129
130 if ( substr( $cache_file, -14 ) != '.needs-rebuild' ) {
131 wp_cache_debug( "wpcache_do_rebuild: base file found: $cache_file" );
132 continue;
133 }
134
135 wp_cache_debug( "wpcache_do_rebuild: found rebuild file: $cache_file" );
136
137 if ( @file_exists( substr( $cache_file, 0, -14 ) ) ) {
138 wp_cache_debug( "wpcache_do_rebuild: rebuild file deleted because base file found: $cache_file" );
139 @unlink( $cache_file ); // delete the rebuild file because index.html already exists
140 continue;
141 }
142
143 $mtime = @filemtime( $cache_file );
144 if ( $mtime && ( time() - $mtime ) < 10 ) {
145 wp_cache_debug( "wpcache_do_rebuild: rebuild file is new: $cache_file" );
146 $base_file = substr( $cache_file, 0, -14 );
147 if ( false == @rename( $cache_file, $base_file ) ) { // rename the rebuild file
148 @unlink( $cache_file );
149 wp_cache_debug( "wpcache_do_rebuild: rebuild file rename failed. Deleted rebuild file: $cache_file" );
150 } else {
151 $do_rebuild_list[ $dir ] = 1;
152 $wpsc_file_mtimes[ $base_file ] = $mtime;
153 wp_cache_debug( "wpcache_do_rebuild: rebuild file renamed: $base_file" );
154 }
155 } else {
156 wp_cache_debug( "wpcache_do_rebuild: rebuild file deleted because it's too old: $cache_file" );
157 @unlink( $cache_file ); // delete the rebuild file because index.html already exists
158 }
159 }
160 }
161
162 function wpcache_logged_in_message() {
163 echo '<!-- WP Super Cache did not cache this page because you are logged in and "Don\'t cache pages for logged in users" is enabled. -->';
164 }
165
166 if ( !function_exists( 'wp_cache_user_agent_is_rejected' ) ) {
167 function wp_cache_user_agent_is_rejected() {
168 global $cache_rejected_user_agent;
169
170 if (!function_exists('apache_request_headers')) return false;
171 $headers = apache_request_headers();
172 if (!isset($headers["User-Agent"])) return false;
173 if ( false == is_array( $cache_rejected_user_agent ) )
174 return false;
175 foreach ($cache_rejected_user_agent as $expr) {
176 if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
177 return true;
178 }
179 return false;
180 }
181 }
182
183 function wp_cache_get_response_headers() {
184 static $known_headers = array(
185 'Access-Control-Allow-Origin',
186 'Accept-Ranges',
187 'Age',
188 'Allow',
189 'Cache-Control',
190 'Connection',
191 'Content-Encoding',
192 'Content-Language',
193 'Content-Length',
194 'Content-Location',
195 'Content-MD5',
196 'Content-Disposition',
197 'Content-Range',
198 'Content-Type',
199 'Date',
200 'ETag',
201 'Expires',
202 'Last-Modified',
203 'Link',
204 'Location',
205 'P3P',
206 'Pragma',
207 'Proxy-Authenticate',
208 "Referrer-Policy",
209 'Refresh',
210 'Retry-After',
211 'Server',
212 'Status',
213 'Strict-Transport-Security',
214 'Trailer',
215 'Transfer-Encoding',
216 'Upgrade',
217 'Vary',
218 'Via',
219 'Warning',
220 'WWW-Authenticate',
221 'X-Frame-Options',
222 'Public-Key-Pins',
223 'X-XSS-Protection',
224 'Content-Security-Policy',
225 "X-Pingback",
226 'X-Content-Security-Policy',
227 'X-WebKit-CSP',
228 'X-Content-Type-Options',
229 'X-Powered-By',
230 'X-UA-Compatible',
231 );
232
233 $known_headers = apply_filters( 'wpsc_known_headers', $known_headers );
234
235 if ( ! isset( $known_headers[ 'age' ] ) ) {
236 $known_headers = array_map( 'strtolower', $known_headers );
237 }
238
239 $headers = array();
240 if ( function_exists( 'apache_response_headers' ) ) {
241 $headers = apache_response_headers();
242 }
243 if ( empty( $headers ) && function_exists( 'headers_list' ) ) {
244 $headers = array();
245 foreach( headers_list() as $hdr ) {
246 $header_parts = explode( ':', $hdr, 2 );
247 $header_name = isset( $header_parts[0] ) ? trim( $header_parts[0] ) : '';
248 $header_value = isset( $header_parts[1] ) ? trim( $header_parts[1] ) : '';
249
250 $headers[$header_name] = $header_value;
251 }
252 }
253
254 foreach( $headers as $key => $value ) {
255 if ( ! in_array( strtolower( $key ), $known_headers ) ) {
256 unset( $headers[ $key ] );
257 }
258 }
259
260 return $headers;
261 }
262
263 function wp_cache_is_rejected($uri) {
264 global $cache_rejected_uri;
265
266 $auto_rejected = array( '/wp-admin/', 'xmlrpc.php', 'wp-app.php' );
267 foreach( $auto_rejected as $u ) {
268 if( strstr( $uri, $u ) )
269 return true; // we don't allow caching of wp-admin for security reasons
270 }
271 if ( false == is_array( $cache_rejected_uri ) )
272 return false;
273 foreach ( $cache_rejected_uri as $expr ) {
274 if( $expr != '' && @preg_match( "~$expr~", $uri ) )
275 return true;
276 }
277 return false;
278 }
279
280 function wp_cache_mutex_init() {
281 global $mutex, $wp_cache_mutex_disabled, $use_flock, $blog_cache_dir, $mutex_filename, $sem_id;
282
283 if ( defined( 'WPSC_DISABLE_LOCKING' ) || ( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled ) )
284 return true;
285
286 if( !is_bool( $use_flock ) ) {
287 if(function_exists('sem_get'))
288 $use_flock = false;
289 else
290 $use_flock = true;
291 }
292
293 $mutex = false;
294 if ($use_flock ) {
295 setup_blog_cache_dir();
296 wp_cache_debug( "Created mutex lock on filename: {$blog_cache_dir}{$mutex_filename}", 5 );
297 $mutex = @fopen( $blog_cache_dir . $mutex_filename, 'w' );
298 } else {
299 wp_cache_debug( "Created mutex lock on semaphore: {$sem_id}", 5 );
300 $mutex = @sem_get( $sem_id, 1, 0666, 1 );
301 }
302 }
303
304 function wp_cache_writers_entry() {
305 global $mutex, $wp_cache_mutex_disabled, $use_flock;
306
307 if ( defined( 'WPSC_DISABLE_LOCKING' ) || ( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled ) )
308 return true;
309
310 if( !$mutex ) {
311 wp_cache_debug( "(writers entry) mutex lock not created. not caching.", 2 );
312 return false;
313 }
314
315 if ( $use_flock ) {
316 wp_cache_debug( "grabbing lock using flock()", 5 );
317 flock($mutex, LOCK_EX);
318 } else {
319 wp_cache_debug( "grabbing lock using sem_acquire()", 5 );
320 @sem_acquire($mutex);
321 }
322
323 return true;
324 }
325
326 function wp_cache_writers_exit() {
327 global $mutex, $wp_cache_mutex_disabled, $use_flock;
328
329 if ( defined( 'WPSC_DISABLE_LOCKING' ) || ( isset( $wp_cache_mutex_disabled ) && $wp_cache_mutex_disabled ) )
330 return true;
331
332 if( !$mutex ) {
333 wp_cache_debug( "(writers exit) mutex lock not created. not caching.", 2 );
334 return false;
335 }
336
337 if ( $use_flock ) {
338 wp_cache_debug( "releasing lock using flock()", 5 );
339 flock( $mutex, LOCK_UN );
340 } else {
341 wp_cache_debug( "releasing lock using sem_release() and sem_remove()", 5 );
342 @sem_release( $mutex );
343 if ( defined( "WPSC_REMOVE_SEMAPHORE" ) )
344 @sem_remove( $mutex );
345 }
346 }
347
348 function wp_super_cache_query_vars() {
349 global $wp_super_cache_query;
350 if ( is_search() )
351 $wp_super_cache_query[ 'is_search' ] = 1;
352 if ( is_page() )
353 $wp_super_cache_query[ 'is_page' ] = 1;
354 if ( is_archive() )
355 $wp_super_cache_query[ 'is_archive' ] = 1;
356 if ( is_tag() )
357 $wp_super_cache_query[ 'is_tag' ] = 1;
358 if ( is_single() )
359 $wp_super_cache_query[ 'is_single' ] = 1;
360 if ( is_category() )
361 $wp_super_cache_query[ 'is_category' ] = 1;
362 if ( is_front_page() )
363 $wp_super_cache_query[ 'is_front_page' ] = 1;
364 if ( is_home() )
365 $wp_super_cache_query[ 'is_home' ] = 1;
366 if ( is_author() )
367 $wp_super_cache_query[ 'is_author' ] = 1;
368 if ( is_feed() )
369 $wp_super_cache_query[ 'is_feed' ] = 1;
370 }
371
372 function wp_cache_ob_callback( $buffer ) {
373 global $wp_cache_pages, $wp_query, $wp_super_cache_query, $cache_acceptable_files, $wp_cache_no_cache_for_get, $wp_cache_object_cache, $wp_cache_request_uri, $do_rebuild_list, $wpsc_file_mtimes, $wpsc_save_headers, $super_cache_enabled;
374 $buffer = apply_filters( 'wp_cache_ob_callback_filter', $buffer );
375
376 $script = basename($_SERVER['PHP_SELF']);
377
378 // All the things that can stop a page being cached
379 $cache_this_page = true;
380 if ( defined( 'DONOTCACHEPAGE' ) ) {
381 wp_cache_debug( 'DONOTCACHEPAGE defined. Caching disabled.', 2 );
382 $cache_this_page = false;
383 } elseif ( $wp_cache_no_cache_for_get && false == empty( $_GET ) && false == defined( 'DOING_CRON' ) ) {
384 wp_cache_debug( "Non empty GET request. Caching disabled on settings page. " . json_encode( $_GET ), 1 );
385 $cache_this_page = false;
386 } elseif ( $_SERVER["REQUEST_METHOD"] == 'POST' || !empty( $_POST ) || get_option( 'gzipcompression' ) ) {
387 wp_cache_debug( 'Not caching POST request.', 5 );
388 $cache_this_page = false;
389 } elseif ( $_SERVER["REQUEST_METHOD"] == 'PUT' ) {
390 wp_cache_debug( 'Not caching PUT request.', 5 );
391 $cache_this_page = false;
392 } elseif ( $_SERVER["REQUEST_METHOD"] == 'DELETE' ) {
393 wp_cache_debug( 'Not caching DELETE request.', 5 );
394 $cache_this_page = false;
395 } elseif ( $wp_cache_object_cache && !empty( $_GET ) ) {
396 wp_cache_debug( 'Not caching GET request while object cache storage enabled.', 5 );
397 $cache_this_page = false;
398 } elseif ( isset( $_GET[ 'preview' ] ) ) {
399 wp_cache_debug( 'Not caching preview post.', 2 );
400 $cache_this_page = false;
401 } elseif ( !in_array($script, $cache_acceptable_files) && wp_cache_is_rejected( $wp_cache_request_uri ) ) {
402 wp_cache_debug( 'URI rejected. Not Caching', 2 );
403 $cache_this_page = false;
404 } elseif ( wp_cache_user_agent_is_rejected() ) {
405 wp_cache_debug( "USER AGENT ({$_SERVER[ 'HTTP_USER_AGENT' ]}) rejected. Not Caching", 4 );
406 $cache_this_page = false;
407 } elseif ( isset( $wp_cache_pages[ 'single' ] ) && $wp_cache_pages[ 'single' ] == 1 && isset( $wp_super_cache_query[ 'is_single' ] ) ) {
408 wp_cache_debug( 'Not caching single post.', 2 );
409 $cache_this_page = false;
410 } elseif ( isset( $wp_cache_pages[ 'pages' ] ) && $wp_cache_pages[ 'pages' ] == 1 && isset( $wp_super_cache_query[ 'is_page' ] ) ) {
411 wp_cache_debug( 'Not caching single page.', 2 );
412 $cache_this_page = false;
413 } elseif ( isset( $wp_cache_pages[ 'archives' ] ) && $wp_cache_pages[ 'archives' ] == 1 && isset( $wp_super_cache_query[ 'is_archive' ] ) ) {
414 wp_cache_debug( 'Not caching archive page.', 2 );
415 $cache_this_page = false;
416 } elseif ( isset( $wp_cache_pages[ 'tag' ] ) && $wp_cache_pages[ 'tag' ] == 1 && isset( $wp_super_cache_query[ 'is_tag' ] ) ) {
417 wp_cache_debug( 'Not caching tag page.', 2 );
418 $cache_this_page = false;
419 } elseif ( isset( $wp_cache_pages[ 'category' ] ) && $wp_cache_pages[ 'category' ] == 1 && isset( $wp_super_cache_query[ 'is_category' ] ) ) {
420 wp_cache_debug( 'Not caching category page.', 2 );
421 $cache_this_page = false;
422 } elseif ( isset( $wp_cache_pages[ 'frontpage' ] ) && $wp_cache_pages[ 'frontpage' ] == 1 && isset( $wp_super_cache_query[ 'is_front_page' ] ) ) {
423 wp_cache_debug( 'Not caching front page.', 2 );
424 $cache_this_page = false;
425 } elseif ( isset( $wp_cache_pages[ 'home' ] ) && $wp_cache_pages[ 'home' ] == 1 && isset( $wp_super_cache_query[ 'is_home' ] ) ) {
426 wp_cache_debug( 'Not caching home page.', 2 );
427 $cache_this_page = false;
428 } elseif ( isset( $wp_cache_pages[ 'search' ] ) && $wp_cache_pages[ 'search' ] == 1 && isset( $wp_super_cache_query[ 'is_search' ] ) ) {
429 wp_cache_debug( 'Not caching search page.', 2 );
430 $cache_this_page = false;
431 } elseif ( isset( $wp_cache_pages[ 'author' ] ) && $wp_cache_pages[ 'author' ] == 1 && isset( $wp_super_cache_query[ 'is_author' ] ) ) {
432 wp_cache_debug( 'Not caching author page.', 2 );
433 $cache_this_page = false;
434 } elseif ( isset( $wp_cache_pages[ 'feed' ] ) && $wp_cache_pages[ 'feed' ] == 1 && isset( $wp_super_cache_query[ 'is_feed' ] ) ) {
435 wp_cache_debug( 'Not caching feed.', 2 );
436 $cache_this_page = false;
437 }
438
439 if ( isset( $wpsc_save_headers ) && $wpsc_save_headers )
440 $super_cache_enabled = false; // use standard caching to record headers
441
442 if ( !isset( $wp_query ) )
443 wp_cache_debug( 'wp_cache_ob_callback: WARNING! $query not defined but the plugin has worked around that problem.', 4 );
444
445 if ( $cache_this_page ) {
446
447 wp_cache_debug( 'Output buffer callback', 4 );
448
449 $buffer = wp_cache_get_ob( $buffer );
450 wp_cache_shutdown_callback();
451
452 if ( isset( $wpsc_file_mtimes ) && is_array( $wpsc_file_mtimes ) && !empty( $wpsc_file_mtimes ) ) {
453 foreach( $wpsc_file_mtimes as $cache_file => $old_mtime ) {
454 if ( $old_mtime == @filemtime( $cache_file ) ) {
455 wp_cache_debug( "wp_cache_ob_callback deleting unmodified rebuilt cache file: $cache_file" );
456 if ( wp_cache_confirm_delete( $cache_file ) ) {
457 @unlink( $cache_file );
458 }
459 }
460 }
461 }
462 return $buffer;
463 } else {
464 if ( is_array( $do_rebuild_list ) && false == empty( $do_rebuild_list ) ) {
465 foreach( $do_rebuild_list as $dir => $n ) {
466 if ( wp_cache_confirm_delete( $dir ) ) {
467 wp_cache_debug( 'wp_cache_ob_callback clearing rebuilt files in ' . $dir );
468 wpsc_delete_files( $dir );
469 }
470 }
471 }
472 return wp_cache_maybe_dynamic( $buffer );
473 }
474 }
475
476 function wp_cache_append_tag( &$buffer ) {
477 global $wp_cache_gmt_offset, $wp_super_cache_comments;
478 global $cache_enabled, $super_cache_enabled;
479
480 if ( false == isset( $wp_super_cache_comments ) )
481 $wp_super_cache_comments = 1;
482
483 if ( $wp_super_cache_comments == 0 )
484 return false;
485
486 $timestamp = gmdate('Y-m-d H:i:s', (time() + ( $wp_cache_gmt_offset * 3600)));
487 if ( $cache_enabled || $super_cache_enabled ) {
488 $msg = "\n<!-- Cached page generated by WP-Super-Cache on $timestamp -->\n";
489 } else {
490 $msg = "\n<!-- Live page served on $timestamp -->\n";
491 }
492
493 if ( strpos( $buffer, '<html' ) === false ) {
494 wp_cache_debug( site_url( $_SERVER[ 'REQUEST_URI' ] ) . " - " . $msg );
495 return false;
496 }
497
498 $buffer .= $msg;
499 }
500
501 function wp_cache_add_to_buffer( &$buffer, $text ) {
502 global $wp_super_cache_comments;
503
504 if ( false == isset( $wp_super_cache_comments ) )
505 $wp_super_cache_comments = 1;
506
507 if ( $wp_super_cache_comments == 0 )
508 return false;
509
510 if ( strpos( $buffer, '<html' ) === false ) {
511 wp_cache_debug( site_url( $_SERVER[ 'REQUEST_URI' ] ) . " - " . $text );
512 return false;
513 }
514
515 $buffer .= "\n<!-- $text -->";
516 }
517
518 /*
519 * If dynamic caching is enabled then run buffer through wpsc_cachedata filter before returning it.
520 * or we'll return template tags to visitors.
521 */
522 function wp_cache_maybe_dynamic( &$buffer ) {
523 global $wp_cache_mfunc_enabled;
524 if ( $wp_cache_mfunc_enabled == 1 && do_cacheaction( 'wpsc_cachedata_safety', 0 ) === 1 ) {
525 wp_cache_debug( 'wp_cache_maybe_dynamic: filtered $buffer through wpsc_cachedata', 4 );
526 return do_cacheaction( 'wpsc_cachedata', $buffer ); // dynamic content for display
527 } else {
528 wp_cache_debug( 'wp_cache_maybe_dynamic: returned $buffer', 4 );
529 return $buffer;
530 }
531 }
532
533 function wp_cache_get_ob(&$buffer) {
534 global $cache_enabled, $cache_path, $cache_filename, $wp_start_time, $supercachedir;
535 global $new_cache, $wp_cache_meta, $cache_compression;
536 global $wp_cache_gzip_encoding, $super_cache_enabled;
537 global $wp_cache_404, $gzsize, $supercacheonly;
538 global $blog_cache_dir, $wp_supercache_cache_list;
539 global $wp_cache_not_logged_in, $wp_cache_object_cache, $cache_max_time;
540 global $wp_cache_is_home, $wp_cache_front_page_checks, $wp_cache_mfunc_enabled;
541
542 if ( isset( $wp_cache_mfunc_enabled ) == false )
543 $wp_cache_mfunc_enabled = 0;
544
545 $new_cache = true;
546 $wp_cache_meta = array();
547
548 /* Mode paranoic, check for closing tags
549 * we avoid caching incomplete files */
550 if ( $buffer == '' ) {
551 $new_cache = false;
552 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
553 wp_cache_debug( "Buffer is blank. Output buffer may have been corrupted by another plugin or this is a redirected URL. Look for text 'ob_start' in the files of your plugins directory.", 2 );
554 wp_cache_add_to_buffer( $buffer, "Page not cached by WP Super Cache. Blank Page. Check output buffer usage by plugins." );
555 }
556 }
557
558 if ( $wp_cache_404 && false == apply_filters( 'wpsupercache_404', false ) ) {
559 $new_cache = false;
560 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
561 wp_cache_debug( "404 file not found not cached", 2 );
562 wp_cache_add_to_buffer( $buffer, "Page not cached by WP Super Cache. 404." );
563 }
564 }
565
566 if ( !preg_match( apply_filters( 'wp_cache_eof_tags', '/(<\/html>|<\/rss>|<\/feed>|<\/urlset|<\?xml)/i' ), $buffer ) ) {
567 $new_cache = false;
568 if( false === strpos( $_SERVER[ 'REQUEST_URI' ], 'robots.txt' ) ) {
569 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
570 wp_cache_debug( "No closing html tag. Not caching.", 2 );
571 wp_cache_add_to_buffer( $buffer, "Page not cached by WP Super Cache. No closing HTML tag. Check your theme." );
572 }
573 } else {
574 wp_cache_debug( "robots.txt detected. Not caching.", 2 );
575 }
576 }
577
578 if( !$new_cache )
579 return wp_cache_maybe_dynamic( $buffer );
580
581 $duration = wp_cache_microtime_diff($wp_start_time, microtime());
582 $duration = sprintf("%0.3f", $duration);
583 wp_cache_add_to_buffer( $buffer, "Dynamic page generated in $duration seconds." );
584
585 if( !wp_cache_writers_entry() ) {
586 wp_cache_add_to_buffer( $buffer, "Page not cached by WP Super Cache. Could not get mutex lock." );
587 wp_cache_debug( "Could not get mutex lock. Not caching.", 1 );
588 return wp_cache_maybe_dynamic( $buffer );
589 }
590
591 if ( $wp_cache_not_logged_in && is_feed() ) {
592 wp_cache_debug( "Feed detected. Writing wpcache cache files.", 5 );
593 $wp_cache_not_logged_in = false;
594 }
595
596 $home_url = parse_url( trailingslashit( get_bloginfo( 'url' ) ) );
597
598 $dir = get_current_url_supercache_dir();
599 $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $home_url[ 'host' ]);
600 if( !empty( $_GET ) || is_feed() || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) ) {
601 wp_cache_debug( "Supercache disabled: GET or feed detected or disabled by config.", 2 );
602 $super_cache_enabled = false;
603 }
604
605 $tmp_wpcache_filename = $cache_path . uniqid( mt_rand(), true ) . '.tmp';
606
607 $supercacheonly = false;
608 if( $super_cache_enabled ) {
609 if ( wp_cache_get_cookies_values() == '' && empty( $_GET ) ) {
610 wp_cache_debug( "Anonymous user detected. Only creating Supercache file.", 3 );
611 $supercacheonly = true;
612 }
613 }
614
615 $cache_error = '';
616 if ( $wp_cache_not_logged_in && wp_cache_get_cookies_values() != '' ) {
617 $super_cache_enabled = false;
618 $cache_enabled = false;
619 $cache_error = 'Not caching requests by known users. (See Advanced Settings page)';
620 wp_cache_debug( 'Not caching for known user.', 5 );
621 }
622
623 if ( $wp_cache_object_cache ) { // half on mode when using the object cache
624 if ( wp_cache_get_cookies_values() != '' ) {
625 $cache_enabled = false;
626 $cache_error = 'Known User and using object. Only anonymous users cached.';
627 }
628 $super_cache_enabled = false;
629 $supercacheonly = false;
630 wp_cache_init(); // PHP5 destroys objects during shutdown
631 }
632
633 if ( !$cache_enabled ) {
634 wp_cache_debug( 'Cache is not enabled. Sending buffer to browser.', 5 );
635 wp_cache_writers_exit();
636 wp_cache_add_to_buffer( $buffer, "Page not cached by WP Super Cache. Check your settings page. $cache_error" );
637 if ( $wp_cache_mfunc_enabled == 1 ) {
638 global $wp_super_cache_late_init;
639 if ( false == isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && $wp_super_cache_late_init == 0 ) )
640 wp_cache_add_to_buffer( $buffer, 'Super Cache dynamic page detected but $wp_super_cache_late_init not set. See the readme.txt for further details.' );
641 }
642
643 return wp_cache_maybe_dynamic( $buffer );
644 }
645
646 if( @is_dir( $dir ) == false )
647 @wp_mkdir_p( $dir );
648 $dir = trailingslashit( wpsc_get_realpath( $dir ) );
649 if ( ! wpsc_is_in_cache_directory( $dir ) ) {
650 wp_cache_debug( "wp_cache_get_ob: not caching as directory is not in cache_path: $dir" );
651 return $buffer;
652 }
653
654 $fr = $fr2 = $gz = false;
655 // Open wp-cache cache file
656 if ( false == $wp_cache_object_cache ) {
657 if ( !$supercacheonly ) {
658 $fr = @fopen($tmp_wpcache_filename, 'w');
659 if (!$fr) {
660 wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename, 1 );
661 wp_cache_add_to_buffer( $buffer, "File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename );
662 wp_cache_writers_exit();
663 return wp_cache_maybe_dynamic( $buffer );
664 }
665 } else {
666 $user_info = wp_cache_get_cookies_values();
667 $do_cache = apply_filters( 'do_createsupercache', $user_info );
668 if ( $super_cache_enabled && ( $user_info == '' || $do_cache === true ) ) {
669
670 $cache_fname = $dir . supercache_filename();
671 $tmp_cache_filename = $dir . uniqid( mt_rand(), true ) . '.tmp';
672 $fr2 = @fopen( $tmp_cache_filename, 'w' );
673 if ( !$fr2 ) {
674 wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $tmp_cache_filename ), 1 );
675 wp_cache_add_to_buffer( $buffer, "File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) );
676 @fclose( $fr );
677 @unlink( $tmp_wpcache_filename );
678 wp_cache_writers_exit();
679 return wp_cache_maybe_dynamic( $buffer );
680 } elseif ( ( !isset( $wp_cache_mfunc_enabled ) || $wp_cache_mfunc_enabled == 0 ) && $cache_compression ) { // don't want to store compressed files if using dynamic content
681 $gz = @fopen( $tmp_cache_filename . ".gz", 'w');
682 if (!$gz) {
683 wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz", 1 );
684 wp_cache_add_to_buffer( $buffer, "File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz" );
685 @fclose( $fr );
686 @unlink( $tmp_wpcache_filename );
687 @fclose( $fr2 );
688 @unlink( $tmp_cache_filename );
689 wp_cache_writers_exit();
690 return wp_cache_maybe_dynamic( $buffer );
691 }
692 }
693 }
694 }
695 }
696
697 $added_cache = 0;
698 $oc_key = get_oc_key();
699 $buffer = apply_filters( 'wpsupercache_buffer', $buffer );
700 wp_cache_append_tag( $buffer );
701
702 /*
703 * Dynamic content enabled: write the buffer to a file and then process any templates found using
704 * the wpsc_cachedata filter. Buffer is then returned to the visitor.
705 */
706 if ( $wp_cache_mfunc_enabled == 1 ) {
707 if ( preg_match( '/<!--mclude|<!--mfunc|<!--dynamic-cached-content-->/', $buffer ) ) { //Dynamic content
708 wp_cache_debug( "mfunc/mclude/dynamic-cached-content tags have been retired. Please update your theme. See docs for updates." );
709 wp_cache_add_to_buffer( $buffer, "Warning! Obsolete mfunc/mclude/dynamic-cached-content tags found. Please update your theme. See http://ocaoimh.ie/y/5b for more information." );
710 }
711
712 global $wp_super_cache_late_init;
713 if ( false == isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && $wp_super_cache_late_init == 0 ) )
714 wp_cache_add_to_buffer( $buffer, 'Super Cache dynamic page detected but late init not set. See the readme.txt for further details.' );
715
716 if ( $fr ) { // wpcache caching
717 wp_cache_debug( "Writing dynamic buffer to wpcache file." );
718 wp_cache_add_to_buffer( $buffer, "Dynamic WPCache Super Cache" );
719 fputs( $fr, '<?php die(); ?>' . $buffer );
720 } elseif ( isset( $fr2 ) ) { // supercache active
721 wp_cache_debug( "Writing dynamic buffer to supercache file." );
722 wp_cache_add_to_buffer( $buffer, "Dynamic Super Cache" );
723 fputs( $fr2, $buffer );
724 } elseif ( true == $wp_cache_object_cache ) {
725 wp_cache_set( $oc_key, $buffer, 'supercache', $cache_max_time );
726 }
727 $wp_cache_meta[ 'dynamic' ] = true;
728 if ( do_cacheaction( 'wpsc_cachedata_safety', 0 ) === 1 )
729 $buffer = do_cacheaction( 'wpsc_cachedata', $buffer ); // dynamic content for display
730
731 if ( $cache_compression && $wp_cache_gzip_encoding ) {
732 wp_cache_debug( "Gzipping dynamic buffer for display.", 5 );
733 wp_cache_add_to_buffer( $buffer, "Compression = gzip" );
734 $gzdata = gzencode( $buffer, 6, FORCE_GZIP );
735 $gzsize = function_exists( 'mb_strlen' ) ? mb_strlen( $gzdata, '8bit' ) : strlen( $gzdata );
736 }
737 } else {
738 if ( $gz || $wp_cache_gzip_encoding ) {
739 wp_cache_debug( "Gzipping buffer.", 5 );
740 wp_cache_add_to_buffer( $buffer, "Compression = gzip" );
741 $gzdata = gzencode( $buffer, 6, FORCE_GZIP );
742 $gzsize = function_exists( 'mb_strlen' ) ? mb_strlen( $gzdata, '8bit' ) : strlen( $gzdata );
743
744 $wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
745 $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
746 // Return uncompressed data & store compressed for later use
747 if ( $fr ) {
748 wp_cache_debug( "Writing gzipped buffer to wp-cache cache file.", 5 );
749 fputs($fr, '<?php die(); ?>' . $gzdata);
750 } elseif ( $cache_enabled && $wp_cache_object_cache ) {
751 wp_cache_set( $oc_key . ".gz", $gzdata, 'supercache', $cache_max_time );
752 $added_cache = 1;
753 }
754 } else { // no compression
755 $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Cookie';
756 if ( $cache_enabled && $wp_cache_object_cache ) {
757 wp_cache_set( $oc_key, $buffer, 'supercache', $cache_max_time );
758 $added_cache = 1;
759 } elseif ( $fr ) {
760 wp_cache_debug( "Writing non-gzipped buffer to wp-cache cache file." );
761 fputs($fr, '<?php die(); ?>' . $buffer);
762 }
763 }
764 if ( $fr2 ) {
765 wp_cache_debug( "Writing non-gzipped buffer to supercache file." );
766 wp_cache_add_to_buffer( $buffer, "super cache" );
767 fputs($fr2, $buffer );
768 }
769 if ( isset( $gzdata ) && $gz ) {
770 wp_cache_debug( "Writing gzipped buffer to supercache file." );
771 fwrite($gz, $gzdata );
772 }
773 }
774
775 $new_cache = true;
776 if ( false == $wp_cache_object_cache ) {
777 if( $fr ) {
778 $supercacheonly = false;
779 fclose($fr);
780 if ( filesize( $tmp_wpcache_filename ) == 0 ) {
781 wp_cache_debug( "Warning! The file $tmp_wpcache_filename was empty. Did not rename to {$dir}/{$cache_filename}", 5 );
782 @unlink( $tmp_wpcache_filename );
783 } else {
784 if ( !@rename( $tmp_wpcache_filename, $dir . '/' . $cache_filename ) ) {
785 if ( false == is_dir( $dir ) )
786 @wp_mkdir_p( $dir );
787 @unlink( $dir . $cache_filename );
788 @rename( $tmp_wpcache_filename, $dir . '/' . $cache_filename );
789 }
790 wp_cache_debug( "Renamed temp wp-cache file to {$dir}/$cache_filename", 5 );
791 $added_cache = 1;
792 }
793 }
794 if( $fr2 ) {
795 fclose($fr2);
796 if ( $wp_cache_front_page_checks && $cache_fname == $supercachedir . $home_url[ 'path' ] . supercache_filename() && !( $wp_cache_is_home ) ) {
797 wp_cache_writers_exit();
798 wp_cache_debug( "Warning! Not writing another page to front page cache.", 1 );
799 return $buffer;
800 } elseif ( filesize( $tmp_cache_filename ) == 0 ) {
801 wp_cache_debug( "Warning! The file $tmp_cache_filename was empty. Did not rename to {$cache_fname}", 5 );
802 @unlink( $tmp_cache_filename );
803 } else {
804 if ( !@rename( $tmp_cache_filename, $cache_fname ) ) {
805 @unlink( $cache_fname );
806 @rename( $tmp_cache_filename, $cache_fname );
807 }
808 wp_cache_debug( "Renamed temp supercache file to $cache_fname", 5 );
809 $added_cache = 1;
810 }
811 }
812 if( $gz ) {
813 fclose($gz);
814 if ( filesize( $tmp_cache_filename . '.gz' ) == 0 ) {
815 wp_cache_debug( "Warning! The file {$tmp_cache_filename}.gz was empty. Did not rename to {$cache_fname}.gz", 5 );
816 @unlink( $tmp_cache_filename . '.gz' );
817 } else {
818 if ( !@rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' ) ) {
819 @unlink( $cache_fname . '.gz' );
820 @rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' );
821 }
822 wp_cache_debug( "Renamed temp supercache gz file to {$cache_fname}.gz", 5 );
823 $added_cache = 1;
824 }
825 }
826 }
827 if ( $added_cache && isset( $wp_supercache_cache_list ) && $wp_supercache_cache_list ) {
828 update_option( 'wpsupercache_count', ( get_option( 'wpsupercache_count' ) + 1 ) );
829 $last_urls = (array)get_option( 'supercache_last_cached' );
830 if ( count( $last_urls ) >= 10 )
831 $last_urls = array_slice( $last_urls, 1, 9 );
832 $last_urls[] = array( 'url' => preg_replace( '/[ <>\'\"\r\n\t\(\)]/', '', $_SERVER[ 'REQUEST_URI' ] ), 'date' => date( 'Y-m-d H:i:s' ) );
833 update_option( 'supercache_last_cached', $last_urls );
834 }
835 wp_cache_writers_exit();
836 if ( !headers_sent() && $wp_cache_gzip_encoding && $gzdata) {
837 wp_cache_debug( "Writing gzip content headers. Sending buffer to browser", 5 );
838 header( 'Content-Encoding: ' . $wp_cache_gzip_encoding );
839 header( 'Vary: Accept-Encoding, Cookie' );
840 header( 'Content-Length: ' . $gzsize );
841 return $gzdata;
842 } else {
843 wp_cache_debug( "Sending buffer to browser", 5 );
844 return $buffer;
845 }
846 }
847
848 function wp_cache_phase2_clean_cache($file_prefix) {
849 global $wpdb, $blog_cache_dir;
850
851 if( !wp_cache_writers_entry() )
852 return false;
853 wp_cache_debug( "wp_cache_phase2_clean_cache: Cleaning cache in $blog_cache_dir" );
854 if ( $handle = @opendir( $blog_cache_dir ) ) {
855 while ( false !== ($file = @readdir($handle))) {
856 if ( strpos( $file, $file_prefix ) !== false ) {
857 if ( strpos( $file, '.html' ) ) {
858 // delete old wpcache files immediately
859 wp_cache_debug( "wp_cache_phase2_clean_cache: Deleting obsolete wpcache cache+meta files: $file" );
860 @unlink( $blog_cache_dir . $file);
861 @unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
862 } else {
863 $meta = json_decode( wp_cache_get_legacy_cache( $blog_cache_dir . 'meta/' . $file ), true );
864 if ( $meta[ 'blog_id' ] == $wpdb->blogid ) {
865 @unlink( $blog_cache_dir . $file );
866 @unlink( $blog_cache_dir . 'meta/' . $file );
867 }
868 }
869 }
870 }
871 closedir($handle);
872 }
873 wp_cache_writers_exit();
874 }
875
876 function prune_super_cache( $directory, $force = false, $rename = false ) {
877
878 // Don't prune a NULL/empty directory.
879 if ( null === $directory || '' === $directory ) {
880 return false;
881 }
882
883 global $cache_max_time, $cache_path, $blog_cache_dir;
884 static $log = 0;
885 static $protected_directories = '';
886
887 $dir = $directory;
888 $directory = wpsc_get_realpath( $directory );
889 if ( $directory == '' ) {
890 wp_cache_debug( "prune_super_cache: exiting as file/directory does not exist : $dir" );
891 return false;
892 }
893 if ( ! wpsc_is_in_cache_directory( $directory ) ) {
894 wp_cache_debug( "prune_super_cache: exiting as directory is not in cache path: *$directory* (was $dir before realpath)" );
895 return false;
896 }
897
898 if ( false == @file_exists( $directory ) ) {
899 wp_cache_debug( "prune_super_cache: exiting as file/dir does not exist: $directory" );
900 return $log;
901 }
902 if( !isset( $cache_max_time ) )
903 $cache_max_time = 3600;
904
905 $now = time();
906
907 if ( $protected_directories == '' ) {
908 $protected_directories = wpsc_get_protected_directories();
909 }
910
911 if (is_dir($directory)) {
912 if( $dh = @opendir( $directory ) ) {
913 $directory = trailingslashit( $directory );
914 while( ( $entry = @readdir( $dh ) ) !== false ) {
915 if ($entry == '.' || $entry == '..')
916 continue;
917 $entry = $directory . $entry;
918 prune_super_cache( $entry, $force, $rename );
919 // If entry is a directory, AND it's not a protected one, AND we're either forcing the delete, OR the file is out of date,
920 if( is_dir( $entry ) && !in_array( $entry, $protected_directories ) && ( $force || @filemtime( $entry ) + $cache_max_time <= $now ) ) {
921 // if the directory isn't empty can't delete it
922 if( $handle = @opendir( $entry ) ) {
923 $donotdelete = false;
924 while( !$donotdelete && ( $file = @readdir( $handle ) ) !== false ) {
925 if ($file == '.' || $file == '..')
926 continue;
927 $donotdelete = true;
928 wp_cache_debug( "gc: could not delete $entry as it's not empty: $file", 2 );
929 }
930 closedir($handle);
931 }
932 if( $donotdelete )
933 continue;
934 if( !$rename ) {
935 @rmdir( $entry );
936 $log++;
937 if ( $force ) {
938 wp_cache_debug( "gc: deleted $entry, forced delete", 2 );
939 } else {
940 wp_cache_debug( "gc: deleted $entry, older than $cache_max_time seconds", 2 );
941 }
942 }
943 } elseif ( in_array( $entry, $protected_directories ) ) {
944 wp_cache_debug( "gc: could not delete $entry as it's protected.", 2 );
945 }
946 }
947 closedir($dh);
948 }
949 } elseif( is_file($directory) && ($force || @filemtime( $directory ) + $cache_max_time <= $now ) ) {
950 $oktodelete = true;
951 if ( in_array( $directory, $protected_directories ) ) {
952 wp_cache_debug( "gc: could not delete $directory as it's protected.", 2 );
953 $oktodelete = false;
954 }
955 if( $oktodelete && !$rename ) {
956 wp_cache_debug( "prune_super_cache: deleted $directory", 5 );
957 @unlink( $directory );
958 $log++;
959 } elseif( $oktodelete && $rename ) {
960 wp_cache_debug( "prune_super_cache: wp_cache_rebuild_or_delete( $directory )", 5 );
961 wp_cache_rebuild_or_delete( $directory );
962 $log++;
963 } else {
964 wp_cache_debug( "prune_super_cache: did not delete file: $directory" );
965 }
966 } else {
967 wp_cache_debug( "prune_super_cache: did not delete file as it wasn't a directory or file and not forced to delete new file: $directory" );
968 }
969 return $log;
970 }
971
972 function wp_cache_rebuild_or_delete( $file ) {
973 global $cache_rebuild_files, $cache_path, $file_prefix;
974
975
976 if ( strpos( $file, '?' ) !== false )
977 $file = substr( $file, 0, strpos( $file, '?' ) );
978
979 $file = wpsc_get_realpath( $file );
980
981 if ( ! wpsc_is_in_cache_directory( $file ) ) {
982 wp_cache_debug( "rebuild_or_gc quitting because file is not in cache_path: $file" );
983 return false;
984 }
985
986 $protected = wpsc_get_protected_directories();
987 foreach( $protected as $id => $directory ) {
988 $protected[ $id ] = wpsc_get_realpath( $directory );
989 }
990
991 if ( in_array( $file, $protected ) ) {
992 wp_cache_debug( "rebuild_or_gc: file is protected: $file" );
993 return false;
994 }
995
996 if ( substr( basename( $file ), 0, mb_strlen( $file_prefix ) ) == $file_prefix ) {
997 @unlink( $file );
998 wp_cache_debug( "rebuild_or_gc: deleted non-anonymous file: $file" );
999 return false;
1000 }
1001
1002 if ( substr( basename( $file ), 0, 5 + mb_strlen( $file_prefix ) ) == 'meta-' . $file_prefix ) {
1003 @unlink( $file );
1004 wp_cache_debug( "rebuild_or_gc: deleted meta file: $file" );
1005 return false;
1006 }
1007
1008 if ( false == @file_exists( $file ) ) {
1009 wp_cache_debug( "rebuild_or_gc: file has disappeared: $file" );
1010 return false;
1011 }
1012 if( $cache_rebuild_files && substr( $file, -14 ) != '.needs-rebuild' ) {
1013 if( @rename($file, $file . '.needs-rebuild') ) {
1014 @touch( $file . '.needs-rebuild' );
1015 wp_cache_debug( "rebuild_or_gc: rename file to {$file}.needs-rebuild", 2 );
1016 } else {
1017 @unlink( $file );
1018 wp_cache_debug( "rebuild_or_gc: rename failed. deleted $file", 2 );
1019 }
1020 } else {
1021 $mtime = @filemtime( $file );
1022 if ( $mtime && ( time() - $mtime ) > 10 ) {
1023 @unlink( $file );
1024 wp_cache_debug( "rebuild_or_gc: rebuild file found. deleted because it was too old: $file", 2 );
1025 }
1026 }
1027 }
1028
1029 function wp_cache_phase2_clean_expired( $file_prefix, $force = false ) {
1030 global $cache_path, $cache_max_time, $blog_cache_dir, $wp_cache_preload_on;
1031
1032 clearstatcache();
1033 if( !wp_cache_writers_entry() )
1034 return false;
1035 $now = time();
1036 wp_cache_debug( "Cleaning expired cache files in $blog_cache_dir", 2 );
1037 $deleted = 0;
1038 if ( ( $handle = @opendir( $blog_cache_dir ) ) ) {
1039 while ( false !== ($file = readdir($handle))) {
1040 if ( preg_match("/^$file_prefix/", $file) &&
1041 (@filemtime( $blog_cache_dir . $file) + $cache_max_time) <= $now ) {
1042 @unlink( $blog_cache_dir . $file );
1043 @unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
1044 wp_cache_debug( "wp_cache_phase2_clean_expired: Deleting obsolete wpcache cache+meta files: $file" );
1045 continue;
1046 }
1047 if($file != '.' && $file != '..') {
1048 if( is_dir( $blog_cache_dir . $file ) == false && (@filemtime($blog_cache_dir . $file) + $cache_max_time) <= $now ) {
1049 if ( substr( $file, -9 ) != '.htaccess' && $file != 'index.html' ) {
1050 @unlink($blog_cache_dir . $file);
1051 wp_cache_debug( "Deleting $blog_cache_dir{$file}, older than $cache_max_time seconds", 5 );
1052 }
1053 }
1054 }
1055 }
1056 closedir($handle);
1057 if ( false == $wp_cache_preload_on || true == $force ) {
1058 wp_cache_debug( "Doing GC on supercache dir: {$cache_path}supercache", 2 );
1059 $deleted = prune_super_cache( $cache_path . 'supercache', false, false );
1060 }
1061 }
1062
1063 wp_cache_writers_exit();
1064 return $deleted;
1065 }
1066
1067 function wp_cache_shutdown_callback() {
1068 global $cache_max_time, $meta_file, $new_cache, $wp_cache_meta, $known_headers, $blog_id, $wp_cache_gzip_encoding, $supercacheonly, $blog_cache_dir;
1069 global $wp_cache_request_uri, $wp_cache_key, $wp_cache_object_cache, $cache_enabled, $wp_cache_blog_charset, $wp_cache_not_logged_in;
1070 global $WPSC_HTTP_HOST;
1071
1072 if ( false == $new_cache ) {
1073 wp_cache_debug( "wp_cache_shutdown_callback: No cache file created. Returning." );
1074 return false;
1075 }
1076
1077 $wp_cache_meta[ 'uri' ] = $WPSC_HTTP_HOST . preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $wp_cache_request_uri); // To avoid XSS attacks
1078 $wp_cache_meta[ 'blog_id' ] = $blog_id;
1079 $wp_cache_meta[ 'post' ] = wp_cache_post_id();
1080 $wp_cache_meta[ 'key' ] = $wp_cache_key;
1081 $wp_cache_meta = apply_filters( 'wp_cache_meta', $wp_cache_meta );
1082
1083 $headers = wp_cache_get_response_headers();
1084 foreach( $headers as $key => $value ) {
1085 $wp_cache_meta[ 'headers' ][ $key ] = "$key: $value";
1086 }
1087
1088 wp_cache_debug( "wp_cache_shutdown_callback: collecting meta data.", 2 );
1089
1090 if (!isset( $response['Last-Modified'] )) {
1091 $value = gmdate('D, d M Y H:i:s') . ' GMT';
1092 /* Dont send this the first time */
1093 /* @header('Last-Modified: ' . $value); */
1094 $wp_cache_meta[ 'headers' ][ 'Last-Modified' ] = "Last-Modified: $value";
1095 }
1096 if ( !isset( $response[ 'Content-Type' ] ) && !isset( $response[ 'Content-type' ] ) ) {
1097 // On some systems, headers set by PHP can't be fetched from
1098 // the output buffer. This is a last ditch effort to set the
1099 // correct Content-Type header for feeds, if we didn't see
1100 // it in the response headers already. -- dougal
1101 if (is_feed()) {
1102 $type = get_query_var('feed');
1103 $type = str_replace('/','',$type);
1104 switch ($type) {
1105 case 'atom':
1106 $value = "application/atom+xml";
1107 break;
1108 case 'rdf':
1109 $value = "application/rdf+xml";
1110 break;
1111 case ( 'sitemap' || 'sitemap_n' ):
1112 $value = "text/xml";
1113 break;
1114 case 'rss':
1115 case 'rss2':
1116 default:
1117 $value = "application/rss+xml";
1118 }
1119 } else { // not a feed
1120 $value = get_option( 'html_type' );
1121 if( $value == '' )
1122 $value = 'text/html';
1123 }
1124 $value .= "; charset=\"" . $wp_cache_blog_charset . "\"";
1125
1126 wp_cache_debug( "Sending 'Content-Type: $value' header.", 2 );
1127 @header("Content-Type: $value");
1128 $wp_cache_meta[ 'headers' ][ 'Content-Type' ] = "Content-Type: $value";
1129 }
1130
1131 if ( $cache_enabled && !$supercacheonly && $new_cache ) {
1132 if( !isset( $wp_cache_meta[ 'dynamic' ] ) && $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $wp_cache_meta[ 'headers' ] ) ) {
1133 wp_cache_debug( "Sending gzip headers.", 2 );
1134 $wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
1135 $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
1136 }
1137
1138 $serial = '<?php die(); ?>' . json_encode( $wp_cache_meta );
1139 $dir = get_current_url_supercache_dir();
1140 if( @is_dir( $dir ) == false )
1141 @wp_mkdir_p( $dir );
1142
1143 if( wp_cache_writers_entry() ) {
1144 wp_cache_debug( "Writing meta file: {$dir}meta-{$meta_file}", 2 );
1145 if ( false == $wp_cache_object_cache ) {
1146 $tmp_meta_filename = $dir . uniqid( mt_rand(), true ) . '.tmp';
1147 $final_meta_filename = $dir . "meta-" . $meta_file;
1148 $fr = @fopen( $tmp_meta_filename, 'w');
1149 if ( $fr ) {
1150 fputs($fr, $serial);
1151 fclose($fr);
1152 @chmod( $tmp_meta_filename, 0666 & ~umask());
1153 if( !@rename( $tmp_meta_filename, $final_meta_filename ) ) {
1154 @unlink( $dir . $final_meta_filename );
1155 @rename( $tmp_meta_filename, $final_meta_filename );
1156 }
1157 } else {
1158 wp_cache_debug( "Problem writing meta file: {$final_meta_filename}" );
1159 }
1160 } elseif ( $cache_enabled ) {
1161 $oc_key = get_oc_key() . ".meta";
1162 if ( gzip_accepted() )
1163 $oc_key .= ".gz";
1164 wp_cache_set( $oc_key, $serial, 'supercache', $cache_max_time );
1165 }
1166 wp_cache_writers_exit();
1167 }
1168 } else {
1169 wp_cache_debug( "Did not write meta file: meta-{$meta_file} *$supercacheonly* *$wp_cache_not_logged_in* *$new_cache*", 2 );
1170 }
1171 global $time_to_gc_cache;
1172 if( isset( $time_to_gc_cache ) && $time_to_gc_cache == 1 ) {
1173 wp_cache_debug( "Executing wp_cache_gc action.", 3 );
1174 do_action( 'wp_cache_gc' );
1175 }
1176 }
1177
1178 function wp_cache_no_postid($id) {
1179 return wp_cache_post_change(wp_cache_post_id());
1180 }
1181
1182 function wp_cache_get_postid_from_comment( $comment_id, $status = 'NA' ) {
1183 global $super_cache_enabled, $wp_cache_request_uri;
1184 $comment = get_comment($comment_id, ARRAY_A);
1185 if ( $status != 'NA' ) {
1186 $comment[ 'old_comment_approved' ] = $comment[ 'comment_approved' ];
1187 $comment[ 'comment_approved' ] = $status;
1188 }
1189
1190 if ( ( $status == 'trash' || $status == 'spam' ) && $comment[ 'old_comment_approved' ] != 1 ) {
1191 // don't modify cache if moderated comments are trashed or spammed
1192 wp_cache_debug( "Moderated comment deleted or spammed. Don't delete any cache files.", 4 );
1193 define( 'DONOTDELETECACHE', 1 );
1194 return wp_cache_post_id();
1195 }
1196 $postid = $comment['comment_post_ID'];
1197 // Do nothing if comment is not moderated
1198 // http://ocaoimh.ie/2006/12/05/caching-wordpress-with-wp-cache-in-a-spam-filled-world
1199 if ( !preg_match('/wp-admin\//', $wp_cache_request_uri) ) {
1200 if ( $comment['comment_approved'] == 'delete' && ( isset( $comment[ 'old_comment_approved' ] ) && $comment[ 'old_comment_approved' ] == 0 ) ) { // do nothing if moderated comments are deleted
1201 wp_cache_debug( "Moderated comment deleted. Don't delete any cache files.", 4 );
1202 define( 'DONOTDELETECACHE', 1 );
1203 return $postid;
1204 } elseif ( $comment['comment_approved'] == 'spam' ) {
1205 wp_cache_debug( "Spam comment. Don't delete any cache files.", 4 );
1206 define( 'DONOTDELETECACHE', 1 );
1207 return $postid;
1208 } elseif( $comment['comment_approved'] == '0' ) {
1209 if ( $comment[ 'comment_type' ] == '' ) {
1210 wp_cache_debug( "Moderated comment. Don't delete supercache file until comment approved.", 4 );
1211 $super_cache_enabled = 0; // don't remove the super cache static file until comment is approved
1212 define( 'DONOTDELETECACHE', 1 );
1213 } else {
1214 wp_cache_debug( "Moderated ping or trackback. Not deleting cache files..", 4 );
1215 define( 'DONOTDELETECACHE', 1 );
1216 return $postid;
1217 }
1218 }
1219 }
1220 // We must check it up again due to WP bugs calling two different actions
1221 // for delete, for example both wp_set_comment_status and delete_comment
1222 // are called when deleting a comment
1223 if ($postid > 0) {
1224 wp_cache_debug( "Post $postid changed. Update cache.", 4 );
1225 return wp_cache_post_change( $postid );
1226 } elseif ( $_GET[ 'delete_all' ] != 'Empty Trash' && $_GET[ 'delete_all2' ] != 'Empty Spam' ) {
1227 wp_cache_debug( "Unknown post changed. Update cache.", 4 );
1228 return wp_cache_post_change( wp_cache_post_id() );
1229 }
1230 }
1231
1232 /* Used by wp_update_nav_menu action to clear current blog's cache files when navigation menu is modified */
1233 function wp_cache_clear_cache_on_menu() {
1234 global $wpdb;
1235 wp_cache_clear_cache( $wpdb->blogid );
1236 }
1237
1238 /* Clear out the cache directory. */
1239 function wp_cache_clear_cache( $blog_id = 0 ) {
1240 global $cache_path, $wp_cache_object_cache;
1241 if ( $wp_cache_object_cache ) {
1242 reset_oc_version();
1243 } else {
1244 if ( $blog_id == 0 ) {
1245 wp_cache_debug( "Clearing all cached files in wp_cache_clear_cache()", 4 );
1246 prune_super_cache( $cache_path . 'supercache/', true );
1247 prune_super_cache( $cache_path, true );
1248 } else {
1249 wp_cache_debug( "Clearing all cached files for blog $blog_id in wp_cache_clear_cache()", 4 );
1250 prune_super_cache( get_supercache_dir( $blog_id ), true );
1251 prune_super_cache( $cache_path . 'blogs/', true );
1252 }
1253 }
1254 }
1255
1256 function wpsc_delete_cats_tags( $post ) {
1257 $post = get_post($post);
1258 $categories = get_the_category($post->ID);
1259 if ( $categories ) {
1260 $category_base = get_option( 'category_base');
1261 if ( $category_base == '' )
1262 $category_base = '/category/';
1263 $category_base = trailingslashit( $category_base ); // paranoid much?
1264 foreach ($categories as $cat) {
1265 prune_super_cache ( get_supercache_dir() . $category_base . $cat->slug . '/', true );
1266 wp_cache_debug( "wpsc_post_transition: deleting category: " . get_supercache_dir() . $category_base . $cat->slug . '/' );
1267 }
1268 }
1269 $posttags = get_the_tags($post->ID);
1270 if ( $posttags ) {
1271 $tag_base = get_option( 'tag_base' );
1272 if ( $tag_base == '' )
1273 $tag_base = '/tag/';
1274 $tag_base = trailingslashit( str_replace( '..', '', $tag_base ) ); // maybe!
1275 foreach ($posttags as $tag) {
1276 prune_super_cache( get_supercache_dir() . $tag_base . $tag->slug . '/', true );
1277 wp_cache_debug( "wpsc_post_transition: deleting tag: " . get_supercache_dir() . $tag_base . $tag->slug . '/' );
1278 }
1279 }
1280 }
1281
1282 function wpsc_post_transition( $new_status, $old_status, $post ) {
1283 if (
1284 ($old_status == 'publish' && $new_status != 'publish' ) // post unpublished
1285 ||
1286 ($old_status != 'publish' && $new_status == 'publish') // post published
1287 ) {
1288 wpsc_delete_cats_tags( $post );
1289 prune_super_cache( get_supercache_dir() . '/' . $post->post_name . '/', true );
1290 wp_cache_debug( "wpsc_post_transition: deleting post: " . get_supercache_dir() . '/' . $post->post_name . '/' );
1291 }
1292 }
1293
1294 /* check if we want to clear out all cached files on post updates, otherwise call standard wp_cache_post_change() */
1295 function wp_cache_post_edit($post_id) {
1296 global $wp_cache_clear_on_post_edit, $cache_path, $blog_cache_dir, $wp_cache_object_cache;
1297 static $last_post_edited = -1;
1298
1299 if ( $post_id == $last_post_edited ) {
1300 wp_cache_debug( "wp_cache_post_edit: Already processed post $post_id.", 4 );
1301 return $post_id;
1302 }
1303
1304 $post = get_post( $post_id );
1305 if ( is_object( $post ) == false )
1306 return $post_id;
1307
1308 // Some users are inexplicibly seeing this error on scheduled posts.
1309 // define this constant to disable the post status check.
1310 if ( false == defined( 'WPSCFORCEUPDATE' ) && !in_array($post->post_status, array( 'publish', 'private' ) ) ) {
1311 wp_cache_debug( "wp_cache_post_edit: draft post, not deleting any cache files. status: " . $post->post_status, 4 );
1312 return $post_id;
1313 }
1314
1315 // we want to process the post again just in case it becomes published before the second time this function is called.
1316 $last_post_edited = $post_id;
1317 if( $wp_cache_clear_on_post_edit ) {
1318 wp_cache_debug( "wp_cache_post_edit: Clearing cache $blog_cache_dir and {$cache_path}supercache/ on post edit per config.", 2 );
1319 if ( $wp_cache_object_cache ) {
1320 reset_oc_version();
1321 } else {
1322 prune_super_cache( $blog_cache_dir, true );
1323 prune_super_cache( get_supercache_dir(), true );
1324 }
1325 } else {
1326 wp_cache_debug( "wp_cache_post_edit: Clearing cache for post $post_id on post edit.", 2 );
1327 wp_cache_post_change( $post_id );
1328 }
1329 }
1330
1331 function wp_cache_post_id_gc( $post_id, $all = 'all' ) {
1332 global $wp_cache_object_cache;
1333
1334 if ( $wp_cache_object_cache )
1335 reset_oc_version();
1336
1337 $post_id = intval( $post_id );
1338 if( $post_id == 0 )
1339 return;
1340
1341 $permalink = trailingslashit( str_replace( get_option( 'home' ), '', get_permalink( $post_id ) ) );
1342 $dir = get_current_url_supercache_dir( $post_id );
1343 wp_cache_debug( "wp_cache_post_id_gc post_id: $post_id " . get_permalink( $post_id ) . " clearing cache in $dir.", 4 );
1344 if ( $all ) {
1345 prune_super_cache( $dir, true, true );
1346 do_action( 'gc_cache', 'prune', $permalink );
1347 @rmdir( $dir );
1348 $supercache_home = get_supercache_dir();
1349 wp_cache_debug( "wp_cache_post_id_gc clearing cache in {$supercache_home}page/." );
1350 prune_super_cache( $supercache_home . 'page/', true );
1351 do_action( 'gc_cache', 'prune', 'page/' );
1352 } else {
1353 wp_cache_debug( "wp_cache_post_id_gc clearing cached index files in $dir.", 4 );
1354 prune_super_cache( $dir, true, true );
1355 do_action( 'gc_cache', 'prune', $permalink );
1356 }
1357 }
1358
1359 function wp_cache_post_change( $post_id ) {
1360 global $file_prefix, $cache_path, $blog_id, $super_cache_enabled, $blog_cache_dir, $wp_cache_refresh_single_only, $wp_cache_object_cache;
1361 static $last_processed = -1;
1362
1363 if ( $post_id == $last_processed ) {
1364 wp_cache_debug( "wp_cache_post_change: Already processed post $post_id.", 4 );
1365 return $post_id;
1366 }
1367 $post = get_post( $post_id );
1368 // Some users are inexplicibly seeing this error on scheduled posts.
1369 // define this constant to disable the post status check.
1370 if ( false == defined( 'WPSCFORCEUPDATE' ) && is_object( $post ) && !in_array($post->post_status, array( 'publish', 'private' ) ) ) {
1371 wp_cache_debug( "wp_cache_post_change: draft post, not deleting any cache files.", 4 );
1372 return $post_id;
1373 }
1374 $last_processed = $post_id;
1375
1376 if( !wp_cache_writers_entry() )
1377 return $post_id;
1378
1379 if ( isset( $wp_cache_refresh_single_only ) && $wp_cache_refresh_single_only && ( strpos( $_SERVER[ 'HTTP_REFERER' ], 'edit-comments.php' ) || strpos( $_SERVER[ 'REQUEST_URI' ], 'wp-comments-post.php' ) ) ) {
1380 if ( defined( 'DONOTDELETECACHE' ) ) {
1381 wp_cache_debug( "wp_cache_post_change: comment detected and it's moderated or spam. Not deleting cached files.", 4 );
1382 return $post_id;
1383 } else {
1384 wp_cache_debug( "wp_cache_post_change: comment detected. only deleting post page.", 4 );
1385 $all = false;
1386 }
1387 } else {
1388 $all = true;
1389 }
1390
1391 $all_backup = $all;
1392 $all = apply_filters( 'wpsc_delete_related_pages_on_edit', $all ); // return 0 to disable deleting homepage and other pages.
1393 if ( $all != $all_backup )
1394 wp_cache_debug( 'wp_cache_post_change: $all changed by wpsc_delete_related_pages_on_edit filter: ' . intval( $all ) );
1395
1396 if ( $wp_cache_object_cache )
1397 reset_oc_version();
1398
1399 // Delete supercache files whenever a post change event occurs, even if supercache is currently disabled.
1400 $dir = get_supercache_dir();
1401 // make sure the front page has a rebuild file
1402 wp_cache_post_id_gc( $post_id, $all );
1403 if ( $all == true ) {
1404 wp_cache_debug( "Post change: supercache enabled: deleting cache files in " . $dir );
1405 wpsc_rebuild_files( $dir );
1406 do_action( 'gc_cache', 'prune', 'homepage' );
1407 } else {
1408 wp_cache_debug( "wp_cache_post_change: not deleting all pages.", 4 );
1409 }
1410 if( $all == true && get_option( 'show_on_front' ) == 'page' ) {
1411 wp_cache_debug( "Post change: deleting page_on_front and page_for_posts pages.", 4 );
1412 wp_cache_debug( "Post change: page_on_front " . get_option( 'page_on_front' ), 4 );
1413 $permalink = trailingslashit( str_replace( get_option( 'home' ), '', get_permalink( get_option( 'page_for_posts' ) ) ) );
1414 wp_cache_debug( "Post change: Deleting files in: " . str_replace( '//', '/', $dir . $permalink ) );
1415 wpsc_rebuild_files( $dir . $permalink );
1416 do_action( 'gc_cache', 'prune', $permalink );
1417 } else {
1418 wp_cache_debug( "wp_cache_post_change: not deleting front static page.", 4 );
1419 }
1420
1421 wp_cache_debug( "wp_cache_post_change: checking {$blog_cache_dir}meta/", 4 );
1422 $supercache_files_deleted = false;
1423 if ( $handle = @opendir( $blog_cache_dir ) ) {
1424 while ( false !== ($file = readdir($handle))) {
1425 if ( strpos( $file, $file_prefix ) !== false ) {
1426 if ( strpos( $file, '.html' ) ) {
1427 // delete old wpcache files immediately
1428 wp_cache_debug( "wp_cache_post_change: Deleting obsolete wpcache cache+meta files: $file" );
1429 @unlink( $blog_cache_dir . $file);
1430 @unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
1431 continue;
1432 } else {
1433 $meta = json_decode( wp_cache_get_legacy_cache( $blog_cache_dir . 'meta/' . $file ), true );
1434 if( false == is_array( $meta ) ) {
1435 wp_cache_debug( "Post change cleaning up stray file: $file", 4 );
1436 @unlink( $blog_cache_dir . 'meta/' . $file );
1437 @unlink( $blog_cache_dir . $file );
1438 continue;
1439 }
1440 if ( $post_id > 0 && $meta ) {
1441 $permalink = trailingslashit( str_replace( get_option( 'home' ), '', get_permalink( $post_id ) ) );
1442 if ( $meta[ 'blog_id' ] == $blog_id && ( ( $all == true && !$meta[ 'post' ] ) || $meta[ 'post' ] == $post_id) ) {
1443 wp_cache_debug( "Post change: deleting post wp-cache files for {$meta[ 'uri' ]}: $file", 4 );
1444 @unlink( $blog_cache_dir . 'meta/' . $file );
1445 @unlink( $blog_cache_dir . $file );
1446 if ( false == $supercache_files_deleted && $super_cache_enabled == true ) {
1447 wp_cache_debug( "Post change: deleting supercache files for {$permalink}" );
1448 wpsc_rebuild_files( $dir . $permalink );
1449 $supercache_files_deleted = true;
1450 do_action( 'gc_cache', 'rebuild', $permalink );
1451 }
1452 }
1453 } elseif ( $meta[ 'blog_id' ] == $blog_id ) {
1454 wp_cache_debug( "Post change: deleting wp-cache files for {$meta[ 'uri' ]}: $file", 4 );
1455 @unlink( $blog_cache_dir . 'meta/' . $file );
1456 @unlink( $blog_cache_dir . $file );
1457 if ( $super_cache_enabled == true ) {
1458 wp_cache_debug( "Post change: deleting supercache files for {$meta[ 'uri' ]}" );
1459 wpsc_rebuild_files( $dir . $meta[ 'uri' ] );
1460 do_action( 'gc_cache', 'rebuild', trailingslashit( $meta[ 'uri' ] ) );
1461 }
1462 }
1463 }
1464 }
1465 }
1466 closedir($handle);
1467 }
1468 wp_cache_writers_exit();
1469 return $post_id;
1470 }
1471
1472 function wp_cache_microtime_diff($a, $b) {
1473 list($a_dec, $a_sec) = explode(' ', $a);
1474 list($b_dec, $b_sec) = explode(' ', $b);
1475 return (float)$b_sec - (float)$a_sec + (float)$b_dec - (float)$a_dec;
1476 }
1477
1478 function wp_cache_post_id() {
1479 global $posts, $comment_post_ID, $post_ID;
1480 // We try hard all options. More frequent first.
1481 if ($post_ID > 0 ) return $post_ID;
1482 if ($comment_post_ID > 0 ) return $comment_post_ID;
1483 if (is_single() || is_page()) return $posts[0]->ID;
1484 if (isset( $_GET[ 'p' ] ) && $_GET['p'] > 0) return $_GET['p'];
1485 if (isset( $_POST[ 'p' ] ) && $_POST['p'] > 0) return $_POST['p'];
1486 return 0;
1487 }
1488
1489 function maybe_stop_gc( $flag ) {
1490
1491 if ( @file_exists( $flag ) ) {
1492 if ( time() - filemtime( $flag ) > 3600 ) {
1493 @unlink( $flag );
1494 wp_cache_debug( "maybe_stop_gc: GC flag found but deleted because it's older than 3600 seconds.", 5 );
1495 return false;
1496 } else {
1497 wp_cache_debug( 'maybe_stop_gc: GC flag found. GC cancelled.', 5 );
1498 return true;
1499 }
1500 } else {
1501 wp_cache_debug( 'maybe_stop_gc: GC flag not found. GC will go ahead..', 5 );
1502 return false;
1503 }
1504 }
1505 function get_gc_flag() {
1506 global $cache_path;
1507 return $cache_path . strtolower( preg_replace( '!/:.*$!', '', str_replace( 'http://', '', str_replace( 'https://', '', get_option( 'home' ) ) ) ) ) . "_wp_cache_gc.txt";
1508 }
1509
1510 function wp_cache_gc_cron() {
1511 global $file_prefix, $cache_max_time, $cache_gc_email_me, $cache_time_interval;
1512
1513 $msg = '';
1514 if ( $cache_max_time == 0 ) {
1515 wp_cache_debug( 'Cache garbage collection disabled because cache expiry time is zero.', 5 );
1516 return false;
1517 }
1518
1519 $gc_flag = get_gc_flag();
1520 if ( maybe_stop_gc( $gc_flag ) ) {
1521 wp_cache_debug( 'GC flag found. GC cancelled.', 5 );
1522 return false;
1523 }
1524
1525 update_option( 'wpsupercache_gc_time', time() );
1526 wp_cache_debug( "wp_cache_gc_cron: Set GC Flag. ($gc_flag)", 5 );
1527 $fp = @fopen( $gc_flag, 'w' );
1528 @fclose( $fp );
1529
1530 wp_cache_debug( 'Cache garbage collection.', 5 );
1531
1532 if( !isset( $cache_max_time ) )
1533 $cache_max_time = 600;
1534
1535 $start = time();
1536 $num = 0;
1537 if( false === ( $num = wp_cache_phase2_clean_expired( $file_prefix ) ) ) {
1538 wp_cache_debug( 'Cache Expiry cron job failed. Probably mutex locked.', 1 );
1539 update_option( 'wpsupercache_gc_time', time() - ( $cache_time_interval - 10 ) ); // if GC failed then run it again in one minute
1540 $msg .= __( 'Cache expiry cron job failed. Job will run again in 10 seconds.', 'wp-super-cache' ) . "\n";
1541 }
1542 if( time() - $start > 30 ) {
1543 wp_cache_debug( "Cache Expiry cron job took more than 30 seconds to execute.\nYou should reduce the Expiry Time in the WP Super Cache admin page\nas you probably have more cache files than your server can handle efficiently.", 1 );
1544 $msg .= __( 'Cache expiry cron job took more than 30 seconds. You should probably run the garbage collector more often.', 'wp-super-cache' ) . "\n";
1545 }
1546
1547 if ( $cache_gc_email_me ) {
1548 if ( $msg != '' )
1549 $msg = "The following warnings were generated by the WP Super Cache Garbage Collector:\n" . $msg;
1550
1551 $msg = "Hi,\n\nThe WP Super Cache Garbage Collector has now run, deleting " . (int)$num . " files and directories.\nIf you want to switch off these emails please see the WP Super Cache Advanced Settings\npage on your blog.\n\n{$msg}\nRegards,\nThe Garbage Collector.";
1552
1553 wp_mail( get_option( 'admin_email' ), sprintf( __( '[%1$s] WP Super Cache GC Report', 'wp-super-cache' ), home_url() ), $msg );
1554 }
1555 @unlink( $gc_flag );
1556 wp_cache_debug( 'GC completed. GC flag deleted.', 5 );
1557 schedule_wp_gc( 1 );
1558 }
1559
1560 function schedule_wp_gc( $forced = 0 ) {
1561 global $cache_schedule_type, $cache_max_time, $cache_time_interval, $cache_scheduled_time, $cache_schedule_interval;
1562
1563 if ( false == isset( $cache_time_interval ) )
1564 $cache_time_interval = 3600;
1565
1566 if ( false == isset( $cache_schedule_type ) ) {
1567 $cache_schedule_type = 'interval';
1568 $cache_schedule_interval = $cache_max_time;
1569 }
1570 if ( $cache_schedule_type == 'interval' ) {
1571 if ( !isset( $cache_max_time ) )
1572 $cache_max_time = 600;
1573 if ( $cache_max_time == 0 )
1574 return false;
1575 $last_gc = get_option( "wpsupercache_gc_time" );
1576
1577 if ( !$last_gc ) {
1578 update_option( 'wpsupercache_gc_time', time() );
1579 $last_gc = get_option( "wpsupercache_gc_time" );
1580 }
1581 if ( $forced || ( $last_gc < ( time() - 60 ) ) ) { // Allow up to 60 seconds for the previous job to run
1582 global $wp_cache_shutdown_gc;
1583 if ( !isset( $wp_cache_shutdown_gc ) || $wp_cache_shutdown_gc == 0 ) {
1584 if ( !($t = wp_next_scheduled( 'wp_cache_gc' ) ) ) {
1585 wp_clear_scheduled_hook( 'wp_cache_gc' );
1586 wp_schedule_single_event( time() + $cache_time_interval, 'wp_cache_gc' );
1587 wp_cache_debug( 'scheduled wp_cache_gc for 10 seconds time.', 5 );
1588 }
1589 } else {
1590 global $time_to_gc_cache;
1591 $time_to_gc_cache = 1; // tell the "shutdown gc" to run!
1592 }
1593 }
1594 } elseif ( $cache_schedule_type == 'time' && !wp_next_scheduled( 'wp_cache_gc' ) ) {
1595 wp_schedule_event( strtotime( $cache_scheduled_time ), $cache_schedule_interval, 'wp_cache_gc' );
1596 }
1597 return true;
1598 }
1599
1600 function wp_cache_gc_watcher() {
1601 if ( false == wp_next_scheduled( 'wp_cache_gc' ) ) {
1602 wp_cache_debug( 'GC Watcher: scheduled new gc cron.', 5 );
1603 schedule_wp_gc();
1604 }
1605 }
1606
1607 ?>
1608