PageRenderTime 68ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/wp-content/plugins/wp-super-cache/wp-cache-phase2.php

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