function wp_cache_gc_cron() { global $file_prefix, $cache_max_time; if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) { wp_cache_debug('Cache garbage collection.', 5); } if (!isset($cache_max_time)) { $cache_max_time = 600; } $start = time(); if (!wp_cache_phase2_clean_expired($file_prefix)) { if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) { wp_cache_debug('Cache Expiry cron job failed. Probably mutex locked.', 1); } update_option('wpsupercache_gc_time', time() - ($cache_max_time - 10)); // if GC failed then run it again in one minute } if (time() - $start > 30) { 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); } } }
function wp_cache_check_site() { global $wp_super_cache_front_page_check, $wp_super_cache_front_page_clear, $wp_super_cache_front_page_text, $wp_super_cache_front_page_notification; if (!isset($wp_super_cache_front_page_check) || isset($wp_super_cache_front_page_check) && $wp_super_cache_front_page_check == 0) { return false; } if (function_exists("wp_remote_get") == false) { return false; } $front_page = wp_remote_get(site_url(), array('timeout' => 60, 'blocking' => true)); if (is_array($front_page)) { // Check for gzipped front page if ($front_page['headers']['content-type'] == 'application/x-gzip') { if (!isset($wp_super_cache_front_page_clear) || isset($wp_super_cache_front_page_clear) && $wp_super_cache_front_page_clear == 0) { wp_mail(get_option('admin_email'), sprintf(__('[%s] Front page is gzipped! Please clear cache!', 'wp-super-cache'), site_url()), sprintf(__("Please visit %s to clear the cache as the front page of your site is now downloading!", 'wp-super-cache'), trailingslashit(site_url()) . "wp-admin/options-general.php?page=wpsupercache")); } else { wp_cache_clear_cache(); wp_mail(get_option('admin_email'), sprintf(__('[%s] Front page is gzipped! Cache Cleared!', 'wp-super-cache'), site_url()), sprintf(__("The cache on your blog has been cleared because the front page of your site is now downloading. Please visit %s to verify the cache has been cleared.", 'wp-super-cache'), trailingslashit(site_url()) . "wp-admin/options-general.php?page=wpsupercache")); } } // Check for broken front page if (isset($wp_super_cache_front_page_text) && $wp_super_cache_front_page_text != '' && false === strpos($front_page['body'], $wp_super_cache_front_page_text)) { if (!isset($wp_super_cache_front_page_clear) || isset($wp_super_cache_front_page_clear) && $wp_super_cache_front_page_clear == 0) { wp_mail(get_option('admin_email'), sprintf(__('[%s] Front page is not correct! Please clear cache!', 'wp-super-cache'), site_url()), sprintf(__('Please visit %1$s to clear the cache as the front page of your site is not correct and missing the text, "%2$s"!', 'wp-super-cache'), trailingslashit(site_url()) . "wp-admin/options-general.php?page=wpsupercache", $wp_super_cache_front_page_text)); } else { wp_cache_clear_cache(); wp_mail(get_option('admin_email'), sprintf(__('[%s] Front page is not correct! Cache Cleared!', 'wp-super-cache'), site_url()), sprintf(__('The cache on your blog has been cleared because the front page of your site is missing the text "%2$s". Please visit %1$s to verify the cache has been cleared.', 'wp-super-cache'), trailingslashit(site_url()) . "wp-admin/options-general.php?page=wpsupercache", $wp_super_cache_front_page_text)); } } } if (isset($wp_super_cache_front_page_notification) && $wp_super_cache_front_page_notification == 1) { wp_mail(get_option('admin_email'), sprintf(__('[%s] Front page check!', 'wp-super-cache'), site_url()), sprintf(__("WP Super Cache has checked the front page of your blog. Please visit %s if you would like to disable this.", 'wp-super-cache') . "\n\n", trailingslashit(site_url()) . "wp-admin/options-general.php?page=wpsupercache#debug") . print_r($front_page, 1)); } if (!wp_next_scheduled('wp_cache_check_site_hook')) { wp_schedule_single_event(time() + 360, 'wp_cache_check_site_hook'); if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) { wp_cache_debug('scheduled wp_cache_check_site_hook for 360 seconds time.', 2); } } }
function wpmp_switcher_wp_cache_check_mobile($cache_key) { if (!isset($_SERVER["HTTP_USER_AGENT"])) { return $cache_key; } $is_mobile = wpmp_switcher_is_mobile_browser(); $mobile_group = ''; $wp_mobile_pack_dir = WP_CONTENT_DIR . '/plugins/wordpress-mobile-pack'; if ($is_mobile && is_file($wp_mobile_pack_dir . '/themes/mobile_pack_base/group_detection.php')) { include_once $wp_mobile_pack_dir . '/themes/mobile_pack_base/group_detection.php'; $mobile_group = group_detection(); } if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) { wp_cache_debug("Lite detection says is_mobile: {$is_mobile} and group: {$mobile_group} for User-Agent: " . $_SERVER["HTTP_USER_AGENT"], 5); } $new_cache_key = $cache_key . $is_mobile . $mobile_group; // In the worst case we return the cache_key as it came in return $new_cache_key; }
function wp_supercache_cache_for_admins() { if (isset($_GET['preview']) || function_exists("is_admin") && is_admin()) { return true; } if (false == do_cacheaction('wp_supercache_remove_cookies', true)) { return true; } $cookie_keys = array('wordpress_logged_in', 'comment_author_'); if (defined('LOGGED_IN_COOKIE')) { $cookie_keys[] = constant('LOGGED_IN_COOKIE'); } reset($_COOKIE); foreach ($_COOKIE as $cookie => $val) { reset($cookie_keys); foreach ($cookie_keys as $key) { if (strpos($cookie, $key) !== FALSE) { if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) { wp_cache_debug('Removing auth from $_COOKIE to allow caching for logged in user (' . $cookie . ')', 5); } unset($_COOKIE[$cookie]); } } } }
function schedule_wp_gc($forced = 0) { global $cache_schedule_type, $cache_max_time, $cache_time_interval, $cache_scheduled_time, $cache_schedule_interval; if (false == isset($cache_schedule_type)) { $cache_schedule_type = 'interval'; $cache_schedule_interval = $cache_max_time; } if ($cache_schedule_type == 'interval') { if ($cache_max_time == 0) { return false; } if (!isset($cache_max_time)) { $cache_max_time = 600; } $last_gc = get_option("wpsupercache_gc_time"); if (!$last_gc) { update_option('wpsupercache_gc_time', time()); $last_gc = get_option("wpsupercache_gc_time"); } if ($forced || $last_gc < time() - 60) { // Allow up to 60 seconds for the previous job to run global $wp_cache_shutdown_gc; if (!isset($wp_cache_shutdown_gc) || $wp_cache_shutdown_gc == 0) { if (!($t = wp_next_scheduled('wp_cache_gc'))) { wp_clear_scheduled_hook('wp_cache_gc'); wp_schedule_single_event(time() + $cache_time_interval, 'wp_cache_gc'); if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) { wp_cache_debug('scheduled wp_cache_gc for 10 seconds time.', 5); } } } else { global $time_to_gc_cache; $time_to_gc_cache = 1; // tell the "shutdown gc" to run! } } } elseif ($cache_schedule_type == 'time' && !wp_next_scheduled('wp_cache_gc')) { wp_schedule_event(strtotime($cache_scheduled_time), $cache_schedule_interval, 'wp_cache_gc'); } return true; }
function wp_cron_preload_cache() { global $wpdb, $wp_cache_preload_interval, $wp_cache_preload_posts, $wp_cache_preload_email_me, $wp_cache_preload_email_volume, $cache_path, $wp_cache_preload_taxonomies; if (get_option('preload_cache_stop')) { delete_option('preload_cache_stop'); wp_cache_debug("wp_cron_preload_cache: preload cancelled", 1); return true; } $mutex = $cache_path . "preload_mutex.tmp"; sleep(3 + mt_rand(1, 5)); if (@file_exists($mutex)) { if (@filemtime($mutex) > time() - 600) { wp_cache_debug("wp_cron_preload_cache: preload mutex found and less than 600 seconds old. Aborting preload.", 1); return true; } else { wp_cache_debug("wp_cron_preload_cache: old preload mutex found and deleted. Preload continues.", 1); @unlink($mutex); } } $fp = @fopen($mutex, 'w'); @fclose($fp); $counter = get_option('preload_cache_counter'); if (is_array($counter) == false) { wp_cache_debug("wp_cron_preload_cache: setting up preload for the first time!", 5); $counter = array('c' => 0, 't' => time()); update_option('preload_cache_counter', $counter); } $c = $counter['c']; update_option('preload_cache_counter', array('c' => $c + 100, 't' => time())); if ($wp_cache_preload_email_me && $c == 0) { wp_mail(get_option('admin_email'), sprintf(__('[%1$s] Cache Preload Started', 'wp-super-cache'), home_url(), ''), ' '); } if ($wp_cache_preload_posts == 'all' || $c < $wp_cache_preload_posts) { wp_cache_debug("wp_cron_preload_cache: doing taxonomy preload.", 5); $permalink_counter_msg = $cache_path . "preload_permalink.txt"; if (isset($wp_cache_preload_taxonomies) && $wp_cache_preload_taxonomies) { $taxonomies = apply_filters('wp_cache_preload_taxonomies', array('post_tag' => 'tag', 'category' => 'category')); foreach ($taxonomies as $taxonomy => $path) { $taxonomy_filename = $cache_path . "taxonomy_" . $taxonomy . ".txt"; if ($c == 0) { @unlink($taxonomy_filename); } if (false == @file_exists($taxonomy_filename)) { $out = ''; $records = get_terms($taxonomy); foreach ($records as $term) { $out .= get_term_link($term) . "\n"; } $fp = fopen($taxonomy_filename, 'w'); if ($fp) { fwrite($fp, $out); fclose($fp); } $details = explode("\n", $out); } else { $details = explode("\n", file_get_contents($taxonomy_filename)); } if (count($details) != 1 && $details[0] != '') { $rows = array_splice($details, 0, 50); if ($wp_cache_preload_email_me && $wp_cache_preload_email_volume == 'many') { wp_mail(get_option('admin_email'), sprintf(__('[%1$s] Refreshing %2$s taxonomy from %3$d to %4$d', 'wp-super-cache'), home_url(), $taxonomy, $c, $c + 100), 'Refreshing: ' . print_r($rows, 1)); } foreach ((array) $rows as $url) { set_time_limit(60); if ($url == '') { continue; } $url_info = parse_url($url); $dir = get_supercache_dir() . $url_info['path']; wp_cache_debug("wp_cron_preload_cache: delete {$dir}", 5); prune_super_cache($dir); $fp = @fopen($permalink_counter_msg, 'w'); if ($fp) { @fwrite($fp, "{$taxonomy}: {$url}"); @fclose($fp); } wp_remote_get($url, array('timeout' => 60, 'blocking' => true)); wp_cache_debug("wp_cron_preload_cache: fetched {$url}", 5); sleep(1); } $fp = fopen($taxonomy_filename, 'w'); if ($fp) { fwrite($fp, implode("\n", $details)); fclose($fp); } } } } } if ($wp_cache_preload_posts == 'all' || $c < $wp_cache_preload_posts) { $types = get_post_types(array('public' => true, 'publicly_queryable' => true), 'names', 'or'); $types = array_map('esc_sql', $types); $types = "'" . implode("','", $types) . "'"; $posts = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE ( post_type IN ( {$types} ) ) AND post_status = 'publish' ORDER BY ID ASC LIMIT {$c}, 100"); wp_cache_debug("wp_cron_preload_cache: got 100 posts from position {$c}.", 5); } else { wp_cache_debug("wp_cron_preload_cache: no more posts to get. Limit ({$wp_cache_preload_posts}) reached.", 5); $posts = false; } if (!isset($wp_cache_preload_email_volume)) { $wp_cache_preload_email_volume = 'medium'; } if ($posts) { if (get_option('show_on_front') == 'page') { $page_on_front = get_option('page_on_front'); $page_for_posts = get_option('page_for_posts'); } else { $page_on_front = $page_for_posts = 0; } if ($wp_cache_preload_email_me && $wp_cache_preload_email_volume == 'many') { wp_mail(get_option('admin_email'), sprintf(__('[%1$s] Refreshing posts from %2$d to %3$d', 'wp-super-cache'), home_url(), $c, $c + 100), ' '); } $msg = ''; $count = $c + 1; $permalink_counter_msg = $cache_path . "preload_permalink.txt"; foreach ($posts as $post_id) { set_time_limit(60); if ($page_on_front != 0 && ($post_id == $page_on_front || $post_id == $page_for_posts)) { continue; } clear_post_supercache($post_id); $url = get_permalink($post_id); $fp = @fopen($permalink_counter_msg, 'w'); if ($fp) { @fwrite($fp, $count . " " . $url); @fclose($fp); } if (@file_exists($cache_path . "stop_preload.txt")) { wp_cache_debug("wp_cron_preload_cache: cancelling preload. stop_preload.txt found.", 5); @unlink($mutex); @unlink($cache_path . "stop_preload.txt"); update_option('preload_cache_counter', array('c' => 0, 't' => time())); if ($wp_cache_preload_email_me) { wp_mail(get_option('admin_email'), sprintf(__('[%1$s] Cache Preload Stopped', 'wp-super-cache'), home_url(), ''), ' '); } return true; } $msg .= "{$url}\n"; wp_remote_get($url, array('timeout' => 60, 'blocking' => true)); wp_cache_debug("wp_cron_preload_cache: fetched {$url}", 5); sleep(1); $count++; } if ($wp_cache_preload_email_me && $wp_cache_preload_email_volume != 'less') { wp_mail(get_option('admin_email'), sprintf(__('[%1$s] %2$d posts refreshed', 'wp-super-cache'), home_url(), $c + 100), __("Refreshed the following posts:", 'wp-super-cache') . "\n{$msg}"); } if (defined('DOING_CRON')) { wp_cache_debug("wp_cron_preload_cache: scheduling the next preload in 30 seconds.", 5); wp_schedule_single_event(time() + 30, 'wp_cache_preload_hook'); } } else { $msg = ''; update_option('preload_cache_counter', array('c' => 0, 't' => time())); if ((int) $wp_cache_preload_interval && defined('DOING_CRON')) { if ($wp_cache_preload_email_me) { $msg = sprintf(__('Scheduling next preload refresh in %d minutes.', 'wp-super-cache'), (int) $wp_cache_preload_interval); } wp_cache_debug("wp_cron_preload_cache: no more posts. scheduling next preload in {$wp_cache_preload_interval} minutes.", 5); wp_schedule_single_event(time() + (int) $wp_cache_preload_interval * 60, 'wp_cache_full_preload_hook'); } global $file_prefix, $cache_max_time; if ($wp_cache_preload_interval > 0) { $cache_max_time = (int) $wp_cache_preload_interval * 60; // fool the GC into expiring really old files } else { $cache_max_time = 86400; // fool the GC into expiring really old files } if ($wp_cache_preload_email_me) { wp_mail(get_option('admin_email'), sprintf(__('[%s] Cache Preload Completed', 'wp-super-cache'), home_url()), __("Cleaning up old supercache files.", 'wp-super-cache') . "\n" . $msg); } wp_cache_debug("wp_cron_preload_cache: clean expired cache files older than {$cache_max_time} seconds.", 5); wp_cache_phase2_clean_expired($file_prefix, true); // force cleanup of old files. } @unlink($mutex); }
function get_current_url_supercache_dir($post_id = 0) { global $cached_direct_pages, $cache_path, $wp_cache_request_uri; static $saved_supercache_dir = array(); if (isset($saved_supercache_dir[$post_id])) { return $saved_supercache_dir[$post_id]; } if ($post_id != 0) { $uri = str_replace(site_url(), '', get_permalink($post_id)); } else { $uri = strtolower($wp_cache_request_uri); } $uri = preg_replace('/[ <>\'\\"\\r\\n\\t\\(\\)]/', '', str_replace('/index.php', '/', str_replace('..', '', preg_replace("/(\\?.*)?\$/", '', $uri)))); $uri = str_replace('\\', '', $uri); $dir = preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"]) . $uri; // To avoid XSS attacks if (function_exists("apply_filters")) { $dir = apply_filters('supercache_dir', $dir); } $dir = $cache_path . 'supercache/' . $dir . '/'; if (is_array($cached_direct_pages) && in_array($_SERVER['REQUEST_URI'], $cached_direct_pages)) { $dir = ABSPATH . $uri . '/'; } $dir = str_replace('//', '/', $dir); if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) { wp_cache_debug("supercache dir: {$dir}", 5); } $saved_supercache_dir[$post_id] = $dir; return $dir; }
function wp_super_cache_jetpack_cookie_check($cache_key) { if (file_exists(dirname(WPCACHEHOME) . '/jetpack/class.jetpack-user-agent.php')) { if (function_exists("jetpack_is_mobile") == false) { include dirname(WPCACHEHOME) . '/jetpack/class.jetpack-user-agent.php'; } if (jetpack_is_mobile()) { return 'mobile'; } else { return 'normal'; } } else { if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) { wp_cache_debug("wp_super_cache_jetpack_cookie_check: jetpack UA file not found."); } return "normal"; } }
if (file_exists($cache_file) && ($mtime = @filemtime($meta_pathname))) { if ($mtime + $cache_max_time > time()) { if (!($meta = unserialize(@file_get_contents($meta_pathname)))) { return true; } if (is_array($meta) == false) { @unlink($meta_pathname); @unlink($cache_file); return true; } $cache_file = do_cacheaction('wp_cache_served_cache_file', $cache_file); // Sometimes the gzip headers are lost. If this is a gzip capable client, send those headers. if ($wp_cache_gzip_encoding && !in_array('Content-Encoding: ' . $wp_cache_gzip_encoding, $meta['headers'])) { $meta['headers']['Content-Encoding'] = 'Content-Encoding: ' . $wp_cache_gzip_encoding; $meta['headers']['Vary'] = 'Vary: Accept-Encoding, Cookie'; wp_cache_debug("Had to add gzip headers to the page {$_SERVER['REQUEST_URI']}."); } 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: WP-Cache'); if (!($content_size = @filesize($cache_file)) > 0 || $mtime < @filemtime($cache_file)) { return true; } if ($meta['dynamic']) { include $cache_file; } else { readfile($cache_file);
function wp_supercache_cache_for_admins() { if (isset($_GET['preview']) || function_exists("is_admin") && is_admin()) { return $cookies; } $cookie_keys = array('wordpress_logged_in', 'comment_author_'); reset($_COOKIE); foreach ($_COOKIE as $cookie => $val) { reset($cookie_keys); foreach ($cookie_keys as $key) { if (strpos($cookie, $key) !== FALSE) { if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) { wp_cache_debug('Removing auth from $_COOKIE to allow caching for logged user (' . $cookie . ')', 5); } unset($_COOKIE[$cookie]); } } } }
function wp_cache_gc_watcher() { if (false == wp_next_scheduled('wp_cache_gc')) { wp_cache_debug('GC Watcher: scheduled new gc cron.', 5); schedule_wp_gc(); } }
function wp_cache_gc_watcher() { if (false == wp_next_scheduled('wp_cache_gc')) { if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) { wp_cache_debug('GC Watcher: scheduled new gc cron.', 5); } schedule_wp_gc(); } }
function wp_cache_check_mobile($cache_key) { global $wp_cache_mobile_enabled, $wp_cache_mobile_browsers, $wp_cache_mobile_whitelist; if (!isset($wp_cache_mobile_enabled) || false == $wp_cache_mobile_enabled) { return $cache_key; } if (!isset($_SERVER["HTTP_USER_AGENT"])) { return $cache_key; } $whitelist = explode(',', $wp_cache_mobile_whitelist); foreach ($whitelist as $browser) { if (strstr($_SERVER["HTTP_USER_AGENT"], trim($browser))) { if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) { wp_cache_debug("whitelst mobile browser detected: " . $_SERVER["HTTP_USER_AGENT"], 5); } return $cache_key; } } $browsers = explode(',', $wp_cache_mobile_browsers); foreach ($browsers as $browser) { if (strstr($_SERVER["HTTP_USER_AGENT"], trim($browser))) { if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) { wp_cache_debug("mobile browser detected: " . $_SERVER["HTTP_USER_AGENT"], 5); } return $cache_key . $browser; } } return $cache_key; }