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 / autoptimizeScripts.php

autoptimizeScripts.php in Autoptimize 0.4, at classes/autoptimizeScripts.php

163 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class autoptimizeScripts extends autoptimizeBase
4 {
5 private $scripts = array();
6 private $dontmove = array('document.write','google_ad_client','show_ads.js');
7 private $domove = array('gaJsHost');
8 private $jscode = '';
9 private $url = '';
10 private $move = array();
11
12 //Reads the page and collects script tags
13 public function read()
14 {
15 //Get script files
16 if(preg_match_all('#<script.*</script>#Usmi',$this->content,$matches))
17 {
18 foreach($matches[0] as $tag)
19 {
20 if(preg_match('#src=("|\')(.*)("|\')#Usmi',$tag,$source))
21 {
22 //External script
23 $url = current(explode('?',$source[2],2));
24 $path = $this->getpath($url);
25 if($path !==false && preg_match('#\.js$#',$path))
26 {
27 //Good script
28 $this->scripts[] = $path;
29 }else{
30 //External script (example: google analytics)
31 //OR Script is dynamic (.php etc)
32 if($this->ismovable($tag))
33 {
34 $this->move[] = $tag;
35 }else{
36 //We shouldn't touch this
37 $tag = '';
38 }
39 }
40 }else{
41 //Inline script
42 if($this->ismergeable($tag))
43 {
44 preg_match('#<script.*>(.*)</script>#Usmi',$tag,$code);
45 $code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm','$1',$code[1]);
46 $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/','',$code);
47 $this->scripts[] = 'INLINE;'.$code;
48 }else{
49 //Can we move this?
50 if($this->ismovable($tag))
51 {
52 $this->move[] = $tag;
53 }else{
54 //We shouldn't touch this
55 $tag = '';
56 }
57 }
58 }
59
60 //Remove the original script tag
61 $this->content = str_replace($tag,'',$this->content);
62 }
63
64 return true;
65 }
66
67 //No script files :(
68 return false;
69 }
70
71 //Joins and optimizes JS
72 public function minify()
73 {
74 foreach($this->scripts as $script)
75 {
76 if(preg_match('#^INLINE;#',$script))
77 {
78 //Inline script
79 $script = preg_replace('#^INLINE;#','',$script);
80 $this->jscode .= "\n".$script;
81 }else{
82 //External script
83 if($script !== false && file_exists($script) && is_readable($script))
84 {
85 $script = file_get_contents($script);
86 $this->jscode .= "\n".$script;
87 }/*else{
88 //Couldn't read JS. Maybe getpath isn't working?
89 }*/
90 }
91 }
92
93 //$this->jscode has all the uncompressed code now.
94 if(class_exists('JSMin'))
95 {
96 $this->jscode = trim(JSMin::minify($this->jscode));
97 return true;
98 }
99
100 return false;
101 }
102
103 //Caches the JS in uncompressed, deflated and gzipped form.
104 public function cache()
105 {
106 $md5 = md5($this->jscode);
107 $cache = new autopimizeCache(WP_PLUGIN_DIR.'/autoptimize/cache/',$md5);
108 if(!$cache->check())
109 {
110 //Cache our code
111 $cache->cache($this->jscode,'text/javascript');
112 }
113 $this->url = WP_PLUGIN_URL.'/autoptimize/cache/'.$cache->getname();
114 }
115
116 //Returns the content
117 public function getcontent()
118 {
119 $bodyreplacement = implode('',$this->move).'<script type="text/javascript" src="'.$this->url.'"></script></body>';
120 $this->content = str_replace('</body>',$bodyreplacement,$this->content);
121 return $this->content;
122 }
123
124 //Checks agains the whitelist
125 private function ismergeable($tag)
126 {
127 foreach($this->domove as $match)
128 {
129 if(strpos($tag,$match)!==false)
130 {
131 //Matched something
132 return false;
133 }
134 }
135
136 //If we're here it's safe to merge
137 return true;
138 }
139
140 //Checks agains the blacklist
141 private function ismovable($tag)
142 {
143 $allowed = !$this->ismergeable($tag);
144
145 if($allowed == true)
146 {
147 return true;
148 }
149
150 foreach($this->dontmove as $match)
151 {
152 if(strpos($tag,$match)!==false)
153 {
154 //Matched something
155 return false;
156 }
157 }
158
159 //If we're here it's safe to move
160 return true;
161 }
162 }
163