| 1 |
<?php |
| 2 |
|
| 3 |
class autoptimizeCache |
| 4 |
{ |
| 5 |
private $filename; |
| 6 |
private $mime; |
| 7 |
private $cachedir; |
| 8 |
private $delayed; |
| 9 |
|
| 10 |
public function __construct($md5,$ext='php') |
| 11 |
{ |
| 12 |
$this->cachedir = AUTOPTIMIZE_CACHE_DIR; |
| 13 |
$this->delayed = AUTOPTIMIZE_CACHE_DELAY; |
| 14 |
$this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP; |
| 15 |
if($this->nogzip == false) |
| 16 |
$this->filename = 'autoptimize_'.$md5.'.php'; |
| 17 |
else |
| 18 |
$this->filename = 'autoptimize_'.$md5.'.'.$ext; |
| 19 |
} |
| 20 |
|
| 21 |
public function check() |
| 22 |
{ |
| 23 |
if(!file_exists($this->cachedir.$this->filename)) |
| 24 |
{ |
| 25 |
//No cached file, sorry |
| 26 |
return false; |
| 27 |
} |
| 28 |
//Cache exists! |
| 29 |
return true; |
| 30 |
} |
| 31 |
|
| 32 |
public function retrieve() |
| 33 |
{ |
| 34 |
if($this->check()) |
| 35 |
{ |
| 36 |
if($this->nogzip == false) |
| 37 |
return file_get_contents($this->cachedir.$this->filename.'.none'); |
| 38 |
else |
| 39 |
return file_get_contents($this->cachedir.$this->filename); |
| 40 |
} |
| 41 |
return false; |
| 42 |
} |
| 43 |
|
| 44 |
public function cache($code,$mime) |
| 45 |
{ |
| 46 |
if($this->nogzip == false) |
| 47 |
{ |
| 48 |
$file = ($this->delayed ? 'delayed.php' : 'default.php'); |
| 49 |
$phpcode = file_get_contents(WP_PLUGIN_DIR.'/autoptimize/config/'.$file); |
| 50 |
$phpcode = str_replace(array('%%CONTENT%%','exit;'),array($mime,''),$phpcode); |
| 51 |
file_put_contents($this->cachedir.$this->filename,$phpcode); |
| 52 |
file_put_contents($this->cachedir.$this->filename.'.none',$code); |
| 53 |
if(!$this->delayed) |
| 54 |
{ |
| 55 |
//Compress now! |
| 56 |
file_put_contents($this->cachedir.$this->filename.'.deflate',gzencode($code,9,FORCE_DEFLATE)); |
| 57 |
file_put_contents($this->cachedir.$this->filename.'.gzip',gzencode($code,9,FORCE_GZIP)); |
| 58 |
} |
| 59 |
}else{ |
| 60 |
//Write code to cache without doing anything else |
| 61 |
file_put_contents($this->cachedir.$this->filename,$code); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public function getname() |
| 66 |
{ |
| 67 |
return $this->filename; |
| 68 |
} |
| 69 |
|
| 70 |
static function clearall() |
| 71 |
{ |
| 72 |
//Cache not available :( |
| 73 |
if(!autoptimizeCache::cacheavail()) |
| 74 |
return false; |
| 75 |
|
| 76 |
//Clean the cachedir |
| 77 |
$scan = scandir(AUTOPTIMIZE_CACHE_DIR); |
| 78 |
foreach($scan as $file) |
| 79 |
{ |
| 80 |
if(!in_array($file,array('.','..')) && strpos($file,'autoptimize') !== false && is_file(AUTOPTIMIZE_CACHE_DIR.$file)) |
| 81 |
{ |
| 82 |
@unlink(AUTOPTIMIZE_CACHE_DIR.$file); |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
//Do we need to clean WP Super Cache's cache files? |
| 87 |
if(function_exists('wp_cache_clear_cache')) |
| 88 |
{ |
| 89 |
//Newer WP-Super-Cache |
| 90 |
//See http://ocaoimh.ie/wp-super-cache-developers/ |
| 91 |
wp_cache_clear_cache(); |
| 92 |
}elseif(file_exists(WP_CONTENT_DIR.'/wp-cache-config.php') && function_exists('prune_super_cache')){ |
| 93 |
//Old WP-Super-Cache |
| 94 |
global $cache_path; |
| 95 |
prune_super_cache($cache_path.'supercache/',true); |
| 96 |
prune_super_cache($cache_path,true); |
| 97 |
} |
| 98 |
|
| 99 |
return true; |
| 100 |
} |
| 101 |
|
| 102 |
static function stats() |
| 103 |
{ |
| 104 |
//Cache not available :( |
| 105 |
if(!autoptimizeCache::cacheavail()) |
| 106 |
return 0; |
| 107 |
|
| 108 |
//Count cached info |
| 109 |
$count = 0; |
| 110 |
$scan = scandir(AUTOPTIMIZE_CACHE_DIR); |
| 111 |
foreach($scan as $file) |
| 112 |
{ |
| 113 |
if(!in_array($file,array('.','..')) && strpos($file,'autoptimize') !== false) |
| 114 |
{ |
| 115 |
if(is_file(AUTOPTIMIZE_CACHE_DIR.$file)) |
| 116 |
{ |
| 117 |
if(AUTOPTIMIZE_CACHE_NOGZIP && (strpos($file,'.js') !== false || strpos($file,'.css') !== false)) |
| 118 |
{ |
| 119 |
$count++; |
| 120 |
}elseif(!AUTOPTIMIZE_CACHE_NOGZIP && strpos($file,'.none') !== false){ |
| 121 |
$count++; |
| 122 |
}/*else{ |
| 123 |
//Tricky one... it was a dir or a gzip/deflate file |
| 124 |
}*/ |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
//Tell the number of instances |
| 130 |
return $count; |
| 131 |
} |
| 132 |
|
| 133 |
static function cacheavail() |
| 134 |
{ |
| 135 |
if(!defined('AUTOPTIMIZE_CACHE_DIR')) |
| 136 |
{ |
| 137 |
//We didn't set a cache |
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
//Check for existence |
| 142 |
if(!file_exists(AUTOPTIMIZE_CACHE_DIR)) |
| 143 |
{ |
| 144 |
@mkdir(AUTOPTIMIZE_CACHE_DIR,0775,true); |
| 145 |
if(!file_exists(AUTOPTIMIZE_CACHE_DIR)) |
| 146 |
{ |
| 147 |
//Where should we cache? |
| 148 |
return false; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
if(!is_writable(AUTOPTIMIZE_CACHE_DIR)) |
| 153 |
{ |
| 154 |
//How are we supposed to write? |
| 155 |
return false; |
| 156 |
} |
| 157 |
|
| 158 |
//All OK |
| 159 |
return true; |
| 160 |
} |
| 161 |
} |
| 162 |
|