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; if (get_option('preload_cache_stop')) { delete_option('preload_cache_stop'); return true; } $mutex = $cache_path . "preload_mutex.tmp"; sleep(3 + mt_rand(1, 5)); if (@file_exists($mutex)) { if (@filemtime($mutex) > time() - 600) { return true; } else { @unlink($mutex); } } $fp = @fopen($mutex, 'w'); @fclose($fp); $counter = get_option('preload_cache_counter'); if (is_array($counter) == false) { $counter = array('c' => 0, 't' => time()); update_option('preload_cache_counter', $counter); } $c = $counter['c']; if ($wp_cache_preload_email_me && $c == 0) { wp_mail(get_option('admin_email'), sprintf(__('[%1$s] Cache Preload Started', 'wp-super-cache'), site_url(), ''), ' '); } $taxonomies = apply_filters('wp_cache_preload_taxonomies', array('post_tag' => 'tag', 'category' => 'category')); $finished = false; $permalink_counter_msg = $cache_path . "preload_permalink.txt"; 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 .= site_url($path . "/" . $term->slug . "/") . "\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'), site_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']; 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)); 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) { $posts = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE ( post_type != 'revision' AND post_type != 'nav_menu_item' ) AND post_status = 'publish' ORDER BY post_date DESC LIMIT {$c}, 100"); } else { $posts = false; } if (!isset($wp_cache_preload_email_volume)) { $wp_cache_preload_email_volume = 'medium'; } update_option('preload_cache_counter', array('c' => $c + 100, 't' => time())); if ($posts) { 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'), site_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); 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")) { @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'), site_url(), ''), ' '); } return true; } $msg .= "{$url}\n"; wp_remote_get($url, array('timeout' => 60, 'blocking' => true)); 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'), $_SERVER['HTTP_HOST'], $c + 100), __("Refreshed the following posts:", 'wp-super-cache') . "\n{$msg}"); } if (defined('DOING_CRON')) { 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_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'), site_url()), __("Cleaning up old supercache files.", 'wp-super-cache') . "\n" . $msg); } wp_cache_phase2_clean_expired($file_prefix, true); // force cleanup of old files. } @unlink($mutex); }
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 wp_cron_preload_cache() { global $wpdb, $wp_cache_preload_interval, $wp_cache_preload_posts, $wp_cache_preload_email_me, $wp_cache_preload_email_volume; if (get_option('preload_cache_stop')) { delete_option('preload_cache_stop'); return true; } $c = (int) get_option('preload_cache_counter'); if ($wp_cache_preload_posts == 'all' || $c <= $wp_cache_preload_posts) { $posts = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE post_status = 'publish' ORDER BY post_date DESC LIMIT {$c}, 100"); } else { $posts = false; } if (!isset($wp_cache_preload_email_volume)) { $wp_cache_preload_email_volume = 'medium'; } update_option('preload_cache_counter', $c + 100); if ($posts) { if ($wp_cache_preload_email_me && $c == 0) { wp_mail(get_option('admin_email'), sprintf(__('[%1$s] Cache Preload Started', 'wp-super-cache'), site_url(), ''), ''); } 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'), site_url(), $c, $c + 100), ''); } $msg = ''; $count = $c + 1; foreach ($posts as $post_id) { clear_post_supercache($post_id); $url = get_permalink($post_id); $msg .= "{$url}\n"; wp_remote_get($url, array('timeout' => 60, 'blocking' => true)); $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'), $_SERVER['HTTP_HOST'], $c + 100), __("Refreshed the following posts:", 'wp-super-cache') . "\n{$msg}"); } if (defined('DOING_CRON')) { wp_schedule_single_event(time() + 30, 'wp_cache_preload_hook'); } } else { $msg = ''; update_option('preload_cache_counter', 0); 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_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'), site_url()), __("Cleaning up old supercache files.", 'wp-super-cache') . "\n" . $msg); } wp_cache_phase2_clean_expired($file_prefix, true); // force cleanup of old files. } }
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; if (get_option('preload_cache_stop')) { delete_option('preload_cache_stop'); return true; } $mutex = $cache_path . "preload_mutex.tmp"; sleep(3 + mt_rand(1, 5)); if (@file_exists($mutex)) { if (@filemtime($mutex) > time() - 600) { return true; } else { @unlink($mutex); } } $fp = @fopen($mutex, 'w'); @fclose($fp); $counter = get_option('preload_cache_counter'); $c = $counter['c']; if ($wp_cache_preload_posts == 'all' || $c <= $wp_cache_preload_posts) { $posts = $wpdb->get_col("SELECT ID FROM {$wpdb->posts} WHERE (post_type = 'post' OR post_type='page') AND post_status = 'publish' ORDER BY post_date DESC LIMIT {$c}, 100"); } else { $posts = false; } if (!isset($wp_cache_preload_email_volume)) { $wp_cache_preload_email_volume = 'medium'; } update_option('preload_cache_counter', array('c' => $c + 100, 't' => time())); if ($posts) { if ($wp_cache_preload_email_me && $c == 0) { wp_mail(get_option('admin_email'), sprintf(__('[%1$s] Cache Preload Started', 'wp-super-cache'), site_url(), ''), ''); } 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'), site_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); 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")) { @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'), site_url(), ''), ''); } return true; } $msg .= "{$url}\n"; wp_remote_get($url, array('timeout' => 60, 'blocking' => true)); 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'), $_SERVER['HTTP_HOST'], $c + 100), __("Refreshed the following posts:", 'wp-super-cache') . "\n{$msg}"); } if (defined('DOING_CRON')) { 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_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'), site_url()), __("Cleaning up old supercache files.", 'wp-super-cache') . "\n" . $msg); } wp_cache_phase2_clean_expired($file_prefix, true); // force cleanup of old files. } @unlink($mutex); }