Esempio n. 1
0
function wp_cache_clean_expired($file_prefix)
{
    global $cache_path, $cache_max_time;
    // If phase2 was compiled, use its function to avoid race-conditions
    if (function_exists('wp_cache_phase2_clean_expired')) {
        if (function_exists('prune_super_cache')) {
            $dir = $cache_path . 'supercache/' . preg_replace('/:.*$/', '', $_SERVER["HTTP_HOST"]);
            if (is_dir($dir)) {
                prune_super_cache($dir);
            } elseif (is_dir($dir . '.disabled')) {
                prune_super_cache($dir . '.disabled');
            }
            $_POST['super_cache_stats'] = 1;
            // regenerate super cache stats;
        }
        return wp_cache_phase2_clean_expired($file_prefix);
    }
    $expr = "/^{$file_prefix}/";
    $now = time();
    if ($handle = opendir($cache_path)) {
        while (false !== ($file = readdir($handle))) {
            if (preg_match($expr, $file) && filemtime($cache_path . $file) + $cache_max_time <= $now) {
                unlink($cache_path . $file);
                unlink($cache_path . 'meta/' . str_replace('.html', '.term', $file));
            }
        }
        closedir($handle);
    }
}
Esempio n. 2
0
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);
}
Esempio n. 3
0
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_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);
}
Esempio n. 5
0
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_cache_ob_end()
{
    global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache;
    global $meta_object, $known_headers;
    /* Preparing... with PHP5 is straightforward, use headers_list() */
    if (function_exists('apache_response_headers')) {
        $response = apache_response_headers();
        $meta_object->headers = array();
        foreach ($known_headers as $key) {
            if (isset($response[$key])) {
                array_push($meta_object->headers, "{$key}: " . $response[$key]);
            }
        }
        /* Not use because it gives problems with some
        		 * PHP installations
        		if (!$response{'Content-Length'}) {
        		// WP does not set content size
        			$content_size = ob_get_length();
        			@header("Content-Length: $content_size");
        			array_push($meta_object->headers, "Content-Length: $content_size");
        		}
        		*/
        if (!$response['Last-Modified']) {
            $value = gmdate('D, d M Y H:i:s') . ' GMT';
            /* Dont send this the first time */
            /* @header('Last-Modified: ' . $value); */
            array_push($meta_object->headers, "Last-Modified: {$value}");
        }
        if (!$response['Content-Type']) {
            $value = "text/html; charset=\"" . get_settings('blog_charset') . "\"";
            @header("Content-Type: {$value}");
            array_push($meta_object->headers, "Content-Type: {$value}");
        }
    }
    ob_end_clean();
    if ($new_cache) {
        $serial = serialize($meta_object);
        wp_cache_writers_entry();
        $fr = fopen($cache_path . $meta_file, 'w');
        fputs($fr, $serial);
        fclose($fr);
        wp_cache_writers_exit();
    }
    if ($file_expired == false) {
        return;
    }
    // we delete expired files
    flush();
    //Ensure we send data to the client
    wp_cache_phase2_clean_expired($file_prefix);
}
function wp_cache_gc_cron()
{
    global $file_prefix, $cache_max_time, $cache_gc_email_me, $cache_time_interval;
    $msg = '';
    if ($cache_max_time == 0) {
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug('Cache garbage collection disabled because cache expiry time is zero.', 5);
        }
        return false;
    }
    update_option('wpsupercache_gc_time', 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();
    $num = 0;
    if (false == ($num = 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_time_interval - 10));
        // if GC failed then run it again in one minute
        $msg .= __('Cache expiry cron job failed. Job will run again in 10 seconds.', 'wp-super-cache') . "\n";
    }
    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);
        }
        $msg .= __('Cache expiry cron job took more than 30 seconds. You should probably run the garbage collector more often.', 'wp-super-cache') . "\n";
    }
    if ($cache_gc_email_me) {
        if ($msg != '') {
            $msg = "The following warnings were generated by the WP Super Cache Garbage Collector:\n" . $msg;
        }
        $msg = "Hi,\n\nThe WP Super Cache Garbage Collector has now run, deleting " . (int) $num . " files and directories.\nIf you want to switch off these emails please see the WP Super Cache Advanced Settings\npage on your blog.\n\n{$msg}\nRegards,\nThe Garbage Collector.";
        wp_mail(get_option('admin_email'), sprintf(__('[%1$s] WP Super Cache GC Report', 'wp-super-cache'), site_url()), $msg);
    }
    schedule_wp_gc(1);
}
function wp_cache_clean_expired($file_prefix)
{
    global $cache_path, $cache_max_time;
    // If phase2 was compiled, use its function to avoid race-conditions
    if (function_exists('wp_cache_phase2_clean_expired')) {
        return wp_cache_phase2_clean_expired($file_prefix);
    }
    $expr = "/^{$file_prefix}/";
    $now = time();
    if ($handle = opendir($cache_path)) {
        while (false !== ($file = readdir($handle))) {
            if (preg_match($expr, $file) && filemtime($cache_path . $file) + $cache_max_time <= $now) {
                unlink($cache_path . $file);
            }
        }
        closedir($handle);
    }
}
function wp_cache_shutdown_callback()
{
    global $cache_path, $cache_max_time, $file_expired, $file_prefix, $meta_file, $new_cache;
    global $wp_cache_meta_object, $known_headers, $blog_id;
    $wp_cache_meta_object->uri = $_SERVER["SERVER_NAME"] . preg_replace('/[ <>\'\\"\\r\\n\\t\\(\\)]/', '', $_SERVER['REQUEST_URI']);
    // To avoid XSS attacs
    $wp_cache_meta_object->blog_id = $blog_id;
    $wp_cache_meta_object->post = wp_cache_post_id();
    $response = wp_cache_get_response_headers();
    $wp_cache_meta_object->headers = array();
    foreach ($known_headers as $key) {
        if (isset($response[$key])) {
            array_push($wp_cache_meta_object->headers, "{$key}: " . $response[$key]);
        }
    }
    /* Not used because it gives problems with some
    	 * PHP installations
    	if (!$response{'Content-Length'}) {
    	// WP does not set content size
    		$content_size = ob_get_length();
    		@header("Content-Length: $content_size");
    		array_push($wp_cache_meta_object->headers, "Content-Length: $content_size");
    	}
    	*/
    if (!$response['Last-Modified']) {
        $value = gmdate('D, d M Y H:i:s') . ' GMT';
        /* Dont send this the first time */
        /* @header('Last-Modified: ' . $value); */
        array_push($wp_cache_meta_object->headers, "Last-Modified: {$value}");
    }
    if (!$response['Content-Type'] && !$response['Content-type']) {
        $value = "text/html; charset=" . get_settings('blog_charset');
        @header("Content-Type: {$value}");
        array_push($wp_cache_meta_object->headers, "Content-Type: {$value}");
    }
    ob_end_flush();
    if ($new_cache) {
        $serial = serialize($wp_cache_meta_object);
        wp_cache_writers_entry();
        $fr = fopen($cache_path . $meta_file, 'w');
        fputs($fr, $serial);
        fclose($fr);
        wp_cache_writers_exit();
    }
    if ($file_expired == false) {
        return;
    }
    // we delete expired files
    flush();
    //Ensure we send data to the client
    wp_cache_phase2_clean_expired($file_prefix);
}
Esempio n. 10
0
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);
}
Esempio n. 11
0
function wp_cache_gc_cron()
{
    global $file_prefix, $wp_cache_gc;
    if (!isset($wp_cache_gc)) {
        $wp_cache_gc = 3600;
    }
    if (!wp_cache_phase2_clean_expired($file_prefix)) {
        update_option('wpsupercache_gc_time', time() - ($wp_cache_gc - 600));
    }
    // if GC failed then run it again in 10 minutes
}
Esempio n. 12
0
function wp_cache_gc_cron()
{
    global $file_prefix;
    wp_cache_phase2_clean_expired($file_prefix);
}