@@ -414,6 +414,10 @@ discard block |
||
| 414 | 414 | } |
| 415 | 415 | |
| 416 | 416 | // Checks against the white- and blacklists |
| 417 | + |
|
| 418 | + /** |
|
| 419 | + * @param string $tag |
|
| 420 | + */ |
|
| 417 | 421 | private function ismergeable($tag) |
| 418 | 422 | { |
| 419 | 423 | if ( ! $this->aggregate ) { |
@@ -534,7 +538,7 @@ discard block |
||
| 534 | 538 | * @param string $filepath Filepath. |
| 535 | 539 | * @param bool $cache_miss Optional. Force a cache miss. Default false. |
| 536 | 540 | * |
| 537 | - * @return bool|string Url pointing to the minified js file or false. |
|
| 541 | + * @return false|string Url pointing to the minified js file or false. |
|
| 538 | 542 | */ |
| 539 | 543 | public function minify_single( $filepath, $cache_miss = false ) |
| 540 | 544 | { |
@@ -1,6 +1,6 @@ discard block |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -if ( ! defined( 'ABSPATH' ) ) { |
|
| 3 | +if (!defined('ABSPATH')) { |
|
| 4 | 4 | exit; |
| 5 | 5 | } |
| 6 | 6 | |
@@ -13,18 +13,18 @@ discard block |
||
| 13 | 13 | ); |
| 14 | 14 | |
| 15 | 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' |
|
| 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 | 20 | ,'logHuman' |
| 21 | 21 | ); |
| 22 | 22 | private $domove = array( |
| 23 | - 'gaJsHost','load_cmc','jd.gallery.transitions.js','swfobject.embedSWF(','tiny_mce.js','tinyMCEPreInit.go' |
|
| 23 | + 'gaJsHost', 'load_cmc', 'jd.gallery.transitions.js', 'swfobject.embedSWF(', 'tiny_mce.js', 'tinyMCEPreInit.go' |
|
| 24 | 24 | ); |
| 25 | 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' |
|
| 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 | 28 | ); |
| 29 | 29 | |
| 30 | 30 | private $aggregate = true; |
@@ -43,84 +43,84 @@ discard block |
||
| 43 | 43 | // Reads the page and collects script tags |
| 44 | 44 | public function read($options) |
| 45 | 45 | { |
| 46 | - $noptimizeJS = apply_filters( 'autoptimize_filter_js_noptimize', false, $this->content ); |
|
| 47 | - if ( $noptimizeJS ) { |
|
| 46 | + $noptimizeJS = apply_filters('autoptimize_filter_js_noptimize', false, $this->content); |
|
| 47 | + if ($noptimizeJS) { |
|
| 48 | 48 | return false; |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | // 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 ) ) ); |
|
| 52 | + $whitelistJS = apply_filters('autoptimize_filter_js_whitelist', '', $this->content); |
|
| 53 | + if (!empty($whitelistJS)) { |
|
| 54 | + $this->whitelist = array_filter(array_map('trim', explode(',', $whitelistJS))); |
|
| 55 | 55 | } |
| 56 | 56 | |
| 57 | 57 | // is there JS we should simply remove |
| 58 | - $removableJS = apply_filters( 'autoptimize_filter_js_removables', '', $this->content ); |
|
| 58 | + $removableJS = apply_filters('autoptimize_filter_js_removables', '', $this->content); |
|
| 59 | 59 | if (!empty($removableJS)) { |
| 60 | - $this->jsremovables = array_filter( array_map( 'trim', explode( ',', $removableJS ) ) ); |
|
| 60 | + $this->jsremovables = array_filter(array_map('trim', explode(',', $removableJS))); |
|
| 61 | 61 | } |
| 62 | 62 | |
| 63 | 63 | // 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>'; |
|
| 64 | + if (apply_filters('autoptimize_filter_js_justhead', $options['justhead'])) { |
|
| 65 | + $content = explode('</head>', $this->content, 2); |
|
| 66 | + $this->content = $content[0].'</head>'; |
|
| 67 | 67 | $this->restofcontent = $content[1]; |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | 70 | // Determine whether we're doing JS-files aggregation or not. |
| 71 | - if ( ! $options['aggregate'] ) { |
|
| 71 | + if (!$options['aggregate']) { |
|
| 72 | 72 | $this->aggregate = false; |
| 73 | 73 | } |
| 74 | 74 | // Returning true for "dontaggregate" turns off aggregation. |
| 75 | - if ( $this->aggregate && apply_filters( 'autoptimize_filter_js_dontaggregate', false ) ) { |
|
| 75 | + if ($this->aggregate && apply_filters('autoptimize_filter_js_dontaggregate', false)) { |
|
| 76 | 76 | $this->aggregate = false; |
| 77 | 77 | } |
| 78 | 78 | |
| 79 | 79 | // include inline? |
| 80 | - if ( apply_filters( 'autoptimize_js_include_inline', $options['include_inline'] ) ) { |
|
| 80 | + if (apply_filters('autoptimize_js_include_inline', $options['include_inline'])) { |
|
| 81 | 81 | $this->include_inline = true; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | // 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 ); |
|
| 85 | + $this->inject_min_late = apply_filters('autoptimize_filter_js_inject_min_late', true); |
|
| 86 | 86 | |
| 87 | 87 | // 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 ); |
|
| 89 | - $this->domovelast = apply_filters( 'autoptimize_filter_js_movelast', $this->domovelast ); |
|
| 90 | - $this->domove = apply_filters( 'autoptimize_filter_js_domove', $this->domove ); |
|
| 88 | + $this->dontmove = apply_filters('autoptimize_filter_js_dontmove', $this->dontmove); |
|
| 89 | + $this->domovelast = apply_filters('autoptimize_filter_js_movelast', $this->domovelast); |
|
| 90 | + $this->domove = apply_filters('autoptimize_filter_js_domove', $this->domove); |
|
| 91 | 91 | |
| 92 | 92 | // get extra exclusions settings or filter |
| 93 | 93 | $excludeJS = $options['js_exclude']; |
| 94 | - $excludeJS = apply_filters( 'autoptimize_filter_js_exclude', $excludeJS, $this->content ); |
|
| 94 | + $excludeJS = apply_filters('autoptimize_filter_js_exclude', $excludeJS, $this->content); |
|
| 95 | 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] ); |
|
| 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 | 101 | $this->jsremovables[] = $removeKey; |
| 102 | 102 | } |
| 103 | 103 | } |
| 104 | - $exclJSArr = array_keys( $excludeJS ); |
|
| 104 | + $exclJSArr = array_keys($excludeJS); |
|
| 105 | 105 | } else { |
| 106 | - $exclJSArr = array_filter( array_map( 'trim', explode( ',', $excludeJS ) ) ); |
|
| 106 | + $exclJSArr = array_filter(array_map('trim', explode(',', $excludeJS))); |
|
| 107 | 107 | } |
| 108 | - $this->dontmove = array_merge( $exclJSArr, $this->dontmove ); |
|
| 108 | + $this->dontmove = array_merge($exclJSArr, $this->dontmove); |
|
| 109 | 109 | } |
| 110 | 110 | |
| 111 | 111 | // Should we add try-catch? |
| 112 | - if ( $options['trycatch'] ) { |
|
| 112 | + if ($options['trycatch']) { |
|
| 113 | 113 | $this->trycatch = true; |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | // force js in head? |
| 117 | - if ( $options['forcehead'] ) { |
|
| 117 | + if ($options['forcehead']) { |
|
| 118 | 118 | $this->forcehead = true; |
| 119 | 119 | } else { |
| 120 | 120 | $this->forcehead = false; |
| 121 | 121 | } |
| 122 | 122 | |
| 123 | - $this->forcehead = apply_filters( 'autoptimize_filter_js_forcehead', $this->forcehead ); |
|
| 123 | + $this->forcehead = apply_filters('autoptimize_filter_js_forcehead', $this->forcehead); |
|
| 124 | 124 | |
| 125 | 125 | // get cdn url |
| 126 | 126 | $this->cdn_url = $options['cdn_url']; |
@@ -135,26 +135,26 @@ discard block |
||
| 135 | 135 | $this->content = $this->hide_comments($this->content); |
| 136 | 136 | |
| 137 | 137 | // Get script files |
| 138 | - if ( preg_match_all( '#<script.*</script>#Usmi', $this->content, $matches ) ) { |
|
| 139 | - foreach( $matches[0] as $tag ) { |
|
| 138 | + if (preg_match_all('#<script.*</script>#Usmi', $this->content, $matches)) { |
|
| 139 | + foreach ($matches[0] as $tag) { |
|
| 140 | 140 | // only consider script aggregation for types whitelisted in should_aggregate-function |
| 141 | 141 | $should_aggregate = $this->should_aggregate($tag); |
| 142 | - if ( ! $should_aggregate ) { |
|
| 142 | + if (!$should_aggregate) { |
|
| 143 | 143 | $tag = ''; |
| 144 | 144 | continue; |
| 145 | 145 | } |
| 146 | 146 | |
| 147 | - if ( preg_match( '#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source ) ) { |
|
| 147 | + if (preg_match('#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source)) { |
|
| 148 | 148 | // non-inline script |
| 149 | - if ( $this->isremovable($tag, $this->jsremovables) ) { |
|
| 150 | - $this->content = str_replace( $tag, '', $this->content ); |
|
| 149 | + if ($this->isremovable($tag, $this->jsremovables)) { |
|
| 150 | + $this->content = str_replace($tag, '', $this->content); |
|
| 151 | 151 | continue; |
| 152 | 152 | } |
| 153 | 153 | |
| 154 | 154 | $origTag = null; |
| 155 | - $url = current( explode( '?', $source[2], 2 ) ); |
|
| 155 | + $url = current(explode('?', $source[2], 2)); |
|
| 156 | 156 | $path = $this->getpath($url); |
| 157 | - if ( false !== $path && preg_match( '#\.js$#', $path ) && $this->ismergeable($tag) ) { |
|
| 157 | + if (false !== $path && preg_match('#\.js$#', $path) && $this->ismergeable($tag)) { |
|
| 158 | 158 | // ok to optimize, add to array |
| 159 | 159 | $this->scripts[] = $path; |
| 160 | 160 | } else { |
@@ -162,35 +162,35 @@ discard block |
||
| 162 | 162 | $newTag = $tag; |
| 163 | 163 | |
| 164 | 164 | // non-mergeable script (excluded or dynamic or external) |
| 165 | - if ( is_array( $excludeJS ) ) { |
|
| 165 | + if (is_array($excludeJS)) { |
|
| 166 | 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 ); |
|
| 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); |
|
| 170 | 170 | } |
| 171 | 171 | } |
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 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 ); |
|
| 175 | + if ($path && apply_filters('autoptimize_filter_js_minify_excluded', true, $url)) { |
|
| 176 | + $minified_url = $this->minify_single($path); |
|
| 177 | 177 | // replace orig URL with minified URL from cache if so |
| 178 | - if ( ! empty( $minified_url ) ) { |
|
| 179 | - $newTag = str_replace( $url, $minified_url, $newTag ); |
|
| 178 | + if (!empty($minified_url)) { |
|
| 179 | + $newTag = str_replace($url, $minified_url, $newTag); |
|
| 180 | 180 | } |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - if ( $this->ismovable($newTag) ) { |
|
| 183 | + if ($this->ismovable($newTag)) { |
|
| 184 | 184 | // can be moved, flags and all |
| 185 | - if ( $this->movetolast($newTag) ) { |
|
| 185 | + if ($this->movetolast($newTag)) { |
|
| 186 | 186 | $this->move['last'][] = $newTag; |
| 187 | 187 | } else { |
| 188 | 188 | $this->move['first'][] = $newTag; |
| 189 | 189 | } |
| 190 | 190 | } else { |
| 191 | 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 ); |
|
| 192 | + if ($origTag !== $newTag) { |
|
| 193 | + $this->content = str_replace($origTag, $newTag, $this->content); |
|
| 194 | 194 | $origTag = ''; |
| 195 | 195 | } |
| 196 | 196 | // and forget about the $tag (not to be touched any more) |
@@ -199,24 +199,24 @@ discard block |
||
| 199 | 199 | } |
| 200 | 200 | } else { |
| 201 | 201 | // Inline script |
| 202 | - if ( $this->isremovable($tag, $this->jsremovables) ) { |
|
| 203 | - $this->content = str_replace( $tag, '', $this->content ); |
|
| 202 | + if ($this->isremovable($tag, $this->jsremovables)) { |
|
| 203 | + $this->content = str_replace($tag, '', $this->content); |
|
| 204 | 204 | continue; |
| 205 | 205 | } |
| 206 | 206 | |
| 207 | 207 | // unhide comments, as javascript may be wrapped in comment-tags for old times' sake |
| 208 | 208 | $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; |
|
| 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; |
|
| 214 | 214 | } else { |
| 215 | 215 | // Can we move this? |
| 216 | - $changedTag = apply_filters( 'autoptimize_inline_js_tag', $tag ); |
|
| 217 | - $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $changedTag ); |
|
| 218 | - if ( $this->ismovable($changedTag) || '' !== $autoptimize_js_moveable ) { |
|
| 219 | - if ( $this->movetolast($changedTag) || 'last' === $autoptimize_js_moveable ) { |
|
| 216 | + $changedTag = apply_filters('autoptimize_inline_js_tag', $tag); |
|
| 217 | + $autoptimize_js_moveable = apply_filters('autoptimize_js_moveable', '', $changedTag); |
|
| 218 | + if ($this->ismovable($changedTag) || '' !== $autoptimize_js_moveable) { |
|
| 219 | + if ($this->movetolast($changedTag) || 'last' === $autoptimize_js_moveable) { |
|
| 220 | 220 | $this->move['last'][] = $changedTag; |
| 221 | 221 | } else { |
| 222 | 222 | $this->move['first'][] = $changedTag; |
@@ -231,7 +231,7 @@ discard block |
||
| 231 | 231 | } |
| 232 | 232 | |
| 233 | 233 | //Remove the original script tag |
| 234 | - $this->content = str_replace( $tag, '', $this->content ); |
|
| 234 | + $this->content = str_replace($tag, '', $this->content); |
|
| 235 | 235 | } |
| 236 | 236 | |
| 237 | 237 | return true; |
@@ -261,21 +261,21 @@ discard block |
||
| 261 | 261 | // We're only interested in the type attribute of the <script> tag itself, not any possible |
| 262 | 262 | // inline code that might just contain the 'type=' string... |
| 263 | 263 | $tag_parts = array(); |
| 264 | - preg_match( '#<(script[^>]*)>#i', $tag, $tag_parts); |
|
| 264 | + preg_match('#<(script[^>]*)>#i', $tag, $tag_parts); |
|
| 265 | 265 | $tag_without_contents = null; |
| 266 | - if ( ! empty( $tag_parts[1] ) ) { |
|
| 266 | + if (!empty($tag_parts[1])) { |
|
| 267 | 267 | $tag_without_contents = $tag_parts[1]; |
| 268 | 268 | } |
| 269 | 269 | |
| 270 | - $has_type = ( strpos( $tag_without_contents, 'type' ) !== false ); |
|
| 270 | + $has_type = (strpos($tag_without_contents, 'type') !== false); |
|
| 271 | 271 | |
| 272 | 272 | $type_valid = false; |
| 273 | - if ( $has_type ) { |
|
| 274 | - $type_valid = (bool) preg_match( '/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents ); |
|
| 273 | + if ($has_type) { |
|
| 274 | + $type_valid = (bool) preg_match('/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents); |
|
| 275 | 275 | } |
| 276 | 276 | |
| 277 | 277 | $should_aggregate = false; |
| 278 | - if ( ! $has_type || $type_valid ) { |
|
| 278 | + if (!$has_type || $type_valid) { |
|
| 279 | 279 | $should_aggregate = true; |
| 280 | 280 | } |
| 281 | 281 | |
@@ -285,40 +285,40 @@ discard block |
||
| 285 | 285 | //Joins and optimizes JS |
| 286 | 286 | public function minify() |
| 287 | 287 | { |
| 288 | - foreach ( $this->scripts as $script ) { |
|
| 288 | + foreach ($this->scripts as $script) { |
|
| 289 | 289 | // TODO/FIXME: some duplicate code here, can be reduced/simplified |
| 290 | - if ( preg_match( '#^INLINE;#', $script ) ) { |
|
| 290 | + if (preg_match('#^INLINE;#', $script)) { |
|
| 291 | 291 | // Inline script |
| 292 | - $script = preg_replace( '#^INLINE;#', '', $script ); |
|
| 293 | - $script = rtrim( $script, ";\n\t\r" ) . ';'; |
|
| 292 | + $script = preg_replace('#^INLINE;#', '', $script); |
|
| 293 | + $script = rtrim($script, ";\n\t\r").';'; |
|
| 294 | 294 | // Add try-catch? |
| 295 | - if ( $this->trycatch ) { |
|
| 296 | - $script = 'try{' . $script . '}catch(e){}'; |
|
| 295 | + if ($this->trycatch) { |
|
| 296 | + $script = 'try{'.$script.'}catch(e){}'; |
|
| 297 | 297 | } |
| 298 | - $tmpscript = apply_filters( 'autoptimize_js_individual_script', $script, '' ); |
|
| 299 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscript ) ) { |
|
| 298 | + $tmpscript = apply_filters('autoptimize_js_individual_script', $script, ''); |
|
| 299 | + if (has_filter('autoptimize_js_individual_script') && !empty($tmpscript)) { |
|
| 300 | 300 | $script = $tmpscript; |
| 301 | 301 | $this->alreadyminified = true; |
| 302 | 302 | } |
| 303 | - $this->jscode .= "\n" . $script; |
|
| 303 | + $this->jscode .= "\n".$script; |
|
| 304 | 304 | } else { |
| 305 | 305 | // External script |
| 306 | - if ( false !== $script && file_exists( $script ) && is_readable( $script ) ) { |
|
| 307 | - $scriptsrc = file_get_contents( $script ); |
|
| 308 | - $scriptsrc = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc ); |
|
| 309 | - $scriptsrc = rtrim( $scriptsrc, ";\n\t\r" ) . ';'; |
|
| 306 | + if (false !== $script && file_exists($script) && is_readable($script)) { |
|
| 307 | + $scriptsrc = file_get_contents($script); |
|
| 308 | + $scriptsrc = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc); |
|
| 309 | + $scriptsrc = rtrim($scriptsrc, ";\n\t\r").';'; |
|
| 310 | 310 | // Add try-catch? |
| 311 | - if ( $this->trycatch ) { |
|
| 312 | - $scriptsrc = 'try{' . $scriptsrc . '}catch(e){}'; |
|
| 311 | + if ($this->trycatch) { |
|
| 312 | + $scriptsrc = 'try{'.$scriptsrc.'}catch(e){}'; |
|
| 313 | 313 | } |
| 314 | - $tmpscriptsrc = apply_filters( 'autoptimize_js_individual_script', $scriptsrc, $script ); |
|
| 315 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscriptsrc ) ) { |
|
| 314 | + $tmpscriptsrc = apply_filters('autoptimize_js_individual_script', $scriptsrc, $script); |
|
| 315 | + if (has_filter('autoptimize_js_individual_script') && !empty($tmpscriptsrc)) { |
|
| 316 | 316 | $scriptsrc = $tmpscriptsrc; |
| 317 | 317 | $this->alreadyminified = true; |
| 318 | - } else if ( $this->can_inject_late($script) ) { |
|
| 318 | + } else if ($this->can_inject_late($script)) { |
|
| 319 | 319 | $scriptsrc = self::build_injectlater_marker($script, md5($scriptsrc)); |
| 320 | 320 | } |
| 321 | - $this->jscode .= "\n" . $scriptsrc; |
|
| 321 | + $this->jscode .= "\n".$scriptsrc; |
|
| 322 | 322 | }/*else{ |
| 323 | 323 | //Couldn't read JS. Maybe getpath isn't working? |
| 324 | 324 | }*/ |
@@ -326,32 +326,32 @@ discard block |
||
| 326 | 326 | } |
| 327 | 327 | |
| 328 | 328 | // Check for already-minified code |
| 329 | - $this->md5hash = md5( $this->jscode ); |
|
| 329 | + $this->md5hash = md5($this->jscode); |
|
| 330 | 330 | $ccheck = new autoptimizeCache($this->md5hash, 'js'); |
| 331 | - if ( $ccheck->check() ) { |
|
| 331 | + if ($ccheck->check()) { |
|
| 332 | 332 | $this->jscode = $ccheck->retrieve(); |
| 333 | 333 | return true; |
| 334 | 334 | } |
| 335 | - unset( $ccheck ); |
|
| 335 | + unset($ccheck); |
|
| 336 | 336 | |
| 337 | 337 | // $this->jscode has all the uncompressed code now. |
| 338 | - if ( true !== $this->alreadyminified ) { |
|
| 339 | - if ( apply_filters( 'autoptimize_js_do_minify', true ) ) { |
|
| 340 | - $tmp_jscode = trim( JSMin::minify( $this->jscode ) ); |
|
| 341 | - if ( ! empty( $tmp_jscode ) ) { |
|
| 338 | + if (true !== $this->alreadyminified) { |
|
| 339 | + if (apply_filters('autoptimize_js_do_minify', true)) { |
|
| 340 | + $tmp_jscode = trim(JSMin::minify($this->jscode)); |
|
| 341 | + if (!empty($tmp_jscode)) { |
|
| 342 | 342 | $this->jscode = $tmp_jscode; |
| 343 | - unset( $tmp_jscode ); |
|
| 343 | + unset($tmp_jscode); |
|
| 344 | 344 | } |
| 345 | - $this->jscode = $this->inject_minified( $this->jscode ); |
|
| 346 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
|
| 345 | + $this->jscode = $this->inject_minified($this->jscode); |
|
| 346 | + $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode); |
|
| 347 | 347 | return true; |
| 348 | 348 | } else { |
| 349 | - $this->jscode = $this->inject_minified( $this->jscode ); |
|
| 349 | + $this->jscode = $this->inject_minified($this->jscode); |
|
| 350 | 350 | return false; |
| 351 | 351 | } |
| 352 | 352 | } |
| 353 | 353 | |
| 354 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
|
| 354 | + $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode); |
|
| 355 | 355 | return true; |
| 356 | 356 | } |
| 357 | 357 | |
@@ -359,11 +359,11 @@ discard block |
||
| 359 | 359 | public function cache() |
| 360 | 360 | { |
| 361 | 361 | $cache = new autoptimizeCache($this->md5hash, 'js'); |
| 362 | - if ( ! $cache->check() ) { |
|
| 362 | + if (!$cache->check()) { |
|
| 363 | 363 | // Cache our code |
| 364 | 364 | $cache->cache($this->jscode, 'text/javascript'); |
| 365 | 365 | } |
| 366 | - $this->url = AUTOPTIMIZE_CACHE_URL . $cache->getname(); |
|
| 366 | + $this->url = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
|
| 367 | 367 | $this->url = $this->url_replace_cdn($this->url); |
| 368 | 368 | } |
| 369 | 369 | |
@@ -371,43 +371,43 @@ discard block |
||
| 371 | 371 | public function getcontent() |
| 372 | 372 | { |
| 373 | 373 | // Restore the full content |
| 374 | - if ( ! empty( $this->restofcontent ) ) { |
|
| 374 | + if (!empty($this->restofcontent)) { |
|
| 375 | 375 | $this->content .= $this->restofcontent; |
| 376 | 376 | $this->restofcontent = ''; |
| 377 | 377 | } |
| 378 | 378 | |
| 379 | 379 | // Add the scripts taking forcehead/ deferred (default) into account |
| 380 | - if ( $this->forcehead ) { |
|
| 381 | - $replaceTag = array( '</head>', 'before' ); |
|
| 380 | + if ($this->forcehead) { |
|
| 381 | + $replaceTag = array('</head>', 'before'); |
|
| 382 | 382 | $defer = ''; |
| 383 | 383 | } else { |
| 384 | - $replaceTag = array( '</body>', 'before' ); |
|
| 384 | + $replaceTag = array('</body>', 'before'); |
|
| 385 | 385 | $defer = 'defer '; |
| 386 | 386 | } |
| 387 | 387 | |
| 388 | - $defer = apply_filters( 'autoptimize_filter_js_defer', $defer ); |
|
| 388 | + $defer = apply_filters('autoptimize_filter_js_defer', $defer); |
|
| 389 | 389 | |
| 390 | - $bodyreplacementpayload = '<script type="text/javascript" ' . $defer . 'src="' . $this->url . '"></script>'; |
|
| 391 | - $bodyreplacementpayload = apply_filters( 'autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload ); |
|
| 390 | + $bodyreplacementpayload = '<script type="text/javascript" '.$defer.'src="'.$this->url.'"></script>'; |
|
| 391 | + $bodyreplacementpayload = apply_filters('autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload); |
|
| 392 | 392 | |
| 393 | - $bodyreplacement = implode( '', $this->move['first'] ); |
|
| 393 | + $bodyreplacement = implode('', $this->move['first']); |
|
| 394 | 394 | $bodyreplacement .= $bodyreplacementpayload; |
| 395 | - $bodyreplacement .= implode( '', $this->move['last'] ); |
|
| 395 | + $bodyreplacement .= implode('', $this->move['last']); |
|
| 396 | 396 | |
| 397 | - $replaceTag = apply_filters( 'autoptimize_filter_js_replacetag', $replaceTag ); |
|
| 397 | + $replaceTag = apply_filters('autoptimize_filter_js_replacetag', $replaceTag); |
|
| 398 | 398 | |
| 399 | - if ( strlen( $this->jscode ) > 0 ) { |
|
| 400 | - $this->inject_in_html( $bodyreplacement, $replaceTag ); |
|
| 399 | + if (strlen($this->jscode) > 0) { |
|
| 400 | + $this->inject_in_html($bodyreplacement, $replaceTag); |
|
| 401 | 401 | } |
| 402 | 402 | |
| 403 | 403 | // Restore comments. |
| 404 | - $this->content = $this->restore_comments( $this->content ); |
|
| 404 | + $this->content = $this->restore_comments($this->content); |
|
| 405 | 405 | |
| 406 | 406 | // Restore IE hacks. |
| 407 | - $this->content = $this->restore_iehacks( $this->content ); |
|
| 407 | + $this->content = $this->restore_iehacks($this->content); |
|
| 408 | 408 | |
| 409 | 409 | // Restore noptimize. |
| 410 | - $this->content = $this->restore_noptimize( $this->content ); |
|
| 410 | + $this->content = $this->restore_noptimize($this->content); |
|
| 411 | 411 | |
| 412 | 412 | // Return the modified HTML. |
| 413 | 413 | return $this->content; |
@@ -416,32 +416,32 @@ discard block |
||
| 416 | 416 | // Checks against the white- and blacklists |
| 417 | 417 | private function ismergeable($tag) |
| 418 | 418 | { |
| 419 | - if ( ! $this->aggregate ) { |
|
| 419 | + if (!$this->aggregate) { |
|
| 420 | 420 | return false; |
| 421 | 421 | } |
| 422 | 422 | |
| 423 | - if ( ! empty( $this->whitelist ) ) { |
|
| 424 | - foreach ( $this->whitelist as $match ) { |
|
| 425 | - if (false !== strpos( $tag, $match ) ) { |
|
| 423 | + if (!empty($this->whitelist)) { |
|
| 424 | + foreach ($this->whitelist as $match) { |
|
| 425 | + if (false !== strpos($tag, $match)) { |
|
| 426 | 426 | return true; |
| 427 | 427 | } |
| 428 | 428 | } |
| 429 | 429 | // no match with whitelist |
| 430 | 430 | return false; |
| 431 | 431 | } else { |
| 432 | - foreach($this->domove as $match) { |
|
| 433 | - if ( false !== strpos( $tag, $match ) ) { |
|
| 432 | + foreach ($this->domove as $match) { |
|
| 433 | + if (false !== strpos($tag, $match)) { |
|
| 434 | 434 | // Matched something |
| 435 | 435 | return false; |
| 436 | 436 | } |
| 437 | 437 | } |
| 438 | 438 | |
| 439 | - if ( $this->movetolast($tag) ) { |
|
| 439 | + if ($this->movetolast($tag)) { |
|
| 440 | 440 | return false; |
| 441 | 441 | } |
| 442 | 442 | |
| 443 | - foreach( $this->dontmove as $match ) { |
|
| 444 | - if ( false !== strpos( $tag, $match ) ) { |
|
| 443 | + foreach ($this->dontmove as $match) { |
|
| 444 | + if (false !== strpos($tag, $match)) { |
|
| 445 | 445 | // Matched something |
| 446 | 446 | return false; |
| 447 | 447 | } |
@@ -455,23 +455,23 @@ discard block |
||
| 455 | 455 | // Checks agains the blacklist |
| 456 | 456 | private function ismovable($tag) |
| 457 | 457 | { |
| 458 | - if ( true !== $this->include_inline || apply_filters( 'autoptimize_filter_js_unmovable', true ) ) { |
|
| 458 | + if (true !== $this->include_inline || apply_filters('autoptimize_filter_js_unmovable', true)) { |
|
| 459 | 459 | return false; |
| 460 | 460 | } |
| 461 | 461 | |
| 462 | - foreach ( $this->domove as $match ) { |
|
| 463 | - if ( false !== strpos( $tag, $match ) ) { |
|
| 462 | + foreach ($this->domove as $match) { |
|
| 463 | + if (false !== strpos($tag, $match)) { |
|
| 464 | 464 | // Matched something |
| 465 | 465 | return true; |
| 466 | 466 | } |
| 467 | 467 | } |
| 468 | 468 | |
| 469 | - if ( $this->movetolast($tag) ) { |
|
| 469 | + if ($this->movetolast($tag)) { |
|
| 470 | 470 | return true; |
| 471 | 471 | } |
| 472 | 472 | |
| 473 | - foreach ( $this->dontmove as $match ) { |
|
| 474 | - if ( false !== strpos( $tag, $match ) ) { |
|
| 473 | + foreach ($this->dontmove as $match) { |
|
| 474 | + if (false !== strpos($tag, $match)) { |
|
| 475 | 475 | // Matched something |
| 476 | 476 | return false; |
| 477 | 477 | } |
@@ -483,8 +483,8 @@ discard block |
||
| 483 | 483 | |
| 484 | 484 | private function movetolast($tag) |
| 485 | 485 | { |
| 486 | - foreach ( $this->domovelast as $match ) { |
|
| 487 | - if ( false !== strpos( $tag, $match ) ) { |
|
| 486 | + foreach ($this->domovelast as $match) { |
|
| 487 | + if (false !== strpos($tag, $match)) { |
|
| 488 | 488 | // Matched, return true |
| 489 | 489 | return true; |
| 490 | 490 | } |
@@ -505,11 +505,11 @@ discard block |
||
| 505 | 505 | * @return bool |
| 506 | 506 | */ |
| 507 | 507 | private function can_inject_late($jsPath) { |
| 508 | - $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); |
|
| 509 | - if ( true !== $this->inject_min_late ) { |
|
| 508 | + $consider_minified_array = apply_filters('autoptimize_filter_js_consider_minified', false); |
|
| 509 | + if (true !== $this->inject_min_late) { |
|
| 510 | 510 | // late-inject turned off |
| 511 | 511 | return false; |
| 512 | - } else if ( ( false === strpos( $jsPath, 'min.js' ) ) && ( false === strpos( $jsPath, 'wp-includes/js/jquery/jquery.js' ) ) && ( str_replace( $consider_minified_array, '', $jsPath ) === $jsPath ) ) { |
|
| 512 | + } else if ((false === strpos($jsPath, 'min.js')) && (false === strpos($jsPath, 'wp-includes/js/jquery/jquery.js')) && (str_replace($consider_minified_array, '', $jsPath) === $jsPath)) { |
|
| 513 | 513 | // file not minified based on filename & filter |
| 514 | 514 | return false; |
| 515 | 515 | } else { |
@@ -536,26 +536,26 @@ discard block |
||
| 536 | 536 | * |
| 537 | 537 | * @return bool|string Url pointing to the minified js file or false. |
| 538 | 538 | */ |
| 539 | - public function minify_single( $filepath, $cache_miss = false ) |
|
| 539 | + public function minify_single($filepath, $cache_miss = false) |
|
| 540 | 540 | { |
| 541 | - $contents = $this->prepare_minify_single( $filepath ); |
|
| 541 | + $contents = $this->prepare_minify_single($filepath); |
|
| 542 | 542 | |
| 543 | - if ( empty( $contents ) ) { |
|
| 543 | + if (empty($contents)) { |
|
| 544 | 544 | return false; |
| 545 | 545 | } |
| 546 | 546 | |
| 547 | 547 | // Check cache. |
| 548 | - $hash = 'single_' . md5( $contents ); |
|
| 549 | - $cache = new autoptimizeCache( $hash, 'js' ); |
|
| 548 | + $hash = 'single_'.md5($contents); |
|
| 549 | + $cache = new autoptimizeCache($hash, 'js'); |
|
| 550 | 550 | |
| 551 | 551 | // If not in cache already, minify... |
| 552 | - if ( ! $cache->check() || $cache_miss ) { |
|
| 553 | - $contents = trim( JSMin::minify( $contents ) ); |
|
| 552 | + if (!$cache->check() || $cache_miss) { |
|
| 553 | + $contents = trim(JSMin::minify($contents)); |
|
| 554 | 554 | // Store in cache. |
| 555 | - $cache->cache( $contents, 'text/javascript' ); |
|
| 555 | + $cache->cache($contents, 'text/javascript'); |
|
| 556 | 556 | } |
| 557 | 557 | |
| 558 | - $url = $this->build_minify_single_url( $cache ); |
|
| 558 | + $url = $this->build_minify_single_url($cache); |
|
| 559 | 559 | |
| 560 | 560 | return $url; |
| 561 | 561 | } |