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

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

159 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 }
36 }
37 }else{
38 //Inline script
39 if($this->ismergeable($tag))
40 {
41 preg_match('#<script.*>(.*)</script>#Usmi',$tag,$code);
42 $code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm','$1',$code[1]);
43 $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/','',$code);
44 $this->scripts[] = 'INLINE;'.$code;
45 }else{
46 //Can we move this?
47 if($this->ismovable($tag))
48 {
49 $this->move[] = $tag;
50 }/*else{
51 //We shouldn't touch this
52 }*/
53 }
54 }
55
56 //Remove the original script tag
57 $this->content = str_replace($tag,'',$this->content);
58 }
59
60 return true;
61 }
62
63 //No script files :(
64 return false;
65 }
66
67 //Joins and optimizes JS
68 public function minify()
69 {
70 foreach($this->scripts as $script)
71 {
72 if(preg_match('#^INLINE;#',$script))
73 {
74 //Inline script
75 $script = preg_replace('#^INLINE;#','',$script);
76 $this->jscode .= "\n".$script;
77 }else{
78 //External script
79 if($script !== false && file_exists($script) && is_readable($script))
80 {
81 $script = file_get_contents($script);
82 $this->jscode .= "\n".$script;
83 }/*else{
84 //Couldn't read JS. Maybe getpath isn't working?
85 }*/
86 }
87 }
88
89 //$this->jscode has all the uncompressed code now.
90 if(class_exists('JSMin'))
91 {
92 $this->jscode = trim(JSMin::minify($this->jscode));
93 return true;
94 }
95
96 return false;
97 }
98
99 //Caches the JS in uncompressed, deflated and gzipped form.
100 public function cache()
101 {
102 $md5 = md5($this->jscode);
103 $cache = new autopimizeCache(WP_PLUGIN_DIR.'/autoptimize/cache/',$md5);
104 if(!$cache->check())
105 {
106 //Cache our code
107 $cache->cache($this->jscode,'text/javascript');
108 }
109 $this->url = WP_PLUGIN_URL.'/autoptimize/cache/'.$cache->getname();
110 }
111
112 //Returns the content
113 public function getcontent()
114 {
115 $bodyreplacement = implode('',$this->move).'<script type="text/javascript" src="'.$this->url.'"></script></body>';
116 $this->content = str_replace('</body>',$bodyreplacement,$this->content);
117 return $this->content;
118 }
119
120 //Checks agains the whitelist
121 private function ismergeable($tag)
122 {
123 foreach($this->domove as $match)
124 {
125 if(strpos($tag,$match)!==false)
126 {
127 //Matched something
128 return false;
129 }
130 }
131
132 //If we're here it's safe to merge
133 return true;
134 }
135
136 //Checks agains the blacklist
137 private function ismovable($tag)
138 {
139 $allowed = !$this->ismergeable($tag);
140
141 if($allowed == true)
142 {
143 return true;
144 }
145
146 foreach($this->dontmove as $match)
147 {
148 if(strpos($tag,$match)!==false)
149 {
150 //Matched something
151 return false;
152 }
153 }
154
155 //If we're here it's safe to move
156 return true;
157 }
158 }
159