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

169 lines 4.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 WP Super Cache's cache files?
88 if(function_exists('wp_cache_clear_cache'))
89 {
90 //Newer WP-Super-Cache
91 //See http://ocaoimh.ie/wp-super-cache-developers/
92 wp_cache_clear_cache();
93 }elseif(file_exists(WP_CONTENT_DIR.'/wp-cache-config.php') && function_exists('prune_super_cache')){
94 //Old WP-Super-Cache
95 global $cache_path;
96 prune_super_cache($cache_path.'supercache/',true);
97 prune_super_cache($cache_path,true);
98 }
99
100 return true;
101 }
102
103 static function stats()
104 {
105 //Cache not available :(
106 if(!autoptimizeCache::cacheavail())
107 return 0;
108
109 //Count cached info
110 $count = 0;
111 $scan = scandir(AUTOPTIMIZE_CACHE_DIR);
112 foreach($scan as $file)
113 {
114 if(!in_array($file,array('.','..')) && strpos($file,'autoptimize') !== false)
115 {
116 if(is_file(AUTOPTIMIZE_CACHE_DIR.$file))
117 {
118 if(AUTOPTIMIZE_CACHE_NOGZIP && (strpos($file,'.js') !== false || strpos($file,'.css') !== false))
119 {
120 $count++;
121 }elseif(!AUTOPTIMIZE_CACHE_NOGZIP && strpos($file,'.none') !== false){
122 $count++;
123 }/*else{
124 //Tricky one... it was a dir or a gzip/deflate file
125 }*/
126 }
127 }
128 }
129
130 //Tell the number of instances
131 return $count;
132 }
133
134 static function cacheavail()
135 {
136 if(!defined('AUTOPTIMIZE_CACHE_DIR'))
137 {
138 //We didn't set a cache
139 return false;
140 }
141
142 //Check for existence
143 if(!file_exists(AUTOPTIMIZE_CACHE_DIR))
144 {
145 @mkdir(AUTOPTIMIZE_CACHE_DIR,0775,true);
146 if(!file_exists(AUTOPTIMIZE_CACHE_DIR))
147 {
148 //Where should we cache?
149 return false;
150 }
151 }
152
153 if(!is_writable(AUTOPTIMIZE_CACHE_DIR))
154 {
155 //How are we supposed to write?
156 return false;
157 }
158
159 /** write index.html here to avoid prying eyes */
160 $indexFile=AUTOPTIMIZE_CACHE_DIR.'/index.html';
161 if(!is_file($indexFile)) {
162 @file_put_contents($indexFile,'<html><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/">Autoptimize</a></body></html>');
163 }
164
165 //All OK
166 return true;
167 }
168 }
169