| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly |
| 3 | 3 | |
| 4 | 4 | class autoptimizeScripts extends autoptimizeBase { |
| 5 | 5 | private $scripts = array(); |
| 6 | - private $dontmove = array('document.write','html5.js','show_ads.js','google_ad','histats.com/js','statcounter.com/counter/counter.js','ws.amazon.com/widgets','media.fastclick.net','/ads/','comment-form-quicktags/quicktags.php','edToolbar','intensedebate.com','scripts.chitika.net/','_gaq.push','jotform.com/','admin-bar.min.js','GoogleAnalyticsObject','plupload.full.min.js','syntaxhighlighter','adsbygoogle','gist.github.com','_stq','nonce','post_id','data-noptimize','wordfence_logHuman'); | |
| 6 | + private $dontmove = array('document.write','html5.js','show_ads.js','google_ad','blogcatalog.com/w','tweetmeme.com/i','mybloglog.com/','histats.com/js','ads.smowtion.com/ad.js','statcounter.com/counter/counter.js','widgets.amung.us','ws.amazon.com/widgets','media.fastclick.net','/ads/','comment-form-quicktags/quicktags.php','edToolbar','intensedebate.com','scripts.chitika.net/','_gaq.push','jotform.com/','admin-bar.min.js','GoogleAnalyticsObject','plupload.full.min.js','syntaxhighlighter','adsbygoogle','gist.github.com','_stq','nonce','post_id','data-noptimize'); | |
| 7 | 7 | private $domove = array('gaJsHost','load_cmc','jd.gallery.transitions.js','swfobject.embedSWF(','tiny_mce.js','tinyMCEPreInit.go'); |
| 8 | 8 | private $domovelast = array('addthis.com','/afsonline/show_afs_search.js','disqus.js','networkedblogs.com/getnetworkwidget','infolinks.com/js/','jd.gallery.js.php','jd.gallery.transitions.js','swfobject.embedSWF(','linkwithin.com/widget.js','tiny_mce.js','tinyMCEPreInit.go'); |
| 9 | 9 | private $trycatch = false; |
| 10 | 10 | private $alreadyminified = false; |
| @@ -24,21 +24,21 @@ | ||
| 24 | 24 | $noptimizeJS = apply_filters( 'autoptimize_filter_js_noptimize', false, $this->content ); |
| 25 | 25 | if ($noptimizeJS) return false; |
| 26 | 26 | |
| 27 | 27 | // only optimize known good JS? |
| 28 | - $whitelistJS = apply_filters( 'autoptimize_filter_js_whitelist', '', $this->content ); | |
| 28 | + $whitelistJS = apply_filters( 'autoptimize_filter_js_whitelist', "" ); | |
| 29 | 29 | if (!empty($whitelistJS)) { |
| 30 | 30 | $this->whitelist = array_filter(array_map('trim',explode(",",$whitelistJS))); |
| 31 | 31 | } |
| 32 | 32 | |
| 33 | 33 | // is there JS we should simply remove |
| 34 | - $removableJS = apply_filters( 'autoptimize_filter_js_removables', '', $this->content ); | |
| 34 | + $removableJS = apply_filters( 'autoptimize_filter_js_removables', ''); | |
| 35 | 35 | if (!empty($removableJS)) { |
| 36 | 36 | $this->jsremovables = array_filter(array_map('trim',explode(",",$removableJS))); |
| 37 | 37 | } |
| 38 | 38 | |
| 39 | 39 | // only header? |
| 40 | - if( apply_filters('autoptimize_filter_js_justhead', $options['justhead']) == true ) { | |
| 40 | + if( apply_filters('autoptimize_filter_js_justhead',$options['justhead']) == true ) { | |
| 41 | 41 | $content = explode('</head>',$this->content,2); |
| 42 | 42 | $this->content = $content[0].'</head>'; |
| 43 | 43 | $this->restofcontent = $content[1]; |
| 44 | 44 | } |
| @@ -43,9 +43,9 @@ | ||
| 43 | 43 | $this->restofcontent = $content[1]; |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | // include inline? |
| 47 | - if( apply_filters('autoptimize_js_include_inline', $options['include_inline']) == true ) { | |
| 47 | + if( apply_filters('autoptimize_js_include_inline',$options['include_inline']) == true ) { | |
| 48 | 48 | $this->include_inline = true; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | // filter to "late inject minified JS", default to true for now (it is faster) |
| @@ -57,21 +57,11 @@ | ||
| 57 | 57 | $this->domove = apply_filters( 'autoptimize_filter_js_domove', $this->domove ); |
| 58 | 58 | |
| 59 | 59 | // get extra exclusions settings or filter |
| 60 | 60 | $excludeJS = $options['js_exclude']; |
| 61 | - $excludeJS = apply_filters( 'autoptimize_filter_js_exclude', $excludeJS, $this->content ); | |
| 61 | + $excludeJS = apply_filters( 'autoptimize_filter_js_exclude', $excludeJS ); | |
| 62 | 62 | if ($excludeJS!=="") { |
| 63 | - if (is_array($excludeJS)) { | |
| 64 | - if(($removeKeys = array_keys($excludeJS,"remove")) !== false) { | |
| 65 | - foreach ($removeKeys as $removeKey) { | |
| 66 | - unset($excludeJS[$removeKey]); | |
| 67 | - $this->jsremovables[]=$removeKey; | |
| 68 | - } | |
| 69 | - } | |
| 70 | - $exclJSArr = array_keys($excludeJS); | |
| 71 | - } else { | |
| 72 | - $exclJSArr = array_filter(array_map('trim',explode(",",$excludeJS))); | |
| 73 | - } | |
| 63 | + $exclJSArr = array_filter(array_map('trim',explode(",",$excludeJS))); | |
| 74 | 64 | $this->dontmove = array_merge($exclJSArr,$this->dontmove); |
| 75 | 65 | } |
| 76 | 66 | |
| 77 | 67 | //Should we add try-catch? |
| @@ -97,73 +87,55 @@ | ||
| 97 | 87 | |
| 98 | 88 | // comments |
| 99 | 89 | $this->content = $this->hide_comments($this->content); |
| 100 | 90 | |
| 101 | - // Get script files | |
| 102 | - if (preg_match_all('#<script.*</script>#Usmi',$this->content,$matches)) { | |
| 91 | + //Get script files | |
| 92 | + if(preg_match_all('#<script.*</script>#Usmi',$this->content,$matches)) { | |
| 103 | 93 | foreach($matches[0] as $tag) { |
| 104 | - // only consider script aggregation for types whitelisted in should_aggregate-function | |
| 94 | + // only consider aggregation whitelisted in should_aggregate-function | |
| 105 | 95 | if( !$this->should_aggregate($tag) ) { |
| 106 | 96 | $tag=''; |
| 107 | 97 | continue; |
| 108 | 98 | } |
| 109 | - | |
| 110 | - if (preg_match('#<script[^>]*src=("|\')([^>]*)("|\')#Usmi',$tag,$source)) { | |
| 111 | - // non-inline script | |
| 99 | + if(preg_match('#<script[^>]*src=("|\')([^>]*)("|\')#Usmi',$tag,$source)) { | |
| 112 | 100 | if ($this->isremovable($tag,$this->jsremovables)) { |
| 113 | 101 | $this->content = str_replace($tag,'',$this->content); |
| 114 | 102 | continue; |
| 115 | 103 | } |
| 116 | - $explUrl = explode('?',$source[2],2); | |
| 117 | - $url = $explUrl[0]; | |
| 104 | + | |
| 105 | + // External script | |
| 106 | + $url = current(explode('?',$source[2],2)); | |
| 118 | 107 | $path = $this->getpath($url); |
| 119 | - if($path !== false && preg_match('#\.js$#',$path) && $this->ismergeable($tag)) { | |
| 120 | - // ok to optimize, add to array | |
| 121 | - $this->scripts[] = $path; | |
| 122 | - } else { | |
| 123 | - $origTag = $tag; | |
| 124 | - $newTag = $tag; | |
| 125 | - | |
| 126 | - // non-mergeable script (excluded or dynamic or external) | |
| 127 | - if (is_array($excludeJS)) { | |
| 128 | - // should we add flags? | |
| 129 | - foreach ($excludeJS as $exclTag => $exclFlags) { | |
| 130 | - if ( strpos($origTag,$exclTag)!==false && in_array($exclFlags,array("async","defer")) ) { | |
| 131 | - $newTag = str_replace('<script ','<script '.$exclFlags.' ',$newTag); | |
| 108 | + if($path !== false && preg_match('#\.js$#',$path)) { | |
| 109 | + //Inline | |
| 110 | + if($this->ismergeable($tag)) { | |
| 111 | + //We can merge it | |
| 112 | + $this->scripts[] = $path; | |
| 113 | + } else { | |
| 114 | + //No merge, but maybe we can move it | |
| 115 | + if($this->ismovable($tag)) { | |
| 116 | + //Yeah, move it | |
| 117 | + if($this->movetolast($tag)) { | |
| 118 | + $this->move['last'][] = $tag; | |
| 119 | + } else { | |
| 120 | + $this->move['first'][] = $tag; | |
| 132 | 121 | } |
| 122 | + } else { | |
| 123 | + //We shouldn't touch this | |
| 124 | + $tag = ''; | |
| 133 | 125 | } |
| 134 | 126 | } |
| 135 | - | |
| 136 | - // should we minify the non-aggregated script? | |
| 137 | - if ($path && apply_filters('autoptimize_filter_js_minify_excluded',false)) { | |
| 138 | - $_CachedMinifiedUrl = $this->minify_single($path); | |
| 139 | - | |
| 140 | - // replace orig URL with minified URL from cache if so | |
| 141 | - if (!empty($_CachedMinifiedUrl)) { | |
| 142 | - $newTag = str_replace($url, $_CachedMinifiedUrl, $newTag); | |
| 143 | - } | |
| 144 | - | |
| 145 | - // remove querystring from URL in newTag | |
| 146 | - if ( !empty($explUrl[1]) ) { | |
| 147 | - $newTag = str_replace("?".$explUrl[1],"",$newTag); | |
| 148 | - } | |
| 149 | - } | |
| 150 | - | |
| 151 | - // should we move the non-aggregated script? | |
| 152 | - if( $this->ismovable($newTag) ) { | |
| 153 | - // can be moved, flags and all | |
| 154 | - if( $this->movetolast($newTag) ) { | |
| 155 | - $this->move['last'][] = $newTag; | |
| 127 | + } else { | |
| 128 | + //External script (example: google analytics) | |
| 129 | + //OR Script is dynamic (.php etc) | |
| 130 | + if($this->ismovable($tag)) { | |
| 131 | + if($this->movetolast($tag)) { | |
| 132 | + $this->move['last'][] = $tag; | |
| 156 | 133 | } else { |
| 157 | - $this->move['first'][] = $newTag; | |
| 134 | + $this->move['first'][] = $tag; | |
| 158 | 135 | } |
| 159 | 136 | } else { |
| 160 | - // cannot be moved, so if flag was added re-inject altered tag immediately | |
| 161 | - if ( $origTag !== $newTag ) { | |
| 162 | - $this->content = str_replace($origTag,$newTag,$this->content); | |
| 163 | - $origTag = ''; | |
| 164 | - } | |
| 165 | - // and forget about the $tag (not to be touched any more) | |
| 137 | + //We shouldn't touch this | |
| 166 | 138 | $tag = ''; |
| 167 | 139 | } |
| 168 | 140 | } |
| 169 | 141 | } else { |
| @@ -181,11 +153,10 @@ | ||
| 181 | 153 | $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/','',$code); |
| 182 | 154 | $this->scripts[] = 'INLINE;'.$code; |
| 183 | 155 | } else { |
| 184 | 156 | // Can we move this? |
| 185 | - $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $tag ); | |
| 186 | - if( $this->ismovable($tag) || $autoptimize_js_moveable !== '' ) { | |
| 187 | - if( $this->movetolast($tag) || $autoptimize_js_moveable === 'last' ) { | |
| 157 | + if($this->ismovable($tag)) { | |
| 158 | + if($this->movetolast($tag)) { | |
| 188 | 159 | $this->move['last'][] = $tag; |
| 189 | 160 | } else { |
| 190 | 161 | $this->move['first'][] = $tag; |
| 191 | 162 | } |
| @@ -219,9 +190,9 @@ | ||
| 219 | 190 | //Add try-catch? |
| 220 | 191 | if($this->trycatch) { |
| 221 | 192 | $script = 'try{'.$script.'}catch(e){}'; |
| 222 | 193 | } |
| 223 | - $tmpscript = apply_filters( 'autoptimize_js_individual_script', $script, '' ); | |
| 194 | + $tmpscript = apply_filters( 'autoptimize_js_individual_script', $script, "" ); | |
| 224 | 195 | if ( has_filter('autoptimize_js_individual_script') && !empty($tmpscript) ) { |
| 225 | 196 | $script=$tmpscript; |
| 226 | 197 | $this->alreadyminified=true; |
| 227 | 198 | } |
| @@ -229,12 +200,10 @@ | ||
| 229 | 200 | } else { |
| 230 | 201 | //External script |
| 231 | 202 | if($script !== false && file_exists($script) && is_readable($script)) { |
| 232 | 203 | $scriptsrc = file_get_contents($script); |
| 233 | - $scripthash = md5($scriptsrc); | |
| 234 | 204 | $scriptsrc = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$scriptsrc); |
| 235 | 205 | $scriptsrc = rtrim($scriptsrc,";\n\t\r").';'; |
| 236 | - | |
| 237 | 206 | //Add try-catch? |
| 238 | 207 | if($this->trycatch) { |
| 239 | 208 | $scriptsrc = 'try{'.$scriptsrc.'}catch(e){}'; |
| 240 | 209 | } |
| @@ -241,10 +210,10 @@ | ||
| 241 | 210 | $tmpscriptsrc = apply_filters( 'autoptimize_js_individual_script', $scriptsrc, $script ); |
| 242 | 211 | if ( has_filter('autoptimize_js_individual_script') && !empty($tmpscriptsrc) ) { |
| 243 | 212 | $scriptsrc=$tmpscriptsrc; |
| 244 | 213 | $this->alreadyminified=true; |
| 245 | - } else if ($this->can_inject_late($script)) { | |
| 246 | - $scriptsrc="/*!%%INJECTLATER".AUTOPTIMIZE_HASH."%%".base64_encode($script)."|".$scripthash."%%INJECTLATER%%*/"; | |
| 214 | + } else if ((( strpos($script,"min.js") !== false ) || ( strpos($script,"wp-includes/js/jquery/jquery.js") !== false )) && ( $this->inject_min_late === true )) { | |
| 215 | + $scriptsrc="%%INJECTLATER".AUTOPTIMIZE_HASH."%%".base64_encode($script)."|".md5($scriptsrc)."%%INJECTLATER%%"; | |
| 247 | 216 | } |
| 248 | 217 | $this->jscode .= "\n".$scriptsrc; |
| 249 | 218 | }/*else{ |
| 250 | 219 | //Couldn't read JS. Maybe getpath isn't working? |
| @@ -278,12 +247,11 @@ | ||
| 278 | 247 | return false; |
| 279 | 248 | } |
| 280 | 249 | } else { |
| 281 | 250 | $this->jscode = $this->inject_minified($this->jscode); |
| 282 | - return false; | |
| 251 | + return false; | |
| 283 | 252 | } |
| 284 | 253 | } |
| 285 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); | |
| 286 | 254 | return true; |
| 287 | 255 | } |
| 288 | 256 | |
| 289 | 257 | //Caches the JS in uncompressed, deflated and gzipped form. |
| @@ -342,11 +310,9 @@ | ||
| 342 | 310 | } |
| 343 | 311 | |
| 344 | 312 | // Checks against the white- and blacklists |
| 345 | 313 | private function ismergeable($tag) { |
| 346 | - if (apply_filters('autoptimize_filter_js_dontaggregate',false)) { | |
| 347 | - return false; | |
| 348 | - } else if (!empty($this->whitelist)) { | |
| 314 | + if (!empty($this->whitelist)) { | |
| 349 | 315 | foreach ($this->whitelist as $match) { |
| 350 | 316 | if(strpos($tag,$match)!==false) { |
| 351 | 317 | return true; |
| 352 | 318 | } |
| @@ -439,31 +405,7 @@ | ||
| 439 | 405 | } else if ( preg_match('/type\s*=\s*["\']?(?:text|application)\/(?:javascript|ecmascript)["\']?/i', $scripttag[1]) ) { |
| 440 | 406 | return true; |
| 441 | 407 | } else { |
| 442 | 408 | return false; |
| 443 | - } | |
| 444 | - } | |
| 445 | - | |
| 446 | - /** | |
| 447 | - * Determines wheter a <script> $tag can be excluded from minification (as already minified) based on: | |
| 448 | - * - inject_min_late being active | |
| 449 | - * - filename ending in `min.js` | |
| 450 | - * - filename matching `js/jquery/jquery.js` (wordpress core jquery, is minified) | |
| 451 | - * - filename matching one passed in the consider minified filter | |
| 452 | - * | |
| 453 | - * @param string $jsPath | |
| 454 | - * @return bool | |
| 455 | - */ | |
| 456 | - private function can_inject_late($jsPath) { | |
| 457 | - $consider_minified_array = apply_filters('autoptimize_filter_js_consider_minified',false); | |
| 458 | - if ( $this->inject_min_late !== true ) { | |
| 459 | - // late-inject turned off | |
| 460 | - return false; | |
| 461 | - } else if ( (strpos($jsPath,"min.js") === false) && ( strpos($jsPath,"wp-includes/js/jquery/jquery.js") === false ) && ( str_replace($consider_minified_array, '', $jsPath) === $jsPath ) ) { | |
| 462 | - // file not minified based on filename & filter | |
| 463 | - return false; | |
| 464 | - } else { | |
| 465 | - // phew, all is safe, we can late-inject | |
| 466 | - return true; | |
| 467 | 409 | } |
| 468 | 410 | } |
| 469 | 411 | } |