Version Description
- Support CSS media
- Fix an issue in the IE Hacks preservation mechanism
- Fix an issue with some urls getting broken in CSS
Download this release
Release Info
Developer | turl |
Plugin | Autoptimize |
Version | 0.2 |
Comparing to | |
See all releases |
Code changes from version 0.1 to 0.2
- autoptimize.php +1 -1
- classes/autoptimizeStyles.php +110 -30
- readme.txt +7 -2
autoptimize.php
CHANGED
@@ -3,7 +3,7 @@
|
|
3 |
Plugin Name: Autoptimize
|
4 |
Plugin URI: http://www.turleando.com.ar/
|
5 |
Description: Optimiza tu sitio web, uniendo el CSS y JavaScript, y comprimiéndolo.
|
6 |
-
Version: 0.
|
7 |
Author: Emilio López
|
8 |
Author URI: http://www.turleando.com.ar/
|
9 |
Released under the GNU General Public License (GPL)
|
3 |
Plugin Name: Autoptimize
|
4 |
Plugin URI: http://www.turleando.com.ar/
|
5 |
Description: Optimiza tu sitio web, uniendo el CSS y JavaScript, y comprimiéndolo.
|
6 |
+
Version: 0.2
|
7 |
Author: Emilio López
|
8 |
Author URI: http://www.turleando.com.ar/
|
9 |
Released under the GNU General Public License (GPL)
|
classes/autoptimizeStyles.php
CHANGED
@@ -1,16 +1,24 @@
|
|
1 |
<?php
|
2 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
3 |
class autoptimizeStyles extends autoptimizeBase
|
4 |
{
|
5 |
private $css = array();
|
6 |
-
private $csscode =
|
7 |
-
private $url =
|
8 |
|
9 |
//Reads the page and collects style tags
|
10 |
public function read()
|
11 |
{
|
12 |
//Save IE hacks
|
13 |
-
$this->content = preg_replace('#(<\!--\[if
|
14 |
'\'%%IEHACK%%\'.base64_encode("$1").\'%%IEHACK%%\'',$this->content);
|
15 |
|
16 |
//Get <style> and <link>
|
@@ -23,10 +31,24 @@ class autoptimizeStyles extends autoptimizeBase
|
|
23 |
//<link>
|
24 |
$url = current(explode('?',$source[2],2));
|
25 |
$path = $this->getpath($url);
|
|
|
|
|
26 |
if($path !==false && preg_match('#\.css$#',$path))
|
27 |
{
|
28 |
//Good link
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
}else{
|
31 |
//Link is dynamic (.php etc)
|
32 |
$tag = '';
|
@@ -34,8 +56,8 @@ class autoptimizeStyles extends autoptimizeBase
|
|
34 |
}else{
|
35 |
//<style>
|
36 |
preg_match('#<style.*>(.*)</style>#Usmi',$tag,$code);
|
37 |
-
$code = preg_replace('
|
38 |
-
$this->css[] = 'INLINE;'.$code;
|
39 |
}
|
40 |
|
41 |
//Remove the original style tag
|
@@ -52,47 +74,95 @@ class autoptimizeStyles extends autoptimizeBase
|
|
52 |
//Joins and optimizes CSS
|
53 |
public function minify()
|
54 |
{
|
55 |
-
foreach($this->css as $
|
56 |
{
|
|
|
|
|
57 |
if(preg_match('#^INLINE;#',$css))
|
58 |
{
|
59 |
//<style>
|
60 |
$css = preg_replace('#^INLINE;#','',$css);
|
61 |
$css = $this->fixurls(ABSPATH.'/index.php',$css);
|
62 |
-
$this->csscode
|
|
|
|
|
63 |
}else{
|
64 |
//<link>
|
65 |
if($css !== false && file_exists($css) && is_readable($css))
|
66 |
{
|
67 |
$css = $this->fixurls($css,file_get_contents($css));
|
68 |
-
$
|
|
|
|
|
|
|
|
|
|
|
69 |
}/*else{
|
70 |
//Couldn't read CSS. Maybe getpath isn't working?
|
71 |
}*/
|
72 |
}
|
73 |
}
|
74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
75 |
//Manage @imports, while is for recursive import management
|
76 |
-
|
77 |
{
|
78 |
-
|
79 |
{
|
80 |
-
$
|
81 |
-
$path = $this->getpath($url);
|
82 |
-
if(file_exists($path) && is_readable($path))
|
83 |
{
|
84 |
-
$
|
85 |
-
$
|
86 |
-
|
87 |
-
|
88 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
89 |
}
|
90 |
}
|
|
|
91 |
|
92 |
//$this->csscode has all the uncompressed code now.
|
93 |
if(class_exists('Minify_CSS_Compressor'))
|
94 |
{
|
95 |
-
$this->csscode
|
|
|
|
|
|
|
|
|
96 |
return true;
|
97 |
}
|
98 |
|
@@ -102,14 +172,17 @@ class autoptimizeStyles extends autoptimizeBase
|
|
102 |
//Caches the CSS in uncompressed, deflated and gzipped form.
|
103 |
public function cache()
|
104 |
{
|
105 |
-
|
106 |
-
$cache = new autopimizeCache(WP_PLUGIN_DIR.'/autoptimize/cache/',$md5);
|
107 |
-
if(!$cache->check())
|
108 |
{
|
109 |
-
|
110 |
-
$cache
|
|
|
|
|
|
|
|
|
|
|
|
|
111 |
}
|
112 |
-
$this->url = WP_PLUGIN_URL.'/autoptimize/cache/'.$cache->getname();
|
113 |
}
|
114 |
|
115 |
//Returns the content
|
@@ -117,7 +190,10 @@ class autoptimizeStyles extends autoptimizeBase
|
|
117 |
{
|
118 |
//Restore IE hacks
|
119 |
$this->content = preg_replace('#%%IEHACK%%(.*)%%IEHACK%%#Usie','base64_decode("$1")',$this->content);
|
120 |
-
$this->
|
|
|
|
|
|
|
121 |
return $this->content;
|
122 |
}
|
123 |
|
@@ -128,10 +204,11 @@ class autoptimizeStyles extends autoptimizeBase
|
|
128 |
|
129 |
if(preg_match_all('#url\((.*)\)#Usi',$code,$matches))
|
130 |
{
|
|
|
131 |
foreach($matches[1] as $url)
|
132 |
{
|
133 |
//Remove quotes
|
134 |
-
$url = preg_replace('
|
135 |
if(substr($url,0,1)=='/' || preg_match('#^(https?|ftp)://#i',$url))
|
136 |
{
|
137 |
//URL is absolute
|
@@ -139,9 +216,12 @@ class autoptimizeStyles extends autoptimizeBase
|
|
139 |
}else{
|
140 |
//relative URL. Let's fix it!
|
141 |
$newurl = get_settings('home').str_replace('//','/',$dir.'/'.$url); //http://yourblog.com/wp-content/../image.png
|
142 |
-
$
|
143 |
}
|
144 |
}
|
|
|
|
|
|
|
145 |
}
|
146 |
|
147 |
return $code;
|
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>
|
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 = '';
|
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
|
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 |
|
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
|
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 |
|
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
|
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;
|
readme.txt
CHANGED
@@ -4,9 +4,9 @@ Donate link: http://www.turleando.com.ar/autoptimize/
|
|
4 |
Tags: css, html, javascript, js, optimize, speed, cache
|
5 |
Requires at least: 2.6
|
6 |
Tested up to: 2.8
|
7 |
-
Stable tag: 0.
|
8 |
|
9 |
-
Autoptimize is a Wordpress plugin that
|
10 |
|
11 |
== Description ==
|
12 |
|
@@ -28,5 +28,10 @@ It concatenates all scripts and styles, minifies and compresses them, adds expir
|
|
28 |
|
29 |
== Changelog ==
|
30 |
|
|
|
|
|
|
|
|
|
|
|
31 |
= 0.1 =
|
32 |
* First released version.
|
4 |
Tags: css, html, javascript, js, optimize, speed, cache
|
5 |
Requires at least: 2.6
|
6 |
Tested up to: 2.8
|
7 |
+
Stable tag: 0.2
|
8 |
|
9 |
+
Autoptimize is a Wordpress plugin that speeds up your website, and helps you save bandwidth.
|
10 |
|
11 |
== Description ==
|
12 |
|
28 |
|
29 |
== Changelog ==
|
30 |
|
31 |
+
= 0.2 =
|
32 |
+
* Support CSS media
|
33 |
+
* Fix an issue in the IE Hacks preservation mechanism
|
34 |
+
* Fix an issue with some urls getting broken in CSS
|
35 |
+
|
36 |
= 0.1 =
|
37 |
* First released version.
|