vivio-swift-cache-onaccess.php 4.15 KB
Newer Older
Jordan Michaels's avatar
Jordan Michaels committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
<?php
class Vivio_Swift_Cache_OnAccess
{
	function __construct()
    {

    }

    function enable()
    {
        global $vivio_swift_global;

        // set config option to enabled
    	$vivio_swift_global->configs->set_value('vivio_swift_cache_enabled','1');//Checkbox
        $vivio_swift_global->configs->save_config();

    	// update htaccess
    	Vivio_Swift_Utility_Htaccess::write_to_htaccess();

    	// log it
    	$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_OnAccess::enable() - OnAccess Cache enabled.", 0);
    }

	function disable()
    {
        global $vivio_swift_global;

    	$vivio_swift_global->configs->set_value('vivio_swift_cache_enabled','');//Checkbox
        $vivio_swift_global->configs->save_config();

    	// update htaccess
    	Vivio_Swift_Utility_Htaccess::write_to_htaccess();

    	// log it
    	$vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_OnAccess::enable() - OnAccess Cache disabled.", 0);
    }

    function clear()
    {
        global $vivio_swift_global;

        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_OnAccess::clear() - Clearing OnAccess Cache...", 1);
        
        Vivio_Swift_Utility_File::clear_cache_dir();

        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_OnAccess::clear() - Resetting Preload Cache Last Run date...", 1);
        $vivio_swift_global->configs->set_value('vivio_swift_preload_last_run_date','');
        $vivio_swift_global->configs->save_config();
    }

    function create_page_cache()
    {
        global $vivio_swift_global;
        $error = '';
        // TODO manually excluded cookies, URI's, useragents, pages

        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_OnAccess::create_page_cache() - creating page cache...",0);

        // process exclusions
        if (!Vivio_Swift_Cache_Excludes::process_cache_excludes($url)){
            // if the url matches one of our excludes, don't cache it, just move on.
            $error = 1;
        }

        if ($error==''){
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_OnAccess::create_page_cache() - loading page....",0);
            ob_start(array($this, "create_page_cache_closer"));
        }
    }

    function create_page_cache_closer($response)
    {
        global $vivio_swift_global;
        global $wp;

        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_OnAccess::create_page_cache_closer() - Initializing closer...",0);

        // don't cache certain responses
        if((function_exists("http_response_code")) && (http_response_code() != 200))
        {
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_OnAccess::create_page_cache_closer() - 200 HTTP response not found. [SKIPPING]",2);
            return $response;
        }
        // TODO add this to page excludes
        if($GLOBALS["pagenow"] == "wp-login.php")
        {
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_OnAccess::create_page_cache_closer() - current page is on exclude list. [SKIPPING]",2);
            return $response;
        }

        // TODO add post-processors here (compression, combination, etc)


        // grab the current page URL
        // $url = home_url(add_query_arg(array(),$wp->request)); // doesn't include index.php
        $url = $_SERVER[REQUEST_URI];
        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_OnAccess::create_page_cache_closer() - URL pulled as: ".$url,0);

        // parse the URI
        if ( ! $uri = $vivio_swift_global->cache_obj->parse_uri($url) ){
            // failed to parse URI
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_OnAccess::create_page_cache_closer() - Failed to parse: ".$url."[SKIPPING]", 4);
            return $response;
        }

        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_OnAccess::create_page_cache_closer() - Passing off to save_cached_page()...",0);
        // send page off to save util
        if ($vivio_swift_global->cache_obj->save_cached_page($uri,$response,1))return $response."<!-- [Vivio Swift] cached page will be displayed next request. -->";
    }

}