PluginProbe ʕ •ᴥ•ʔ
WP Super Cache / 0.9.9.7
WP Super Cache v0.9.9.7
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 15 years ago plugins 15 years ago Changelog.txt 15 years ago advanced-cache.php 17 years ago ossdl-cdn.php 15 years ago readme.txt 15 years ago uninstall.php 15 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
1041 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, $wp_cache_gmt_offset, $wp_cache_blog_charset;
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 $wp_cache_gmt_offset = get_option( 'gmt_offset' ); // caching for later use when wpdb is gone. http://wordpress.org/support/topic/224349
15 $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 $mutex, $wp_cache_mutex_disabled, $use_flock, $blog_cache_dir, $mutex_filename, $sem_id;
185
186 if( isset( $wp_cache_mutex_disabled) && $wp_cache_mutex_disabled )
187 return true;
188
189 if( !is_bool( $use_flock ) ) {
190 if(function_exists('sem_get'))
191 $use_flock = false;
192 else
193 $use_flock = true;
194 }
195
196 $mutex = false;
197 if ($use_flock ) {
198 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Created mutex lock on filename: {$blog_cache_dir}{$mutex_filename}", 5 );
199 $mutex = @fopen( $blog_cache_dir . $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: {$sem_id}", 5 );
202 $mutex = @sem_get( $sem_id, 1, 0644 | IPC_CREAT, 1 );
203 }
204 }
205
206 function wp_cache_writers_entry() {
207 global $mutex, $wp_cache_mutex_disabled, $use_flock;
208
209 if( isset( $wp_cache_mutex_disabled ) && $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 ( $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 $mutex, $wp_cache_mutex_disabled, $use_flock;
230
231 if( isset( $wp_cache_mutex_disabled ) && $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 ( $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 $buffer = apply_filters( 'wp_cache_ob_callback_filter', $buffer );
251 if( defined( 'DONOTCACHEPAGE' ) ) {
252 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'DONOTCACHEPAGE defined. Caching disabled.', 2 );
253 return $buffer;
254 }
255
256 if ( isset( $wp_cache_pages[ 'single' ] ) && $wp_cache_pages[ 'single' ] == 1 && is_single() ) {
257 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching single post.', 2 );
258 return $buffer;
259 } elseif ( isset( $wp_cache_pages[ 'pages' ] ) && $wp_cache_pages[ 'pages' ] == 1 && is_page() ) {
260 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching single page.', 2 );
261 return $buffer;
262 } elseif ( isset( $wp_cache_pages[ 'archives' ] ) && $wp_cache_pages[ 'archives' ] == 1 && is_archive() ) {
263 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching archive page.', 2 );
264 return $buffer;
265 } elseif ( isset( $wp_cache_pages[ 'tag' ] ) && $wp_cache_pages[ 'tag' ] == 1 && is_tag() ) {
266 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching tag page.', 2 );
267 return $buffer;
268 } elseif ( isset( $wp_cache_pages[ 'category' ] ) && $wp_cache_pages[ 'category' ] == 1 && is_category() ) {
269 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching category page.', 2 );
270 return $buffer;
271 } elseif ( isset( $wp_cache_pages[ 'frontpage' ] ) && $wp_cache_pages[ 'frontpage' ] == 1 && is_front_page() ) {
272 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching front page.', 2 );
273 return $buffer;
274 } elseif ( isset( $wp_cache_pages[ 'home' ] ) && $wp_cache_pages[ 'home' ] == 1 && is_home() ) {
275 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching home page.', 2 );
276 return $buffer;
277 } elseif ( isset( $wp_cache_pages[ 'search' ] ) && $wp_cache_pages[ 'search' ] == 1 && is_search() ) {
278 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching search page.', 2 );
279 return $buffer;
280 } elseif ( isset( $wp_cache_pages[ 'feed' ] ) && $wp_cache_pages[ 'feed' ] == 1 && is_feed() ) {
281 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching feed.', 2 );
282 return $buffer;
283 }
284
285 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Output buffer callback', 4 );
286
287 $buffer = &wp_cache_get_ob( $buffer );
288 wp_cache_shutdown_callback();
289 return $buffer;
290 }
291
292 function wp_cache_append_tag( &$buffer ) {
293 global $wp_cache_gmt_offset;
294 global $cache_enabled, $super_cache_enabled;
295 $timestamp = gmdate('Y-m-d H:i:s', (time() + ( $wp_cache_gmt_offset * 3600)));
296 if ( $cache_enabled || $super_cache_enabled ) {
297 $buffer .= "<!-- Cached page generated by WP-Super-Cache on $timestamp -->\n";
298 } else {
299 $buffer .= "<!-- Live page served on $timestamp -->\n";
300 }
301 }
302
303 function wp_cache_get_ob(&$buffer) {
304 global $wpsc_settings;
305 global $cache_enabled, $cache_path, $cache_filename, $meta_file, $wp_start_time, $supercachedir;
306 global $new_cache, $wp_cache_meta, $file_expired, $blog_id, $cache_compression;
307 global $wp_cache_gzip_encoding, $super_cache_enabled, $cached_direct_pages;
308 global $wp_cache_404, $gzsize, $supercacheonly;
309 global $blog_cache_dir, $wp_cache_request_uri, $wp_supercache_cache_list;
310 global $wp_cache_not_logged_in, $wp_cache_object_cache, $cache_max_time;
311
312 $new_cache = true;
313 $wp_cache_meta = '';
314
315 /* Mode paranoic, check for closing tags
316 * we avoid caching incomplete files */
317 if ( $buffer == '' ) {
318 $new_cache = false;
319 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
320 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 );
321 $buffer .= "\n<!-- Page not cached by WP Super Cache. Blank Page. Check output buffer usage by plugins. -->\n";
322 }
323 }
324
325 if ( $wp_cache_404 && false == apply_filters( 'wpsupercache_404', false ) ) {
326 $new_cache = false;
327 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
328 wp_cache_debug( "404 file not found not cached", 2 );
329 $buffer .= "\n<!-- Page not cached by WP Super Cache. 404. -->\n";
330 }
331 }
332
333 if (!preg_match('/(<\/html>|<\/rss>|<\/feed>|<\/urlset)/i',$buffer) ) {
334 $new_cache = false;
335 if( false === strpos( $_SERVER[ 'REQUEST_URI' ], 'robots.txt' ) ) {
336 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) {
337 wp_cache_debug( "No closing html tag. Not caching.", 2 );
338 $buffer .= "\n<!-- Page not cached by WP Super Cache. No closing HTML tag. Check your theme. -->\n";
339 }
340 } else {
341 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "robots.txt detected. Not caching.", 2 );
342 }
343 }
344
345 if( !$new_cache )
346 return $buffer;
347
348 $duration = wp_cache_microtime_diff($wp_start_time, microtime());
349 $duration = sprintf("%0.3f", $duration);
350 $buffer .= "\n<!-- Dynamic page generated in $duration seconds. -->\n";
351
352 if( !wp_cache_writers_entry() ) {
353 $buffer .= "\n<!-- Page not cached by WP Super Cache. Could not get mutex lock. -->\n";
354 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Could not get mutex lock. Not caching.", 1 );
355 return $buffer;
356 }
357
358 $home_url = parse_url( trailingslashit( get_bloginfo( 'url' ) ) );
359
360 $dir = get_current_url_supercache_dir();
361 $supercachedir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $home_url[ 'host' ]);
362 if( !empty( $_GET ) || is_feed() || ( $super_cache_enabled == true && is_dir( substr( $supercachedir, 0, -1 ) . '.disabled' ) ) ) {
363 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 );
364 $super_cache_enabled = false;
365 }
366
367 $tmp_wpcache_filename = $cache_path . uniqid( mt_rand(), true ) . '.tmp';
368
369 $supercacheonly = false;
370 if( $super_cache_enabled ) {
371 if ( wp_cache_get_cookies_values() == '' ) {
372 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Anonymous user detected. Only creating Supercache file.", 3 );
373 $supercacheonly = true;
374 }
375 }
376
377 if ( $wp_cache_not_logged_in && wp_cache_get_cookies_values() != '' ) {
378 $super_cache_enabled = false;
379 $cache_enabled = false;
380 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Not caching for known user.', 5 );
381 }
382
383 if ( $wp_cache_object_cache ) { // half on mode when using the object cache
384 if ( wp_cache_get_cookies_values() != '' ) {
385 $cache_enabled = false;
386 }
387 $super_cache_enabled = false;
388 $supercacheonly = false;
389 wp_cache_init(); // PHP5 destroys objects during shutdown
390 }
391
392 $fr = $fr2 = $gz = false;
393 if ( $cache_enabled ) {
394 // Open wp-cache cache file
395 if ( !$supercacheonly && ( !@file_exists( $blog_cache_dir . $cache_filename ) || ( @file_exists( $blog_cache_dir . $cache_filename ) && ( time() - @filemtime( $blog_cache_dir . $cache_filename ) ) > 5 ) ) ) {
396 if ( false == $wp_cache_object_cache ) {
397 $fr = @fopen($tmp_wpcache_filename, 'w');
398 if (!$fr) {
399 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 );
400 $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $cache_path ) . $cache_filename . " -->\n";
401 wp_cache_writers_exit();
402 return $buffer;
403 }
404 }
405 } else {
406 $user_info = wp_cache_get_cookies_values();
407 $do_cache = apply_filters( 'do_createsupercache', $user_info );
408 if ( $super_cache_enabled && ( $user_info == '' || $do_cache === true ) ) {
409
410 if( @is_dir( $dir ) == false )
411 @wp_mkdir_p( $dir );
412
413 $cache_fname = "{$dir}index.html";
414 $tmp_cache_filename = $dir . uniqid( mt_rand(), true ) . '.tmp';
415 if ( !@file_exists( $cache_fname ) || ( @file_exists( $cache_fname ) && ( time() - @filemtime( $cache_fname ) ) > 5 ) ) {
416 $fr2 = @fopen( $tmp_cache_filename, 'w' );
417 if (!$fr2) {
418 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 );
419 $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . " -->\n";
420 @fclose( $fr );
421 @unlink( $tmp_wpcache_filename );
422 wp_cache_writers_exit();
423 return $buffer;
424 } elseif ( $cache_compression ) {
425 $gz = @fopen( $tmp_cache_filename . ".gz", 'w');
426 if (!$gz) {
427 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 );
428 $buffer .= "<!-- File not cached! Super Cache Couldn't write to: " . str_replace( ABSPATH, '', $tmp_cache_filename ) . ".gz -->\n";
429 @fclose( $fr );
430 @unlink( $tmp_wpcache_filename );
431 @fclose( $fr2 );
432 @unlink( $tmp_cache_filename );
433 wp_cache_writers_exit();
434 return $buffer;
435 }
436 }
437 }
438 }
439 }
440 }
441
442 $added_cache = 0;
443 $oc_key = get_oc_key();
444 if ( preg_match( '/<!--mclude|<!--mfunc|<!--dynamic-cached-content-->/', $buffer ) ) { //Dynamic content
445 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Dynamic content found in buffer.", 4 );
446 $store = preg_replace('|<!--mclude (.*?)-->(.*?)<!--/mclude-->|is',
447 "<!--mclude-->\n<?php include_once('" . ABSPATH . "$1'); ?>\n<!--/mclude-->", $buffer);
448 $store = preg_replace('|<!--mfunc (.*?)-->(.*?)<!--/mfunc-->|is',
449 "<!--mfunc-->\n<?php $1 ;?>\n<!--/mfunc-->", $store);
450 $store = preg_replace('|<!--dynamic-cached-content-->(.*?)<!--(.*?)--><!--/dynamic-cached-content-->|is',
451 "<!--dynamic-cached-content-->\n<?php$2?>\n<!--/dynamic-cached-content-->", $store);
452 $wp_cache_meta[ 'dynamic' ] = true;
453 /* Clean function calls in tag */
454 $buffer = preg_replace('|<!--mclude (.*?)-->|is', '<!--mclude-->', $buffer);
455 $buffer = preg_replace('|<!--mfunc (.*?)-->|is', '<!--mfunc-->', $buffer);
456 $buffer = preg_replace('|<!--dynamic-cached-content-->(.*?)<!--(.*?)--><!--/dynamic-cached-content-->|is',
457 "<!--dynamic-cached-content-->$1<!--/dynamic-cached-content-->", $buffer);
458 $store = apply_filters( 'wpsupercache_buffer', $store );
459 // Append WP Super Cache or Live page comment tag
460 wp_cache_append_tag($buffer);
461 wp_cache_append_tag($store);
462 global $wp_super_cache_late_init;
463 if ( false == isset( $wp_super_cache_late_init ) || ( isset( $wp_super_cache_late_init ) && $wp_super_cache_late_init == 0 ) )
464 $buffer .= '<!-- Super Cache dynamic page detected but $wp_super_cache_late_init not set. See the readme.txt for further details. -->';
465
466 if ( false == $wp_cache_object_cache ) {
467 if( $fr )
468 fputs($fr, $store);
469 } else {
470 wp_cache_set( $oc_key, $store, 'supercache', $cache_max_time );
471 }
472 } else {
473 $buffer = apply_filters( 'wpsupercache_buffer', $buffer );
474 // Append WP Super Cache or Live page comment tag
475 wp_cache_append_tag($buffer);
476 if( $gz || $wp_cache_gzip_encoding ) {
477 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Gzipping buffer.", 5 );
478 $gzdata = gzencode( $buffer . "<!-- Compression = gzip -->", 6, FORCE_GZIP );
479 $gzsize = strlen($gzdata);
480 }
481 if ($wp_cache_gzip_encoding) {
482 $wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
483 $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
484 // Return uncompressed data & store compressed for later use
485 if ( false == $wp_cache_object_cache ) {
486 if( $fr ) {
487 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzipped buffer to wp-cache cache file.", 5 );
488 fputs($fr, $gzdata);
489 }
490 } elseif ( $cache_enabled ) {
491 wp_cache_set( $oc_key . ".gz", $gzdata, 'supercache', $cache_max_time );
492 $added_cache = 1;
493 }
494 } else { // no compression
495 $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Cookie';
496 if ( false == $wp_cache_object_cache ) {
497 if( $fr ) {
498 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 );
499 fputs($fr, $buffer);
500 }
501 } elseif ( $cache_enabled ) {
502 wp_cache_set( $oc_key, $buffer, 'supercache', $cache_max_time );
503 $added_cache = 1;
504 }
505 }
506 if ( false == $wp_cache_object_cache ) {
507 if( $fr2 ) {
508 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing non-gzipped buffer to supercache file.", 5 );
509 fputs($fr2, $buffer . '<!-- super cache -->' );
510 }
511 if( $gz ) {
512 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzipped buffer to supercache file.", 5 );
513 fwrite($gz, $gzdata );
514 }
515 }
516 }
517 $new_cache = true;
518 if ( false == $wp_cache_object_cache ) {
519 if( $fr ) {
520 $supercacheonly = false;
521 fclose($fr);
522 if ( filesize( $tmp_wpcache_filename ) == 0 ) {
523 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 );
524 @unlink( $tmp_wpcache_filename );
525 } else {
526 if ( !rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename ) ) {
527 unlink( $blog_cache_dir . $cache_filename );
528 rename( $tmp_wpcache_filename, $blog_cache_dir . $cache_filename );
529 }
530 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 );
531 $added_cache = 1;
532 }
533 }
534 if( $fr2 ) {
535 fclose($fr2);
536 if ( $cache_fname == $supercachedir . $home_url[ 'path' ] . 'index.html' && !( is_front_page() || is_home() ) ) {
537 wp_cache_writers_exit();
538 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Warning! Not writing another page to front page cache.", 1 );
539 return $buffer;
540 } elseif ( filesize( $tmp_cache_filename ) == 0 ) {
541 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 );
542 @unlink( $tmp_cache_filename );
543 } else {
544 if ( !@rename( $tmp_cache_filename, $cache_fname ) ) {
545 @unlink( $cache_fname );
546 @rename( $tmp_cache_filename, $cache_fname );
547 }
548 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Renamed temp supercache file to $cache_fname", 5 );
549 $added_cache = 1;
550 }
551 }
552 if( $gz ) {
553 fclose($gz);
554 if ( filesize( $tmp_cache_filename . '.gz' ) == 0 ) {
555 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 );
556 @unlink( $tmp_cache_filename . '.gz' );
557 } else {
558 if ( !@rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' ) ) {
559 @unlink( $cache_fname . '.gz' );
560 @rename( $tmp_cache_filename . '.gz', $cache_fname . '.gz' );
561 }
562 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 );
563 $added_cache = 1;
564 }
565 }
566 }
567 if ( $added_cache && isset( $wp_supercache_cache_list ) && $wp_supercache_cache_list ) {
568 update_option( 'wpsupercache_count', ( get_option( 'wpsupercache_count' ) + 1 ) );
569 $last_urls = (array)get_option( 'supercache_last_cached' );
570 if ( count( $last_urls ) >= 10 )
571 $last_urls = array_slice( $last_urls, 1, 9 );
572 $last_urls[] = array( 'url' => $_SERVER[ 'REQUEST_URI' ], 'date' => date( 'Y-m-d H:i:s' ) );
573 update_option( 'supercache_last_cached', $last_urls );
574 }
575 wp_cache_writers_exit();
576 if ( !headers_sent() && $wp_cache_gzip_encoding && $gzdata) {
577 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Writing gzip content headers. Sending buffer to browser", 5 );
578 header( 'Content-Encoding: ' . $wp_cache_gzip_encoding );
579 header( 'Vary: Accept-Encoding, Cookie' );
580 header( 'Content-Length: ' . $gzsize );
581 return $gzdata;
582 } else {
583 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending buffer to browser", 5 );
584 return $buffer;
585 }
586 }
587
588 function wp_cache_phase2_clean_cache($file_prefix) {
589 global $cache_path, $blog_cache_dir;
590
591 if( !wp_cache_writers_entry() )
592 return false;
593 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cleaning cache in $blog_cache_dir", 3 );
594 if ( ( $handle = @opendir( $blog_cache_dir ) ) ) {
595 while ( false !== ($file = @readdir($handle))) {
596 if ( preg_match("/^$file_prefix/", $file) )
597 @unlink( $blog_cache_dir . $file );
598 }
599 closedir($handle);
600 }
601 wp_cache_writers_exit();
602 }
603
604 function prune_super_cache( $directory, $force = false, $rename = false ) {
605 global $cache_max_time, $cache_path, $super_cache_enabled, $cache_rebuild_files, $blog_cache_dir;
606
607 if( !is_admin() && $super_cache_enabled == 0 )
608 return false;
609
610 if( !isset( $cache_max_time ) )
611 $cache_max_time = 3600;
612
613 $now = time();
614
615 $protected_directories = array( $cache_path . '.htaccess', $cache_path . $blog_cache_dir . 'meta', $cache_path . 'supercache' );
616
617 $oktodelete = false;
618 if (is_dir($directory)) {
619 if( $dh = @opendir( $directory ) ) {
620 $directory = trailingslashit( $directory );
621 while( ( $entry = @readdir( $dh ) ) !== false ) {
622 if ($entry == '.' || $entry == '..')
623 continue;
624 $entry = $directory . $entry;
625 prune_super_cache( $entry, $force, $rename );
626 // 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,
627 if( is_dir( $entry ) && !in_array( $entry, $protected_directories ) && ( $force || @filemtime( $entry ) + $cache_max_time <= $now ) ) {
628 // if the directory isn't empty can't delete it
629 if( $handle = @opendir( $entry ) ) {
630 $donotdelete = false;
631 while( !$donotdelete && ( $file = @readdir( $handle ) ) !== false ) {
632 if ($file == '.' || $file == '..')
633 continue;
634 $donotdelete = true;
635 }
636 closedir($handle);
637 }
638 if( $donotdelete )
639 continue;
640 if( !$rename ) {
641 @rmdir( $entry );
642 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "gc: deleted $entry", 2 );
643 }
644 }
645 }
646 closedir($dh);
647 }
648 } elseif( is_file($directory) && ($force || @filemtime( $directory ) + $cache_max_time <= $now ) ) {
649 $oktodelete = true;
650 if( in_array( $directory, $protected_directories ) )
651 $oktodelete = false;
652 if( $oktodelete && !$rename ) {
653 @unlink( $directory );
654 } elseif( $oktodelete && $rename ) {
655 wp_cache_rebuild_or_delete( $directory );
656 }
657 }
658 }
659
660 function wp_cache_rebuild_or_delete( $file ) {
661 global $cache_rebuild_files;
662 if( strpos( $file, '?' ) !== false )
663 $file = substr( $file, 0, strpos( $file, '?' ) );
664 if( $cache_rebuild_files && substr( $file, -14 ) != '.needs-rebuild' ) {
665 if( @rename($file, $file . '.needs-rebuild') ) {
666 @touch( $file . '.needs-rebuild' );
667 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "rebuild_or_gc: rename to {$file}.needs-rebuild", 2 );
668 } else {
669 @unlink( $file );
670 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "rebuild_or_gc: deleted $file", 2 );
671 }
672 } else {
673 @unlink( $file );
674 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "rebuild_or_gc: deleted $file", 2 );
675 }
676 }
677
678 function wp_cache_phase2_clean_expired( $file_prefix, $force = false ) {
679 global $cache_path, $cache_max_time, $blog_cache_dir, $wp_cache_preload_on;
680
681 clearstatcache();
682 if( !wp_cache_writers_entry() )
683 return false;
684 $now = time();
685 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Cleaning expired cache files in $blog_cache_dir", 2 );
686 if ( ( $handle = @opendir( $blog_cache_dir ) ) ) {
687 while ( false !== ($file = readdir($handle))) {
688 if ( preg_match("/^$file_prefix/", $file) &&
689 (@filemtime( $blog_cache_dir . $file) + $cache_max_time) <= $now ) {
690 @unlink( $blog_cache_dir . $file );
691 @unlink( $blog_cache_dir . 'meta/' . str_replace( '.html', '.meta', $file ) );
692 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Deleting $blog_cache_dir{$file} (plus meta)", 5 );
693 continue;
694 }
695 if($file != '.' && $file != '..') {
696 if( is_dir( $blog_cache_dir . $file ) == false && (@filemtime($blog_cache_dir . $file) + $cache_max_time) <= $now ) {
697 if( substr( $file, -9 ) != '.htaccess' ) {
698 @unlink($blog_cache_dir . $file);
699 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Deleting $blog_cache_dir{$file}", 5 );
700 }
701 }
702 }
703 }
704 closedir($handle);
705 if ( false == $wp_cache_preload_on || true == $force ) {
706 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Doing GC on supercache dir: {$cache_path}supercache", 2 );
707 prune_super_cache( $cache_path . 'supercache' );
708 }
709 }
710
711 wp_cache_writers_exit();
712 return true;
713 }
714
715 function wp_cache_shutdown_callback() {
716 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;
717 global $wp_cache_request_uri, $wp_cache_key, $wp_cache_object_cache, $cache_enabled, $wp_cache_blog_charset;
718
719 $wp_cache_meta[ 'uri' ] = $_SERVER["SERVER_NAME"].preg_replace('/[ <>\'\"\r\n\t\(\)]/', '', $wp_cache_request_uri); // To avoid XSS attacks
720 $wp_cache_meta[ 'blog_id' ] = $blog_id;
721 $wp_cache_meta[ 'post' ] = wp_cache_post_id();
722 $wp_cache_meta[ 'key' ] = $wp_cache_key;
723 $wp_cache_meta = apply_filters( 'wp_cache_meta', $wp_cache_meta );
724
725 $response = wp_cache_get_response_headers();
726 foreach ($known_headers as $key) {
727 if(isset($response[$key])) {
728 $wp_cache_meta[ 'headers' ][ $key ] = "$key: " . $response[$key];
729 }
730 }
731 if (!isset( $response['Last-Modified'] )) {
732 $value = gmdate('D, d M Y H:i:s') . ' GMT';
733 /* Dont send this the first time */
734 /* @header('Last-Modified: ' . $value); */
735 $wp_cache_meta[ 'headers' ][ 'Last-Modified' ] = "Last-Modified: $value";
736 }
737 if ( !isset( $response[ 'Content-Type' ] ) && !isset( $response[ 'Content-type' ] ) ) {
738 // On some systems, headers set by PHP can't be fetched from
739 // the output buffer. This is a last ditch effort to set the
740 // correct Content-Type header for feeds, if we didn't see
741 // it in the response headers already. -- dougal
742 if (is_feed()) {
743 $type = get_query_var('feed');
744 $type = str_replace('/','',$type);
745 switch ($type) {
746 case 'atom':
747 $value = "application/atom+xml";
748 break;
749 case 'rdf':
750 $value = "application/rdf+xml";
751 break;
752 case 'rss':
753 case 'rss2':
754 default:
755 $value = "application/rss+xml";
756 }
757 } else { // not a feed
758 $value = get_option( 'html_type' );
759 if( $value == '' )
760 $value = 'text/html';
761 }
762 $value .= "; charset=\"" . $wp_cache_blog_charset . "\"";
763
764 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending 'Content-Type: $value' header.", 2 );
765 @header("Content-Type: $value");
766 $wp_cache_meta[ 'headers' ][ 'Content-Type' ] = "Content-Type: $value";
767 }
768
769 if ( ! $supercacheonly && $new_cache ) {
770 if( !isset( $wp_cache_meta[ 'dynamic' ] ) && $wp_cache_gzip_encoding && !in_array( 'Content-Encoding: ' . $wp_cache_gzip_encoding, $wp_cache_meta[ 'headers' ] ) ) {
771 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Sending gzip headers.", 2 );
772 $wp_cache_meta[ 'headers' ][ 'Content-Encoding' ] = 'Content-Encoding: ' . $wp_cache_gzip_encoding;
773 $wp_cache_meta[ 'headers' ][ 'Vary' ] = 'Vary: Accept-Encoding, Cookie';
774 }
775
776 $serial = serialize($wp_cache_meta);
777 if( wp_cache_writers_entry() ) {
778 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 );
779 if ( false == $wp_cache_object_cache ) {
780 $tmp_meta_filename = $blog_cache_dir . 'meta/' . uniqid( mt_rand(), true ) . '.tmp';
781 $fr = @fopen( $tmp_meta_filename, 'w');
782 if( !$fr )
783 @mkdir( $blog_cache_dir . 'meta' );
784 $fr = fopen( $tmp_meta_filename, 'w');
785 fputs($fr, $serial);
786 fclose($fr);
787 @chmod( $tmp_meta_filename, 0666 & ~umask());
788 if( !@rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file ) ) {
789 @unlink( $blog_cache_dir . 'meta/' . $meta_file );
790 @rename( $tmp_meta_filename, $blog_cache_dir . 'meta/' . $meta_file );
791 }
792 } elseif ( $cache_enabled ) {
793 $oc_key = get_oc_key() . ".meta";
794 if ( gzip_accepted() )
795 $oc_key .= ".gz";
796 wp_cache_set( $oc_key, $serial, 'supercache', $cache_max_time );
797 }
798 wp_cache_writers_exit();
799 }
800 }
801 global $time_to_gc_cache;
802 if( isset( $time_to_gc_cache ) && $time_to_gc_cache == 1 ) {
803 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Executing wp_cache_gc action.", 3 );
804 do_action( 'wp_cache_gc' );
805 }
806 }
807
808 function wp_cache_no_postid($id) {
809 return wp_cache_post_change(wp_cache_post_id());
810 }
811
812 function wp_cache_get_postid_from_comment( $comment_id, $status = 'NA' ) {
813 global $super_cache_enabled, $wp_cache_request_uri;
814 $comment = get_comment($comment_id, ARRAY_A);
815 if ( $status != 'NA' ) {
816 $comment[ 'old_comment_approved' ] = $comment[ 'comment_approved' ];
817 $comment[ 'comment_approved' ] = $status;
818 }
819
820 if ( ( $status == 'trash' || $status == 'spam' ) && $comment[ 'comment_approved' ] != 1 ) {
821 // don't modify cache if moderated comments are trashed or spammed
822 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 );
823 return wp_cache_post_id();
824 }
825 $postid = $comment['comment_post_ID'];
826 // Do nothing if comment is not moderated
827 // http://ocaoimh.ie/2006/12/05/caching-wordpress-with-wp-cache-in-a-spam-filled-world
828 if ( !preg_match('/wp-admin\//', $wp_cache_request_uri) ) {
829 if ( $comment['comment_approved'] == 'delete' && ( isset( $comment[ 'old_comment_approved' ] ) && $comment[ 'old_comment_approved' ] == 0 ) ) { // do nothing if moderated comments are deleted
830 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 );
831 return $postid;
832 } elseif ( $comment['comment_approved'] == 'spam' ) {
833 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Spam comment. Don't delete any cache files.", 4 );
834 return $postid;
835 } elseif( $comment['comment_approved'] == '0' ) {
836 if ( $comment[ 'content_type' ] == '' ) {
837 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 );
838 $super_cache_enabled = 0; // don't remove the super cache static file until comment is approved
839 } else {
840 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Moderated ping or trackback. Not deleting cache files..", 4 );
841 return $postid;
842 }
843 }
844 }
845 // We must check it up again due to WP bugs calling two different actions
846 // for delete, for example both wp_set_comment_status and delete_comment
847 // are called when deleting a comment
848 if ($postid > 0) {
849 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post $postid changed. Update cache.", 4 );
850 return wp_cache_post_change( $postid );
851 } elseif ( $_GET[ 'delete_all' ] != 'Empty Trash' && $_GET[ 'delete_all2' ] != 'Empty Spam' ) {
852 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Unknown post changed. Update cache.", 4 );
853 return wp_cache_post_change( wp_cache_post_id() );
854 }
855 }
856
857 /* Clear out the cache directory. */
858 function wp_cache_clear_cache() {
859 global $cache_path, $wp_cache_object_cache;
860 if ( $wp_cache_object_cache ) {
861 reset_oc_version();
862 } else {
863 prune_super_cache( $cache_path . 'supercache/', true );
864 prune_super_cache( $cache_path, true );
865 }
866 }
867
868 function wp_cache_post_edit($post_id) {
869 global $wp_cache_clear_on_post_edit, $cache_path, $blog_cache_dir;
870 if( $wp_cache_clear_on_post_edit ) {
871 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 );
872 if ( $wp_cache_object_cache ) {
873 reset_oc_version();
874 } else {
875 prune_super_cache( $blog_cache_dir, true );
876 prune_super_cache( $cache_path . 'supercache/', true );
877 }
878 } else {
879 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 );
880 wp_cache_post_change( $post_id );
881 }
882 }
883
884 function wp_cache_post_id_gc( $siteurl, $post_id, $all = 'all' ) {
885 global $cache_path, $wp_cache_object_cache;
886
887 if ( $wp_cache_object_cache )
888 reset_oc_version();
889
890 $post_id = intval( $post_id );
891 if( $post_id == 0 )
892 return;
893
894 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 );
895 $permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( $post_id ) ) );
896 $dir = $cache_path . 'supercache/' . $siteurl;
897 if ( $all == 'all' ) {
898 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 );
899 prune_super_cache( $dir . $permalink, true, true );
900 do_action( 'gc_cache', 'prune', $permalink );
901 @rmdir( $dir . $permalink );
902 } else {
903 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 );
904 prune_super_cache( $dir . $permalink . 'index.html', true, true );
905 prune_super_cache( $dir . $permalink . 'index.html.gz', true, true );
906 do_action( 'gc_cache', 'prune', $permalink );
907 }
908 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 );
909 prune_super_cache( $dir . 'page/', true );
910 do_action( 'gc_cache', 'prune', '/page/' );
911 }
912
913 function wp_cache_post_change( $post_id ) {
914 global $file_prefix, $cache_path, $blog_id, $super_cache_enabled, $blog_cache_dir, $blogcacheid, $wp_cache_refresh_single_only;
915 static $last_processed = -1;
916
917 if ($post_id == $last_processed) return $post_id;
918 $last_processed = $post_id;
919 $post = get_post( $post_id );
920 if( $post->post_status == 'draft' ) {
921 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 );
922 return $post_id;
923 }
924
925 if( !wp_cache_writers_entry() )
926 return $post_id;
927
928 if ( isset( $wp_cache_refresh_single_only ) && $wp_cache_refresh_single_only && strpos( $_SERVER[ 'REQUEST_URI' ], 'wp-comments-post.php' ) ) {
929 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 );
930 $all = false;
931 } else {
932 $all = true;
933 }
934
935 if ( $wp_cache_object_cache )
936 reset_oc_version();
937
938 $permalink = trailingslashit( str_replace( get_option( 'siteurl' ), '', post_permalink( $post_id ) ) );
939 if( $super_cache_enabled ) {
940 $siteurl = trailingslashit( strtolower( preg_replace( '/:.*$/', '', str_replace( 'http://', '', get_option( 'home' ) ) ) ) );
941 // make sure the front page has a rebuild file
942 if ( $all == true ) {
943 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 );
944 prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html', true, true );
945 prune_super_cache( $cache_path . 'supercache/' . $siteurl . 'index.html.gz', true, true );
946 do_action( 'gc_cache', 'prune', 'homepage' );
947 }
948 wp_cache_post_id_gc( $siteurl, $post_id );
949 if( $all == true && get_option( 'show_on_front' ) == 'page' ) {
950 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 );
951 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 );
952 wp_cache_post_id_gc( $siteurl, get_option( 'page_on_front' ), 'single' );
953 $permalink = trailingslashit( str_replace( get_option( 'home' ), '', post_permalink( get_option( 'page_for_posts' ) ) ) );
954 prune_super_cache( $cache_path . 'supercache/' . $siteurl . $permalink . 'index.html', true, true );
955 prune_super_cache( $cache_path . 'supercache/' . $siteurl . $permalink . 'index.html.gz', true, true );
956 do_action( 'gc_cache', 'prune', $permalink );
957 }
958 }
959
960 $matches = array();
961 if ( ($handle = @opendir( $blog_cache_dir . 'meta/' )) ) {
962 while ( false !== ($file = readdir($handle))) {
963 if ( preg_match("/^({$file_prefix}{$blogcacheid}.*)\.meta/", $file, $matches) ) {
964 $meta_pathname = $blog_cache_dir . 'meta/' . $file;
965 $content_pathname = $blog_cache_dir . $matches[1] . ".html";
966 $meta = unserialize(@file_get_contents($meta_pathname));
967 if( false == is_array( $meta ) ) {
968 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( "Post change cleaning up stray file: $content_pathname", 4 );
969 @unlink($meta_pathname);
970 @unlink($content_pathname);
971 continue;
972 }
973 if ($post_id > 0 && $meta) {
974 if ( $meta[ 'blog_id' ] == $blog_id && ( ( $all == true && !$meta[ 'post' ] ) || $meta[ 'post' ] == $post_id) ) {
975 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 );
976 @unlink($meta_pathname);
977 @unlink($content_pathname);
978 if ( $super_cache_enabled == true ) {
979 @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
980 @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
981 do_action( 'gc_cache', 'rebuild', trailingslashit( $meta[ 'uri' ] ) );
982 }
983 }
984 } elseif ($meta[ 'blog_id' ] == $blog_id) {
985 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 );
986 @unlink($meta_pathname);
987 @unlink($content_pathname);
988 if ( $super_cache_enabled == true ) {
989 @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html');
990 @wp_cache_rebuild_or_delete($cache_path . 'supercache/' . trailingslashit( $meta[ 'uri' ] ) . 'index.html.gz');
991 do_action( 'gc_cache', 'rebuild', trailingslashit( $meta[ 'uri' ] ) );
992 }
993 }
994
995 }
996 }
997 closedir($handle);
998 }
999 wp_cache_writers_exit();
1000 return $post_id;
1001 }
1002
1003 function wp_cache_microtime_diff($a, $b) {
1004 list($a_dec, $a_sec) = explode(' ', $a);
1005 list($b_dec, $b_sec) = explode(' ', $b);
1006 return $b_sec - $a_sec + $b_dec - $a_dec;
1007 }
1008
1009 function wp_cache_post_id() {
1010 global $posts, $comment_post_ID, $post_ID;
1011 // We try hard all options. More frequent first.
1012 if ($post_ID > 0 ) return $post_ID;
1013 if ($comment_post_ID > 0 ) return $comment_post_ID;
1014 if (is_single() || is_page()) return $posts[0]->ID;
1015 if (isset( $_GET[ 'p' ] ) && $_GET['p'] > 0) return $_GET['p'];
1016 if (isset( $_POST[ 'p' ] ) && $_POST['p'] > 0) return $_POST['p'];
1017 return 0;
1018 }
1019
1020 function wp_cache_gc_cron() {
1021 global $file_prefix, $cache_max_time;
1022
1023 if ( $cache_max_time == 0 )
1024 return false;
1025
1026 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Cache garbage collection.', 5 );
1027
1028 if( !isset( $cache_max_time ) )
1029 $cache_max_time = 600;
1030
1031 $start = time();
1032 if( !wp_cache_phase2_clean_expired($file_prefix ) ) {
1033 if ( isset( $GLOBALS[ 'wp_super_cache_debug' ] ) && $GLOBALS[ 'wp_super_cache_debug' ] ) wp_cache_debug( 'Cache Expiry cron job failed. Probably mutex locked.', 1 );
1034 update_option( 'wpsupercache_gc_time', time() - ( $cache_max_time - 10 ) ); // if GC failed then run it again in one minute
1035 }
1036 if( time() - $start > 30 )
1037 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 );
1038 }
1039
1040 ?>
1041