Completed
Push — master ( e98509...ab381e )
by frank
03:01
created
classes/autoptimizeCache.php 1 patch
Spacing   +61 added lines, -61 removed lines patch added patch discarded remove patch
@@ -1,5 +1,5 @@  discard block
 block discarded – undo
1 1
 <?php
2
-if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
2
+if (!defined('ABSPATH')) exit; // Exit if accessed directly
3 3
 
4 4
 class autoptimizeCache {
5 5
     private $filename;
@@ -7,14 +7,14 @@  discard block
 block discarded – undo
7 7
     private $cachedir;
8 8
     private $delayed;
9 9
     
10
-    public function __construct($md5,$ext='php') {
10
+    public function __construct($md5, $ext = 'php') {
11 11
         $this->cachedir = AUTOPTIMIZE_CACHE_DIR;
12 12
         $this->delayed = AUTOPTIMIZE_CACHE_DELAY;
13 13
         $this->nogzip = AUTOPTIMIZE_CACHE_NOGZIP;
14
-        if($this->nogzip == false) {
14
+        if ($this->nogzip == false) {
15 15
             $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.php';
16 16
         } else {
17
-            if (in_array($ext, array("js","css")))     {
17
+            if (in_array($ext, array("js", "css"))) {
18 18
                 $this->filename = $ext.'/'.AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.'.$ext;
19 19
             } else {
20 20
                 $this->filename = AUTOPTIMIZE_CACHEFILE_PREFIX.$md5.'.'.$ext;
@@ -23,7 +23,7 @@  discard block
 block discarded – undo
23 23
     }
24 24
     
25 25
     public function check() {
26
-        if(!file_exists($this->cachedir.$this->filename)) {
26
+        if (!file_exists($this->cachedir.$this->filename)) {
27 27
             // No cached file, sorry
28 28
             return false;
29 29
         }
@@ -32,8 +32,8 @@  discard block
 block discarded – undo
32 32
     }
33 33
     
34 34
     public function retrieve() {
35
-        if($this->check()) {
36
-            if($this->nogzip == false) {
35
+        if ($this->check()) {
36
+            if ($this->nogzip == false) {
37 37
                 return file_get_contents($this->cachedir.$this->filename.'.none');
38 38
             } else {
39 39
                 return file_get_contents($this->cachedir.$this->filename);
@@ -42,48 +42,48 @@  discard block
 block discarded – undo
42 42
         return false;
43 43
     }
44 44
     
45
-    public function cache($code,$mime) {
46
-        if($this->nogzip == false) {
45
+    public function cache($code, $mime) {
46
+        if ($this->nogzip == false) {
47 47
             $file = ($this->delayed ? 'delayed.php' : 'default.php');
48 48
             $phpcode = file_get_contents(AUTOPTIMIZE_PLUGIN_DIR.'/config/'.$file);
49
-            $phpcode = str_replace(array('%%CONTENT%%','exit;'),array($mime,''),$phpcode);
50
-            file_put_contents($this->cachedir.$this->filename,$phpcode, LOCK_EX);
51
-            file_put_contents($this->cachedir.$this->filename.'.none',$code, LOCK_EX);
52
-            if(!$this->delayed) {
49
+            $phpcode = str_replace(array('%%CONTENT%%', 'exit;'), array($mime, ''), $phpcode);
50
+            file_put_contents($this->cachedir.$this->filename, $phpcode, LOCK_EX);
51
+            file_put_contents($this->cachedir.$this->filename.'.none', $code, LOCK_EX);
52
+            if (!$this->delayed) {
53 53
                 // Compress now!
54
-                file_put_contents($this->cachedir.$this->filename.'.deflate',gzencode($code,9,FORCE_DEFLATE), LOCK_EX);
55
-                file_put_contents($this->cachedir.$this->filename.'.gzip',gzencode($code,9,FORCE_GZIP), LOCK_EX);
54
+                file_put_contents($this->cachedir.$this->filename.'.deflate', gzencode($code, 9, FORCE_DEFLATE), LOCK_EX);
55
+                file_put_contents($this->cachedir.$this->filename.'.gzip', gzencode($code, 9, FORCE_GZIP), LOCK_EX);
56 56
             }
57 57
         } else {
58 58
             // Write code to cache without doing anything else
59
-            file_put_contents($this->cachedir.$this->filename,$code, LOCK_EX);
59
+            file_put_contents($this->cachedir.$this->filename, $code, LOCK_EX);
60 60
             if (apply_filters('autoptimize_filter_cache_create_static_gzip', false)) {
61 61
                 // Create an additional cached gzip file
62
-                file_put_contents($this->cachedir.$this->filename.'.gz', gzencode($code,9,FORCE_GZIP), LOCK_EX);
62
+                file_put_contents($this->cachedir.$this->filename.'.gz', gzencode($code, 9, FORCE_GZIP), LOCK_EX);
63 63
             }
64 64
         }
65 65
     }
66 66
     
67 67
     public function getname() {
68
-        apply_filters('autoptimize_filter_cache_getname',AUTOPTIMIZE_CACHE_URL.$this->filename);
68
+        apply_filters('autoptimize_filter_cache_getname', AUTOPTIMIZE_CACHE_URL.$this->filename);
69 69
         return $this->filename;
70 70
     }
71 71
     
72 72
     static function clearall() {
73
-        if(!autoptimizeCache::cacheavail()) {
73
+        if (!autoptimizeCache::cacheavail()) {
74 74
             return false;
75 75
         }
76 76
     
77 77
         // scan the cachedirs        
78
-        foreach (array("","js","css") as $scandirName) {
78
+        foreach (array("", "js", "css") as $scandirName) {
79 79
             $scan[$scandirName] = scandir(AUTOPTIMIZE_CACHE_DIR.$scandirName);
80 80
         }
81 81
         
82 82
         // clear the cachedirs
83 83
         foreach ($scan as $scandirName=>$scanneddir) {
84
-            $thisAoCacheDir=rtrim(AUTOPTIMIZE_CACHE_DIR.$scandirName,"/")."/";
85
-            foreach($scanneddir as $file) {
86
-                if(!in_array($file,array('.','..')) && strpos($file,AUTOPTIMIZE_CACHEFILE_PREFIX) !== false && is_file($thisAoCacheDir.$file)) {
84
+            $thisAoCacheDir = rtrim(AUTOPTIMIZE_CACHE_DIR.$scandirName, "/")."/";
85
+            foreach ($scanneddir as $file) {
86
+                if (!in_array($file, array('.', '..')) && strpos($file, AUTOPTIMIZE_CACHEFILE_PREFIX) !== false && is_file($thisAoCacheDir.$file)) {
87 87
                     @unlink($thisAoCacheDir.$file);
88 88
                 }
89 89
             }
@@ -98,16 +98,16 @@  discard block
 block discarded – undo
98 98
                 do_action("autoptimize_action_cachepurged");
99 99
             }
100 100
         }
101
-        add_action("shutdown","autoptimize_do_cachepurged_action",11);
101
+        add_action("shutdown", "autoptimize_do_cachepurged_action", 11);
102 102
         
103 103
         // try to purge caching plugins cache-files?
104 104
         include_once(AUTOPTIMIZE_PLUGIN_DIR.'classlesses/autoptimizePageCacheFlush.php');
105
-        add_action("autoptimize_action_cachepurged","autoptimize_flush_pagecache",10,0);
105
+        add_action("autoptimize_action_cachepurged", "autoptimize_flush_pagecache", 10, 0);
106 106
 
107 107
         // warm cache (part of speedupper)?
108
-        if ( apply_filters('autoptimize_filter_speedupper', true) && get_transient('autoptimize_warmcache_buster') == false ) {
108
+        if (apply_filters('autoptimize_filter_speedupper', true) && get_transient('autoptimize_warmcache_buster') == false) {
109 109
             set_transient('autoptimize_warmcache_buster', 'Mamsie & Liessie zehhe: ZWIJH!', HOUR_IN_SECONDS);
110
-            $warmCacheUrl = site_url()."/?ao_speedup_cachebuster=".rand(1,100000);
110
+            $warmCacheUrl = site_url()."/?ao_speedup_cachebuster=".rand(1, 100000);
111 111
             $warmCache = @wp_remote_get($warmCacheUrl);
112 112
             unset($warmCache);
113 113
         }
@@ -115,12 +115,12 @@  discard block
 block discarded – undo
115 115
         return true;
116 116
     }
117 117
 
118
-    static function stats()    {
119
-        $AOstats=get_transient("autoptimize_stats");
118
+    static function stats() {
119
+        $AOstats = get_transient("autoptimize_stats");
120 120
 
121 121
         if (empty($AOstats)) {
122 122
             // Cache not available :(
123
-            if(!autoptimizeCache::cacheavail()) {
123
+            if (!autoptimizeCache::cacheavail()) {
124 124
                 return 0;
125 125
             }
126 126
             
@@ -129,28 +129,28 @@  discard block
 block discarded – undo
129 129
             $size = 0;
130 130
             
131 131
             // scan the cachedirs        
132
-            foreach (array("","js","css") as $scandirName) {
132
+            foreach (array("", "js", "css") as $scandirName) {
133 133
                 $scan[$scandirName] = scandir(AUTOPTIMIZE_CACHE_DIR.$scandirName);
134 134
             }
135 135
             
136 136
             foreach ($scan as $scandirName=>$scanneddir) {
137
-                $thisAoCacheDir=rtrim(AUTOPTIMIZE_CACHE_DIR.$scandirName,"/")."/";
138
-                foreach($scanneddir as $file) {
139
-                    if(!in_array($file,array('.','..')) && strpos($file,AUTOPTIMIZE_CACHEFILE_PREFIX) !== false) {
140
-                        if(is_file($thisAoCacheDir.$file)) {
141
-                            if(AUTOPTIMIZE_CACHE_NOGZIP && (strpos($file,'.js') !== false || strpos($file,'.css') !== false || strpos($file,'.img') !== false || strpos($file,'.txt') !== false )) {
137
+                $thisAoCacheDir = rtrim(AUTOPTIMIZE_CACHE_DIR.$scandirName, "/")."/";
138
+                foreach ($scanneddir as $file) {
139
+                    if (!in_array($file, array('.', '..')) && strpos($file, AUTOPTIMIZE_CACHEFILE_PREFIX) !== false) {
140
+                        if (is_file($thisAoCacheDir.$file)) {
141
+                            if (AUTOPTIMIZE_CACHE_NOGZIP && (strpos($file, '.js') !== false || strpos($file, '.css') !== false || strpos($file, '.img') !== false || strpos($file, '.txt') !== false)) {
142 142
                                 $count++;
143
-                            } elseif(!AUTOPTIMIZE_CACHE_NOGZIP && strpos($file,'.none') !== false) {
143
+                            } elseif (!AUTOPTIMIZE_CACHE_NOGZIP && strpos($file, '.none') !== false) {
144 144
                                 $count++;
145 145
                             }
146
-                            $size+=filesize($thisAoCacheDir.$file);
146
+                            $size += filesize($thisAoCacheDir.$file);
147 147
                         }
148 148
                     }
149 149
                 }
150 150
             }
151
-            $AOstats=array($count,$size,time());
152
-            if ($count>100) {
153
-                set_transient( "autoptimize_stats", $AOstats, apply_filters( 'autoptimize_filter_cache_statsexpiry', HOUR_IN_SECONDS ) );
151
+            $AOstats = array($count, $size, time());
152
+            if ($count > 100) {
153
+                set_transient("autoptimize_stats", $AOstats, apply_filters('autoptimize_filter_cache_statsexpiry', HOUR_IN_SECONDS));
154 154
             }
155 155
         }
156 156
         // print the number of instances
@@ -158,36 +158,36 @@  discard block
 block discarded – undo
158 158
     }    
159 159
 
160 160
     static function cacheavail() {
161
-        if(!defined('AUTOPTIMIZE_CACHE_DIR')) {
161
+        if (!defined('AUTOPTIMIZE_CACHE_DIR')) {
162 162
             // We didn't set a cache
163 163
             return false;
164 164
         }
165 165
         
166
-        foreach (array("","js","css") as $checkDir) {
167
-            if(!autoptimizeCache::checkCacheDir(AUTOPTIMIZE_CACHE_DIR.$checkDir)) {
166
+        foreach (array("", "js", "css") as $checkDir) {
167
+            if (!autoptimizeCache::checkCacheDir(AUTOPTIMIZE_CACHE_DIR.$checkDir)) {
168 168
                 return false;
169 169
             }
170 170
         }
171 171
         
172 172
         /** write index.html here to avoid prying eyes */
173
-        $indexFile=AUTOPTIMIZE_CACHE_DIR.'/index.html';
174
-        if(!is_file($indexFile)) {
175
-            @file_put_contents($indexFile,'<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>');
173
+        $indexFile = AUTOPTIMIZE_CACHE_DIR.'/index.html';
174
+        if (!is_file($indexFile)) {
175
+            @file_put_contents($indexFile, '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>');
176 176
         }
177 177
 
178 178
         /** write .htaccess here to overrule wp_super_cache */
179
-        $htAccess=AUTOPTIMIZE_CACHE_DIR.'/.htaccess';
180
-        if(!is_file($htAccess)) {
179
+        $htAccess = AUTOPTIMIZE_CACHE_DIR.'/.htaccess';
180
+        if (!is_file($htAccess)) {
181 181
             /** 
182 182
              * create wp-content/AO_htaccess_tmpl with 
183 183
              * whatever htaccess rules you might need
184 184
              * if you want to override default AO htaccess
185 185
              */
186
-            $htaccess_tmpl=WP_CONTENT_DIR."/AO_htaccess_tmpl";
186
+            $htaccess_tmpl = WP_CONTENT_DIR."/AO_htaccess_tmpl";
187 187
             if (is_file($htaccess_tmpl)) { 
188
-                $htAccessContent=file_get_contents($htaccess_tmpl);
188
+                $htAccessContent = file_get_contents($htaccess_tmpl);
189 189
             } else if (is_multisite() || AUTOPTIMIZE_CACHE_NOGZIP == false) {
190
-                $htAccessContent='<IfModule mod_expires.c>
190
+                $htAccessContent = '<IfModule mod_expires.c>
191 191
         ExpiresActive On
192 192
         ExpiresByType text/css A30672000
193 193
         ExpiresByType text/javascript A30672000
@@ -213,7 +213,7 @@  discard block
 block discarded – undo
213 213
     </Files>
214 214
 </IfModule>';
215 215
             } else {
216
-                $htAccessContent='<IfModule mod_expires.c>
216
+                $htAccessContent = '<IfModule mod_expires.c>
217 217
         ExpiresActive On
218 218
         ExpiresByType text/css A30672000
219 219
         ExpiresByType text/javascript A30672000
@@ -239,7 +239,7 @@  discard block
 block discarded – undo
239 239
     </Files>
240 240
 </IfModule>';
241 241
             }
242
-            @file_put_contents($htAccess,$htAccessContent);
242
+            @file_put_contents($htAccess, $htAccessContent);
243 243
         }
244 244
 
245 245
         // All OK
@@ -248,22 +248,22 @@  discard block
 block discarded – undo
248 248
 
249 249
     static function checkCacheDir($dir) {
250 250
         // Check and create if not exists
251
-        if(!file_exists($dir))    {
252
-            @mkdir($dir,0775,true);
253
-            if(!file_exists($dir))    {
251
+        if (!file_exists($dir)) {
252
+            @mkdir($dir, 0775, true);
253
+            if (!file_exists($dir)) {
254 254
                 return false;
255 255
             }
256 256
         }
257 257
 
258 258
         // check if we can now write
259
-        if(!is_writable($dir))    {
259
+        if (!is_writable($dir)) {
260 260
             return false;
261 261
         }
262 262
 
263 263
         // and write index.html here to avoid prying eyes
264
-        $indexFile=$dir.'/index.html';
265
-        if(!is_file($indexFile)) {
266
-            @file_put_contents($indexFile,'<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>');
264
+        $indexFile = $dir.'/index.html';
265
+        if (!is_file($indexFile)) {
266
+            @file_put_contents($indexFile, '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>');
267 267
         }
268 268
         
269 269
         return true;
Please login to merge, or discard this patch.