PluginProbe
Autoptimize / 1.8.1
Autoptimize v1.8.1
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
autoptimize / classes / autoptimizeCache.php

autoptimizeCache.php in Autoptimize 1.8.1, at classes/autoptimizeCache.php

222 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
3
4 class autoptimizeCache
5 {
6 private $filename;
7 private $mime;
8 private $cachedir;
9 private $delayed;
10
11 public function __construct($md5,$ext='php')
12 {
13 $this->cachedir = AUTOPTIMIZE_CACHE_DIR;
14 $this->delayed = AUTOPTIMIZE_CACHE_DELAY;
15 $this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP;
16 if($this->nogzip == false)
17 $this->filename = 'autoptimize_'.$md5.'.php';
18 else
19 $this->filename = 'autoptimize_'.$md5.'.'.$ext;
20 }
21
22 public function check()
23 {
24 if(!file_exists($this->cachedir.$this->filename))
25 {
26 //No cached file, sorry
27 return false;
28 }
29 //Cache exists!
30 return true;
31 }
32
33 public function retrieve()
34 {
35 if($this->check())
36 {
37 if($this->nogzip == false)
38 return file_get_contents($this->cachedir.$this->filename.'.none');
39 else
40 return file_get_contents($this->cachedir.$this->filename);
41 }
42 return false;
43 }
44
45 public function cache($code,$mime)
46 {
47 if($this->nogzip == false)
48 {
49 $file = ($this->delayed ? 'delayed.php' : 'default.php');
50 $phpcode = file_get_contents(WP_PLUGIN_DIR.'/autoptimize/config/'.$file);
51 $phpcode = str_replace(array('%%CONTENT%%','exit;'),array($mime,''),$phpcode);
52 file_put_contents($this->cachedir.$this->filename,$phpcode);
53 file_put_contents($this->cachedir.$this->filename.'.none',$code);
54 if(!$this->delayed)
55 {
56 //Compress now!
57 file_put_contents($this->cachedir.$this->filename.'.deflate',gzencode($code,9,FORCE_DEFLATE));
58 file_put_contents($this->cachedir.$this->filename.'.gzip',gzencode($code,9,FORCE_GZIP));
59 }
60 }else{
61 //Write code to cache without doing anything else
62 file_put_contents($this->cachedir.$this->filename,$code);
63 }
64 }
65
66 public function getname()
67 {
68 return $this->filename;
69 }
70
71 static function clearall()
72 {
73 //Cache not available :(
74 if(!autoptimizeCache::cacheavail())
75 return false;
76
77 //Clean the cachedir
78 $scan = scandir(AUTOPTIMIZE_CACHE_DIR);
79 foreach($scan as $file)
80 {
81 if(!in_array($file,array('.','..')) && strpos($file,'autoptimize') !== false && is_file(AUTOPTIMIZE_CACHE_DIR.$file))
82 {
83 @unlink(AUTOPTIMIZE_CACHE_DIR.$file);
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 wp_cache_clear_cache(); // wp super cache
92 } else if ( function_exists('w3tc_pgcache_flush') ) {
93 w3tc_pgcache_flush(); //w3 total cache
94 } else if ( function_exists('hyper_cache_invalidate') ) {
95 hyper_cache_invalidate(); // hypercache
96 } else if ( function_exists('wp_fast_cache_bulk_delete_all') ) {
97 wp_fast_cache_bulk_delete_all(); // wp fast cache
98 } else if (class_exists("WpFastestCache")) {
99 $wpfc = new WpFastestCache(); // wp fastest cache
100 $wpfc -> deleteCache();
101 } else if ( class_exists("c_ws_plugin__qcache_purging_routines") ) {
102 c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache
103 } else if(file_exists(WP_CONTENT_DIR.'/wp-cache-config.php') && function_exists('prune_super_cache')){
104 // fallback for WP-Super-Cache
105 global $cache_path;
106 prune_super_cache($cache_path.'supercache/',true);
107 prune_super_cache($cache_path,true);
108 }
109
110 return true;
111 }
112
113 static function stats()
114 {
115 //Cache not available :(
116 if(!autoptimizeCache::cacheavail())
117 return 0;
118
119 //Count cached info
120 $count = 0;
121 $scan = scandir(AUTOPTIMIZE_CACHE_DIR);
122 foreach($scan as $file)
123 {
124 if(!in_array($file,array('.','..')) && strpos($file,'autoptimize') !== false)
125 {
126 if(is_file(AUTOPTIMIZE_CACHE_DIR.$file))
127 {
128 if(AUTOPTIMIZE_CACHE_NOGZIP && (strpos($file,'.js') !== false || strpos($file,'.css') !== false))
129 {
130 $count++;
131 }elseif(!AUTOPTIMIZE_CACHE_NOGZIP && strpos($file,'.none') !== false){
132 $count++;
133 }/*else{
134 //Tricky one... it was a dir or a gzip/deflate file
135 }*/
136 }
137 }
138 }
139
140 //Tell the number of instances
141 return $count;
142 }
143
144 static function cacheavail()
145 {
146 if(!defined('AUTOPTIMIZE_CACHE_DIR'))
147 {
148 //We didn't set a cache
149 return false;
150 }
151
152 //Check for existence
153 if(!file_exists(AUTOPTIMIZE_CACHE_DIR))
154 {
155 @mkdir(AUTOPTIMIZE_CACHE_DIR,0775,true);
156 if(!file_exists(AUTOPTIMIZE_CACHE_DIR))
157 {
158 //Where should we cache?
159 return false;
160 }
161 }
162
163 if(!is_writable(AUTOPTIMIZE_CACHE_DIR))
164 {
165 //How are we supposed to write?
166 return false;
167 }
168
169 /** write index.html here to avoid prying eyes */
170 $indexFile=AUTOPTIMIZE_CACHE_DIR.'/index.html';
171 if(!is_file($indexFile)) {
172 @file_put_contents($indexFile,'<html><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/">Autoptimize</a></body></html>');
173 }
174
175 /** write .htaccess here to overrule wp_super_cache */
176 $htAccess=AUTOPTIMIZE_CACHE_DIR.'/.htaccess';
177 if(!is_file($htAccess)) {
178 if (is_multisite() || AUTOPTIMIZE_CACHE_NOGZIP == false) {
179 @file_put_contents($htAccess,'<IfModule mod_headers.c>
180 Header set Vary "Accept-Encoding"
181 Header set Cache-Control "max-age=10672000, must-revalidate"
182 </IfModule>
183 <IfModule mod_expires.c>
184 ExpiresActive On
185 ExpiresByType text/css A30672000
186 ExpiresByType text/javascript A30672000
187 ExpiresByType application/javascript A30672000
188 </IfModule>
189 <IfModule mod_deflate.c>
190 <FilesMatch "\.(js|css)$">
191 SetOutputFilter DEFLATE
192 </FilesMatch>
193 </IfModule>
194 <Files *.php>
195 Allow from all
196 </Files>');
197 } else {
198 @file_put_contents($htAccess,'<IfModule mod_headers.c>
199 Header set Vary "Accept-Encoding"
200 Header set Cache-Control "max-age=10672000, must-revalidate"
201 </IfModule>
202 <IfModule mod_expires.c>
203 ExpiresActive On
204 ExpiresByType text/css A30672000
205 ExpiresByType text/javascript A30672000
206 ExpiresByType application/javascript A30672000
207 </IfModule>
208 <IfModule mod_deflate.c>
209 <FilesMatch "\.(js|css)$">
210 SetOutputFilter DEFLATE
211 </FilesMatch>
212 </IfModule>
213 <Files *.php>
214 Deny from all
215 </Files>');
216 }
217 }
218 //All OK
219 return true;
220 }
221 }
222