| @@ -1,131 +1,85 @@ | ||
| 1 | 1 | <?php |
| 2 | +if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
| 2 | 3 | |
| 3 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 4 | - exit; | |
| 5 | -} | |
| 6 | - | |
| 7 | -class autoptimizeScripts extends autoptimizeBase | |
| 8 | -{ | |
| 4 | +class autoptimizeScripts extends autoptimizeBase { | |
| 9 | 5 | private $scripts = array(); |
| 10 | - private $move = array( | |
| 11 | - 'first' => array(), | |
| 12 | - 'last' => array() | |
| 13 | - ); | |
| 14 | - | |
| 15 | - private $dontmove = array( | |
| 16 | - 'document.write','html5.js','show_ads.js','google_ad','histats.com/js','statcounter.com/counter/counter.js', | |
| 17 | - 'ws.amazon.com/widgets','media.fastclick.net','/ads/','comment-form-quicktags/quicktags.php','edToolbar', | |
| 18 | - 'intensedebate.com','scripts.chitika.net/','_gaq.push','jotform.com/','admin-bar.min.js','GoogleAnalyticsObject', | |
| 19 | - 'plupload.full.min.js','syntaxhighlighter','adsbygoogle','gist.github.com','_stq','nonce','post_id','data-noptimize' | |
| 20 | - ,'logHuman' | |
| 21 | - ); | |
| 22 | - private $domove = array( | |
| 23 | - 'gaJsHost','load_cmc','jd.gallery.transitions.js','swfobject.embedSWF(','tiny_mce.js','tinyMCEPreInit.go' | |
| 24 | - ); | |
| 25 | - private $domovelast = array( | |
| 26 | - 'addthis.com','/afsonline/show_afs_search.js','disqus.js','networkedblogs.com/getnetworkwidget','infolinks.com/js/', | |
| 27 | - 'jd.gallery.js.php','jd.gallery.transitions.js','swfobject.embedSWF(','linkwithin.com/widget.js','tiny_mce.js','tinyMCEPreInit.go' | |
| 28 | - ); | |
| 29 | - | |
| 30 | - private $aggregate = true; | |
| 31 | - private $trycatch = false; | |
| 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 | + private $domove = array('gaJsHost','load_cmc','jd.gallery.transitions.js','swfobject.embedSWF(','tiny_mce.js','tinyMCEPreInit.go'); | |
| 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 | + private $trycatch = false; | |
| 32 | 10 | private $alreadyminified = false; |
| 33 | - private $forcehead = true; | |
| 34 | - private $include_inline = false; | |
| 35 | - private $jscode = ''; | |
| 36 | - private $url = ''; | |
| 37 | - private $restofcontent = ''; | |
| 38 | - private $md5hash = ''; | |
| 39 | - private $whitelist = ''; | |
| 40 | - private $jsremovables = array(); | |
| 11 | + private $forcehead = true; | |
| 12 | + private $include_inline = false; | |
| 13 | + private $jscode = ''; | |
| 14 | + private $url = ''; | |
| 15 | + private $move = array('first' => array(), 'last' => array()); | |
| 16 | + private $restofcontent = ''; | |
| 17 | + private $md5hash = ''; | |
| 18 | + private $whitelist = ''; | |
| 19 | + private $jsremovables = array(); | |
| 41 | 20 | private $inject_min_late = ''; |
| 42 | - | |
| 43 | - // Reads the page and collects script tags | |
| 44 | - public function read($options) | |
| 45 | - { | |
| 21 | + | |
| 22 | + //Reads the page and collects script tags | |
| 23 | + public function read($options) { | |
| 46 | 24 | $noptimizeJS = apply_filters( 'autoptimize_filter_js_noptimize', false, $this->content ); |
| 47 | - if ( $noptimizeJS ) { | |
| 48 | - return false; | |
| 49 | - } | |
| 25 | + if ($noptimizeJS) return false; | |
| 50 | 26 | |
| 51 | 27 | // only optimize known good JS? |
| 52 | - $whitelistJS = apply_filters( 'autoptimize_filter_js_whitelist', '', $this->content ); | |
| 53 | - if ( ! empty( $whitelistJS ) ) { | |
| 54 | - $this->whitelist = array_filter( array_map( 'trim', explode( ',', $whitelistJS ) ) ); | |
| 28 | + $whitelistJS = apply_filters( 'autoptimize_filter_js_whitelist', "" ); | |
| 29 | + if (!empty($whitelistJS)) { | |
| 30 | + $this->whitelist = array_filter(array_map('trim',explode(",",$whitelistJS))); | |
| 55 | 31 | } |
| 56 | 32 | |
| 57 | 33 | // is there JS we should simply remove |
| 58 | - $removableJS = apply_filters( 'autoptimize_filter_js_removables', '', $this->content ); | |
| 34 | + $removableJS = apply_filters( 'autoptimize_filter_js_removables', ''); | |
| 59 | 35 | if (!empty($removableJS)) { |
| 60 | - $this->jsremovables = array_filter( array_map( 'trim', explode( ',', $removableJS ) ) ); | |
| 36 | + $this->jsremovables = array_filter(array_map('trim',explode(",",$removableJS))); | |
| 61 | 37 | } |
| 62 | 38 | |
| 63 | 39 | // only header? |
| 64 | - if ( apply_filters( 'autoptimize_filter_js_justhead', $options['justhead'] ) ) { | |
| 65 | - $content = explode( '</head>', $this->content, 2 ); | |
| 66 | - $this->content = $content[0] . '</head>'; | |
| 40 | + if( apply_filters('autoptimize_filter_js_justhead',$options['justhead']) == true ) { | |
| 41 | + $content = explode('</head>',$this->content,2); | |
| 42 | + $this->content = $content[0].'</head>'; | |
| 67 | 43 | $this->restofcontent = $content[1]; |
| 68 | 44 | } |
| 69 | - | |
| 70 | - // Determine whether we're doing JS-files aggregation or not. | |
| 71 | - if ( ! $options['aggregate'] ) { | |
| 72 | - $this->aggregate = false; | |
| 73 | - } | |
| 74 | - // Returning true for "dontaggregate" turns off aggregation. | |
| 75 | - if ( $this->aggregate && apply_filters( 'autoptimize_filter_js_dontaggregate', false ) ) { | |
| 76 | - $this->aggregate = false; | |
| 77 | - } | |
| 78 | - | |
| 45 | + | |
| 79 | 46 | // include inline? |
| 80 | - if ( apply_filters( 'autoptimize_js_include_inline', $options['include_inline'] ) ) { | |
| 47 | + if( apply_filters('autoptimize_js_include_inline',$options['include_inline']) == true ) { | |
| 81 | 48 | $this->include_inline = true; |
| 82 | 49 | } |
| 83 | 50 | |
| 84 | 51 | // filter to "late inject minified JS", default to true for now (it is faster) |
| 85 | - $this->inject_min_late = apply_filters( 'autoptimize_filter_js_inject_min_late', true ); | |
| 52 | + $this->inject_min_late = apply_filters('autoptimize_filter_js_inject_min_late',true); | |
| 86 | 53 | |
| 87 | 54 | // filters to override hardcoded do(nt)move(last) array contents (array in, array out!) |
| 88 | - $this->dontmove = apply_filters( 'autoptimize_filter_js_dontmove', $this->dontmove ); | |
| 55 | + $this->dontmove = apply_filters( 'autoptimize_filter_js_dontmove', $this->dontmove ); | |
| 89 | 56 | $this->domovelast = apply_filters( 'autoptimize_filter_js_movelast', $this->domovelast ); |
| 90 | 57 | $this->domove = apply_filters( 'autoptimize_filter_js_domove', $this->domove ); |
| 91 | 58 | |
| 92 | 59 | // get extra exclusions settings or filter |
| 93 | 60 | $excludeJS = $options['js_exclude']; |
| 94 | - $excludeJS = apply_filters( 'autoptimize_filter_js_exclude', $excludeJS, $this->content ); | |
| 95 | - | |
| 96 | - if ( '' !== $excludeJS ) { | |
| 97 | - if ( is_array( $excludeJS ) ) { | |
| 98 | - if ( ( $removeKeys = array_keys( $excludeJS, 'remove' ) ) !== false ) { | |
| 99 | - foreach ( $removeKeys as $removeKey ) { | |
| 100 | - unset( $excludeJS[$removeKey] ); | |
| 101 | - $this->jsremovables[] = $removeKey; | |
| 102 | - } | |
| 103 | - } | |
| 104 | - $exclJSArr = array_keys( $excludeJS ); | |
| 105 | - } else { | |
| 106 | - $exclJSArr = array_filter( array_map( 'trim', explode( ',', $excludeJS ) ) ); | |
| 107 | - } | |
| 108 | - $this->dontmove = array_merge( $exclJSArr, $this->dontmove ); | |
| 61 | + $excludeJS = apply_filters( 'autoptimize_filter_js_exclude', $excludeJS ); | |
| 62 | + if ($excludeJS!=="") { | |
| 63 | + $exclJSArr = array_filter(array_map('trim',explode(",",$excludeJS))); | |
| 64 | + $this->dontmove = array_merge($exclJSArr,$this->dontmove); | |
| 109 | 65 | } |
| 110 | 66 | |
| 111 | - // Should we add try-catch? | |
| 112 | - if ( $options['trycatch'] ) { | |
| 67 | + //Should we add try-catch? | |
| 68 | + if($options['trycatch'] == true) | |
| 113 | 69 | $this->trycatch = true; |
| 114 | - } | |
| 115 | 70 | |
| 116 | - // force js in head? | |
| 117 | - if ( $options['forcehead'] ) { | |
| 71 | + // force js in head? | |
| 72 | + if($options['forcehead'] == true) { | |
| 118 | 73 | $this->forcehead = true; |
| 119 | 74 | } else { |
| 120 | 75 | $this->forcehead = false; |
| 121 | 76 | } |
| 122 | - | |
| 123 | 77 | $this->forcehead = apply_filters( 'autoptimize_filter_js_forcehead', $this->forcehead ); |
| 124 | 78 | |
| 125 | 79 | // get cdn url |
| 126 | 80 | $this->cdn_url = $options['cdn_url']; |
| 127 | - | |
| 81 | + | |
| 128 | 82 | // noptimize me |
| 129 | 83 | $this->content = $this->hide_noptimize($this->content); |
| 130 | 84 | |
| 131 | 85 | // Save IE hacks |
| @@ -133,192 +87,135 @@ | ||
| 133 | 87 | |
| 134 | 88 | // comments |
| 135 | 89 | $this->content = $this->hide_comments($this->content); |
| 136 | 90 | |
| 137 | - // Get script files | |
| 138 | - if ( preg_match_all( '#<script.*</script>#Usmi', $this->content, $matches ) ) { | |
| 139 | - foreach( $matches[0] as $tag ) { | |
| 140 | - // only consider script aggregation for types whitelisted in should_aggregate-function | |
| 141 | - $should_aggregate = $this->should_aggregate($tag); | |
| 142 | - if ( ! $should_aggregate ) { | |
| 143 | - $tag = ''; | |
| 91 | + //Get script files | |
| 92 | + if(preg_match_all('#<script.*</script>#Usmi',$this->content,$matches)) { | |
| 93 | + foreach($matches[0] as $tag) { | |
| 94 | + // only consider aggregation whitelisted in should_aggregate-function | |
| 95 | + if( !$this->should_aggregate($tag) ) { | |
| 96 | + $tag=''; | |
| 144 | 97 | continue; |
| 145 | 98 | } |
| 146 | - | |
| 147 | - if ( preg_match( '#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source ) ) { | |
| 148 | - // non-inline script | |
| 149 | - if ( $this->isremovable($tag, $this->jsremovables) ) { | |
| 150 | - $this->content = str_replace( $tag, '', $this->content ); | |
| 99 | + if(preg_match('#<script[^>]*src=("|\')([^>]*)("|\')#Usmi',$tag,$source)) { | |
| 100 | + if ($this->isremovable($tag,$this->jsremovables)) { | |
| 101 | + $this->content = str_replace($tag,'',$this->content); | |
| 151 | 102 | continue; |
| 152 | 103 | } |
| 153 | - | |
| 154 | - $origTag = null; | |
| 155 | - $url = current( explode( '?', $source[2], 2 ) ); | |
| 104 | + | |
| 105 | + // External script | |
| 106 | + $url = current(explode('?',$source[2],2)); | |
| 156 | 107 | $path = $this->getpath($url); |
| 157 | - if ( false !== $path && preg_match( '#\.js$#', $path ) && $this->ismergeable($tag) ) { | |
| 158 | - // ok to optimize, add to array | |
| 159 | - $this->scripts[] = $path; | |
| 160 | - } else { | |
| 161 | - $origTag = $tag; | |
| 162 | - $newTag = $tag; | |
| 163 | - | |
| 164 | - // non-mergeable script (excluded or dynamic or external) | |
| 165 | - if ( is_array( $excludeJS ) ) { | |
| 166 | - // should we add flags? | |
| 167 | - foreach ( $excludeJS as $exclTag => $exclFlags) { | |
| 168 | - if ( false !== strpos( $origTag, $exclTag ) && in_array( $exclFlags, array( 'async', 'defer' ) ) ) { | |
| 169 | - $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; | |
| 170 | 121 | } |
| 122 | + } else { | |
| 123 | + //We shouldn't touch this | |
| 124 | + $tag = ''; | |
| 171 | 125 | } |
| 172 | 126 | } |
| 173 | - | |
| 174 | - // Should we minify the non-aggregated script? | |
| 175 | - if ( $path && apply_filters( 'autoptimize_filter_js_minify_excluded', true, $url ) ) { | |
| 176 | - $minified_url = $this->minify_single( $path ); | |
| 177 | - // replace orig URL with minified URL from cache if so | |
| 178 | - if ( ! empty( $minified_url ) ) { | |
| 179 | - $newTag = str_replace( $url, $minified_url, $newTag ); | |
| 180 | - } | |
| 181 | - } | |
| 182 | - | |
| 183 | - if ( $this->ismovable($newTag) ) { | |
| 184 | - // can be moved, flags and all | |
| 185 | - if ( $this->movetolast($newTag) ) { | |
| 186 | - $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; | |
| 187 | 133 | } else { |
| 188 | - $this->move['first'][] = $newTag; | |
| 134 | + $this->move['first'][] = $tag; | |
| 189 | 135 | } |
| 190 | 136 | } else { |
| 191 | - // cannot be moved, so if flag was added re-inject altered tag immediately | |
| 192 | - if ( $origTag !== $newTag ) { | |
| 193 | - $this->content = str_replace( $origTag, $newTag, $this->content ); | |
| 194 | - $origTag = ''; | |
| 195 | - } | |
| 196 | - // and forget about the $tag (not to be touched any more) | |
| 137 | + //We shouldn't touch this | |
| 197 | 138 | $tag = ''; |
| 198 | 139 | } |
| 199 | 140 | } |
| 200 | 141 | } else { |
| 201 | 142 | // Inline script |
| 202 | - if ( $this->isremovable($tag, $this->jsremovables) ) { | |
| 203 | - $this->content = str_replace( $tag, '', $this->content ); | |
| 143 | + if ($this->isremovable($tag,$this->jsremovables)) { | |
| 144 | + $this->content = str_replace($tag,'',$this->content); | |
| 204 | 145 | continue; |
| 205 | 146 | } |
| 206 | - | |
| 147 | + | |
| 207 | 148 | // unhide comments, as javascript may be wrapped in comment-tags for old times' sake |
| 208 | 149 | $tag = $this->restore_comments($tag); |
| 209 | - if ( $this->ismergeable($tag) && $this->include_inline ) { | |
| 210 | - preg_match( '#<script.*>(.*)</script>#Usmi', $tag , $code ); | |
| 211 | - $code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1] ); | |
| 212 | - $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code ); | |
| 213 | - $this->scripts[] = 'INLINE;' . $code; | |
| 150 | + if($this->ismergeable($tag) && ( $this->include_inline )) { | |
| 151 | + preg_match('#<script.*>(.*)</script>#Usmi',$tag,$code); | |
| 152 | + $code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm','$1',$code[1]); | |
| 153 | + $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/','',$code); | |
| 154 | + $this->scripts[] = 'INLINE;'.$code; | |
| 214 | 155 | } else { |
| 215 | 156 | // Can we move this? |
| 216 | - $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $tag ); | |
| 217 | - if ( $this->ismovable($tag) || '' !== $autoptimize_js_moveable ) { | |
| 218 | - if ( $this->movetolast($tag) || 'last' === $autoptimize_js_moveable ) { | |
| 157 | + if($this->ismovable($tag)) { | |
| 158 | + if($this->movetolast($tag)) { | |
| 219 | 159 | $this->move['last'][] = $tag; |
| 220 | 160 | } else { |
| 221 | 161 | $this->move['first'][] = $tag; |
| 222 | 162 | } |
| 223 | 163 | } else { |
| 224 | - // We shouldn't touch this | |
| 164 | + //We shouldn't touch this | |
| 225 | 165 | $tag = ''; |
| 226 | 166 | } |
| 227 | 167 | } |
| 228 | - // Re-hide comments to be able to do the removal based on tag from $this->content | |
| 168 | + // re-hide comments to be able to do the removal based on tag from $this->content | |
| 229 | 169 | $tag = $this->hide_comments($tag); |
| 230 | 170 | } |
| 231 | - | |
| 171 | + | |
| 232 | 172 | //Remove the original script tag |
| 233 | - $this->content = str_replace( $tag, '', $this->content ); | |
| 173 | + $this->content = str_replace($tag,'',$this->content); | |
| 234 | 174 | } |
| 235 | - | |
| 175 | + | |
| 236 | 176 | return true; |
| 237 | 177 | } |
| 238 | - | |
| 178 | + | |
| 239 | 179 | // No script files, great ;-) |
| 240 | 180 | return false; |
| 241 | 181 | } |
| 242 | - | |
| 243 | - /** | |
| 244 | - * Determines wheter a certain `<script>` $tag should be aggregated or not. | |
| 245 | - * | |
| 246 | - * We consider these as "aggregation-safe" currently: | |
| 247 | - * - script tags without a `type` attribute | |
| 248 | - * - script tags with these `type` attribute values: `text/javascript`, `text/ecmascript`, `application/javascript`, | |
| 249 | - * and `application/ecmascript` | |
| 250 | - * | |
| 251 | - * Everything else should return false. | |
| 252 | - * | |
| 253 | - * @link https://developer.mozilla.org/en/docs/Web/HTML/Element/script#attr-type | |
| 254 | - * | |
| 255 | - * @param string $tag | |
| 256 | - * @return bool | |
| 257 | - */ | |
| 258 | - public function should_aggregate($tag) | |
| 259 | - { | |
| 260 | - // We're only interested in the type attribute of the <script> tag itself, not any possible | |
| 261 | - // inline code that might just contain the 'type=' string... | |
| 262 | - $tag_parts = array(); | |
| 263 | - preg_match( '#<(script[^>]*)>#i', $tag, $tag_parts); | |
| 264 | - $tag_without_contents = null; | |
| 265 | - if ( ! empty( $tag_parts[1] ) ) { | |
| 266 | - $tag_without_contents = $tag_parts[1]; | |
| 267 | - } | |
| 268 | - | |
| 269 | - $has_type = ( strpos( $tag_without_contents, 'type' ) !== false ); | |
| 270 | - | |
| 271 | - $type_valid = false; | |
| 272 | - if ( $has_type ) { | |
| 273 | - $type_valid = (bool) preg_match( '/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents ); | |
| 274 | - } | |
| 275 | - | |
| 276 | - $should_aggregate = false; | |
| 277 | - if ( ! $has_type || $type_valid ) { | |
| 278 | - $should_aggregate = true; | |
| 279 | - } | |
| 280 | - | |
| 281 | - return $should_aggregate; | |
| 282 | - } | |
| 283 | - | |
| 182 | + | |
| 284 | 183 | //Joins and optimizes JS |
| 285 | - public function minify() | |
| 286 | - { | |
| 287 | - foreach ( $this->scripts as $script ) { | |
| 288 | - // TODO/FIXME: some duplicate code here, can be reduced/simplified | |
| 289 | - if ( preg_match( '#^INLINE;#', $script ) ) { | |
| 290 | - // Inline script | |
| 291 | - $script = preg_replace( '#^INLINE;#', '', $script ); | |
| 184 | + public function minify() { | |
| 185 | + foreach($this->scripts as $script) { | |
| 186 | + if(preg_match('#^INLINE;#',$script)) { | |
| 187 | + //Inline script | |
| 188 | + $script = preg_replace('#^INLINE;#','',$script); | |
| 292 | 189 | $script = rtrim( $script, ";\n\t\r" ) . ';'; |
| 293 | - // Add try-catch? | |
| 294 | - if ( $this->trycatch ) { | |
| 295 | - $script = 'try{' . $script . '}catch(e){}'; | |
| 190 | + //Add try-catch? | |
| 191 | + if($this->trycatch) { | |
| 192 | + $script = 'try{'.$script.'}catch(e){}'; | |
| 296 | 193 | } |
| 297 | - $tmpscript = apply_filters( 'autoptimize_js_individual_script', $script, '' ); | |
| 298 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscript ) ) { | |
| 299 | - $script = $tmpscript; | |
| 300 | - $this->alreadyminified = true; | |
| 194 | + $tmpscript = apply_filters( 'autoptimize_js_individual_script', $script, "" ); | |
| 195 | + if ( has_filter('autoptimize_js_individual_script') && !empty($tmpscript) ) { | |
| 196 | + $script=$tmpscript; | |
| 197 | + $this->alreadyminified=true; | |
| 301 | 198 | } |
| 302 | 199 | $this->jscode .= "\n" . $script; |
| 303 | 200 | } else { |
| 304 | - // External script | |
| 305 | - if ( false !== $script && file_exists( $script ) && is_readable( $script ) ) { | |
| 306 | - $scriptsrc = file_get_contents( $script ); | |
| 307 | - $scriptsrc = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc ); | |
| 308 | - $scriptsrc = rtrim( $scriptsrc, ";\n\t\r" ) . ';'; | |
| 309 | - // Add try-catch? | |
| 310 | - if ( $this->trycatch ) { | |
| 311 | - $scriptsrc = 'try{' . $scriptsrc . '}catch(e){}'; | |
| 201 | + //External script | |
| 202 | + if($script !== false && file_exists($script) && is_readable($script)) { | |
| 203 | + $scriptsrc = file_get_contents($script); | |
| 204 | + $scriptsrc = preg_replace('/\x{EF}\x{BB}\x{BF}/','',$scriptsrc); | |
| 205 | + $scriptsrc = rtrim($scriptsrc,";\n\t\r").';'; | |
| 206 | + //Add try-catch? | |
| 207 | + if($this->trycatch) { | |
| 208 | + $scriptsrc = 'try{'.$scriptsrc.'}catch(e){}'; | |
| 312 | 209 | } |
| 313 | 210 | $tmpscriptsrc = apply_filters( 'autoptimize_js_individual_script', $scriptsrc, $script ); |
| 314 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscriptsrc ) ) { | |
| 315 | - $scriptsrc = $tmpscriptsrc; | |
| 316 | - $this->alreadyminified = true; | |
| 317 | - } else if ( $this->can_inject_late($script) ) { | |
| 318 | - $scriptsrc = self::build_injectlater_marker($script, md5($scriptsrc)); | |
| 211 | + if ( has_filter('autoptimize_js_individual_script') && !empty($tmpscriptsrc) ) { | |
| 212 | + $scriptsrc=$tmpscriptsrc; | |
| 213 | + $this->alreadyminified=true; | |
| 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%%"; | |
| 319 | 216 | } |
| 320 | - $this->jscode .= "\n" . $scriptsrc; | |
| 217 | + $this->jscode .= "\n".$scriptsrc; | |
| 321 | 218 | }/*else{ |
| 322 | 219 | //Couldn't read JS. Maybe getpath isn't working? |
| 323 | 220 | }*/ |
| 324 | 221 | } |
| @@ -323,106 +220,101 @@ | ||
| 323 | 220 | }*/ |
| 324 | 221 | } |
| 325 | 222 | } |
| 326 | 223 | |
| 327 | - // Check for already-minified code | |
| 328 | - $this->md5hash = md5( $this->jscode ); | |
| 329 | - $ccheck = new autoptimizeCache($this->md5hash, 'js'); | |
| 330 | - if ( $ccheck->check() ) { | |
| 224 | + //Check for already-minified code | |
| 225 | + $this->md5hash = md5($this->jscode); | |
| 226 | + $ccheck = new autoptimizeCache($this->md5hash,'js'); | |
| 227 | + if($ccheck->check()) { | |
| 331 | 228 | $this->jscode = $ccheck->retrieve(); |
| 332 | 229 | return true; |
| 333 | 230 | } |
| 334 | - unset( $ccheck ); | |
| 335 | - | |
| 336 | - // $this->jscode has all the uncompressed code now. | |
| 337 | - if ( true !== $this->alreadyminified ) { | |
| 338 | - if ( apply_filters( 'autoptimize_js_do_minify', true ) ) { | |
| 339 | - $tmp_jscode = trim( JSMin::minify( $this->jscode ) ); | |
| 340 | - if ( ! empty( $tmp_jscode ) ) { | |
| 231 | + unset($ccheck); | |
| 232 | + | |
| 233 | + //$this->jscode has all the uncompressed code now. | |
| 234 | + if ($this->alreadyminified!==true) { | |
| 235 | + if (class_exists('JSMin') && apply_filters( 'autoptimize_js_do_minify' , true)) { | |
| 236 | + if (@is_callable(array("JSMin","minify"))) { | |
| 237 | + $tmp_jscode = trim(JSMin::minify($this->jscode)); | |
| 238 | + if (!empty($tmp_jscode)) { | |
| 341 | 239 | $this->jscode = $tmp_jscode; |
| 342 | - unset( $tmp_jscode ); | |
| 240 | + unset($tmp_jscode); | |
| 343 | 241 | } |
| 344 | - $this->jscode = $this->inject_minified( $this->jscode ); | |
| 242 | + $this->jscode = $this->inject_minified($this->jscode); | |
| 345 | 243 | $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
| 346 | 244 | return true; |
| 347 | 245 | } else { |
| 348 | - $this->jscode = $this->inject_minified( $this->jscode ); | |
| 246 | + $this->jscode = $this->inject_minified($this->jscode); | |
| 349 | 247 | return false; |
| 350 | 248 | } |
| 249 | + } else { | |
| 250 | + $this->jscode = $this->inject_minified($this->jscode); | |
| 251 | + return false; | |
| 252 | + } | |
| 351 | 253 | } |
| 352 | - | |
| 353 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); | |
| 354 | 254 | return true; |
| 355 | 255 | } |
| 356 | - | |
| 357 | - // Caches the JS in uncompressed, deflated and gzipped form. | |
| 358 | - public function cache() | |
| 359 | - { | |
| 360 | - $cache = new autoptimizeCache($this->md5hash, 'js'); | |
| 361 | - if ( ! $cache->check() ) { | |
| 362 | - // Cache our code | |
| 363 | - $cache->cache($this->jscode, 'text/javascript'); | |
| 256 | + | |
| 257 | + //Caches the JS in uncompressed, deflated and gzipped form. | |
| 258 | + public function cache() { | |
| 259 | + $cache = new autoptimizeCache($this->md5hash,'js'); | |
| 260 | + if(!$cache->check()) { | |
| 261 | + //Cache our code | |
| 262 | + $cache->cache($this->jscode,'text/javascript'); | |
| 364 | 263 | } |
| 365 | - $this->url = AUTOPTIMIZE_CACHE_URL . $cache->getname(); | |
| 264 | + $this->url = AUTOPTIMIZE_CACHE_URL.$cache->getname(); | |
| 366 | 265 | $this->url = $this->url_replace_cdn($this->url); |
| 367 | 266 | } |
| 368 | - | |
| 267 | + | |
| 369 | 268 | // Returns the content |
| 370 | - public function getcontent() | |
| 371 | - { | |
| 269 | + public function getcontent() { | |
| 372 | 270 | // Restore the full content |
| 373 | - if ( ! empty( $this->restofcontent ) ) { | |
| 271 | + if(!empty($this->restofcontent)) { | |
| 374 | 272 | $this->content .= $this->restofcontent; |
| 375 | 273 | $this->restofcontent = ''; |
| 376 | 274 | } |
| 377 | - | |
| 275 | + | |
| 378 | 276 | // Add the scripts taking forcehead/ deferred (default) into account |
| 379 | - if ( $this->forcehead ) { | |
| 380 | - $replaceTag = array( '</head>', 'before' ); | |
| 381 | - $defer = ''; | |
| 277 | + if($this->forcehead == true) { | |
| 278 | + $replaceTag=array("</head>","before"); | |
| 279 | + $defer=""; | |
| 382 | 280 | } else { |
| 383 | - $replaceTag = array( '</body>', 'before' ); | |
| 384 | - $defer = 'defer '; | |
| 281 | + $replaceTag=array("</body>","before"); | |
| 282 | + $defer="defer "; | |
| 385 | 283 | } |
| 386 | - | |
| 284 | + | |
| 387 | 285 | $defer = apply_filters( 'autoptimize_filter_js_defer', $defer ); |
| 286 | + $bodyreplacementpayload = '<script type="text/javascript" '.$defer.'src="'.$this->url.'"></script>'; | |
| 287 | + $bodyreplacementpayload = apply_filters('autoptimize_filter_js_bodyreplacementpayload',$bodyreplacementpayload); | |
| 388 | 288 | |
| 389 | - $bodyreplacementpayload = '<script type="text/javascript" ' . $defer . 'src="' . $this->url . '"></script>'; | |
| 390 | - $bodyreplacementpayload = apply_filters( 'autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload ); | |
| 391 | - | |
| 392 | - $bodyreplacement = implode( '', $this->move['first'] ); | |
| 289 | + $bodyreplacement = implode('',$this->move['first']); | |
| 393 | 290 | $bodyreplacement .= $bodyreplacementpayload; |
| 394 | - $bodyreplacement .= implode( '', $this->move['last'] ); | |
| 291 | + $bodyreplacement .= implode('',$this->move['last']); | |
| 395 | 292 | |
| 396 | 293 | $replaceTag = apply_filters( 'autoptimize_filter_js_replacetag', $replaceTag ); |
| 397 | 294 | |
| 398 | - if ( strlen( $this->jscode ) > 0 ) { | |
| 399 | - $this->inject_in_html( $bodyreplacement, $replaceTag ); | |
| 295 | + if (strlen($this->jscode)>0) { | |
| 296 | + $this->inject_in_html($bodyreplacement,$replaceTag); | |
| 400 | 297 | } |
| 298 | + | |
| 299 | + // restore comments | |
| 300 | + $this->content = $this->restore_comments($this->content); | |
| 401 | 301 | |
| 402 | - // Restore comments. | |
| 403 | - $this->content = $this->restore_comments( $this->content ); | |
| 302 | + // Restore IE hacks | |
| 303 | + $this->content = $this->restore_iehacks($this->content); | |
| 304 | + | |
| 305 | + // Restore noptimize | |
| 306 | + $this->content = $this->restore_noptimize($this->content); | |
| 404 | 307 | |
| 405 | - // Restore IE hacks. | |
| 406 | - $this->content = $this->restore_iehacks( $this->content ); | |
| 407 | - | |
| 408 | - // Restore noptimize. | |
| 409 | - $this->content = $this->restore_noptimize( $this->content ); | |
| 410 | - | |
| 411 | - // Return the modified HTML. | |
| 308 | + // Return the modified HTML | |
| 412 | 309 | return $this->content; |
| 413 | 310 | } |
| 414 | - | |
| 311 | + | |
| 415 | 312 | // Checks against the white- and blacklists |
| 416 | - private function ismergeable($tag) | |
| 417 | - { | |
| 418 | - if ( ! $this->aggregate ) { | |
| 419 | - return false; | |
| 420 | - } | |
| 421 | - | |
| 422 | - if ( ! empty( $this->whitelist ) ) { | |
| 423 | - foreach ( $this->whitelist as $match ) { | |
| 424 | - if (false !== strpos( $tag, $match ) ) { | |
| 313 | + private function ismergeable($tag) { | |
| 314 | + if (!empty($this->whitelist)) { | |
| 315 | + foreach ($this->whitelist as $match) { | |
| 316 | + if(strpos($tag,$match)!==false) { | |
| 425 | 317 | return true; |
| 426 | 318 | } |
| 427 | 319 | } |
| 428 | 320 | // no match with whitelist |
| @@ -428,134 +320,92 @@ | ||
| 428 | 320 | // no match with whitelist |
| 429 | 321 | return false; |
| 430 | 322 | } else { |
| 431 | 323 | foreach($this->domove as $match) { |
| 432 | - if ( false !== strpos( $tag, $match ) ) { | |
| 324 | + if(strpos($tag,$match)!==false) { | |
| 433 | 325 | // Matched something |
| 434 | 326 | return false; |
| 435 | 327 | } |
| 436 | 328 | } |
| 437 | - | |
| 438 | - if ( $this->movetolast($tag) ) { | |
| 329 | + | |
| 330 | + if ($this->movetolast($tag)) { | |
| 439 | 331 | return false; |
| 440 | 332 | } |
| 441 | - | |
| 442 | - foreach( $this->dontmove as $match ) { | |
| 443 | - if ( false !== strpos( $tag, $match ) ) { | |
| 444 | - // Matched something | |
| 333 | + | |
| 334 | + foreach($this->dontmove as $match) { | |
| 335 | + if(strpos($tag,$match)!==false) { | |
| 336 | + //Matched something | |
| 445 | 337 | return false; |
| 446 | 338 | } |
| 447 | 339 | } |
| 448 | - | |
| 340 | + | |
| 449 | 341 | // If we're here it's safe to merge |
| 450 | 342 | return true; |
| 451 | 343 | } |
| 452 | 344 | } |
| 453 | - | |
| 454 | - // Checks agains the blacklist | |
| 455 | - private function ismovable($tag) | |
| 456 | - { | |
| 457 | - if ( true !== $this->include_inline || apply_filters( 'autoptimize_filter_js_unmovable', true ) ) { | |
| 345 | + | |
| 346 | + // Checks againstt the blacklist | |
| 347 | + private function ismovable($tag) { | |
| 348 | + if ($this->include_inline !== true || apply_filters('autoptimize_filter_js_unmovable',true)) { | |
| 458 | 349 | return false; |
| 459 | 350 | } |
| 460 | - | |
| 461 | - foreach ( $this->domove as $match ) { | |
| 462 | - if ( false !== strpos( $tag, $match ) ) { | |
| 351 | + | |
| 352 | + foreach($this->domove as $match) { | |
| 353 | + if(strpos($tag,$match)!==false) { | |
| 463 | 354 | // Matched something |
| 464 | 355 | return true; |
| 465 | 356 | } |
| 466 | 357 | } |
| 467 | - | |
| 468 | - if ( $this->movetolast($tag) ) { | |
| 358 | + | |
| 359 | + if ($this->movetolast($tag)) { | |
| 469 | 360 | return true; |
| 470 | 361 | } |
| 471 | - | |
| 472 | - foreach ( $this->dontmove as $match ) { | |
| 473 | - if ( false !== strpos( $tag, $match ) ) { | |
| 362 | + | |
| 363 | + foreach($this->dontmove as $match) { | |
| 364 | + if(strpos($tag,$match)!==false) { | |
| 474 | 365 | // Matched something |
| 475 | 366 | return false; |
| 476 | 367 | } |
| 477 | 368 | } |
| 478 | - | |
| 369 | + | |
| 479 | 370 | // If we're here it's safe to move |
| 480 | 371 | return true; |
| 481 | 372 | } |
| 482 | - | |
| 483 | - private function movetolast($tag) | |
| 484 | - { | |
| 485 | - foreach ( $this->domovelast as $match ) { | |
| 486 | - if ( false !== strpos( $tag, $match ) ) { | |
| 373 | + | |
| 374 | + private function movetolast($tag) { | |
| 375 | + foreach($this->domovelast as $match) { | |
| 376 | + if(strpos($tag,$match)!==false) { | |
| 487 | 377 | // Matched, return true |
| 488 | 378 | return true; |
| 489 | 379 | } |
| 490 | 380 | } |
| 491 | - | |
| 381 | + | |
| 492 | 382 | // Should be in 'first' |
| 493 | 383 | return false; |
| 494 | 384 | } |
| 495 | - | |
| 385 | + | |
| 496 | 386 | /** |
| 497 | - * Determines wheter a <script> $tag can be excluded from minification (as already minified) based on: | |
| 498 | - * - inject_min_late being active | |
| 499 | - * - filename ending in `min.js` | |
| 500 | - * - filename matching `js/jquery/jquery.js` (wordpress core jquery, is minified) | |
| 501 | - * - filename matching one passed in the consider minified filter | |
| 387 | + * Determines wheter a <script> $tag should be aggregated or not. | |
| 502 | 388 | * |
| 503 | - * @param string $jsPath | |
| 389 | + * We consider these as "aggregation-safe" currently: | |
| 390 | + * - script tags without a `type` attribute | |
| 391 | + * - script tags with an explicit `type` of `text/javascript`, 'text/ecmascript', | |
| 392 | + * 'application/javascript' or 'application/ecmascript' | |
| 393 | + * | |
| 394 | + * Everything else should return false. | |
| 395 | + * | |
| 396 | + * @param string $tag | |
| 504 | 397 | * @return bool |
| 398 | + * | |
| 399 | + * original function by https://github.com/zytzagoo/ on his AO fork, thanks Tomas! | |
| 505 | 400 | */ |
| 506 | - private function can_inject_late($jsPath) { | |
| 507 | - $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); | |
| 508 | - if ( true !== $this->inject_min_late ) { | |
| 509 | - // late-inject turned off | |
| 510 | - return false; | |
| 511 | - } else if ( ( false === strpos( $jsPath, 'min.js' ) ) && ( false === strpos( $jsPath, 'wp-includes/js/jquery/jquery.js' ) ) && ( str_replace( $consider_minified_array, '', $jsPath ) === $jsPath ) ) { | |
| 512 | - // file not minified based on filename & filter | |
| 513 | - return false; | |
| 401 | + public function should_aggregate($tag) { | |
| 402 | + preg_match('#<(script[^>]*)>#i',$tag,$scripttag); | |
| 403 | + if ( strpos($scripttag[1], 'type')===false ) { | |
| 404 | + return true; | |
| 405 | + } else if ( preg_match('/type\s*=\s*["\']?(?:text|application)\/(?:javascript|ecmascript)["\']?/i', $scripttag[1]) ) { | |
| 406 | + return true; | |
| 514 | 407 | } else { |
| 515 | - // phew, all is safe, we can late-inject | |
| 516 | - return true; | |
| 517 | - } | |
| 518 | - } | |
| 519 | - | |
| 520 | - /** | |
| 521 | - * Returns whether we're doing aggregation or not. | |
| 522 | - * | |
| 523 | - * @return bool | |
| 524 | - */ | |
| 525 | - public function aggregating() | |
| 526 | - { | |
| 527 | - return $this->aggregate; | |
| 528 | - } | |
| 529 | - | |
| 530 | - /** | |
| 531 | - * Minifies a single local js file and returns its (cached) url. | |
| 532 | - * | |
| 533 | - * @param string $filepath Filepath. | |
| 534 | - * @param bool $cache_miss Optional. Force a cache miss. Default false. | |
| 535 | - * | |
| 536 | - * @return bool|string Url pointing to the minified js file or false. | |
| 537 | - */ | |
| 538 | - public function minify_single( $filepath, $cache_miss = false ) | |
| 539 | - { | |
| 540 | - $contents = $this->prepare_minify_single( $filepath ); | |
| 541 | - | |
| 542 | - if ( empty( $contents ) ) { | |
| 543 | 408 | return false; |
| 544 | 409 | } |
| 545 | - | |
| 546 | - // Check cache. | |
| 547 | - $hash = 'single_' . md5( $contents ); | |
| 548 | - $cache = new autoptimizeCache( $hash, 'js' ); | |
| 549 | - | |
| 550 | - // If not in cache already, minify... | |
| 551 | - if ( ! $cache->check() || $cache_miss ) { | |
| 552 | - $contents = trim( JSMin::minify( $contents ) ); | |
| 553 | - // Store in cache. | |
| 554 | - $cache->cache( $contents, 'text/javascript' ); | |
| 555 | - } | |
| 556 | - | |
| 557 | - $url = $this->build_minify_single_url( $cache ); | |
| 558 | - | |
| 559 | - return $url; | |
| 560 | 410 | } |
| 561 | 411 | } |