Ejemplo n.º 1
0
function wp_cache_manager_updates()
{
    global $wp_cache_mobile_enabled, $wp_supercache_cache_list, $wp_cache_config_file, $wp_cache_hello_world, $wp_cache_clear_on_post_edit, $cache_rebuild_files, $wp_cache_mutex_disabled, $wp_cache_not_logged_in, $wp_cache_make_known_anon, $cache_path, $wp_cache_object_cache, $_wp_using_ext_object_cache, $wp_cache_refresh_single_only, $cache_compression, $wp_cache_mod_rewrite, $wp_supercache_304, $wp_super_cache_late_init, $wp_cache_front_page_checks, $cache_page_secret;
    if (!wpsupercache_site_admin()) {
        return false;
    }
    if (false == isset($cache_page_secret)) {
        $cache_page_secret = md5(date('H:i:s') . mt_rand());
        wp_cache_replace_line('^ *\\$cache_page_secret', "\$cache_page_secret = '" . $cache_page_secret . "';", $wp_cache_config_file);
    }
    // Delete cache for a specific page
    if (isset($_GET['action']) && $_GET['action'] == 'delete' && (isset($_GET['_wpnonce']) ? wp_verify_nonce($_REQUEST['_wpnonce'], 'delete-cache') : false)) {
        $path = get_supercache_dir() . preg_replace('/:.*$/', '', $_GET['path']);
        $files = get_all_supercache_filenames($path);
        foreach ($files as $cache_file) {
            prune_super_cache($path . $cache_file, true);
        }
        wp_redirect(preg_replace('/[ <>\'\\"\\r\\n\\t\\(\\)]/', '', $_GET['path']));
        die;
    }
    $valid_nonce = isset($_REQUEST['_wpnonce']) ? wp_verify_nonce($_REQUEST['_wpnonce'], 'wp-cache') : false;
    if ($valid_nonce == false) {
        return false;
    }
    if (isset($_POST['action']) && $_POST['action'] == 'easysetup') {
        $_POST['action'] = 'scupdates';
        if (isset($_POST['wp_cache_easy_on']) && $_POST['wp_cache_easy_on'] == 1) {
            $_POST['wp_cache_mobile_enabled'] = 1;
            $_POST['wp_cache_status'] = 'all';
            $_POST['super_cache_enabled'] = 2;
            // PHP
            $_POST['cache_rebuild_files'] = 1;
            unset($_POST['cache_compression']);
        } else {
            unset($_POST['wp_cache_status']);
            $_POST['super_cache_enabled'] = 0;
        }
    }
    if (isset($_POST['action']) && $_POST['action'] == 'scupdates') {
        if (isset($_POST['wp_super_cache_late_init'])) {
            $wp_super_cache_late_init = 1;
        } else {
            $wp_super_cache_late_init = 0;
        }
        wp_cache_replace_line('^ *\\$wp_super_cache_late_init', "\$wp_super_cache_late_init = " . $wp_super_cache_late_init . ";", $wp_cache_config_file);
        if (isset($_POST['wp_supercache_304'])) {
            $wp_supercache_304 = 1;
        } else {
            $wp_supercache_304 = 0;
        }
        wp_cache_replace_line('^ *\\$wp_supercache_304', "\$wp_supercache_304 = " . $wp_supercache_304 . ";", $wp_cache_config_file);
        if (isset($_POST['wp_cache_mobile_enabled'])) {
            $wp_cache_mobile_enabled = 1;
        } else {
            $wp_cache_mobile_enabled = 0;
        }
        wp_cache_replace_line('^ *\\$wp_cache_mobile_enabled', "\$wp_cache_mobile_enabled = " . $wp_cache_mobile_enabled . ";", $wp_cache_config_file);
        if (isset($_POST['wp_cache_front_page_checks'])) {
            $wp_cache_front_page_checks = 1;
        } else {
            $wp_cache_front_page_checks = 0;
        }
        wp_cache_replace_line('^ *\\$wp_cache_front_page_checks', "\$wp_cache_front_page_checks = " . $wp_cache_front_page_checks . ";", $wp_cache_config_file);
        $wp_supercache_cache_list = $_POST['wp_supercache_cache_list'] == 1 ? 1 : 0;
        wp_cache_replace_line('^ *\\$wp_supercache_cache_list', "\$wp_supercache_cache_list = " . $wp_supercache_cache_list . ";", $wp_cache_config_file);
        if (isset($_POST['wp_cache_status'])) {
            if ($_POST['wp_cache_status'] == 'all') {
                wp_cache_enable();
            }
            if (isset($_POST['super_cache_enabled'])) {
                if ($_POST['super_cache_enabled'] == 0) {
                    wp_cache_enable();
                    wp_super_cache_disable();
                }
                if ($_POST['super_cache_enabled'] == 1) {
                    $wp_cache_mod_rewrite = 1;
                    // we need this because supercached files can be served by PHP too.
                } else {
                    $wp_cache_mod_rewrite = 0;
                }
                wp_cache_replace_line('^ *\\$wp_cache_mod_rewrite', '$wp_cache_mod_rewrite = ' . $wp_cache_mod_rewrite . ";", $wp_cache_config_file);
            }
        } else {
            wp_cache_disable();
        }
        if (isset($_POST['wp_cache_hello_world'])) {
            $wp_cache_hello_world = 1;
        } else {
            $wp_cache_hello_world = 0;
        }
        wp_cache_replace_line('^ *\\$wp_cache_hello_world', '$wp_cache_hello_world = ' . $wp_cache_hello_world . ";", $wp_cache_config_file);
        if (isset($_POST['wp_cache_clear_on_post_edit'])) {
            $wp_cache_clear_on_post_edit = 1;
        } else {
            $wp_cache_clear_on_post_edit = 0;
        }
        wp_cache_replace_line('^ *\\$wp_cache_clear_on_post_edit', "\$wp_cache_clear_on_post_edit = " . $wp_cache_clear_on_post_edit . ";", $wp_cache_config_file);
        if (isset($_POST['cache_rebuild_files'])) {
            $cache_rebuild_files = 1;
        } else {
            $cache_rebuild_files = 0;
        }
        wp_cache_replace_line('^ *\\$cache_rebuild_files', "\$cache_rebuild_files = " . $cache_rebuild_files . ";", $wp_cache_config_file);
        if (isset($_POST['wp_cache_mutex_disabled'])) {
            $wp_cache_mutex_disabled = 0;
        } else {
            $wp_cache_mutex_disabled = 1;
        }
        if (defined('WPSC_DISABLE_LOCKING')) {
            $wp_cache_mutex_disabled = 1;
        }
        wp_cache_replace_line('^ *\\$wp_cache_mutex_disabled', "\$wp_cache_mutex_disabled = " . $wp_cache_mutex_disabled . ";", $wp_cache_config_file);
        if (isset($_POST['wp_cache_not_logged_in'])) {
            if ($wp_cache_not_logged_in == 0 && function_exists('prune_super_cache')) {
                prune_super_cache($cache_path, true);
            }
            $wp_cache_not_logged_in = 1;
        } else {
            $wp_cache_not_logged_in = 0;
        }
        wp_cache_replace_line('^ *\\$wp_cache_not_logged_in', "\$wp_cache_not_logged_in = " . $wp_cache_not_logged_in . ";", $wp_cache_config_file);
        if (isset($_POST['wp_cache_make_known_anon'])) {
            if ($wp_cache_make_known_anon == 0 && function_exists('prune_super_cache')) {
                prune_super_cache($cache_path, true);
            }
            $wp_cache_make_known_anon = 1;
        } else {
            $wp_cache_make_known_anon = 0;
        }
        wp_cache_replace_line('^ *\\$wp_cache_make_known_anon', "\$wp_cache_make_known_anon = " . $wp_cache_make_known_anon . ";", $wp_cache_config_file);
        if ($_wp_using_ext_object_cache && isset($_POST['wp_cache_object_cache'])) {
            if ($wp_cache_object_cache == 0 && function_exists('prune_super_cache')) {
                prune_super_cache($cache_path, true);
            }
            $wp_cache_object_cache = 1;
        } else {
            $wp_cache_object_cache = 0;
        }
        wp_cache_replace_line('^ *\\$wp_cache_object_cache', "\$wp_cache_object_cache = " . $wp_cache_object_cache . ";", $wp_cache_config_file);
        if (isset($_POST['wp_cache_refresh_single_only'])) {
            $wp_cache_refresh_single_only = 1;
        } else {
            $wp_cache_refresh_single_only = 0;
        }
        wp_cache_replace_line('^ *\\$wp_cache_refresh_single_only', "\$wp_cache_refresh_single_only = '" . $wp_cache_refresh_single_only . "';", $wp_cache_config_file);
        if (defined('WPSC_DISABLE_COMPRESSION')) {
            $cache_compression = 0;
            wp_cache_replace_line('^ *\\$cache_compression', "\$cache_compression = " . $cache_compression . ";", $wp_cache_config_file);
        } else {
            if (isset($_POST['cache_compression'])) {
                $new_cache_compression = 1;
            } else {
                $new_cache_compression = 0;
            }
            if (1 == ini_get('zlib.output_compression') || "on" == strtolower(ini_get('zlib.output_compression'))) {
                echo '<div id="message" class="updated fade">' . __("<strong>Warning!</strong> You attempted to enable compression but <code>zlib.output_compression</code> is enabled. See #21 in the Troubleshooting section of the readme file.", 'wp-super-cache') . '</div>';
            } else {
                if ($new_cache_compression != $cache_compression) {
                    $cache_compression = $new_cache_compression;
                    wp_cache_replace_line('^ *\\$cache_compression', "\$cache_compression = " . $cache_compression . ";", $wp_cache_config_file);
                    if (function_exists('prune_super_cache')) {
                        prune_super_cache($cache_path, true);
                    }
                    delete_option('super_cache_meta');
                }
            }
        }
    }
}
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;
    static $last_processed = -1;
    if ($post_id == $last_processed) {
        return $post_id;
    }
    $last_processed = $post_id;
    $post = get_post($post_id);
    if ($post->post_status == 'draft') {
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            wp_cache_debug("wp_cache_post_change: draft post, not deleting any cache files.", 4);
        }
        return $post_id;
    }
    if (!wp_cache_writers_entry()) {
        return $post_id;
    }
    if (isset($wp_cache_refresh_single_only) && $wp_cache_refresh_single_only && strpos($_SERVER['REQUEST_URI'], 'wp-comments-post.php')) {
        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
            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'), '', post_permalink($post_id)));
    if ($super_cache_enabled) {
        $dir = get_supercache_dir();
        $siteurl = trailingslashit(strtolower(preg_replace('/:.*$/', '', str_replace('http://', '', get_option('home')))));
        // make sure the front page has a rebuild file
        wp_cache_post_id_gc($siteurl, $post_id);
        // make sure the front page has a rebuild file
        if ($all == true) {
            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);
            }
            $files_to_check = get_all_supercache_filenames($dir);
            foreach ($files_to_check as $cache_file) {
                prune_super_cache($dir . $cache_file, true, true);
            }
            do_action('gc_cache', 'prune', 'homepage');
        }
        wp_cache_post_id_gc($siteurl, $post_id);
        if ($all == true && get_option('show_on_front') == 'page') {
            if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                wp_cache_debug("Post change: deleting page_on_front and page_for_posts pages.", 4);
            }
            if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                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'), '', post_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);
        }
    }
    $matches = array();
    if ($handle = @opendir($blog_cache_dir . 'meta/')) {
        while (false !== ($file = readdir($handle))) {
            if (preg_match("/^({$file_prefix}{$blogcacheid}.*)\\.meta/", $file, $matches)) {
                $meta_pathname = $blog_cache_dir . 'meta/' . $file;
                $content_pathname = $blog_cache_dir . $matches[1] . ".html";
                $meta = unserialize(@file_get_contents($meta_pathname));
                if (false == is_array($meta)) {
                    if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                        wp_cache_debug("Post change cleaning up stray file: {$content_pathname}", 4);
                    }
                    @unlink($meta_pathname);
                    @unlink($content_pathname);
                    continue;
                }
                if ($post_id > 0 && $meta) {
                    $permalink = trailingslashit(str_replace(get_option('home'), '', post_permalink($post_id)));
                    if ($meta['blog_id'] == $blog_id && ($all == true && !$meta['post'] || $meta['post'] == $post_id)) {
                        if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                            wp_cache_debug("Post change: deleting post cache files for {$meta['uri']}: {$content_pathname}", 4);
                        }
                        @unlink($meta_pathname);
                        @unlink($content_pathname);
                        if ($super_cache_enabled == true) {
                            @prune_super_cache($dir . $permalink, true, true);
                            do_action('gc_cache', 'rebuild', $permalink);
                        }
                    }
                } elseif ($meta['blog_id'] == $blog_id) {
                    if (isset($GLOBALS['wp_super_cache_debug']) && $GLOBALS['wp_super_cache_debug']) {
                        wp_cache_debug("Post change: deleting cache files for {$meta['uri']}: {$content_pathname}", 4);
                    }
                    @unlink($meta_pathname);
                    @unlink($content_pathname);
                    if ($super_cache_enabled == true) {
                        $files_to_check = get_all_supercache_filenames($dir . $meta['uri']);
                        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;
}
function clear_post_supercache($post_id)
{
    $dir = get_current_url_supercache_dir($post_id);
    if (false == @is_dir($dir)) {
        return false;
    }
    if (!function_exists('prune_super_cache')) {
        include_once 'wp-cache-phase2.php';
    }
    wp_cache_debug("clear_post_supercache: deleting {$dir}/index*.html files", 2);
    $files_to_check = get_all_supercache_filenames($dir);
    foreach ($files_to_check as $cache_file) {
        prune_super_cache($dir . $cache_file, true);
    }
}
Ejemplo n.º 4
0
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'), '', post_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'), '', post_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);
    $matches = array();
    if ($handle = @opendir($blog_cache_dir . 'meta/')) {
        while (false !== ($file = readdir($handle))) {
            if (preg_match("/^({$file_prefix}{$blogcacheid}.*)\\.meta/", $file, $matches)) {
                $meta_pathname = $blog_cache_dir . 'meta/' . $file;
                $content_pathname = $blog_cache_dir . $matches[1] . ".html";
                $meta = unserialize(@file_get_contents($meta_pathname));
                if (false == is_array($meta)) {
                    wp_cache_debug("Post change cleaning up stray file: {$content_pathname}", 4);
                    @unlink($meta_pathname);
                    @unlink($content_pathname);
                    continue;
                }
                if ($post_id > 0 && $meta) {
                    $permalink = trailingslashit(str_replace(get_option('home'), '', post_permalink($post_id)));
                    if ($meta['blog_id'] == $blog_id && ($all == true && !$meta['post'] || $meta['post'] == $post_id)) {
                        wp_cache_debug("Post change: deleting post cache files for {$meta['uri']}: {$content_pathname}", 4);
                        @unlink($meta_pathname);
                        @unlink($content_pathname);
                        if ($super_cache_enabled == true) {
                            @prune_super_cache($dir . $permalink, true, true);
                            do_action('gc_cache', 'rebuild', $permalink);
                        }
                    }
                } elseif ($meta['blog_id'] == $blog_id) {
                    wp_cache_debug("Post change: deleting cache files for {$meta['uri']}: {$content_pathname}", 4);
                    @unlink($meta_pathname);
                    @unlink($content_pathname);
                    if ($super_cache_enabled == true) {
                        $files_to_check = get_all_supercache_filenames($dir . $meta['uri']);
                        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;
}