| 1 |
<?php |
| 2 |
|
| 3 |
class autoptimizeScripts extends autoptimizeBase |
| 4 |
{ |
| 5 |
private $scripts = array(); |
| 6 |
private $dontmove = array('document.write','show_ads.js','google_ad','blogcatalog.com/w','tweetmeme.com/i','mybloglog.com/'); |
| 7 |
private $domove = array('gaJsHost'); |
| 8 |
private $domovelast = array('addthis.com','/afsonline/show_afs_search.js'); |
| 9 |
private $jscode = ''; |
| 10 |
private $url = ''; |
| 11 |
private $move = array('first' => array(), 'last' => array()); |
| 12 |
private $restofcontent = ''; |
| 13 |
|
| 14 |
//Reads the page and collects script tags |
| 15 |
public function read($justhead) |
| 16 |
{ |
| 17 |
//Remove everything that's not the header |
| 18 |
if($justhead == true) |
| 19 |
{ |
| 20 |
$content = explode('</head>',$this->content,2); |
| 21 |
$this->content = $content[0].'</head>'; |
| 22 |
$this->restofcontent = $content[1]; |
| 23 |
} |
| 24 |
|
| 25 |
//Get script files |
| 26 |
if(preg_match_all('#<script.*</script>#Usmi',$this->content,$matches)) |
| 27 |
{ |
| 28 |
foreach($matches[0] as $tag) |
| 29 |
{ |
| 30 |
if(preg_match('#src=("|\')(.*)("|\')#Usmi',$tag,$source)) |
| 31 |
{ |
| 32 |
//External script |
| 33 |
$url = current(explode('?',$source[2],2)); |
| 34 |
$path = $this->getpath($url); |
| 35 |
if($path !==false && preg_match('#\.js$#',$path)) |
| 36 |
{ |
| 37 |
//Good script |
| 38 |
$this->scripts[] = $path; |
| 39 |
}else{ |
| 40 |
//External script (example: google analytics) |
| 41 |
//OR Script is dynamic (.php etc) |
| 42 |
if($this->ismovable($tag)) |
| 43 |
{ |
| 44 |
if($this->movetolast($tag)) |
| 45 |
{ |
| 46 |
$this->move['last'][] = $tag; |
| 47 |
}else{ |
| 48 |
$this->move['first'][] = $tag; |
| 49 |
} |
| 50 |
}else{ |
| 51 |
//We shouldn't touch this |
| 52 |
$tag = ''; |
| 53 |
} |
| 54 |
} |
| 55 |
}else{ |
| 56 |
//Inline script |
| 57 |
if($this->ismergeable($tag)) |
| 58 |
{ |
| 59 |
preg_match('#<script.*>(.*)</script>#Usmi',$tag,$code); |
| 60 |
$code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm','$1',$code[1]); |
| 61 |
$code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/','',$code); |
| 62 |
$this->scripts[] = 'INLINE;'.$code; |
| 63 |
}else{ |
| 64 |
//Can we move this? |
| 65 |
if($this->ismovable($tag)) |
| 66 |
{ |
| 67 |
$this->move[] = $tag; |
| 68 |
}else{ |
| 69 |
//We shouldn't touch this |
| 70 |
$tag = ''; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
//Remove the original script tag |
| 76 |
$this->content = str_replace($tag,'',$this->content); |
| 77 |
} |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
//No script files :( |
| 83 |
return false; |
| 84 |
} |
| 85 |
|
| 86 |
//Joins and optimizes JS |
| 87 |
public function minify() |
| 88 |
{ |
| 89 |
foreach($this->scripts as $script) |
| 90 |
{ |
| 91 |
if(preg_match('#^INLINE;#',$script)) |
| 92 |
{ |
| 93 |
//Inline script |
| 94 |
$script = preg_replace('#^INLINE;#','',$script); |
| 95 |
$this->jscode .= "\n".$script; |
| 96 |
}else{ |
| 97 |
//External script |
| 98 |
if($script !== false && file_exists($script) && is_readable($script)) |
| 99 |
{ |
| 100 |
$script = file_get_contents($script); |
| 101 |
$this->jscode .= "\n".$script; |
| 102 |
}/*else{ |
| 103 |
//Couldn't read JS. Maybe getpath isn't working? |
| 104 |
}*/ |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
//$this->jscode has all the uncompressed code now. |
| 109 |
if(class_exists('JSMin')) |
| 110 |
{ |
| 111 |
$this->jscode = trim(JSMin::minify($this->jscode)); |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
return false; |
| 116 |
} |
| 117 |
|
| 118 |
//Caches the JS in uncompressed, deflated and gzipped form. |
| 119 |
public function cache() |
| 120 |
{ |
| 121 |
$md5 = md5($this->jscode); |
| 122 |
$cache = new autopimizeCache(WP_PLUGIN_DIR.'/autoptimize/cache/',$md5); |
| 123 |
if(!$cache->check()) |
| 124 |
{ |
| 125 |
//Cache our code |
| 126 |
$cache->cache($this->jscode,'text/javascript'); |
| 127 |
} |
| 128 |
$this->url = WP_PLUGIN_URL.'/autoptimize/cache/'.$cache->getname(); |
| 129 |
} |
| 130 |
|
| 131 |
//Returns the content |
| 132 |
public function getcontent() |
| 133 |
{ |
| 134 |
//Restore the full content |
| 135 |
if(!empty($this->restofcontent)) |
| 136 |
{ |
| 137 |
$this->content .= $this->restofcontent; |
| 138 |
$this->restofcontent = ''; |
| 139 |
} |
| 140 |
|
| 141 |
//Add the scripts |
| 142 |
$bodyreplacement = implode('',$this->move['first']); |
| 143 |
$bodyreplacement .= '<script type="text/javascript" src="'.$this->url.'"></script>'; |
| 144 |
$bodyreplacement .= implode('',$this->move['last']).'</body>'; |
| 145 |
$this->content = str_replace('</body>',$bodyreplacement,$this->content); |
| 146 |
|
| 147 |
//Return the modified HTML |
| 148 |
return $this->content; |
| 149 |
} |
| 150 |
|
| 151 |
//Checks agains the whitelist |
| 152 |
private function ismergeable($tag) |
| 153 |
{ |
| 154 |
foreach($this->domove as $match) |
| 155 |
{ |
| 156 |
if(strpos($tag,$match)!==false) |
| 157 |
{ |
| 158 |
//Matched something |
| 159 |
return false; |
| 160 |
} |
| 161 |
} |
| 162 |
|
| 163 |
foreach($this->dontmove as $match) |
| 164 |
{ |
| 165 |
if(strpos($tag,$match)!==false) |
| 166 |
{ |
| 167 |
//Matched something |
| 168 |
return false; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
//If we're here it's safe to merge |
| 173 |
return true; |
| 174 |
} |
| 175 |
|
| 176 |
//Checks agains the blacklist |
| 177 |
private function ismovable($tag) |
| 178 |
{ |
| 179 |
|
| 180 |
foreach($this->domove as $match) |
| 181 |
{ |
| 182 |
if(strpos($tag,$match)!==false) |
| 183 |
{ |
| 184 |
//Matched something |
| 185 |
return true; |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
foreach($this->dontmove as $match) |
| 190 |
{ |
| 191 |
if(strpos($tag,$match)!==false) |
| 192 |
{ |
| 193 |
//Matched something |
| 194 |
return false; |
| 195 |
} |
| 196 |
} |
| 197 |
|
| 198 |
//If we're here it's safe to move |
| 199 |
return true; |
| 200 |
} |
| 201 |
|
| 202 |
private function movetolast($tag) |
| 203 |
{ |
| 204 |
foreach($this->domovelast as $match) |
| 205 |
{ |
| 206 |
if(strpos($tag,$match)!==false) |
| 207 |
{ |
| 208 |
//Matched, return true |
| 209 |
return true; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
//Should be in 'first' |
| 214 |
return false; |
| 215 |
} |
| 216 |
} |
| 217 |
|