function wp_cache_serve_cache_file() { global $key, $blogcacheid, $wp_cache_request_uri, $file_prefix, $blog_cache_dir, $meta_file, $cache_file, $cache_filename, $meta_pathname, $wp_cache_gzip_encoding, $meta; global $wp_cache_object_cache, $cache_compression, $wp_cache_slash_check, $wp_supercache_304, $wp_cache_home_path, $wp_cache_no_cache_for_get; global $wp_cache_disable_utf8, $wp_cache_mfunc_enabled; extract(wp_super_cache_init()); if (wp_cache_user_agent_is_rejected()) { wp_cache_debug("No wp-cache file served as user agent rejected.", 5); return false; } if ($wp_cache_no_cache_for_get && false == empty($_GET)) { wp_cache_debug("Non empty GET request. Caching disabled on settings page. " . json_encode($_GET), 1); return false; } if ($wp_cache_object_cache && wp_cache_get_cookies_values() == '') { if (!empty($_GET)) { wp_cache_debug("Non empty GET request. Not serving request from object cache. " . json_encode($_GET), 1); return false; } $oc_key = get_oc_key(); $meta_filename = $oc_key . ".meta"; if (gzip_accepted()) { $oc_key .= ".gz"; $meta_filename .= ".gz"; } $cache = wp_cache_get($oc_key, 'supercache'); $meta = json_decode(wp_cache_get($meta_filename, 'supercache'), true); if (is_array($meta) == false) { wp_cache_debug("Meta array from object cache corrupt. Ignoring cache.", 1); return true; } } elseif (file_exists($cache_file)) { wp_cache_debug("wp-cache file exists: {$cache_file}", 5); if (!($meta = json_decode(wp_cache_get_legacy_cache($meta_pathname), true))) { wp_cache_debug("couldn't load wp-cache meta file", 5); return true; } if (is_array($meta) == false) { wp_cache_debug("meta array corrupt, deleting {$meta_pathname} and {$cache_file}", 1); @unlink($meta_pathname); @unlink($cache_file); return true; } } else { // last chance, check if a supercache file exists. Just in case .htaccess rules don't work on this host $filename = supercache_filename(); $file = get_current_url_supercache_dir() . $filename; if (false == file_exists($file)) { wp_cache_debug("No Super Cache file found for current URL: {$file}"); return false; } elseif (false == empty($_GET)) { wp_cache_debug("GET array not empty. Cannot serve a supercache file. " . json_encode($_GET)); return false; } elseif (wp_cache_get_cookies_values() != '') { wp_cache_debug("Cookies found. Cannot serve a supercache file. " . wp_cache_get_cookies_values()); return false; } if (isset($wp_cache_mfunc_enabled) == false) { $wp_cache_mfunc_enabled = 0; } if (false == isset($wp_cache_home_path)) { $wp_cache_home_path = '/'; } // make sure ending slashes are ok if ($wp_cache_request_uri == $wp_cache_home_path || $wp_cache_slash_check && substr($wp_cache_request_uri, -1) == '/' || $wp_cache_slash_check == 0 && substr($wp_cache_request_uri, -1) != '/') { if ($wp_cache_mfunc_enabled == 0) { // get data from file if ($wp_cache_gzip_encoding) { if (file_exists($file . '.gz')) { $cachefiledata = file_get_contents($file . '.gz'); wp_cache_debug("Fetched gzip static page data from supercache file using PHP. File: {$file}.gz"); } else { $cachefiledata = gzencode(file_get_contents($file), 6, FORCE_GZIP); wp_cache_debug("Fetched static page data from supercache file using PHP and gzipped it. File: {$file}"); } } else { $cachefiledata = file_get_contents($file); wp_cache_debug("Fetched static page data from supercache file using PHP. File: {$file}"); } } else { // get dynamic data from filtered file $cachefiledata = do_cacheaction('wpsc_cachedata', file_get_contents($file)); if ($wp_cache_gzip_encoding) { $cachefiledata = gzencode($cachefiledata, 6, FORCE_GZIP); wp_cache_debug("Fetched dynamic page data from supercache file using PHP and gzipped it. File: {$file}"); } else { wp_cache_debug("Fetched dynamic page data from supercache file using PHP. File: {$file}"); } } if (isset($wp_cache_disable_utf8) == false || $wp_cache_disable_utf8 == 0) { header("Content-type: text/html; charset=UTF-8"); } header("Vary: Accept-Encoding, Cookie"); header("Cache-Control: max-age=3, must-revalidate"); header("WP-Super-Cache: Served supercache file from PHP"); $size = function_exists('mb_strlen') ? mb_strlen($cachefiledata, '8bit') : strlen($cachefiledata); if ($wp_cache_gzip_encoding) { header('Content-Encoding: ' . $wp_cache_gzip_encoding); header('Content-Length: ' . $size); } elseif ($wp_supercache_304) { header('Content-Length: ' . $size); } // don't try to match modified dates if using dynamic code. if ($wp_cache_mfunc_enabled == 0 && $wp_supercache_304) { if (function_exists('apache_request_headers')) { $request = apache_request_headers(); $remote_mod_time = isset($request['If-Modified-Since']) ? $request['If-Modified-Since'] : 0; } else { if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) { $remote_mod_time = $_SERVER['HTTP_IF_MODIFIED_SINCE']; } else { $remote_mod_time = 0; } } $local_mod_time = gmdate("D, d M Y H:i:s", filemtime($file)) . ' GMT'; if ($remote_mod_time != 0 && $remote_mod_time == $local_mod_time) { header("HTTP/1.0 304 Not Modified"); exit; } header('Last-Modified: ' . $local_mod_time); } echo $cachefiledata; exit; } else { wp_cache_debug("No wp-cache file exists. Must generate a new one."); return false; } } $cache_file = do_cacheaction('wp_cache_served_cache_file', $cache_file); // Sometimes the gzip headers are lost. Make sure html returned isn't compressed! if ($cache_compression && $wp_cache_gzip_encoding && !in_array('Content-Encoding: ' . $wp_cache_gzip_encoding, $meta['headers'])) { $ungzip = true; wp_cache_debug("GZIP headers not found. Force uncompressed output.", 1); } else { $ungzip = false; } foreach ($meta['headers'] as $t => $header) { // godaddy fix, via http://blog.gneu.org/2008/05/wp-supercache-on-godaddy/ and http://www.littleredrails.com/blog/2007/09/08/using-wp-cache-on-godaddy-500-error/ if (strpos($header, 'Last-Modified:') === false) { header($header); } } header('WP-Super-Cache: Served legacy cache file'); if ($wp_cache_object_cache) { if ($cache) { if ($ungzip) { // attempt to uncompress the cached file just in case it's gzipped $uncompressed = gzuncompress($cache); if ($uncompressed) { wp_cache_debug("Uncompressed gzipped cache file from object cache", 1); $cache = $uncompressed; unset($uncompressed); } } if (isset($meta['dynamic']) && $meta['dynamic']) { wp_cache_debug("Serving wp-cache dynamic file from object cache", 5); echo do_cacheaction('wpsc_cachedata', $cache); } else { wp_cache_debug("Serving wp-cache static file from object cache", 5); echo $cache; } wp_cache_debug("exit request", 5); die; } } else { if (isset($meta['dynamic'])) { wp_cache_debug("Serving wp-cache dynamic file", 5); if ($ungzip) { // attempt to uncompress the cached file just in case it's gzipped $cache = wp_cache_get_legacy_cache($cache_file); $uncompressed = @gzuncompress($cache); if ($uncompressed) { wp_cache_debug("Uncompressed gzipped cache file from wp-cache", 1); unset($cache); echo do_cacheaction('wpsc_cachedata', $uncompressed); } else { echo do_cacheaction('wpsc_cachedata', $cache); } } else { echo do_cacheaction('wpsc_cachedata', wp_cache_get_legacy_cache($cache_file)); } } else { wp_cache_debug("Serving wp-cache static file", 5); if ($ungzip) { $cache = wp_cache_get_legacy_cache($cache_file); $uncompressed = gzuncompress($cache); if ($uncompressed) { wp_cache_debug("Uncompressed gzipped cache file from wp-cache", 1); echo $uncompressed; } else { echo $cache; } } else { echo wp_cache_get_legacy_cache($cache_file); } } wp_cache_debug("exit request", 5); die; } }
function wp_cache_clean_legacy_files($dir, $file_prefix) { global $wpdb; $dir = trailingslashit($dir); if (@is_dir($dir . 'meta') == false) { return false; } if ($handle = @opendir($dir)) { while (false !== ($file = readdir($handle))) { if (is_file($dir . $file) == false || $file == 'index.html') { continue; } if (strpos($file, $file_prefix) !== false) { if (strpos($file, '.html')) { // delete old legacy files immediately @unlink($dir . $file); @unlink($dir . 'meta/' . str_replace('.html', '.meta', $file)); } else { $meta = json_decode(wp_cache_get_legacy_cache($dir . 'meta/' . $file), true); if ((defined('VHOST') || defined('WP_ALLOW_MULTISITE') && constant('WP_ALLOW_MULTISITE') == true) && $meta['blog_id'] != $wpdb->blogid) { continue; } @unlink($dir . $file); @unlink($dir . 'meta/' . $file); } } } closedir($handle); } }
function wp_cache_post_change($post_id) { global $file_prefix, $cache_path, $blog_id, $super_cache_enabled, $blog_cache_dir, $blogcacheid, $wp_cache_refresh_single_only, $wp_cache_object_cache; static $last_processed = -1; if ($post_id == $last_processed) { wp_cache_debug("wp_cache_post_change: Already processed post {$post_id}.", 4); return $post_id; } $post = get_post($post_id); // Some users are inexplicibly seeing this error on scheduled posts. // define this constant to disable the post status check. if (false == defined('WPSCFORCEUPDATE') && is_object($post) && $post->post_status != 'publish') { wp_cache_debug("wp_cache_post_change: draft post, not deleting any cache files.", 4); return $post_id; } $last_processed = $post_id; if (!wp_cache_writers_entry()) { return $post_id; } if (isset($wp_cache_refresh_single_only) && $wp_cache_refresh_single_only && (strpos($_SERVER['HTTP_REFERER'], 'edit-comments.php') || strpos($_SERVER['REQUEST_URI'], 'wp-comments-post.php'))) { if (defined('DONOTDELETECACHE')) { wp_cache_debug("wp_cache_post_change: comment detected and it's moderated or spam. Not deleting cached files.", 4); return $post_id; } else { wp_cache_debug("wp_cache_post_change: comment detected. only deleting post page.", 4); $all = false; } } else { $all = true; } if ($wp_cache_object_cache) { reset_oc_version(); } $permalink = trailingslashit(str_replace(get_option('siteurl'), '', get_permalink($post_id))); if ($super_cache_enabled) { $dir = get_supercache_dir(); $siteurl = trailingslashit(strtolower(preg_replace('/:.*$/', '', str_replace('https://', '', str_replace('http://', '', get_option('home')))))); // make sure the front page has a rebuild file wp_cache_post_id_gc($siteurl, $post_id); if ($all == true) { wp_cache_debug("Post change: supercache enabled: deleting cache files in " . $cache_path . 'supercache/' . $siteurl, 4); $files_to_check = get_all_supercache_filenames($dir); foreach ($files_to_check as $cache_file) { wp_cache_debug("Post change: deleting cache file: " . $dir . $cache_file, 4); prune_super_cache($dir . $cache_file, true, true); } do_action('gc_cache', 'prune', 'homepage'); } else { wp_cache_debug("wp_cache_post_change: not deleting all pages.", 4); } if ($all == true && get_option('show_on_front') == 'page') { wp_cache_debug("Post change: deleting page_on_front and page_for_posts pages.", 4); wp_cache_debug("Post change: page_on_front " . get_option('page_on_front'), 4); wp_cache_post_id_gc($siteurl, get_option('page_on_front'), 'single'); $permalink = trailingslashit(str_replace(get_option('home'), '', get_permalink(get_option('page_for_posts')))); $files_to_check = get_all_supercache_filenames($dir . $permalink); foreach ($files_to_check as $cache_file) { prune_super_cache($dir . $permalink . $cache_file, true, true); } do_action('gc_cache', 'prune', $permalink); } else { wp_cache_debug("wp_cache_post_change: not deleting front static page.", 4); } } wp_cache_debug("wp_cache_post_change: checking {$blog_cache_dir}meta/", 4); $supercache_files_deleted = false; if ($handle = @opendir($blog_cache_dir)) { while (false !== ($file = readdir($handle))) { if (strpos($file, $file_prefix) !== false) { if (strpos($file, '.html')) { // delete old legacy files immediately wp_cache_debug("wp_cache_post_change: Deleting obsolete legacy cache+meta files: {$file}"); @unlink($blog_cache_dir . $file); @unlink($blog_cache_dir . 'meta/' . str_replace('.html', '.meta', $file)); continue; } else { $meta = json_decode(wp_cache_get_legacy_cache($blog_cache_dir . 'meta/' . $file), true); if (false == is_array($meta)) { wp_cache_debug("Post change cleaning up stray file: {$file}", 4); @unlink($blog_cache_dir . 'meta/' . $file); @unlink($blog_cache_dir . $file); continue; } if ($post_id > 0 && $meta) { $permalink = trailingslashit(str_replace(get_option('home'), '', get_permalink($post_id))); if ($meta['blog_id'] == $blog_id && ($all == true && !$meta['post'] || $meta['post'] == $post_id)) { wp_cache_debug("Post change: deleting post wp-cache files for {$meta['uri']}: {$file}", 4); @unlink($blog_cache_dir . 'meta/' . $file); @unlink($blog_cache_dir . $file); if (false == $supercache_files_deleted && $super_cache_enabled == true) { $files_to_check = get_all_supercache_filenames($dir . $permalink); wp_cache_debug("Post change: deleting supercache files for {$permalink}: {$file} " . print_r($files_to_check, 1), 4); foreach ($files_to_check as $cache_file) { @wp_cache_rebuild_or_delete($dir . trailingslashit($permalink) . $cache_file); } $supercache_files_deleted = true; do_action('gc_cache', 'rebuild', $permalink); } } } elseif ($meta['blog_id'] == $blog_id) { wp_cache_debug("Post change: deleting wp-cache files for {$meta['uri']}: {$file}", 4); @unlink($blog_cache_dir . 'meta/' . $file); @unlink($blog_cache_dir . $file); if ($super_cache_enabled == true) { $files_to_check = get_all_supercache_filenames($dir . $meta['uri']); wp_cache_debug("Post change: deleting supercache files for {$meta['uri']}: {$file} " . print_r($files_to_check, 1), 4); foreach ($files_to_check as $cache_file) { @wp_cache_rebuild_or_delete($dir . trailingslashit($meta['uri']) . $cache_file); } do_action('gc_cache', 'rebuild', trailingslashit($meta['uri'])); } } } } } closedir($handle); } wp_cache_writers_exit(); return $post_id; }