| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 |
|
| 4 |
class autoptimizeCache { |
| 5 |
private $filename; |
| 6 |
private $mime; |
| 7 |
private $cachedir; |
| 8 |
private $delayed; |
| 9 |
|
| 10 |
public function __construct($md5,$ext='php') { |
| 11 |
$this->cachedir = AUTOPTIMIZE_CACHE_DIR; |
| 12 |
$this->delayed = AUTOPTIMIZE_CACHE_DELAY; |
| 13 |
$this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP; |
| 14 |
if($this->nogzip == false) { |
| 15 |
$this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.php'; |
| 16 |
} else { |
| 17 |
if (in_array($ext, array("js","css"))) { |
| 18 |
$this->filename = $ext.'/'.AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.'.$ext; |
| 19 |
} else { |
| 20 |
$this->filename = '/'.AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.'.$ext; |
| 21 |
} |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
public function check() { |
| 26 |
if(!file_exists($this->cachedir.$this->filename)) { |
| 27 |
// No cached file, sorry |
| 28 |
return false; |
| 29 |
} |
| 30 |
// Cache exists! |
| 31 |
return true; |
| 32 |
} |
| 33 |
|
| 34 |
public function retrieve() { |
| 35 |
if($this->check()) { |
| 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 |
} |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
public function cache($code,$mime) { |
| 46 |
if($this->nogzip == false) { |
| 47 |
$file = ($this->delayed ? 'delayed.php' : 'default.php'); |
| 48 |
$phpcode = file_get_contents(WP_PLUGIN_DIR.'/autoptimize/config/'.$file); |
| 49 |
$phpcode = str_replace(array('%%CONTENT%%','exit;'),array($mime,''),$phpcode); |
| 50 |
file_put_contents($this->cachedir.$this->filename,$phpcode); |
| 51 |
file_put_contents($this->cachedir.$this->filename.'.none',$code); |
| 52 |
if(!$this->delayed) { |
| 53 |
// Compress now! |
| 54 |
file_put_contents($this->cachedir.$this->filename.'.deflate',gzencode($code,9,FORCE_DEFLATE)); |
| 55 |
file_put_contents($this->cachedir.$this->filename.'.gzip',gzencode($code,9,FORCE_GZIP)); |
| 56 |
} |
| 57 |
} else { |
| 58 |
// Write code to cache without doing anything else |
| 59 |
file_put_contents($this->cachedir.$this->filename,$code); |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
public function getname() { |
| 64 |
return $this->filename; |
| 65 |
} |
| 66 |
|
| 67 |
static function clearall() { |
| 68 |
if(!autoptimizeCache::cacheavail()) { |
| 69 |
return false; |
| 70 |
} |
| 71 |
|
| 72 |
// scan the cachedirs |
| 73 |
foreach (array("","js","css") as $scandirName) { |
| 74 |
$scan[$scandirName] = scandir(AUTOPTIMIZE_CACHE_DIR.$scandirName); |
| 75 |
} |
| 76 |
|
| 77 |
// clear the cachedirs |
| 78 |
foreach ($scan as $scandirName=>$scanneddir) { |
| 79 |
$thisAoCacheDir=rtrim(AUTOPTIMIZE_CACHE_DIR.$scandirName,"/")."/"; |
| 80 |
foreach($scanneddir as $file) { |
| 81 |
if(!in_array($file,array('.','..')) && strpos($file,AUTOPTIMIZE_CACHEFILE_PREFIX) !== false && is_file($thisAoCacheDir.$file)) { |
| 82 |
@unlink($thisAoCacheDir.$file); |
| 83 |
} |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
@unlink(AUTOPTIMIZE_CACHE_DIR."/.htaccess"); |
| 88 |
|
| 89 |
// Do we need to clean any caching plugins cache-files? |
| 90 |
if(function_exists('wp_cache_clear_cache')) { |
| 91 |
if (is_multisite()) { |
| 92 |
$blog_id = get_current_blog_id(); |
| 93 |
wp_cache_clear_cache($blog_id); |
| 94 |
} else { |
| 95 |
wp_cache_clear_cache(); |
| 96 |
} |
| 97 |
} else if ( function_exists('w3tc_pgcache_flush') ) { |
| 98 |
w3tc_pgcache_flush(); // w3 total cache |
| 99 |
} else if ( function_exists('hyper_cache_invalidate') ) { |
| 100 |
hyper_cache_invalidate(); // hypercache |
| 101 |
} else if ( function_exists('wp_fast_cache_bulk_delete_all') ) { |
| 102 |
wp_fast_cache_bulk_delete_all(); // wp fast cache |
| 103 |
} else if (class_exists("WpFastestCache")) { |
| 104 |
$wpfc = new WpFastestCache(); // wp fastest cache |
| 105 |
$wpfc -> deleteCache(); |
| 106 |
} else if ( class_exists("c_ws_plugin__qcache_purging_routines") ) { |
| 107 |
c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache |
| 108 |
} else if(file_exists(WP_CONTENT_DIR.'/wp-cache-config.php') && function_exists('prune_super_cache')){ |
| 109 |
// fallback for WP-Super-Cache |
| 110 |
global $cache_path; |
| 111 |
if (is_multisite()) { |
| 112 |
$blog_id = get_current_blog_id(); |
| 113 |
prune_super_cache( get_supercache_dir( $blog_id ), true ); |
| 114 |
prune_super_cache( $cache_path . 'blogs/', true ); |
| 115 |
} else { |
| 116 |
prune_super_cache($cache_path.'supercache/',true); |
| 117 |
prune_super_cache($cache_path,true); |
| 118 |
} |
| 119 |
} else { |
| 120 |
// fallback; schedule event and try to clear there |
| 121 |
wp_schedule_single_event( time() + 1, 'ao_flush_pagecache' , array(time())); |
| 122 |
} |
| 123 |
return true; |
| 124 |
} |
| 125 |
|
| 126 |
static function stats() { |
| 127 |
// Cache not available :( |
| 128 |
if(!autoptimizeCache::cacheavail()) { |
| 129 |
return 0; |
| 130 |
} |
| 131 |
|
| 132 |
// Count cached info |
| 133 |
$count = 0; |
| 134 |
|
| 135 |
// scan the cachedirs |
| 136 |
foreach (array("","js","css") as $scandirName) { |
| 137 |
$scan[$scandirName] = scandir(AUTOPTIMIZE_CACHE_DIR.$scandirName); |
| 138 |
} |
| 139 |
|
| 140 |
foreach ($scan as $scandirName=>$scanneddir) { |
| 141 |
$thisAoCacheDir=rtrim(AUTOPTIMIZE_CACHE_DIR.$scandirName,"/")."/"; |
| 142 |
foreach($scanneddir as $file) { |
| 143 |
if(!in_array($file,array('.','..')) && strpos($file,'autoptimize') !== false) { |
| 144 |
if(is_file($thisAoCacheDir.$file)) { |
| 145 |
if(AUTOPTIMIZE_CACHE_NOGZIP && (strpos($file,'.js') !== false || strpos($file,'.css') !== false)) { |
| 146 |
$count++; |
| 147 |
} elseif(!AUTOPTIMIZE_CACHE_NOGZIP && strpos($file,'.none') !== false) { |
| 148 |
$count++; |
| 149 |
} |
| 150 |
} |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
// print the number of instances |
| 156 |
return $count; |
| 157 |
} |
| 158 |
|
| 159 |
static function cacheavail() { |
| 160 |
if(!defined('AUTOPTIMIZE_CACHE_DIR')) { |
| 161 |
// We didn't set a cache |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
foreach (array("","js","css") as $checkDir) { |
| 166 |
if(!autoptimizeCache::checkCacheDir(AUTOPTIMIZE_CACHE_DIR.$checkDir)) { |
| 167 |
return false; |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
/** write index.html here to avoid prying eyes */ |
| 172 |
$indexFile=AUTOPTIMIZE_CACHE_DIR.'/index.html'; |
| 173 |
if(!is_file($indexFile)) { |
| 174 |
@file_put_contents($indexFile,'<html><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/">Autoptimize</a></body></html>'); |
| 175 |
} |
| 176 |
/** write .htaccess here to overrule wp_super_cache */ |
| 177 |
$htAccess=AUTOPTIMIZE_CACHE_DIR.'/.htaccess'; |
| 178 |
if(!is_file($htAccess)) { |
| 179 |
if (is_multisite() || AUTOPTIMIZE_CACHE_NOGZIP == false) { |
| 180 |
|
| 181 |
@file_put_contents($htAccess,'<IfModule mod_headers.c> |
| 182 |
Header set Vary "Accept-Encoding" |
| 183 |
Header set Cache-Control "max-age=10672000, must-revalidate" |
| 184 |
</IfModule> |
| 185 |
<IfModule mod_expires.c> |
| 186 |
ExpiresActive On |
| 187 |
ExpiresByType text/css A30672000 |
| 188 |
ExpiresByType text/javascript A30672000 |
| 189 |
ExpiresByType application/javascript A30672000 |
| 190 |
</IfModule> |
| 191 |
<IfModule mod_deflate.c> |
| 192 |
<FilesMatch "\.(js|css)$"> |
| 193 |
SetOutputFilter DEFLATE |
| 194 |
</FilesMatch> |
| 195 |
</IfModule> |
| 196 |
<IfModule mod_authz_core.c> |
| 197 |
<Files *.php> |
| 198 |
Require all granted |
| 199 |
</Files> |
| 200 |
</IfModule> |
| 201 |
<IfModule !mod_authz_core.c> |
| 202 |
<Files *.php> |
| 203 |
Order allow,deny |
| 204 |
Allow from all |
| 205 |
</Files> |
| 206 |
</IfModule>'); |
| 207 |
} else { |
| 208 |
@file_put_contents($htAccess,'<IfModule mod_headers.c> |
| 209 |
Header set Vary "Accept-Encoding" |
| 210 |
Header set Cache-Control "max-age=10672000, must-revalidate" |
| 211 |
</IfModule> |
| 212 |
<IfModule mod_expires.c> |
| 213 |
ExpiresActive On |
| 214 |
ExpiresByType text/css A30672000 |
| 215 |
ExpiresByType text/javascript A30672000 |
| 216 |
ExpiresByType application/javascript A30672000 |
| 217 |
</IfModule> |
| 218 |
<IfModule mod_deflate.c> |
| 219 |
<FilesMatch "\.(js|css)$"> |
| 220 |
SetOutputFilter DEFLATE |
| 221 |
</FilesMatch> |
| 222 |
</IfModule> |
| 223 |
<IfModule mod_authz_core.c> |
| 224 |
<Files *.php> |
| 225 |
Require all denied |
| 226 |
</Files> |
| 227 |
</IfModule> |
| 228 |
<IfModule !mod_authz_core.c> |
| 229 |
<Files *.php> |
| 230 |
Order deny,allow |
| 231 |
Deny from all |
| 232 |
</Files> |
| 233 |
</IfModule>'); |
| 234 |
} |
| 235 |
} |
| 236 |
// All OK |
| 237 |
return true; |
| 238 |
} |
| 239 |
|
| 240 |
static function checkCacheDir($dir) { |
| 241 |
// Check and create if not exists |
| 242 |
if(!file_exists($dir)) { |
| 243 |
@mkdir($dir,0775,true); |
| 244 |
if(!file_exists($dir)) { |
| 245 |
return false; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// check if we can now write |
| 250 |
if(!is_writable($dir)) { |
| 251 |
return false; |
| 252 |
} |
| 253 |
|
| 254 |
// and write index.html here to avoid prying eyes |
| 255 |
$indexFile=$dir.'/index.html'; |
| 256 |
if(!is_file($indexFile)) { |
| 257 |
@file_put_contents($indexFile,'<html><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/">Autoptimize</a></body></html>'); |
| 258 |
} |
| 259 |
|
| 260 |
return true; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
|