PluginProbe
Autoptimize / 1.7.1
Autoptimize v1.7.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.7.1, at classes/autoptimizeCache.php

193 lines 5.1 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 // Do we need to clean any caching plugins cache-files?
88 if(function_exists('wp_cache_clear_cache')) {
89 wp_cache_clear_cache(); // wp super cache
90 } else if ( function_exists('w3tc_pgcache_flush') ) {
91 w3tc_pgcache_flush(); //w3 total cache
92 } else if ( function_exists('hyper_cache_invalidate') ) {
93 hyper_cache_invalidate(); // hypercache
94 } else if ( function_exists('wp_fast_cache_bulk_delete_all') ) {
95 wp_fast_cache_bulk_delete_all(); // wp fast cache
96 } else if (class_exists("WpFastestCache")) {
97 $wpfc = new WpFastestCache(); // wp fastest cache
98 $wpfc -> deleteCache();
99 } else if ( class_exists("c_ws_plugin__qcache_purging_routines") ) {
100 c_ws_plugin__qcache_purging_routines::purge_cache_dir(); // quick cache
101 } else if(file_exists(WP_CONTENT_DIR.'/wp-cache-config.php') && function_exists('prune_super_cache')){
102 // fallback for WP-Super-Cache
103 global $cache_path;
104 prune_super_cache($cache_path.'supercache/',true);
105 prune_super_cache($cache_path,true);
106 }
107
108 return true;
109 }
110
111 static function stats()
112 {
113 //Cache not available :(
114 if(!autoptimizeCache::cacheavail())
115 return 0;
116
117 //Count cached info
118 $count = 0;
119 $scan = scandir(AUTOPTIMIZE_CACHE_DIR);
120 foreach($scan as $file)
121 {
122 if(!in_array($file,array('.','..')) && strpos($file,'autoptimize') !== false)
123 {
124 if(is_file(AUTOPTIMIZE_CACHE_DIR.$file))
125 {
126 if(AUTOPTIMIZE_CACHE_NOGZIP && (strpos($file,'.js') !== false || strpos($file,'.css') !== false))
127 {
128 $count++;
129 }elseif(!AUTOPTIMIZE_CACHE_NOGZIP && strpos($file,'.none') !== false){
130 $count++;
131 }/*else{
132 //Tricky one... it was a dir or a gzip/deflate file
133 }*/
134 }
135 }
136 }
137
138 //Tell the number of instances
139 return $count;
140 }
141
142 static function cacheavail()
143 {
144 if(!defined('AUTOPTIMIZE_CACHE_DIR'))
145 {
146 //We didn't set a cache
147 return false;
148 }
149
150 //Check for existence
151 if(!file_exists(AUTOPTIMIZE_CACHE_DIR))
152 {
153 @mkdir(AUTOPTIMIZE_CACHE_DIR,0775,true);
154 if(!file_exists(AUTOPTIMIZE_CACHE_DIR))
155 {
156 //Where should we cache?
157 return false;
158 }
159 }
160
161 if(!is_writable(AUTOPTIMIZE_CACHE_DIR))
162 {
163 //How are we supposed to write?
164 return false;
165 }
166
167 /** write index.html here to avoid prying eyes */
168 $indexFile=AUTOPTIMIZE_CACHE_DIR.'/index.html';
169 if(!is_file($indexFile)) {
170 @file_put_contents($indexFile,'<html><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/">Autoptimize</a></body></html>');
171 }
172
173 /** write .htaccess here to overrule wp_super_cache */
174 $htAccess=AUTOPTIMIZE_CACHE_DIR.'/.htaccess';
175 if(!is_file($htAccess)) {
176 @file_put_contents($htAccess,
177 '<IfModule mod_headers.c>
178 Header set Vary "Accept-Encoding"
179 Header set Cache-Control "max-age=10672000, must-revalidate"
180 </IfModule>
181 <IfModule mod_expires.c>
182 ExpiresActive On
183 ExpiresByType text/css A30672000
184 ExpiresByType text/javascript A30672000
185 ExpiresByType application/javascript A30672000
186 </IfModule>');
187 }
188
189 //All OK
190 return true;
191 }
192 }
193