PluginProbe ʕ •ᴥ•ʔ
WP Super Cache / 0.9.9.4
WP Super Cache v0.9.9.4
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 16 years ago plugins 15 years ago Changelog.txt 15 years ago advanced-cache.php 17 years ago readme.txt 15 years ago uninstall.php 16 years ago wp-cache-base.php 18 years ago wp-cache-config-sample.php 15 years ago wp-cache-phase1.php 15 years ago wp-cache-phase2.php 15 years ago wp-cache.php 15 years ago wp-super-cache.pot 15 years ago
wp-cache-phase2.php
1028 lines
1 <?php
2
3 function wp_cache_phase2() {
4 global $wpsc_settings;
5 global $cache_filename, $cache_acceptable_files, $wp_cache_gzip_encoding, $super_cache_enabled, $cache_rebuild_files, $wp_cache_last_gc;
6 global $cache_max_time, $wp_cache_request_uri, $super_cache_enabled, $wp_cache_object_cache;
7 global $cache_enabled;
8
9 if ( $cache_enabled == false )
10 return false;
11
12 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'In WP Cache Phase 2', 5 );
13
14 $wpsc_settings[ 'wp_cache_gmt_offset' ] = get_option( 'gmt_offset' ); // caching for later use when wpdb is gone. http://wordpress.org/support/topic/224349
15 $wpsc_settings[ 'wp_cache_blog_charset' ] = get_option( 'blog_charset' );
16
17 wp_cache_mutex_init();
18 if(function_exists('add_action') && ( !defined( 'WPLOCKDOWN' ) || ( defined( 'WPLOCKDOWN' ) && constant( 'WPLOCKDOWN' ) == '0' ) ) ) {
19 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Setting up WordPress actions', 5 );
20 // Post ID is received
21 add_action('publish_post', 'wp_cache_post_edit', 0);
22 add_action('edit_post', 'wp_cache_post_change', 0); // leaving a comment called edit_post
23 add_action('delete_post', 'wp_cache_post_edit', 0);
24 add_action('publish_phone', 'wp_cache_post_edit', 0);
25 // Coment ID is received
26 add_action('trackback_post', 'wp_cache_get_postid_from_comment', 99);
27 add_action('pingback_post', 'wp_cache_get_postid_from_comment', 99);
28 add_action('comment_post', 'wp_cache_get_postid_from_comment', 99);
29 add_action('edit_comment', 'wp_cache_get_postid_from_comment', 99);
30 add_action('wp_set_comment_status', 'wp_cache_get_postid_from_comment', 99, 2);
31 // No post_id is available
32 add_action('switch_theme', 'wp_cache_no_postid', 99);
33 add_action('edit_user_profile_update', 'wp_cache_no_postid', 99);
34
35 add_action( 'wp_update_nav_menu', 'wp_cache_clear_cache' );
36
37 add_action('wp_cache_gc','wp_cache_gc_cron');
38
39 do_cacheaction( 'add_cacheaction' );
40 }
41
42 if ( is_admin() ) {
43 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching wp-admin requests.', 5 );
44 return false;
45 }
46
47 if ( $_SERVER["REQUEST_METHOD"] == 'POST' || !empty( $_POST ) || get_option( 'gzipcompression' ) ) {
48 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching POST request.', 5 );
49 return false;
50 }
51
52 if ( $wp_cache_object_cache && !empty( $_GET ) ) {
53 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching GET request while object cache storage enabled.', 5 );
54 return false;
55 }
56
57 if ( isset( $_GET[ 'preview' ] ) ) {
58 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching preview post.', 2 );
59 return false;
60 }
61
62 if ( !empty( $_GET ) ) {
63 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Supercache caching disabled. Only using wp-cache. Non empty GET request.', 5 );
64 $super_cache_enabled = false;
65 }
66
67 $script = basename($_SERVER['PHP_SELF']);
68 if (!in_array($script, $cache_acceptable_files) && wp_cache_is_rejected($wp_cache_request_uri)) {
69 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'URI rejected. Not Caching', 2 );
70 return false;
71 }
72 if (wp_cache_user_agent_is_rejected()) {
73 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "USER AGENT ({$_SERVER[ 'HTTP_USER_AGENT' ]}) rejected. Not Caching", 4 );
74 return;
75 }
76 if($wp_cache_gzip_encoding)
77 header('Vary: Accept-Encoding, Cookie');
78 else
79 header('Vary: Cookie');
80 ob_start( 'wp_cache_ob_callback' );
81 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Created output buffer', 4 );
82
83 // restore old supercache file temporarily
84 if( $super_cache_enabled && $cache_rebuild_files ) {
85 $user_info = wp_cache_get_cookies_values();
86 $do_cache = apply_filters( 'do_createsupercache', $user_info );
87 if( $user_info == '' || $do_cache === true ) {
88 $dir = get_current_url_supercache_dir();
89 $files_to_check = array( $dir . 'index.html', $dir . 'index.html.gz' );
90 foreach( $files_to_check as $cache_file ) {
91 if( !@file_exists( $cache_file . '.needs-rebuild' ) )
92 continue;
93 $mtime = @filemtime($cache_file . '.needs-rebuild');
94 if( $mtime && (time() - $mtime) < 30 ) {
95 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Rebuild file renamed to cache file temporarily", 3 );
96 @rename( $cache_file . '.needs-rebuild', $cache_file );
97 }
98 // cleanup old files or if rename fails
99 if( @file_exists( $cache_file . '.needs-rebuild' ) ) {
100 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Rebuild file deleted", 3 );
101 @unlink( $cache_file . '.needs-rebuild' );
102 }
103 }
104 }
105 }
106
107 if ( $cache_max_time == 0 )
108 return false;
109 if( !isset( $cache_max_time ) )
110 $cache_max_time = 600;
111 $last_gc = get_option( "wpsupercache_gc_time" );
112
113 if( !$last_gc ) {
114 update_option( 'wpsupercache_gc_time', time() );
115 }
116 $next_gc = $cache_max_time < 1800 ? $cache_max_time : 600;
117 if( $last_gc < ( time() - $next_gc ) ) {
118 update_option( 'wpsupercache_gc_time', time() );
119
120 global $wp_cache_shutdown_gc;
121 if( !isset( $wp_cache_shutdown_gc ) || $wp_cache_shutdown_gc == 0 ) {
122 if(!wp_next_scheduled('wp_cache_gc')) {
123 wp_schedule_single_event(time() + 10 , 'wp_cache_gc');
124 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'scheduled wp_cache_gc for 10 seconds time.', 5 );
125 }
126 } else {
127 global $time_to_gc_cache;
128 $time_to_gc_cache = 1; // tell the "shutdown gc" to run!
129 }
130 }
131 }
132
133 function wpcache_logged_in_message() {
134 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. -->';
135 }
136
137 if ( !function_exists( 'wp_cache_user_agent_is_rejected' ) ) {
138 function wp_cache_user_agent_is_rejected() {
139 global $cache_rejected_user_agent;
140
141 if (!function_exists('apache_request_headers')) return false;
142 $headers = apache_request_headers();
143 if (!isset($headers["User-Agent"])) return false;
144 foreach ($cache_rejected_user_agent as $expr) {
145 if (strlen($expr) > 0 && stristr($headers["User-Agent"], $expr))
146 return true;
147 }
148 return false;
149 }
150 }
151
152 function wp_cache_get_response_headers() {
153 if(function_exists('apache_response_headers')) {
154 flush();
155 $headers = apache_response_headers();
156 } else if(function_exists('headers_list')) {
157 $headers = array();
158 foreach(headers_list() as $hdr) {
159 list($header_name, $header_value) = explode(': ', $hdr, 2);
160 $headers[$header_name] = $header_value;
161 }
162 } else
163 $headers = null;
164
165 return $headers;
166 }
167
168 function wp_cache_is_rejected($uri) {
169 global $cache_rejected_uri;
170
171 $auto_rejected = array( '/wp-admin/', 'xmlrpc.php', 'wp-app.php' );
172 foreach( $auto_rejected as $u ) {
173 if( strstr( $uri, $u ) )
174 return true; // we don't allow caching of wp-admin for security reasons
175 }
176 foreach ( $cache_rejected_uri as $expr ) {
177 if( $expr != '' && preg_match( "~$expr~", $uri ) )
178 return true;
179 }
180 return false;
181 }
182
183 function wp_cache_mutex_init() {
184 global $wpsc_settings, $mutex;
185
186 if( isset( $wpsc_settings[ 'wp_cache_mutex_disabled' ] ) && $wpsc_settings[ 'wp_cache_mutex_disabled' ] )
187 return true;
188
189 if( !is_bool( $wpsc_settings[ 'use_flock' ] ) ) {
190 if(function_exists('sem_get'))
191 $wpsc_settings[ 'use_flock' ] = false;
192 else
193 $wpsc_settings[ 'use_flock' ] = true;
194 }
195
196 $mutex = false;
197 if ($wpsc_settings[ 'use_flock' ] ) {
198 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Created mutex lock on filename: {$wpsc_settings[ 'blog_cache_dir' ]}{$wpsc_settings[ 'mutex_filename' ]}", 5 );
199 $mutex = @fopen( $wpsc_settings[ 'blog_cache_dir' ] . $wpsc_settings[ 'mutex_filename' ], 'w' );
200 } else {
201 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Created mutex lock on semaphore: {$wpsc_settings[ 'sem_id' ]}", 5 );
202 $mutex = @sem_get( $wpsc_settings[ 'sem_id' ], 1, 0644 | IPC_CREAT, 1 );
203 }
204 }
205
206 function wp_cache_writers_entry() {
207 global $wpsc_settings, $mutex;
208
209 if( isset( $wpsc_settings[ 'wp_cache_mutex_disabled' ] ) && $wpsc_settings[ 'wp_cache_mutex_disabled' ] )
210 return true;
211
212 if( !$mutex ) {
213 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "(writers entry) mutex lock not created. not caching.", 2 );
214 return false;
215 }
216
217 if ( $wpsc_settings[ 'use_flock' ] ) {
218 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "grabbing lock using flock()", 5 );
219 flock($mutex, LOCK_EX);
220 } else {
221 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "grabbing lock using sem_acquire()", 5 );
222 sem_acquire($mutex);
223 }
224
225 return true;
226 }
227
228 function wp_cache_writers_exit() {
229 global $wpsc_settings, $mutex;
230
231 if( isset( $wpsc_settings[ 'wp_cache_mutex_disabled' ] ) && $wpsc_settings[ 'wp_cache_mutex_disabled' ] )
232 return true;
233
234 if( !$mutex ) {
235 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "(writers exit) mutex lock not created. not caching.", 2 );
236 return false;
237 }
238
239 if ( $wpsc_settings[ 'use_flock' ] ) {
240 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "releasing lock using flock()", 5 );
241 flock($mutex, LOCK_UN);
242 } else {
243 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "releasing lock using sem_release()", 5 );
244 sem_release($mutex);
245 }
246 }
247
248 function wp_cache_ob_callback( $buffer ) {
249 global $wp_cache_pages;
250 if( defined( 'DONOTCACHEPAGE' ) ) {
251 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'DONOTCACHEPAGE defined. Caching disabled.', 2 );
252 return $buffer;
253 }
254
255 if ( isset( $wp_cache_pages[ 'single' ] ) && $wp_cache_pages[ 'single' ] == 1 && is_single() ) {
256 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching single post.', 2 );
257 return $buffer;
258 } elseif ( isset( $wp_cache_pages[ 'pages' ] ) && $wp_cache_pages[ 'pages' ] == 1 && is_page() ) {
259 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching single page.', 2 );
260 return $buffer;
261 } elseif ( isset( $wp_cache_pages[ 'archives' ] ) && $wp_cache_pages[ 'archives' ] == 1 && is_archive() ) {
262 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching archive page.', 2 );
263 return $buffer;
264 } elseif ( isset( $wp_cache_pages[ 'tag' ] ) && $wp_cache_pages[ 'tag' ] == 1 && is_tag() ) {
265 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching tag page.', 2 );
266 return $buffer;
267 } elseif ( isset( $wp_cache_pages[ 'category' ] ) && $wp_cache_pages[ 'category' ] == 1 && is_category() ) {
268 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching category page.', 2 );
269 return $buffer;
270 } elseif ( isset( $wp_cache_pages[ 'frontpage' ] ) && $wp_cache_pages[ 'frontpage' ] == 1 && is_front_page() ) {
271 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching front page.', 2 );
272 return $buffer;
273 } elseif ( isset( $wp_cache_pages[ 'home' ] ) && $wp_cache_pages[ 'home' ] == 1 && is_home() ) {
274 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching home page.', 2 );
275 return $buffer;
276 } elseif ( isset( $wp_cache_pages[ 'search' ] ) && $wp_cache_pages[ 'search' ] == 1 && is_search() ) {
277 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching search page.', 2 );
278 return $buffer;
279 } elseif ( isset( $wp_cache_pages[ 'feed' ] ) && $wp_cache_pages[ 'feed' ] == 1 && is_feed() ) {
280 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching feed.', 2 );
281 return $buffer;
282 }
283
284 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Output buffer callback', 4 );
285
286 $buffer = &wp_cache_get_ob( $buffer );
287 wp_cache_shutdown_callback();
288 return $buffer;
289 }
290
291 function wp_cache_append_tag( &$buffer ) {
292 global $wpsc_settings;
293 global $cache_enabled, $super_cache_enabled;
294 $timestamp = gmdate('Y-m-d H:i:s', (time() + ( $wpsc_settings[ 'wp_cache_gmt_offset' ] * 3600)));
295 if ( $cache_enabled || $super_cache_enabled ) {
296 $buffer .= "<!-- Cached page generated by WP-Super-Cache on $timestamp -->\n";
297 } else {
298 $buffer .= "<!-- Live page served on $timestamp -->\n";
299 }
300 }
301
302 function wp_cache_get_ob(&$buffer) {
303 global $wpsc_settings;
304 global $cache_enabled, $cache_path, $cache_filename, $meta_file, $wp_start_time, $supercachedir;
305 global $new_cache, $wp_cache_meta, $file_expired, $blog_id, $cache_compression;
306 global $wp_cache_gzip_encoding, $super_cache_enabled, $cached_direct_pages;
307 global $wp_cache_404, $gzsize, $supercacheonly;
308 global $blog_cache_dir, $wp_cache_request_uri, $wp_supercache_cache_list;
309 global $wp_cache_not_logged_in, $wp_cache_object_cache, $cache_max_time;
310
311 $new_cache = true;
312 $wp_cache_meta = '';
313
314 /* Mode paranoic, check for closing tags
315 * we avoid caching incomplete files */
316 if ( $buffer == '' ) {
317 $new_cache = false;
318 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
319 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 );
320 $buffer .= "\n<!-- Page not cached by WP Super Cache. Blank Page. Check output buffer usage by plugins. -->\n";
321 }
322 }
323
324 if ( $wp_cache_404 && false == apply_filters( 'wpsupercache_404', false ) ) {
325 $new_cache = false;
326 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
327 wp_cache_debug( "404 file not found not cached", 2 );
328 $buffer .= "\n<!-- Page not cached by WP Super Cache. 404. -->\n";
329 }
330 }
331
332 if (!preg_match('/(<\/html>|<\/rss>|<\/feed>|<\/urlset)/i',$buffer) ) {
333 $new_cache = false;
334 if( false === strpos( $_SERVER[ 'REQUEST_URI' ], 'robots.txt' ) ) {
335 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
336 wp_cache_debug( "No closing html tag. Not caching.", 2 );
337 $buffer .= "\n<!-- Page not cached by WP Super Cache. No closing HTML tag. Check your theme. -->\n";
338 }
339 } else {
340 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "robots.txt detected. Not caching.", 2 );
341 }
342 }
343
344 if( !$new_cache )
345 return $buffer;
346
347 $duration = wp_cache_microtime_diff($wp_start_time, microtime());
348 $duration = sprintf("%0.3f", $duration);
349 $buffer .= "\n<!-- Dynamic page generated in $duration seconds. -->\n";
350
351 if( !wp_cache_writers_entry() ) {
352 $buffer .= "\n<!-- Page not cached by WP Super Cache. Could not get mutex lock. -->\n";
353 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Could not get mutex lock. Not caching.", 1 );
354 return $buffer;
355 }
356
357 $dir = get_current_url_supercache_dir();
358 $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"]);
359 if( !empty( $_GET ) || is_feed() || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) ) {
360 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Supercache disabled: GET or feed detected or disabled by config.", 2 );
361 $super_cache_enabled = false;
362 }
363
364 $tmp_wpcache_filename = $cache_path . uniqid( mt_rand(), true ) . '.tmp';
365
366 $supercacheonly = false;
367 if( $super_cache_enabled ) {
368 if ( wp_cache_get_cookies_values() == '' ) {
369 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Anonymous user detected. Only creating Supercache file.", 3 );
370 $supercacheonly = true;
371 }
372 }
373
374 if ( $wp_cache_not_logged_in && wp_cache_get_cookies_values() != '' ) {
375 $super_cache_enabled = false;
376 $cache_enabled = false;
377 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching for known user.', 5 );
378 }
379
380 if ( $wp_cache_object_cache ) { // half on mode when using the object cache
381 if ( wp_cache_get_cookies_values() != '' ) {
382 $cache_enabled = false;
383 }
384 $super_cache_enabled = false;
385 $supercacheonly = false;
386 wp_cache_init(); // PHP5 destroys objects during shutdown
387 }
388
389 if ( $cache_enabled ) {
390 // Open wp-cache cache file
391 if ( !$supercacheonly && ( !@file_exists( $blog_cache_dir . $cache_filename ) || ( @file_exists( $blog_cache_dir . $cache_filename ) && ( time() - @filemtime( $blog_cache_dir . $cache_filename ) ) > 5 ) ) ) {
392 if ( false == $wp_cache_object_cache ) {
393 $fr = @fopen($tmp_wpcache_filename, 'w');
394 if (!$fr) {
395 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename, 1 );
396 $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename . " -->\n";
397 wp_cache_writers_exit();
398 return $buffer;
399 }
400 }
401 }
402 $user_info = wp_cache_get_cookies_values();
403 $do_cache = apply_filters( 'do_createsupercache', $user_info );
404 if ( $super_cache_enabled && ( $user_info == '' || $do_cache === true ) ) {
405
406 if( @is_dir( $dir ) == false )
407 @wp_mkdir_p( $dir );
408
409 $cache_fname = "{$dir}index.html";
410 $tmp_cache_filename = $dir . uniqid( mt_rand(), true ) . '.tmp';
411 if ( !@file_exists( $cache_fname ) || ( @file_exists( $cache_fname ) && ( time() - @filemtime( $cache_fname ) ) > 5 ) ) {
412 $fr2 = @fopen( $tmp_cache_filename, 'w' );
413 if (!$fr2) {
414 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $tmp_cache_filename ), 1 );
415 $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . " -->\n";
416 @fclose( $fr );
417 @unlink( $tmp_wpcache_filename );
418 wp_cache_writers_exit();
419 return $buffer;
420 } elseif ( $cache_compression ) {
421 $gz = @fopen( $tmp_cache_filename . ".gz", 'w');
422 if (!$gz) {
423 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Error. Supercache could not write to " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz", 1 );
424 $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz -->\n";
425 @fclose( $fr );
426 @unlink( $tmp_wpcache_filename );
427 @fclose( $fr2 );
428 @unlink( $tmp_cache_filename );
429 wp_cache_writers_exit();
430 return $buffer;
431 }
432 }
433 }
434 }
435 }
436
437 $added_cache = 0;
438 $oc_key = get_oc_key();
439 if ( preg_match('/<!--mclude|<!--mfunc/', $buffer)) { //Dynamic content
440 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Dynamic content found in buffer.", 4 );
441 $store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is',
442 "<!--mclude-->\n<?php include_once('" . ABSPATH . "$1'); ?>\n<!--/mclude-->", $buffer);
443 $store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is',
444 "<!--mfunc-->\n<?php $1 ;?>\n<!--/mfunc-->", $store);
445 $wp_cache_meta[ 'dynamic' ] = true;
446 /* Clean function calls in tag */
447 $buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer);
448 $buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer);
449 $store = apply_filters( 'wpsupercache_buffer', $store );
450 // Append WP Super Cache or Live page comment tag
451 wp_cache_append_tag($buffer);
452 global $wp_super_cache_late_init;
453 if ( false == isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && $wp_super_cache_late_init == 0 ) )
454 $buffer .= '<!-- Super Cache dynamic page detected but $wp_super_cache_late_init not set. See the readme.txt for further details. -->';
455
456 if ( false == $wp_cache_object_cache ) {
457 if( $fr )
458 fputs($fr, $store);
459 } else {
460 wp_cache_set( $oc_key, $store, 'supercache', $cache_max_time );
461 }
462 } else {
463 $buffer = apply_filters( 'wpsupercache_buffer', $buffer );
464 // Append WP Super Cache or Live page comment tag
465 wp_cache_append_tag($buffer);
466 if( $gz || $wp_cache_gzip_encoding ) {
467 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Gzipping buffer.", 5 );
468 $gzdata = gzencode( $buffer . "<!-- Compression = gzip -->", 3, FORCE_GZIP );
469 $gzsize = strlen($gzdata);
470 }
471 if ($wp_cache_gzip_encoding) {
472 $wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
473 $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
474 // Return uncompressed data & store compressed for later use
475 if ( false == $wp_cache_object_cache ) {
476 if( $fr ) {
477 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzipped buffer to wp-cache cache file.", 5 );
478 fputs($fr, $gzdata);
479 }
480 } elseif ( $cache_enabled ) {
481 wp_cache_set( $oc_key . ".gz", $gzdata, 'supercache', $cache_max_time );
482 $added_cache = 1;
483 }
484 } else { // no compression
485 $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Cookie';
486 if ( false == $wp_cache_object_cache ) {
487 if( $fr ) {
488 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing non-gzipped buffer to wp-cache cache file.", 5 );
489 fputs($fr, $buffer);
490 }
491 } elseif ( $cache_enabled ) {
492 wp_cache_set( $oc_key, $buffer, 'supercache', $cache_max_time );
493 $added_cache = 1;
494 }
495 }
496 if ( false == $wp_cache_object_cache ) {
497 if( $fr2 ) {
498 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing non-gzipped buffer to supercache file.", 5 );
499 fputs($fr2, $buffer . '<!-- super cache -->' );
500 }
501 if( $gz ) {
502 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzipped buffer to supercache file.", 5 );
503 fwrite($gz, $gzdata );
504 }
505 }
506 }
507 $new_cache = true;
508 if ( false == $wp_cache_object_cache ) {
509 if( $fr ) {
510 $supercacheonly = false;
511 fclose($fr);
512 if ( filesize( $tmp_wpcache_filename ) == 0 ) {
513 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! The file $tmp_wpcache_filename was empty. Did not rename to {$blog_cache_dir}{$cache_filename}", 5 );
514 @unlink( $tmp_wpcache_filename );
515 } else {
516 if ( !rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename ) ) {
517 unlink( $blog_cache_dir . $cache_filename );
518 rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename );
519 }
520 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp wp-cache file to {$blog_cache_dir}$cache_filename", 5 );
521 $added_cache = 1;
522 }
523 }
524 if( $fr2 ) {
525 fclose($fr2);
526 if ( filesize( $tmp_cache_filename ) == 0 ) {
527 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! The file $tmp_cache_filename was empty. Did not rename to {$cache_fname}", 5 );
528 @unlink( $tmp_cache_filename );
529 } else {
530 if ( !@rename( $tmp_cache_filename, $cache_fname ) ) {
531 @unlink( $cache_fname );
532 @rename( $tmp_cache_filename, $cache_fname );
533 }
534 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp supercache file to $cache_fname", 5 );
535 $added_cache = 1;
536 }
537 }
538 if( $gz ) {
539 fclose($gz);
540 if ( filesize( $tmp_cache_filename . '.gz' ) == 0 ) {
541 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! The file {$tmp_cache_filename}.gz was empty. Did not rename to {$cache_fname}.gz", 5 );
542 @unlink( $tmp_cache_filename . '.gz' );
543 } else {
544 if ( !@rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' ) ) {
545 @unlink( $cache_fname . '.gz' );
546 @rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' );
547 }
548 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp supercache gz file to {$cache_fname}.gz", 5 );
549 $added_cache = 1;
550 }
551 }
552 }
553 if ( $added_cache && isset( $wp_supercache_cache_list ) && $wp_supercache_cache_list ) {
554 update_option( 'wpsupercache_count', ( get_option( 'wpsupercache_count' ) + 1 ) );
555 $last_urls = (array)get_option( 'supercache_last_cached' );
556 if ( count( $last_urls ) >= 10 )
557 $last_urls = array_slice( $last_urls, 1, 9 );
558 $last_urls[] = array( 'url' => $_SERVER[ 'REQUEST_URI' ], 'date' => date( 'Y-m-d H:i:s' ) );
559 update_option( 'supercache_last_cached', $last_urls );
560 }
561 wp_cache_writers_exit();
562 if ( !headers_sent() && $wp_cache_gzip_encoding && $gzdata) {
563 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzip content headers. Sending buffer to browser", 5 );
564 header( 'Content-Encoding: ' . $wp_cache_gzip_encoding );
565 header( 'Vary: Accept-Encoding, Cookie' );
566 header( 'Content-Length: ' . $gzsize );
567 return $gzdata;
568 } else {
569 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending buffer to browser", 5 );
570 return $buffer;
571 }
572 }
573
574 function wp_cache_phase2_clean_cache($file_prefix) {
575 global $cache_path, $blog_cache_dir;
576
577 if( !wp_cache_writers_entry() )
578 return false;
579 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cleaning cache in $blog_cache_dir", 3 );
580 if ( ( $handle = @opendir( $blog_cache_dir ) ) ) {
581 while ( false !== ($file = @readdir($handle))) {
582 if ( preg_match("/^$file_prefix/", $file) )
583 @unlink( $blog_cache_dir . $file );
584 }
585 closedir($handle);
586 }
587 wp_cache_writers_exit();
588 }
589
590 function prune_super_cache( $directory, $force = false, $rename = false ) {
591 global $cache_max_time, $cache_path, $super_cache_enabled, $cache_rebuild_files, $blog_cache_dir;
592
593 if( !is_admin() && $super_cache_enabled == 0 )
594 return false;
595
596 if( !isset( $cache_max_time ) )
597 $cache_max_time = 3600;
598
599 $now = time();
600
601 $protected_directories = array( $cache_path . '.htaccess', $cache_path . $blog_cache_dir . 'meta', $cache_path . 'supercache' );
602
603 $oktodelete = false;
604 if (is_dir($directory)) {
605 if( $dh = @opendir( $directory ) ) {
606 $directory = trailingslashit( $directory );
607 while( ( $entry = @readdir( $dh ) ) !== false ) {
608 if ($entry == '.' || $entry == '..')
609 continue;
610 $entry = $directory . $entry;
611 prune_super_cache( $entry, $force, $rename );
612 // 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,
613 if( is_dir( $entry ) && !in_array( $entry, $protected_directories ) && ( $force || @filemtime( $entry ) + $cache_max_time <= $now ) ) {
614 // if the directory isn't empty can't delete it
615 if( $handle = @opendir( $entry ) ) {
616 $donotdelete = false;
617 while( !$donotdelete && ( $file = @readdir( $handle ) ) !== false ) {
618 if ($file == '.' || $file == '..')
619 continue;
620 $donotdelete = true;
621 }
622 closedir($handle);
623 }
624 if( $donotdelete )
625 continue;
626 if( !$rename ) {
627 @rmdir( $entry );
628 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "gc: deleted $entry", 2 );
629 }
630 }
631 }
632 closedir($dh);
633 }
634 } elseif( is_file($directory) && ($force || @filemtime( $directory ) + $cache_max_time <= $now ) ) {
635 $oktodelete = true;
636 if( in_array( $directory, $protected_directories ) )
637 $oktodelete = false;
638 if( $oktodelete && !$rename ) {
639 @unlink( $directory );
640 } elseif( $oktodelete && $rename ) {
641 wp_cache_rebuild_or_delete( $directory );
642 }
643 }
644 }
645
646 function wp_cache_rebuild_or_delete( $file ) {
647 global $cache_rebuild_files;
648 if( strpos( $file, '?' ) !== false )
649 $file = substr( $file, 0, strpos( $file, '?' ) );
650 if( $cache_rebuild_files && substr( $file, -14 ) != '.needs-rebuild' ) {
651 if( @rename($file, $file . '.needs-rebuild') ) {
652 @touch( $file . '.needs-rebuild' );
653 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "rebuild_or_gc: rename to {$file}.needs-rebuild", 2 );
654 } else {
655 @unlink( $file );
656 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "rebuild_or_gc: deleted $file", 2 );
657 }
658 } else {
659 @unlink( $file );
660 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "rebuild_or_gc: deleted $file", 2 );
661 }
662 }
663
664 function wp_cache_phase2_clean_expired( $file_prefix, $force = false ) {
665 global $cache_path, $cache_max_time, $blog_cache_dir, $wp_cache_preload_on;
666
667 clearstatcache();
668 if( !wp_cache_writers_entry() )
669 return false;
670 $now = time();
671 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cleaning expired cache files in $blog_cache_dir", 2 );
672 if ( ( $handle = @opendir( $blog_cache_dir ) ) ) {
673 while ( false !== ($file = readdir($handle))) {
674 if ( preg_match("/^$file_prefix/", $file) &&
675 (@filemtime( $blog_cache_dir . $file) + $cache_max_time) <= $now ) {
676 @unlink( $blog_cache_dir . $file );
677 @unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
678 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Deleting $blog_cache_dir{$file} (plus meta)", 5 );
679 continue;
680 }
681 if($file != '.' && $file != '..') {
682 if( is_dir( $blog_cache_dir . $file ) == false && (@filemtime($blog_cache_dir . $file) + $cache_max_time) <= $now ) {
683 if( substr( $file, -9 ) != '.htaccess' ) {
684 @unlink($blog_cache_dir . $file);
685 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Deleting $blog_cache_dir{$file}", 5 );
686 }
687 }
688 }
689 }
690 closedir($handle);
691 if ( false == $wp_cache_preload_on || true == $force ) {
692 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Doing GC on supercache dir: {$cache_path}supercache", 2 );
693 prune_super_cache( $cache_path . 'supercache' );
694 }
695 }
696
697 wp_cache_writers_exit();
698 return true;
699 }
700
701 function wp_cache_shutdown_callback() {
702 global $wpsc_settings;
703 global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache, $wp_cache_meta, $known_headers, $blog_id, $wp_cache_gzip_encoding, $gzsize, $cache_filename, $supercacheonly, $blog_cache_dir;
704 global $wp_cache_request_uri, $wp_cache_key, $wp_cache_object_cache, $cache_enabled;
705
706 $wp_cache_meta[ 'uri' ] = $_SERVER["SERVER_NAME"].preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $wp_cache_request_uri); // To avoid XSS attacks
707 $wp_cache_meta[ 'blog_id' ] = $blog_id;
708 $wp_cache_meta[ 'post' ] = wp_cache_post_id();
709 $wp_cache_meta[ 'key' ] = $wp_cache_key;
710 $wp_cache_meta = apply_filters( 'wp_cache_meta', $wp_cache_meta );
711
712 $response = wp_cache_get_response_headers();
713 foreach ($known_headers as $key) {
714 if(isset($response[$key])) {
715 $wp_cache_meta[ 'headers' ][ $key ] = "$key: " . $response[$key];
716 }
717 }
718 if (!isset( $response['Last-Modified'] )) {
719 $value = gmdate('D, d M Y H:i:s') . ' GMT';
720 /* Dont send this the first time */
721 /* @header('Last-Modified: ' . $value); */
722 $wp_cache_meta[ 'headers' ][ 'Last-Modified' ] = "Last-Modified: $value";
723 }
724 if ( !isset( $response[ 'Content-Type' ] ) && !isset( $response[ 'Content-type' ] ) ) {
725 // On some systems, headers set by PHP can't be fetched from
726 // the output buffer. This is a last ditch effort to set the
727 // correct Content-Type header for feeds, if we didn't see
728 // it in the response headers already. -- dougal
729 if (is_feed()) {
730 $type = get_query_var('feed');
731 $type = str_replace('/','',$type);
732 switch ($type) {
733 case 'atom':
734 $value = "application/atom+xml";
735 break;
736 case 'rdf':
737 $value = "application/rdf+xml";
738 break;
739 case 'rss':
740 case 'rss2':
741 default:
742 $value = "application/rss+xml";
743 }
744 } else { // not a feed
745 $value = get_option( 'html_type' );
746 if( $value == '' )
747 $value = 'text/html';
748 }
749 $value .= "; charset=\"" . $wpsc_settings[ 'wp_cache_blog_charset' ] . "\"";
750
751 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending 'Content-Type: $value' header.", 2 );
752 @header("Content-Type: $value");
753 $wp_cache_meta[ 'headers' ][ 'Content-Type' ] = "Content-Type: $value";
754 }
755
756 if ( ! $supercacheonly && $new_cache ) {
757 if( !isset( $wp_cache_meta[ 'dynamic' ] ) && $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $wp_cache_meta[ 'headers' ] ) ) {
758 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending gzip headers.", 2 );
759 $wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
760 $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
761 }
762
763 $serial = serialize($wp_cache_meta);
764 if( wp_cache_writers_entry() ) {
765 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing meta file: {$blog_cache_dir}meta/{$meta_file}", 2 );
766 if ( false == $wp_cache_object_cache ) {
767 $tmp_meta_filename = $blog_cache_dir . 'meta/' . uniqid( mt_rand(), true ) . '.tmp';
768 $fr = @fopen( $tmp_meta_filename, 'w');
769 if( !$fr )
770 @mkdir( $blog_cache_dir . 'meta' );
771 $fr = fopen( $tmp_meta_filename, 'w');
772 fputs($fr, $serial);
773 fclose($fr);
774 @chmod( $tmp_meta_filename, 0666 & ~umask());
775 if( !@rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file ) ) {
776 @unlink( $blog_cache_dir . 'meta/' . $meta_file );
777 @rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file );
778 }
779 } elseif ( $cache_enabled ) {
780 $oc_key = get_oc_key() . ".meta";
781 if ( gzip_accepted() )
782 $oc_key .= ".gz";
783 wp_cache_set( $oc_key, $serial, 'supercache', $cache_max_time );
784 }
785 wp_cache_writers_exit();
786 }
787 }
788 global $time_to_gc_cache;
789 if( isset( $time_to_gc_cache ) && $time_to_gc_cache == 1 ) {
790 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Executing wp_cache_gc action.", 3 );
791 do_action( 'wp_cache_gc' );
792 }
793 }
794
795 function wp_cache_no_postid($id) {
796 return wp_cache_post_change(wp_cache_post_id());
797 }
798
799 function wp_cache_get_postid_from_comment( $comment_id, $status = 'NA' ) {
800 global $super_cache_enabled, $wp_cache_request_uri;
801 $comment = get_comment($comment_id, ARRAY_A);
802 if ( $status != 'NA' ) {
803 $comment[ 'old_comment_approved' ] = $comment[ 'comment_approved' ];
804 $comment[ 'comment_approved' ] = $status;
805 }
806
807 if ( ( $status == 'trash' || $status == 'spam' ) && $comment[ 'comment_approved' ] != 1 ) {
808 // don't modify cache if moderated comments are trashed or spammed
809 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated comment deleted or spammed. Don't delete any cache files.", 4 );
810 return wp_cache_post_id();
811 }
812 $postid = $comment['comment_post_ID'];
813 // Do nothing if comment is not moderated
814 // http://ocaoimh.ie/2006/12/05/caching-wordpress-with-wp-cache-in-a-spam-filled-world
815 if ( !preg_match('/wp-admin\//', $wp_cache_request_uri) ) {
816 if ( $comment['comment_approved'] == 'delete' && ( isset( $comment[ 'old_comment_approved' ] ) && $comment[ 'old_comment_approved' ] == 0 ) ) { // do nothing if moderated comments are deleted
817 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated comment deleted. Don't delete any cache files.", 4 );
818 return $postid;
819 } elseif ( $comment['comment_approved'] == 'spam' ) {
820 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Spam comment. Don't delete any cache files.", 4 );
821 return $postid;
822 } elseif( $comment['comment_approved'] == '0' ) {
823 if ( $comment[ 'content_type' ] == '' ) {
824 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated comment. Don't delete supercache file until comment approved.", 4 );
825 $super_cache_enabled = 0; // don't remove the super cache static file until comment is approved
826 } else {
827 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated ping or trackback. Not deleting cache files..", 4 );
828 return $postid;
829 }
830 }
831 }
832 // We must check it up again due to WP bugs calling two different actions
833 // for delete, for example both wp_set_comment_status and delete_comment
834 // are called when deleting a comment
835 if ($postid > 0) {
836 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post $postid changed. Update cache.", 4 );
837 return wp_cache_post_change( $postid );
838 } elseif ( $_GET[ 'delete_all' ] != 'Empty Trash' && $_GET[ 'delete_all2' ] != 'Empty Spam' ) {
839 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Unknown post changed. Update cache.", 4 );
840 return wp_cache_post_change( wp_cache_post_id() );
841 }
842 }
843
844 /* Clear out the cache directory. */
845 function wp_cache_clear_cache() {
846 global $cache_path, $wp_cache_object_cache;
847 if ( $wp_cache_object_cache ) {
848 reset_oc_version();
849 } else {
850 prune_super_cache( $cache_path . 'supercache/', true );
851 prune_super_cache( $cache_path, true );
852 }
853 }
854
855 function wp_cache_post_edit($post_id) {
856 global $wp_cache_clear_on_post_edit, $cache_path, $blog_cache_dir;
857 if( $wp_cache_clear_on_post_edit ) {
858 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Clearing cache $blog_cache_dir and {$cache_path}supercache/ on post edit per config.", 2 );
859 if ( $wp_cache_object_cache ) {
860 reset_oc_version();
861 } else {
862 prune_super_cache( $blog_cache_dir, true );
863 prune_super_cache( $cache_path . 'supercache/', true );
864 }
865 } else {
866 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Clearing cache for post $post_id on post edit.", 2 );
867 wp_cache_post_change( $post_id );
868 }
869 }
870
871 function wp_cache_post_id_gc( $siteurl, $post_id, $all = 'all' ) {
872 global $cache_path, $wp_cache_object_cache;
873
874 if ( $wp_cache_object_cache )
875 reset_oc_version();
876
877 $post_id = intval( $post_id );
878 if( $post_id == 0 )
879 return;
880
881 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc post_id: $post_id " . post_permalink( $post_id ) . " clearing cache in $dir{$permalink}.", 4 );
882 $permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( $post_id ) ) );
883 $dir = $cache_path . 'supercache/' . $siteurl;
884 if ( $all == 'all' ) {
885 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc clearing cache in $dir{$permalink}.", 4 );
886 prune_super_cache( $dir . $permalink, true, true );
887 do_action( 'gc_cache', 'prune', $permalink );
888 @rmdir( $dir . $permalink );
889 } else {
890 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc clearing cached index.html(.gz) in $dir{$permalink}.", 4 );
891 prune_super_cache( $dir . $permalink . 'index.html', true, true );
892 prune_super_cache( $dir . $permalink . 'index.html.gz', true, true );
893 do_action( 'gc_cache', 'prune', $permalink );
894 }
895 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_id_gc clearing cache in {$dir}page/.", 4 );
896 prune_super_cache( $dir . 'page/', true );
897 do_action( 'gc_cache', 'prune', '/page/' );
898 }
899
900 function wp_cache_post_change( $post_id ) {
901 global $file_prefix, $cache_path, $blog_id, $super_cache_enabled, $blog_cache_dir, $blogcacheid, $wp_cache_refresh_single_only;
902 static $last_processed = -1;
903
904 if ($post_id == $last_processed) return $post_id;
905 $last_processed = $post_id;
906 $post = get_post( $post_id );
907 if( $post->post_status == 'draft' ) {
908 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_change: draft post, not deleting any cache files.", 4 );
909 return $post_id;
910 }
911
912 if( !wp_cache_writers_entry() )
913 return $post_id;
914
915 if ( isset( $wp_cache_refresh_single_only ) && $wp_cache_refresh_single_only && strpos( $_SERVER[ 'REQUEST_URI' ], 'wp-comments-post.php' ) ) {
916 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "wp_cache_post_change: comment detected. only deleting post page.", 4 );
917 $all = false;
918 } else {
919 $all = true;
920 }
921
922 if ( $wp_cache_object_cache )
923 reset_oc_version();
924
925 $permalink = trailingslashit( str_replace( get_option( 'siteurl' ), '', post_permalink( $post_id ) ) );
926 if( $super_cache_enabled ) {
927 $siteurl = trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', get_option( 'home' ) ) ) ) );
928 // make sure the front page has a rebuild file
929 if ( $all == true ) {
930 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting cache files in " . $cache_path . 'supercache/' . $siteurl, 4 );
931 prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html', true, true );
932 prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html.gz', true, true );
933 do_action( 'gc_cache', 'prune', 'homepage' );
934 }
935 wp_cache_post_id_gc( $siteurl, $post_id );
936 if( $all == true && get_option( 'show_on_front' ) == 'page' ) {
937 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting page_on_front and_page_for_posts pages.", 4 );
938 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: page_on_front " . get_option( 'page_on_front' ), 4 );
939 wp_cache_post_id_gc( $siteurl, get_option( 'page_on_front' ), 'single' );
940 $permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( get_option( 'page_for_posts' ) ) ) );
941 prune_super_cache( $cache_path . 'supercache/' . $siteurl . $permalink . 'index.html', true, true );
942 prune_super_cache( $cache_path . 'supercache/' . $siteurl . $permalink . 'index.html.gz', true, true );
943 do_action( 'gc_cache', 'prune', $permalink );
944 }
945 }
946
947 $matches = array();
948 if ( ($handle = @opendir( $blog_cache_dir . 'meta/' )) ) {
949 while ( false !== ($file = readdir($handle))) {
950 if ( preg_match("/^({$file_prefix}{$blogcacheid}.*)\.meta/", $file, $matches) ) {
951 $meta_pathname = $blog_cache_dir . 'meta/' . $file;
952 $content_pathname = $blog_cache_dir . $matches[1] . ".html";
953 $meta = unserialize(@file_get_contents($meta_pathname));
954 if( false == is_array( $meta ) ) {
955 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change cleaning up stray file: $content_pathname", 4 );
956 @unlink($meta_pathname);
957 @unlink($content_pathname);
958 continue;
959 }
960 if ($post_id > 0 && $meta) {
961 if ( $meta[ 'blog_id' ] == $blog_id && ( ( $all == true && !$meta[ 'post' ] ) || $meta[ 'post' ] == $post_id) ) {
962 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting post cache files for {$meta[ 'uri' ]}: $content_pathname", 4 );
963 @unlink($meta_pathname);
964 @unlink($content_pathname);
965 if ( $super_cache_enabled == true ) {
966 @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
967 @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
968 do_action( 'gc_cache', 'rebuild', trailingslashit( $meta[ 'uri' ] ) );
969 }
970 }
971 } elseif ($meta[ 'blog_id' ] == $blog_id) {
972 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change: deleting cache files for {$meta[ 'uri' ]}: $content_pathname", 4 );
973 @unlink($meta_pathname);
974 @unlink($content_pathname);
975 if ( $super_cache_enabled == true ) {
976 @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
977 @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
978 do_action( 'gc_cache', 'rebuild', trailingslashit( $meta[ 'uri' ] ) );
979 }
980 }
981
982 }
983 }
984 closedir($handle);
985 }
986 wp_cache_writers_exit();
987 return $post_id;
988 }
989
990 function wp_cache_microtime_diff($a, $b) {
991 list($a_dec, $a_sec) = explode(' ', $a);
992 list($b_dec, $b_sec) = explode(' ', $b);
993 return $b_sec - $a_sec + $b_dec - $a_dec;
994 }
995
996 function wp_cache_post_id() {
997 global $posts, $comment_post_ID, $post_ID;
998 // We try hard all options. More frequent first.
999 if ($post_ID > 0 ) return $post_ID;
1000 if ($comment_post_ID > 0 ) return $comment_post_ID;
1001 if (is_single() || is_page()) return $posts[0]->ID;
1002 if (isset( $_GET[ 'p' ] ) && $_GET['p'] > 0) return $_GET['p'];
1003 if (isset( $_POST[ 'p' ] ) && $_POST['p'] > 0) return $_POST['p'];
1004 return 0;
1005 }
1006
1007 function wp_cache_gc_cron() {
1008 global $file_prefix, $cache_max_time;
1009
1010 if ( $cache_max_time == 0 )
1011 return false;
1012
1013 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Cache garbage collection.', 5 );
1014
1015 if( !isset( $cache_max_time ) )
1016 $cache_max_time = 600;
1017
1018 $start = time();
1019 if( !wp_cache_phase2_clean_expired($file_prefix ) ) {
1020 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Cache Expiry cron job failed. Probably mutex locked.', 1 );
1021 update_option( 'wpsupercache_gc_time', time() - ( $cache_max_time - 10 ) ); // if GC failed then run it again in one minute
1022 }
1023 if( time() - $start > 30 )
1024 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) 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 );
1025 }
1026
1027 ?>
1028