PluginProbe
Autoptimize / 0.2
Autoptimize v0.2
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 / autoptimizeStyles.php

autoptimizeStyles.php in Autoptimize 0.2, at classes/autoptimizeStyles.php

230 lines 6.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 function dodebug($txt,$txt2=null){
3 static $text = '';
4 if($txt2 != null)
5 { $txt = $txt2;
6 }
7 file_put_contents(ABSPATH.'/debug.txt',$text.$txt);
8 $text .= $txt;
9 }
10 set_error_handler('dodebug');
11 class autoptimizeStyles extends autoptimizeBase
12 {
13 private $css = array();
14 private $csscode = array();
15 private $url = array();
16
17 //Reads the page and collects style tags
18 public function read()
19 {
20 //Save IE hacks
21 $this->content = preg_replace('#(<\!--\[if.*\]>.*<\!\[endif\]-->)#Usie',
22 '\'%%IEHACK%%\'.base64_encode("$1").\'%%IEHACK%%\'',$this->content);
23
24 //Get <style> and <link>
25 if(preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*text/css[^>]*>)#Usmi',$this->content,$matches))
26 {
27 foreach($matches[0] as $tag)
28 {
29 if(preg_match('#<link.*href=("|\')(.*)("|\')#Usmi',$tag,$source))
30 {
31 //<link>
32 $url = current(explode('?',$source[2],2));
33 $path = $this->getpath($url);
34 $media = array('all');
35
36 if($path !==false && preg_match('#\.css$#',$path))
37 {
38 //Good link
39 //Get the media
40 if(strpos($tag,'media=')!==false)
41 {
42 $medias = preg_replace('#^.*media=(?:"|\')(.*)(?:"|\').*$#U','$1',$tag);
43 $medias = explode(',',$medias);
44 $media = array();
45 foreach($medias as $elem)
46 {
47 $media[] = current(explode(' ',trim($elem),2));
48 }
49 }
50
51 $this->css[] = array($media,$path);
52 }else{
53 //Link is dynamic (.php etc)
54 $tag = '';
55 }
56 }else{
57 //<style>
58 preg_match('#<style.*>(.*)</style>#Usmi',$tag,$code);
59 $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm','$1',$code[1]);
60 $this->css[] = array('all','INLINE;'.$code);
61 }
62
63 //Remove the original style tag
64 $this->content = str_replace($tag,'',$this->content);
65 }
66
67 return true;
68 }
69
70 //No styles :(
71 return false;
72 }
73
74 //Joins and optimizes CSS
75 public function minify()
76 {
77 foreach($this->css as $group)
78 {
79 list($media,$css) = $group;
80 dodebug('corro con media css'.$media.' '.$css.'END');
81 if(preg_match('#^INLINE;#',$css))
82 {
83 //<style>
84 $css = preg_replace('#^INLINE;#','',$css);
85 $css = $this->fixurls(ABSPATH.'/index.php',$css);
86 if(!isset($this->csscode['all']))
87 $this->csscode['all'] = '';
88 $this->csscode['all'] .= "\n/*FILESTART*/".$css;
89 }else{
90 //<link>
91 if($css !== false && file_exists($css) && is_readable($css))
92 {
93 $css = $this->fixurls($css,file_get_contents($css));
94 foreach($media as $elem)
95 {
96 if(!isset($this->csscode[$elem]))
97 $this->csscode[$elem] = '';
98 $this->csscode[$elem] .= "\n/*FILESTART*/".$css;
99 }
100 }/*else{
101 //Couldn't read CSS. Maybe getpath isn't working?
102 }*/
103 }
104 }
105
106 //Check for duplicate code
107 $md5list = array();
108 $tmpcss = $this->csscode;
109 foreach($tmpcss as $media => $code)
110 {
111 $md5sum = md5($code);
112 $medianame = $media;
113 foreach($md5list as $med => $sum)
114 {
115 //If same code
116 if($sum === $md5sum)
117 {
118 //Add the merged code
119 $medianame = $med.', '.$media;
120 $this->csscode[$medianame] = $code;
121 $md5list[$medianame] = $md5list[$med];
122 unset($this->csscode[$med], $this->csscode[$media]);
123 unset($md5list[$med]);
124 }
125 }
126 $md5list[$medianame] = $md5sum;
127 }
128 unset($tmpcss);
129
130 //Manage @imports, while is for recursive import management
131 foreach($this->csscode as &$thiscss)
132 {
133 while(preg_match_all('#@import (?:url\()?.*(?:\)?).*?;#U',$thiscss,$matches))
134 {
135 foreach($matches[0] as $import)
136 {
137 $url = preg_replace('#^.*(?:url\()?(?:"|\')(.*)(?:"|\')(?:\))?.*$#','$1',$import);
138 $path = $this->getpath($url);
139 if(file_exists($path) && is_readable($path))
140 {
141 $code = $this->fixurls($path,file_get_contents($path));
142 $media = preg_replace('#^.*(?:\)|"|\')(.*)(?:\s|;).*$#','$1',$import);
143 $media = array_map('trim',explode(' ',$media));
144 if(empty($media))
145 {
146 $thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import,'#').'#Us',$code.'$1',$thiscss);
147 }/*else{
148 //media in @import - how should I handle these?
149 }*/
150 }/*else{
151 //getpath is not working?
152 }*/
153 }
154 }
155 }
156 unset($thiscss);
157
158 //$this->csscode has all the uncompressed code now.
159 if(class_exists('Minify_CSS_Compressor'))
160 {
161 foreach($this->csscode as &$code)
162 {
163 $code = trim(Minify_CSS_Compressor::process($code));
164 }
165 unset($code);
166 return true;
167 }
168
169 return false;
170 }
171
172 //Caches the CSS in uncompressed, deflated and gzipped form.
173 public function cache()
174 {
175 foreach($this->csscode as $media => $code)
176 {
177 $md5 = md5($code);
178 $cache = new autopimizeCache(WP_PLUGIN_DIR.'/autoptimize/cache/',$md5);
179 if(!$cache->check())
180 {
181 //Cache our code
182 $cache->cache($code,'text/css');
183 }
184 $this->url[$media] = WP_PLUGIN_URL.'/autoptimize/cache/'.$cache->getname();
185 }
186 }
187
188 //Returns the content
189 public function getcontent()
190 {
191 //Restore IE hacks
192 $this->content = preg_replace('#%%IEHACK%%(.*)%%IEHACK%%#Usie','base64_decode("$1")',$this->content);
193 foreach($this->url as $media => $url)
194 {
195 $this->content = str_replace('</head>','<link type="text/css" media="'.$media.'" href="'.$url.'" rel="stylesheet" /></head>',$this->content);
196 }
197 return $this->content;
198 }
199
200 private function fixurls($file,$code)
201 {
202 $file = str_replace(ABSPATH,'/',$file); //Sth like /wp-content/file.css
203 $dir = dirname($file); //Like /wp-content
204
205 if(preg_match_all('#url\((.*)\)#Usi',$code,$matches))
206 {
207 $replace = array();
208 foreach($matches[1] as $url)
209 {
210 //Remove quotes
211 $url = preg_replace('#^.*(?:"|\')(.*)(?:"|\').*$#','$1',$url);
212 if(substr($url,0,1)=='/' || preg_match('#^(https?|ftp)://#i',$url))
213 {
214 //URL is absolute
215 continue;
216 }else{
217 //relative URL. Let's fix it!
218 $newurl = get_settings('home').str_replace('//','/',$dir.'/'.$url); //http://yourblog.com/wp-content/../image.png
219 $replace[$url] = $newurl;
220 }
221 }
222
223 //Do the replacing here to avoid breaking URLs
224 $code = str_replace(array_keys($replace),array_values($replace),$code);
225 }
226
227 return $code;
228 }
229 }
230