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