vivio-swift-cache-preload.php 9.94 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
<?php
class Vivio_Swift_Cache_Preload
{
	function __construct()
    {
    	
    }

    function enable()
    {
        global $vivio_swift_global;

        // set config option to enabled
    	$vivio_swift_global->configs->set_value('vivio_swift_preload_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_Preload::enable() - Preload Cache enabled.", 1);
    }

	function disable()
    {
        global $vivio_swift_global;

    	$vivio_swift_global->configs->set_value('vivio_swift_preload_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_Preload::enable() - Preload Cache disabled.", 1);
    }

    function clear()
    {
        global $vivio_swift_global;

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

        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::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_preload_cache($force=0)
    {
        global $vivio_swift_global;
        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::create_preload_cache() - Create Preload Cache Init.", 0);

        $urls = array();

        // make sure the specified amount of time has passed before we do another run
59
        if ( $force || ($this->verify_preload_cache_expired()) ) {
Jordan Michaels's avatar
Jordan Michaels committed
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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

            // TODO Add option to select mobile cache as well
            // TODO Add options to select what we want to cache. For now, CACHE ALL THE THINGS

            // home page URI
            $home_url = rtrim(get_option("home"), '/').'/';
            array_push($urls, $home_url);

            // log it
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::create_preload_cache() - Added HOME URI: ".get_option("home"), 1);

            // post URI's
            $posts = get_posts(array('post_type'=>'post','numberposts'=>'-1','post_status'=>'publish'));

            if (count($posts) > 0){
                foreach ($posts as $p){
                    if ($post_permalink = get_permalink($p)){
                        // if we're using permalinks, use the permalink uri
                        array_push($urls, $post_permalink);

                        // log it
                        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::create_preload_cache() - Added POST URI: ".$post_permalink, 1);
                    } else {
                        // otherwise just log it as incompatible
                        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::create_preload_cache() - Failed to get Permalink for Post ID:".$p->ID."[SKIPPING]", 4);
                    }
                } // end foreach
            }

            // attachment URI's
            $posts = get_posts(array('post_type'=>'attachment','numberposts'=>'-1','post_status'=>'publish','post_parent' => null));

            if (count($posts) > 0){
                foreach ($posts as $p){
                    if ($post_permalink = get_permalink($p)){
                        // if we're using permalinks, use the permalink uri
                        array_push($urls, $post_permalink);

                        // log it
                        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::create_preload_cache() - Added ATTACHMENT URI: ".$post_permalink, 1);
                    } else {
                        // otherwise just log it as incompatible
                        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::create_preload_cache() - Failed to get Permalink for Post ID:".$p->ID."[SKIPPING]", 4);
                    }
                } // end foreach
            }

            // page URI's
            $posts = get_posts(array('post_type'=>'page','numberposts'=>'-1','post_status'=>'publish'));

            if (count($posts) > 0){
                foreach ($posts as $p){
                    if ($post_permalink = get_permalink($p)){
                        // if we're using permalinks, use the permalink uri
                        array_push($urls, $post_permalink);

                        // log it
                        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::create_preload_cache() - Added PAGE URI: ".$post_permalink, 1);
                    } else {
                        // otherwise just log it as incompatible
                        $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::create_preload_cache() - Failed to get Permalink for Post ID:".$p->ID."[SKIPPING]", 4);
                    }
                } // end foreach
            }

            // Category URI's
            $categories = get_categories( array(
                'orderby' => 'id',
                'order'   => 'ASC'
            ) );

            foreach ($categories as $cat){
                $cat_link = get_category_link( $cat->term_id );
                
                // save the link
                array_push($urls, $cat_link);

                // log it
                $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::create_preload_cache() - Added PAGE URI: ".$cat_link, 1);
            }

            // Tag URI's
            $tags = get_tags();

            foreach ($tags as $tag){
                $tag_link = get_tag_link( $tag->term_id );

                // save the link
                array_push($urls, $tag_link);

                // log it
                $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::create_preload_cache() - Added PAGE URI: ".$tag_link, 1);

            }

            foreach ($urls as $url){

157 158 159
                if ($exclude_response = Vivio_Swift_Cache_Excludes::process_cache_excludes($url,$force)){
                    // if theres a response, it means the URL should be excluded so just log and  move on.
                    $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::create_preload_cache() - Exclude rule hit: ".$exclude_response."[SKIPPING]", 1);
Jordan Michaels's avatar
Jordan Michaels committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
                    continue;
                }

                // send urls off to the caching process
                Vivio_Swift_Cache::cache_url_with_agent($url, VIVIO_SWIFT_CACHE_BOT_NAME);
            }

            // caching process finished. record time.
            $date = new DateTime();
            $datetime = $date->format('Y-m-d H:i:s');
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::create_preload_cache() - Recording preload cache runtime: ".$datetime, 1);
            $vivio_swift_global->configs->set_value('vivio_swift_preload_last_run_date',$datetime);
            $vivio_swift_global->configs->save_config();
        }
    
    } // end create_preload_cache

    // checks the current time against the last run date to see if the preload cache has expired
    function verify_preload_cache_expired()
    {
        global $vivio_swift_global;

        // if the last run is null, it means that the preload process has never run
        if ($vivio_swift_global->configs->get_value('vivio_swift_preload_last_run_date')=='') {
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::verify_preload_cache_expired() - no preload cache on record.", 2);
            return true;
        }

        // create new datetime objects
        $runtime = new DateTime( $vivio_swift_global->configs->get_value('vivio_swift_preload_last_run_date') );
        $nowtime = new DateTime( date('Y-m-d H:i:s') );

        $expire_hours = $vivio_swift_global->configs->get_value('vivio_swift_preload_cache_expire_hours');
        $elapsed_hours = $nowtime->diff($runtime);

        if ( $elapsed_hours->format('%h') >= $expire_hours) {
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::verify_preload_cache_expired() - hour limit reached: ".$elapsed_hours->format('%h'), 2);
            return true;
        } else {
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::verify_preload_cache_expired() - Elapsed Hours: ".$elapsed_hours->format('%h'), 2);
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::verify_preload_cache_expired() - Expire Hours: ".$expire_hours, 2);
            $vivio_swift_global->debug_logger->log_debug("Vivio_Swift_Cache_Preload::verify_preload_cache_expired() - current cache is still valid. Try forcing a preload if you wish to refresh your cache.", 2);
        }
        
        // default return false (don't run preload cache)
        return false;
    
    }

	// scheduled task for Vivio_Swift_Cron()
    function schedule_create_preload_cache()
    {
        global $vivio_swift_global;

        // IMPORTANT: Not currently using WP-CRON for preload cache as many folks disable it and it is somewhat unreliable ~JM
        
        // TODO: clean some of these checks since they're probably not needed

        // don't cache the admin user
        if (!is_admin()){
            // don't cache our bot
            $is_bot = preg_match("/".VIVIO_SWIFT_CACHE_BOT_NAME."/i", $_SERVER['HTTP_USER_AGENT'])?true:false;
            if (!$is_bot){
                $preload_enabled = ($vivio_swift_global->configs->get_value('vivio_swift_preload_cache_enabled')=='1')?true:false;
                if ($preload_enabled){ //preload
                    $vivio_swift_global->cache_obj->create_preload_cache();
                }
            }
        } // end is_admin()
    }

}