| @@ -1,5 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Class for JS optimization. | |
| 4 | + */ | |
| 2 | 5 | |
| 3 | 6 | if ( ! defined( 'ABSPATH' ) ) { |
| 4 | 7 | exit; |
| 5 | 8 | } |
| @@ -5,60 +8,243 @@ | ||
| 5 | 8 | } |
| 6 | 9 | |
| 7 | 10 | class autoptimizeScripts extends autoptimizeBase |
| 8 | 11 | { |
| 12 | + | |
| 13 | + /** | |
| 14 | + * Stores founds scripts. | |
| 15 | + * | |
| 16 | + * @var array | |
| 17 | + */ | |
| 9 | 18 | private $scripts = array(); |
| 10 | - private $move = array( | |
| 19 | + | |
| 20 | + /** | |
| 21 | + * Stores to be moved JS. | |
| 22 | + * | |
| 23 | + * @var array | |
| 24 | + */ | |
| 25 | + private $move = array( | |
| 11 | 26 | 'first' => array(), |
| 12 | - 'last' => array() | |
| 27 | + 'last' => array(), | |
| 13 | 28 | ); |
| 14 | 29 | |
| 30 | + /** | |
| 31 | + * List of not to be moved JS. | |
| 32 | + * | |
| 33 | + * @var array | |
| 34 | + */ | |
| 15 | 35 | 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' | |
| 36 | + 'document.write', | |
| 37 | + 'html5.js', | |
| 38 | + 'show_ads.js', | |
| 39 | + 'google_ad', | |
| 40 | + 'histats.com/js', | |
| 41 | + 'statcounter.com/counter/counter.js', | |
| 42 | + 'ws.amazon.com/widgets', | |
| 43 | + 'media.fastclick.net', | |
| 44 | + '/ads/', | |
| 45 | + 'comment-form-quicktags/quicktags.php', | |
| 46 | + 'edToolbar', | |
| 47 | + 'intensedebate.com', | |
| 48 | + 'scripts.chitika.net/', | |
| 49 | + '_gaq.push', | |
| 50 | + 'jotform.com/', | |
| 51 | + 'admin-bar.min.js', | |
| 52 | + 'GoogleAnalyticsObject', | |
| 53 | + 'plupload.full.min.js', | |
| 54 | + 'syntaxhighlighter', | |
| 55 | + 'adsbygoogle', | |
| 56 | + 'gist.github.com', | |
| 57 | + '_stq', | |
| 58 | + 'nonce', | |
| 59 | + 'post_id', | |
| 60 | + 'data-noptimize', | |
| 61 | + 'data-cfasync', | |
| 62 | + 'data-pagespeed-no-defer', | |
| 63 | + 'logHuman', | |
| 64 | + 'amp-mobile-version-switcher', | |
| 65 | + 'data-rocketlazyloadscript', | |
| 66 | + 'rocket-browser-checker-js-after', | |
| 21 | 67 | ); |
| 22 | - private $domove = array( | |
| 23 | - 'gaJsHost','load_cmc','jd.gallery.transitions.js','swfobject.embedSWF(','tiny_mce.js','tinyMCEPreInit.go' | |
| 68 | + | |
| 69 | + /** | |
| 70 | + * List of to be moved JS. | |
| 71 | + * | |
| 72 | + * @var array | |
| 73 | + */ | |
| 74 | + private $domove = array( | |
| 75 | + 'gaJsHost', | |
| 76 | + 'load_cmc', | |
| 77 | + 'jd.gallery.transitions.js', | |
| 78 | + 'swfobject.embedSWF(', | |
| 79 | + 'tiny_mce.js', | |
| 80 | + 'tinyMCEPreInit.go', | |
| 24 | 81 | ); |
| 82 | + | |
| 83 | + /** | |
| 84 | + * List of JS that can be moved last (not used any more). | |
| 85 | + * | |
| 86 | + * @var array | |
| 87 | + */ | |
| 25 | 88 | 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' | |
| 89 | + 'addthis.com', | |
| 90 | + '/afsonline/show_afs_search.js', | |
| 91 | + 'disqus.js', | |
| 92 | + 'networkedblogs.com/getnetworkwidget', | |
| 93 | + 'infolinks.com/js/', | |
| 94 | + 'jd.gallery.js.php', | |
| 95 | + 'jd.gallery.transitions.js', | |
| 96 | + 'swfobject.embedSWF(', | |
| 97 | + 'linkwithin.com/widget.js', | |
| 98 | + 'tiny_mce.js', | |
| 99 | + 'tinyMCEPreInit.go', | |
| 28 | 100 | ); |
| 29 | 101 | |
| 30 | - private $aggregate = true; | |
| 31 | - private $trycatch = false; | |
| 102 | + /** | |
| 103 | + * Setting CDN base URL. | |
| 104 | + * | |
| 105 | + * @var string | |
| 106 | + */ | |
| 107 | + public $cdn_url = ''; | |
| 108 | + | |
| 109 | + /** | |
| 110 | + * Setting; aggregate or not. | |
| 111 | + * | |
| 112 | + * @var bool | |
| 113 | + */ | |
| 114 | + private $aggregate = true; | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * Setting; if not aggregated, should we defer? | |
| 118 | + * | |
| 119 | + * @var bool | |
| 120 | + */ | |
| 121 | + private $defer_not_aggregate = false; | |
| 122 | + | |
| 123 | + /** | |
| 124 | + * Setting; defer inline JS? | |
| 125 | + * | |
| 126 | + * @var bool | |
| 127 | + */ | |
| 128 | + private $defer_inline = false; | |
| 129 | + | |
| 130 | + /** | |
| 131 | + * Setting; try/catch wrapping or not. | |
| 132 | + * | |
| 133 | + * @var bool | |
| 134 | + */ | |
| 135 | + private $trycatch = false; | |
| 136 | + | |
| 137 | + /** | |
| 138 | + * State; is JS already minified. | |
| 139 | + * | |
| 140 | + * @var bool | |
| 141 | + */ | |
| 32 | 142 | 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(); | |
| 41 | - private $inject_min_late = ''; | |
| 42 | 143 | |
| 43 | - // Reads the page and collects script tags | |
| 44 | - public function read($options) | |
| 144 | + /** | |
| 145 | + * Setting; force JS in head or not. | |
| 146 | + * | |
| 147 | + * @var bool | |
| 148 | + */ | |
| 149 | + private $forcehead = true; | |
| 150 | + | |
| 151 | + /** | |
| 152 | + * Setting; aggregate inline JS or not. | |
| 153 | + * | |
| 154 | + * @var bool | |
| 155 | + */ | |
| 156 | + private $include_inline = false; | |
| 157 | + | |
| 158 | + /** | |
| 159 | + * State; holds JS code. | |
| 160 | + * | |
| 161 | + * @var string | |
| 162 | + */ | |
| 163 | + private $jscode = ''; | |
| 164 | + | |
| 165 | + /** | |
| 166 | + * State; holds URL of JS-file. | |
| 167 | + * | |
| 168 | + * @var string | |
| 169 | + */ | |
| 170 | + private $url = ''; | |
| 171 | + | |
| 172 | + /** | |
| 173 | + * State; stores rest of HTML if (old) option "only in head" is on. | |
| 174 | + * | |
| 175 | + * @var string | |
| 176 | + */ | |
| 177 | + private $restofcontent = ''; | |
| 178 | + | |
| 179 | + /** | |
| 180 | + * State; holds md5-hash. | |
| 181 | + * | |
| 182 | + * @var string | |
| 183 | + */ | |
| 184 | + private $md5hash = ''; | |
| 185 | + | |
| 186 | + /** | |
| 187 | + * Setting (filter); allowlist of to be aggregated JS. | |
| 188 | + * | |
| 189 | + * @var string | |
| 190 | + */ | |
| 191 | + private $allowlist = ''; | |
| 192 | + | |
| 193 | + /** | |
| 194 | + * Setting (filter); holds JS that should be removed. | |
| 195 | + * | |
| 196 | + * @var array | |
| 197 | + */ | |
| 198 | + private $jsremovables = array(); | |
| 199 | + | |
| 200 | + /** | |
| 201 | + * Setting (filter); can we inject already minified files after the | |
| 202 | + * unminified aggregate JS has been minified. | |
| 203 | + * | |
| 204 | + * @var bool | |
| 205 | + */ | |
| 206 | + private $inject_min_late = true; | |
| 207 | + | |
| 208 | + /** | |
| 209 | + * Setting; should excluded JS be minified (if not already). | |
| 210 | + * | |
| 211 | + * @var bool | |
| 212 | + */ | |
| 213 | + private $minify_excluded = true; | |
| 214 | + | |
| 215 | + /** | |
| 216 | + * Reads the page and collects script tags. | |
| 217 | + * | |
| 218 | + * @param array $options all options. | |
| 219 | + */ | |
| 220 | + public function read( $options ) | |
| 45 | 221 | { |
| 46 | - $noptimizeJS = apply_filters( 'autoptimize_filter_js_noptimize', false, $this->content ); | |
| 47 | - if ( $noptimizeJS ) { | |
| 222 | + $noptimize_js = false; | |
| 223 | + | |
| 224 | + // If page/ post check post_meta to see if optimize is off. | |
| 225 | + if ( false === autoptimizeConfig::get_post_meta_ao_settings( 'ao_post_js_optimize' ) ) { | |
| 226 | + $noptimize_js = true; | |
| 227 | + } | |
| 228 | + | |
| 229 | + // And a filter to enforce JS noptimize. | |
| 230 | + $noptimize_js = apply_filters( 'autoptimize_filter_js_noptimize', $noptimize_js, $this->content ); | |
| 231 | + | |
| 232 | + // And finally bail if noptimize_js is true. | |
| 233 | + if ( $noptimize_js ) { | |
| 48 | 234 | return false; |
| 49 | 235 | } |
| 50 | 236 | |
| 51 | 237 | // 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 ) ) ); | |
| 238 | + $allowlist_js = apply_filters( 'autoptimize_filter_js_allowlist', '', $this->content ); | |
| 239 | + if ( ! empty( $allowlist_js ) ) { | |
| 240 | + $this->allowlist = array_filter( array_map( 'trim', explode( ',', $allowlist_js ) ) ); | |
| 55 | 241 | } |
| 56 | 242 | |
| 57 | - // is there JS we should simply remove | |
| 58 | - $removableJS = apply_filters( 'autoptimize_filter_js_removables', '', $this->content ); | |
| 59 | - if (!empty($removableJS)) { | |
| 60 | - $this->jsremovables = array_filter( array_map( 'trim', explode( ',', $removableJS ) ) ); | |
| 243 | + // is there JS we should simply remove? | |
| 244 | + $removable_js = apply_filters( 'autoptimize_filter_js_removables', '', $this->content ); | |
| 245 | + if ( ! empty( $removable_js ) ) { | |
| 246 | + $this->jsremovables = array_filter( array_map( 'trim', explode( ',', $removable_js ) ) ); | |
| 61 | 247 | } |
| 62 | 248 | |
| 63 | 249 | // only header? |
| 64 | 250 | if ( apply_filters( 'autoptimize_filter_js_justhead', $options['justhead'] ) ) { |
| @@ -74,39 +260,58 @@ | ||
| 74 | 260 | // Returning true for "dontaggregate" turns off aggregation. |
| 75 | 261 | if ( $this->aggregate && apply_filters( 'autoptimize_filter_js_dontaggregate', false ) ) { |
| 76 | 262 | $this->aggregate = false; |
| 77 | 263 | } |
| 264 | + // and the filter that should have been there to begin with. | |
| 265 | + $this->aggregate = apply_filters( 'autoptimize_filter_js_aggregate', $this->aggregate ); | |
| 78 | 266 | |
| 267 | + // Defer when not aggregating. | |
| 268 | + if ( false === $this->aggregate && apply_filters( 'autoptimize_filter_js_defer_not_aggregate', $options['defer_not_aggregate'] ) ) { | |
| 269 | + $this->defer_not_aggregate = true; | |
| 270 | + } | |
| 271 | + | |
| 272 | + // Defer inline JS? | |
| 273 | + if ( ( true === $this->defer_not_aggregate && apply_filters( 'autoptimize_js_filter_defer_inline', $options['defer_inline'] ) ) || apply_filters( 'autoptimize_js_filter_force_defer_inline', false ) ) { | |
| 274 | + $this->defer_inline = true; | |
| 275 | + } | |
| 276 | + | |
| 79 | 277 | // include inline? |
| 80 | 278 | if ( apply_filters( 'autoptimize_js_include_inline', $options['include_inline'] ) ) { |
| 81 | 279 | $this->include_inline = true; |
| 82 | 280 | } |
| 83 | 281 | |
| 84 | - // filter to "late inject minified JS", default to true for now (it is faster) | |
| 282 | + // filter to "late inject minified JS", default to true for now (it is faster). | |
| 85 | 283 | $this->inject_min_late = apply_filters( 'autoptimize_filter_js_inject_min_late', true ); |
| 86 | 284 | |
| 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 ); | |
| 285 | + // filters to override hardcoded do(nt)move(last) array contents (array in, array out!). | |
| 286 | + $this->dontmove = apply_filters( 'autoptimize_filter_js_dontmove', $this->dontmove ); | |
| 89 | 287 | $this->domovelast = apply_filters( 'autoptimize_filter_js_movelast', $this->domovelast ); |
| 90 | - $this->domove = apply_filters( 'autoptimize_filter_js_domove', $this->domove ); | |
| 288 | + $this->domove = apply_filters( 'autoptimize_filter_js_domove', $this->domove ); | |
| 91 | 289 | |
| 92 | - // get extra exclusions settings or filter | |
| 93 | - $excludeJS = $options['js_exclude']; | |
| 94 | - $excludeJS = apply_filters( 'autoptimize_filter_js_exclude', $excludeJS, $this->content ); | |
| 290 | + // Determine whether excluded files should be minified if not yet so. | |
| 291 | + if ( ! $options['minify_excluded'] && $options['aggregate'] ) { | |
| 292 | + $this->minify_excluded = false; | |
| 293 | + } | |
| 294 | + $this->minify_excluded = apply_filters( 'autoptimize_filter_js_minify_excluded', $this->minify_excluded, '' ); | |
| 95 | 295 | |
| 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; | |
| 296 | + // get extra exclusions settings or filter. | |
| 297 | + $exclude_js = $options['js_exclude']; | |
| 298 | + $exclude_js = apply_filters( 'autoptimize_filter_js_exclude', $exclude_js, $this->content ); | |
| 299 | + | |
| 300 | + if ( '' !== $exclude_js ) { | |
| 301 | + if ( is_array( $exclude_js ) ) { | |
| 302 | + $remove_keys = array_keys( $exclude_js, 'remove' ); | |
| 303 | + if ( false !== $remove_keys ) { | |
| 304 | + foreach ( $remove_keys as $remove_key ) { | |
| 305 | + unset( $exclude_js[ $remove_key ] ); | |
| 306 | + $this->jsremovables[] = $remove_key; | |
| 102 | 307 | } |
| 103 | 308 | } |
| 104 | - $exclJSArr = array_keys( $excludeJS ); | |
| 309 | + $excl_js_arr = array_keys( $exclude_js ); | |
| 105 | 310 | } else { |
| 106 | - $exclJSArr = array_filter( array_map( 'trim', explode( ',', $excludeJS ) ) ); | |
| 311 | + $excl_js_arr = array_filter( array_map( 'trim', explode( ',', $exclude_js ) ) ); | |
| 107 | 312 | } |
| 108 | - $this->dontmove = array_merge( $exclJSArr, $this->dontmove ); | |
| 313 | + $this->dontmove = array_merge( $excl_js_arr, $this->dontmove ); | |
| 109 | 314 | } |
| 110 | 315 | |
| 111 | 316 | // Should we add try-catch? |
| 112 | 317 | if ( $options['trycatch'] ) { |
| @@ -121,25 +326,25 @@ | ||
| 121 | 326 | } |
| 122 | 327 | |
| 123 | 328 | $this->forcehead = apply_filters( 'autoptimize_filter_js_forcehead', $this->forcehead ); |
| 124 | 329 | |
| 125 | - // get cdn url | |
| 330 | + // get cdn url. | |
| 126 | 331 | $this->cdn_url = $options['cdn_url']; |
| 127 | 332 | |
| 128 | - // noptimize me | |
| 129 | - $this->content = $this->hide_noptimize($this->content); | |
| 333 | + // noptimize me. | |
| 334 | + $this->content = $this->hide_noptimize( $this->content ); | |
| 130 | 335 | |
| 131 | - // Save IE hacks | |
| 132 | - $this->content = $this->hide_iehacks($this->content); | |
| 336 | + // Save IE hacks. | |
| 337 | + $this->content = $this->hide_iehacks( $this->content ); | |
| 133 | 338 | |
| 134 | - // comments | |
| 135 | - $this->content = $this->hide_comments($this->content); | |
| 339 | + // comments. | |
| 340 | + $this->content = $this->hide_comments( $this->content ); | |
| 136 | 341 | |
| 137 | - // Get script files | |
| 342 | + // Get script files. | |
| 138 | 343 | 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); | |
| 344 | + foreach ( $matches[0] as $tag ) { | |
| 345 | + // only consider script aggregation for types allowlisted in should_aggregate-function. | |
| 346 | + $should_aggregate = $this->should_aggregate( $tag ); | |
| 142 | 347 | if ( ! $should_aggregate ) { |
| 143 | 348 | $tag = ''; |
| 144 | 349 | continue; |
| 145 | 350 | } |
| @@ -144,93 +349,129 @@ | ||
| 144 | 349 | continue; |
| 145 | 350 | } |
| 146 | 351 | |
| 147 | 352 | if ( preg_match( '#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source ) ) { |
| 148 | - // non-inline script | |
| 149 | - if ( $this->isremovable($tag, $this->jsremovables) ) { | |
| 353 | + // non-inline script. | |
| 354 | + if ( $this->isremovable( $tag, $this->jsremovables ) ) { | |
| 150 | 355 | $this->content = str_replace( $tag, '', $this->content ); |
| 151 | 356 | continue; |
| 152 | 357 | } |
| 153 | 358 | |
| 154 | - $origTag = null; | |
| 155 | - $url = current( explode( '?', $source[2], 2 ) ); | |
| 156 | - $path = $this->getpath($url); | |
| 157 | - if ( false !== $path && preg_match( '#\.js$#', $path ) && $this->ismergeable($tag) ) { | |
| 158 | - // ok to optimize, add to array | |
| 359 | + $orig_tag = null; | |
| 360 | + $url = current( explode( '?', $source[2], 2 ) ); | |
| 361 | + $path = $this->getpath( $url ); | |
| 362 | + if ( false !== $path && preg_match( '#\.js$#', $path ) && $this->ismergeable( $tag ) ) { | |
| 363 | + // ok to optimize, add to array. | |
| 159 | 364 | $this->scripts[] = $path; |
| 160 | 365 | } else { |
| 161 | - $origTag = $tag; | |
| 162 | - $newTag = $tag; | |
| 366 | + $orig_tag = $tag; | |
| 367 | + $new_tag = $tag; | |
| 163 | 368 | |
| 164 | - // non-mergeable script (excluded or dynamic or external) | |
| 165 | - if ( is_array( $excludeJS ) ) { | |
| 369 | + // non-mergeable script (excluded or dynamic or external). | |
| 370 | + if ( is_array( $exclude_js ) ) { | |
| 166 | 371 | // 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 ); | |
| 372 | + foreach ( $exclude_js as $excl_tag => $excl_flags ) { | |
| 373 | + if ( false !== strpos( $orig_tag, $excl_tag ) && in_array( $excl_flags, array( 'async', 'defer' ) ) ) { | |
| 374 | + $new_tag = str_replace( '<script ', '<script ' . $excl_flags . ' ', $new_tag ); | |
| 170 | 375 | } |
| 171 | 376 | } |
| 172 | 377 | } |
| 173 | 378 | |
| 379 | + // not aggregating but deferring? | |
| 380 | + if ( $this->defer_not_aggregate && false === $this->aggregate && ( str_replace( $this->dontmove, '', $path ) === $path || ( apply_filters( 'autoptimize_filter_js_defer_external', true ) && str_replace( $this->dontmove, '', $orig_tag ) === $orig_tag ) ) && strpos( $new_tag, ' defer' ) === false && strpos( $new_tag, ' async' ) === false ) { | |
| 381 | + $new_tag = str_replace( '<script ', '<script defer ', $new_tag ); | |
| 382 | + } | |
| 383 | + | |
| 174 | 384 | // 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 ); | |
| 385 | + // -> if aggregate is on and exclude minify is on | |
| 386 | + // -> if aggregate is off and the file is not in dontmove. | |
| 387 | + if ( $path && $this->minify_excluded ) { | |
| 388 | + $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); | |
| 389 | + if ( ( false === $this->aggregate && str_replace( $this->dontmove, '', $path ) === $path ) || ( true === $this->aggregate && ( false === $consider_minified_array || str_replace( $consider_minified_array, '', $path ) === $path ) ) ) { | |
| 390 | + $minified_url = $this->minify_single( $path ); | |
| 391 | + if ( ! empty( $minified_url ) ) { | |
| 392 | + // Replace original URL with minified URL from cache. | |
| 393 | + $new_tag = str_replace( $url, $minified_url, $new_tag ); | |
| 394 | + } elseif ( apply_filters( 'autoptimize_filter_ccsjs_remove_empty_minified_url', false ) ) { | |
| 395 | + // Remove the original script tag, because cache content is empty but only if filter | |
| 396 | + // is trued because $minified_url is also false if original JS is minified already. | |
| 397 | + $new_tag = ''; | |
| 398 | + } | |
| 180 | 399 | } |
| 181 | 400 | } |
| 182 | 401 | |
| 183 | - if ( $this->ismovable($newTag) ) { | |
| 184 | - // can be moved, flags and all | |
| 185 | - if ( $this->movetolast($newTag) ) { | |
| 186 | - $this->move['last'][] = $newTag; | |
| 402 | + // Check if we still need to CDN (esp. for already minified resources). | |
| 403 | + if ( ! empty( $this->cdn_url ) || has_filter( 'autoptimize_filter_base_replace_cdn' ) ) { | |
| 404 | + $new_tag = str_replace( $url, $this->url_replace_cdn( $url ), $new_tag ); | |
| 405 | + } | |
| 406 | + | |
| 407 | + if ( $this->ismovable( $new_tag ) ) { | |
| 408 | + // can be moved, flags and all. | |
| 409 | + if ( $this->movetolast( $new_tag ) ) { | |
| 410 | + $this->move['last'][] = $new_tag; | |
| 187 | 411 | } else { |
| 188 | - $this->move['first'][] = $newTag; | |
| 412 | + $this->move['first'][] = $new_tag; | |
| 189 | 413 | } |
| 190 | 414 | } 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 = ''; | |
| 415 | + // cannot be moved, so if flag was added re-inject altered tag immediately. | |
| 416 | + if ( ( '' !== $new_tag && $orig_tag !== $new_tag ) || ( '' === $new_tag && apply_filters( 'autoptimize_filter_js_remove_empty_files', false ) ) ) { | |
| 417 | + $this->content = str_replace( $orig_tag, $new_tag, $this->content ); | |
| 418 | + $orig_tag = ''; | |
| 195 | 419 | } |
| 196 | - // and forget about the $tag (not to be touched any more) | |
| 420 | + // and forget about the $tag (not to be touched any more). | |
| 197 | 421 | $tag = ''; |
| 198 | 422 | } |
| 199 | 423 | } |
| 200 | 424 | } else { |
| 201 | - // Inline script | |
| 202 | - if ( $this->isremovable($tag, $this->jsremovables) ) { | |
| 425 | + // Inline script. | |
| 426 | + if ( $this->isremovable( $tag, $this->jsremovables ) ) { | |
| 203 | 427 | $this->content = str_replace( $tag, '', $this->content ); |
| 204 | 428 | continue; |
| 205 | 429 | } |
| 206 | 430 | |
| 207 | - // unhide comments, as javascript may be wrapped in comment-tags for old times' sake | |
| 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 ); | |
| 431 | + // unhide comments, as javascript may be wrapped in comment-tags for old times' sake. | |
| 432 | + $tag = $this->restore_comments( $tag ); | |
| 433 | + if ( $this->ismergeable( $tag ) && $this->include_inline ) { | |
| 434 | + preg_match( '#<script.*>(.*)</script>#Usmi', $tag, $code ); | |
| 435 | + $code = preg_replace( '#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1] ); | |
| 436 | + $code = preg_replace( '/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code ); | |
| 213 | 437 | $this->scripts[] = 'INLINE;' . $code; |
| 214 | 438 | } else { |
| 215 | - // 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 ) { | |
| 219 | - $this->move['last'][] = $tag; | |
| 439 | + $_inline_deferable = apply_filters( 'autoptimize_filters_js_inline_deferable', array( 'nonce', 'post_id', 'syntaxhighlighter' ) ); | |
| 440 | + $_inline_dontmove = array_values( array_diff( $this->dontmove, $_inline_deferable ) ); | |
| 441 | + if ( false === $this->defer_inline ) { | |
| 442 | + // Can we move this? | |
| 443 | + $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $tag ); | |
| 444 | + if ( $this->ismovable( $tag ) || '' !== $autoptimize_js_moveable ) { | |
| 445 | + if ( $this->movetolast( $tag ) || 'last' === $autoptimize_js_moveable ) { | |
| 446 | + $this->move['last'][] = $tag; | |
| 447 | + } else { | |
| 448 | + $this->move['first'][] = $tag; | |
| 449 | + } | |
| 220 | 450 | } else { |
| 221 | - $this->move['first'][] = $tag; | |
| 451 | + $tag = ''; | |
| 222 | 452 | } |
| 453 | + } else if ( str_replace( $_inline_dontmove, '', $tag ) === $tag && strlen( $tag ) < apply_filters( 'autoptimize_filter_script_defer_inline_maxsize', 200000 ) ) { | |
| 454 | + // defer inline JS by base64 encoding it but only if string is not ridiculously huge (to avoid issues with below regex mainly). | |
| 455 | + preg_match( '#<script(?:[^>](?!id=))*\s*(?:id=(["\'])([^"\']+)\1)*+[^>]*+>(.*?)<\/script>#is', $tag, $match ); | |
| 456 | + if ( $match[2] ) { | |
| 457 | + $_id = 'id="' . $match[2] . '" '; | |
| 458 | + } else { | |
| 459 | + $_id = ''; | |
| 460 | + } | |
| 461 | + | |
| 462 | + $new_tag = '<script defer ' . $_id . 'src="data:text/javascript;base64,' . base64_encode( $match[3] ) . '"></script>'; | |
| 463 | + $this->content = str_replace( $tag, $new_tag, $this->content ); | |
| 464 | + $tag = ''; | |
| 223 | 465 | } else { |
| 224 | - // We shouldn't touch this | |
| 225 | 466 | $tag = ''; |
| 226 | 467 | } |
| 227 | 468 | } |
| 228 | - // Re-hide comments to be able to do the removal based on tag from $this->content | |
| 229 | - $tag = $this->hide_comments($tag); | |
| 469 | + // Re-hide comments to be able to do the removal based on tag from $this->content. | |
| 470 | + $tag = $this->hide_comments( $tag ); | |
| 230 | 471 | } |
| 231 | 472 | |
| 232 | - //Remove the original script tag | |
| 473 | + // Remove the original script tag. | |
| 233 | 474 | $this->content = str_replace( $tag, '', $this->content ); |
| 234 | 475 | } |
| 235 | 476 | |
| 236 | 477 | return true; |
| @@ -235,9 +476,9 @@ | ||
| 235 | 476 | |
| 236 | 477 | return true; |
| 237 | 478 | } |
| 238 | 479 | |
| 239 | - // No script files, great ;-) | |
| 480 | + // No script files, great ;-) . | |
| 240 | 481 | return false; |
| 241 | 482 | } |
| 242 | 483 | |
| 243 | 484 | /** |
| @@ -251,17 +492,21 @@ | ||
| 251 | 492 | * Everything else should return false. |
| 252 | 493 | * |
| 253 | 494 | * @link https://developer.mozilla.org/en/docs/Web/HTML/Element/script#attr-type |
| 254 | 495 | * |
| 255 | - * @param string $tag | |
| 496 | + * @param string $tag Script node & child(ren). | |
| 256 | 497 | * @return bool |
| 257 | 498 | */ |
| 258 | - public function should_aggregate($tag) | |
| 499 | + public static function should_aggregate( $tag ) | |
| 259 | 500 | { |
| 501 | + if ( empty( $tag ) ) { | |
| 502 | + return false; | |
| 503 | + } | |
| 504 | + | |
| 260 | 505 | // We're only interested in the type attribute of the <script> tag itself, not any possible |
| 261 | 506 | // inline code that might just contain the 'type=' string... |
| 262 | 507 | $tag_parts = array(); |
| 263 | - preg_match( '#<(script[^>]*)>#i', $tag, $tag_parts); | |
| 508 | + preg_match( '#<(script[^>]*)>#i', $tag, $tag_parts ); | |
| 264 | 509 | $tag_without_contents = null; |
| 265 | 510 | if ( ! empty( $tag_parts[1] ) ) { |
| 266 | 511 | $tag_without_contents = $tag_parts[1]; |
| 267 | 512 | } |
| @@ -280,15 +525,21 @@ | ||
| 280 | 525 | |
| 281 | 526 | return $should_aggregate; |
| 282 | 527 | } |
| 283 | 528 | |
| 284 | - //Joins and optimizes JS | |
| 529 | + /** | |
| 530 | + * Joins and optimizes JS. | |
| 531 | + */ | |
| 285 | 532 | public function minify() |
| 286 | 533 | { |
| 287 | 534 | foreach ( $this->scripts as $script ) { |
| 288 | - // TODO/FIXME: some duplicate code here, can be reduced/simplified | |
| 535 | + if ( empty( $script ) ) { | |
| 536 | + continue; | |
| 537 | + } | |
| 538 | + | |
| 539 | + // TODO/FIXME: some duplicate code here, can be reduced/simplified. | |
| 289 | 540 | if ( preg_match( '#^INLINE;#', $script ) ) { |
| 290 | - // Inline script | |
| 541 | + // Inline script. | |
| 291 | 542 | $script = preg_replace( '#^INLINE;#', '', $script ); |
| 292 | 543 | $script = rtrim( $script, ";\n\t\r" ) . ';'; |
| 293 | 544 | // Add try-catch? |
| 294 | 545 | if ( $this->trycatch ) { |
| @@ -295,14 +546,14 @@ | ||
| 295 | 546 | $script = 'try{' . $script . '}catch(e){}'; |
| 296 | 547 | } |
| 297 | 548 | $tmpscript = apply_filters( 'autoptimize_js_individual_script', $script, '' ); |
| 298 | 549 | if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscript ) ) { |
| 299 | - $script = $tmpscript; | |
| 550 | + $script = $tmpscript; | |
| 300 | 551 | $this->alreadyminified = true; |
| 301 | 552 | } |
| 302 | 553 | $this->jscode .= "\n" . $script; |
| 303 | 554 | } else { |
| 304 | - // External script | |
| 555 | + // External script. | |
| 305 | 556 | if ( false !== $script && file_exists( $script ) && is_readable( $script ) ) { |
| 306 | 557 | $scriptsrc = file_get_contents( $script ); |
| 307 | 558 | $scriptsrc = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc ); |
| 308 | 559 | $scriptsrc = rtrim( $scriptsrc, ";\n\t\r" ) . ';'; |
| @@ -311,23 +562,21 @@ | ||
| 311 | 562 | $scriptsrc = 'try{' . $scriptsrc . '}catch(e){}'; |
| 312 | 563 | } |
| 313 | 564 | $tmpscriptsrc = apply_filters( 'autoptimize_js_individual_script', $scriptsrc, $script ); |
| 314 | 565 | if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscriptsrc ) ) { |
| 315 | - $scriptsrc = $tmpscriptsrc; | |
| 566 | + $scriptsrc = $tmpscriptsrc; | |
| 316 | 567 | $this->alreadyminified = true; |
| 317 | - } else if ( $this->can_inject_late($script) ) { | |
| 318 | - $scriptsrc = self::build_injectlater_marker($script, md5($scriptsrc)); | |
| 568 | + } elseif ( $this->can_inject_late( $script ) ) { | |
| 569 | + $scriptsrc = self::build_injectlater_marker( $script, md5( $scriptsrc ) ); | |
| 319 | 570 | } |
| 320 | 571 | $this->jscode .= "\n" . $scriptsrc; |
| 321 | - }/*else{ | |
| 322 | - //Couldn't read JS. Maybe getpath isn't working? | |
| 323 | - }*/ | |
| 572 | + } | |
| 324 | 573 | } |
| 325 | 574 | } |
| 326 | 575 | |
| 327 | - // Check for already-minified code | |
| 576 | + // Check for already-minified code. | |
| 328 | 577 | $this->md5hash = md5( $this->jscode ); |
| 329 | - $ccheck = new autoptimizeCache($this->md5hash, 'js'); | |
| 578 | + $ccheck = new autoptimizeCache( $this->md5hash, 'js' ); | |
| 330 | 579 | if ( $ccheck->check() ) { |
| 331 | 580 | $this->jscode = $ccheck->retrieve(); |
| 332 | 581 | return true; |
| 333 | 582 | } |
| @@ -353,51 +602,59 @@ | ||
| 353 | 602 | $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
| 354 | 603 | return true; |
| 355 | 604 | } |
| 356 | 605 | |
| 357 | - // Caches the JS in uncompressed, deflated and gzipped form. | |
| 606 | + /** | |
| 607 | + * Caches the JS in uncompressed, deflated and gzipped form. | |
| 608 | + */ | |
| 358 | 609 | public function cache() |
| 359 | 610 | { |
| 360 | - $cache = new autoptimizeCache($this->md5hash, 'js'); | |
| 611 | + $cache = new autoptimizeCache( $this->md5hash, 'js' ); | |
| 361 | 612 | if ( ! $cache->check() ) { |
| 362 | - // Cache our code | |
| 363 | - $cache->cache($this->jscode, 'text/javascript'); | |
| 613 | + // Cache our code. | |
| 614 | + $cache->cache( $this->jscode, 'text/javascript' ); | |
| 364 | 615 | } |
| 365 | 616 | $this->url = AUTOPTIMIZE_CACHE_URL . $cache->getname(); |
| 366 | - $this->url = $this->url_replace_cdn($this->url); | |
| 617 | + $this->url = $this->url_replace_cdn( $this->url ); | |
| 367 | 618 | } |
| 368 | 619 | |
| 369 | - // Returns the content | |
| 620 | + /** | |
| 621 | + * Returns the content. | |
| 622 | + */ | |
| 370 | 623 | public function getcontent() |
| 371 | 624 | { |
| 372 | - // Restore the full content | |
| 625 | + // Restore the full content. | |
| 373 | 626 | if ( ! empty( $this->restofcontent ) ) { |
| 374 | - $this->content .= $this->restofcontent; | |
| 627 | + $this->content .= $this->restofcontent; | |
| 375 | 628 | $this->restofcontent = ''; |
| 376 | 629 | } |
| 377 | 630 | |
| 378 | - // Add the scripts taking forcehead/ deferred (default) into account | |
| 631 | + // Add the scripts taking forcehead/ deferred (default) into account. | |
| 379 | 632 | if ( $this->forcehead ) { |
| 380 | - $replaceTag = array( '</head>', 'before' ); | |
| 381 | - $defer = ''; | |
| 633 | + $replace_tag = array( '</head>', 'before' ); | |
| 634 | + $defer = ''; | |
| 382 | 635 | } else { |
| 383 | - $replaceTag = array( '</body>', 'before' ); | |
| 384 | - $defer = 'defer '; | |
| 636 | + $replace_tag = array( '</body>', 'before' ); | |
| 637 | + $defer = 'defer '; | |
| 385 | 638 | } |
| 386 | 639 | |
| 387 | - $defer = apply_filters( 'autoptimize_filter_js_defer', $defer ); | |
| 640 | + $defer = apply_filters( 'autoptimize_filter_js_defer', $defer ); | |
| 641 | + $type_js = ''; | |
| 642 | + if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { | |
| 643 | + $type_js = 'type="text/javascript" '; | |
| 644 | + } | |
| 388 | 645 | |
| 389 | - $bodyreplacementpayload = '<script type="text/javascript" ' . $defer . 'src="' . $this->url . '"></script>'; | |
| 646 | + $bodyreplacementpayload = '<script ' . $type_js . $defer . 'src="' . $this->url . '"></script>'; | |
| 390 | 647 | $bodyreplacementpayload = apply_filters( 'autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload ); |
| 391 | 648 | |
| 392 | - $bodyreplacement = implode( '', $this->move['first'] ); | |
| 649 | + $bodyreplacement = implode( '', $this->move['first'] ); | |
| 393 | 650 | $bodyreplacement .= $bodyreplacementpayload; |
| 394 | 651 | $bodyreplacement .= implode( '', $this->move['last'] ); |
| 395 | 652 | |
| 396 | - $replaceTag = apply_filters( 'autoptimize_filter_js_replacetag', $replaceTag ); | |
| 653 | + $replace_tag = apply_filters( 'autoptimize_filter_js_replacetag', $replace_tag ); | |
| 397 | 654 | |
| 398 | 655 | if ( strlen( $this->jscode ) > 0 ) { |
| 399 | - $this->inject_in_html( $bodyreplacement, $replaceTag ); | |
| 656 | + $this->inject_in_html( $bodyreplacement, $replace_tag ); | |
| 400 | 657 | } |
| 401 | 658 | |
| 402 | 659 | // Restore comments. |
| 403 | 660 | $this->content = $this->restore_comments( $this->content ); |
| @@ -411,86 +668,98 @@ | ||
| 411 | 668 | // Return the modified HTML. |
| 412 | 669 | return $this->content; |
| 413 | 670 | } |
| 414 | 671 | |
| 415 | - // Checks against the white- and blacklists | |
| 416 | - private function ismergeable($tag) | |
| 672 | + /** | |
| 673 | + * Checks against the allow- and blocklists. | |
| 674 | + * | |
| 675 | + * @param string $tag JS tag. | |
| 676 | + */ | |
| 677 | + private function ismergeable( $tag ) | |
| 417 | 678 | { |
| 418 | - if ( ! $this->aggregate ) { | |
| 679 | + if ( empty( $tag ) || ! $this->aggregate ) { | |
| 419 | 680 | return false; |
| 420 | 681 | } |
| 421 | 682 | |
| 422 | - if ( ! empty( $this->whitelist ) ) { | |
| 423 | - foreach ( $this->whitelist as $match ) { | |
| 424 | - if (false !== strpos( $tag, $match ) ) { | |
| 683 | + if ( ! empty( $this->allowlist ) ) { | |
| 684 | + foreach ( $this->allowlist as $match ) { | |
| 685 | + if ( false !== strpos( $tag, $match ) ) { | |
| 425 | 686 | return true; |
| 426 | 687 | } |
| 427 | 688 | } |
| 428 | - // no match with whitelist | |
| 689 | + // No match with allowlist. | |
| 429 | 690 | return false; |
| 430 | 691 | } else { |
| 431 | - foreach($this->domove as $match) { | |
| 692 | + foreach ( $this->domove as $match ) { | |
| 432 | 693 | if ( false !== strpos( $tag, $match ) ) { |
| 433 | - // Matched something | |
| 694 | + // Matched something. | |
| 434 | 695 | return false; |
| 435 | 696 | } |
| 436 | 697 | } |
| 437 | 698 | |
| 438 | - if ( $this->movetolast($tag) ) { | |
| 699 | + if ( $this->movetolast( $tag ) ) { | |
| 439 | 700 | return false; |
| 440 | 701 | } |
| 441 | 702 | |
| 442 | - foreach( $this->dontmove as $match ) { | |
| 703 | + foreach ( $this->dontmove as $match ) { | |
| 443 | 704 | if ( false !== strpos( $tag, $match ) ) { |
| 444 | - // Matched something | |
| 705 | + // Matched something. | |
| 445 | 706 | return false; |
| 446 | 707 | } |
| 447 | 708 | } |
| 448 | 709 | |
| 449 | - // If we're here it's safe to merge | |
| 710 | + // If we're here it's safe to merge. | |
| 450 | 711 | return true; |
| 451 | 712 | } |
| 452 | 713 | } |
| 453 | 714 | |
| 454 | - // Checks agains the blacklist | |
| 455 | - private function ismovable($tag) | |
| 715 | + /** | |
| 716 | + * Checks agains the blocklist. | |
| 717 | + * | |
| 718 | + * @param string $tag tag to check for blocklist (exclusions). | |
| 719 | + */ | |
| 720 | + private function ismovable( $tag ) | |
| 456 | 721 | { |
| 457 | - if ( true !== $this->include_inline || apply_filters( 'autoptimize_filter_js_unmovable', true ) ) { | |
| 722 | + if ( empty( $tag ) || true !== $this->include_inline || apply_filters( 'autoptimize_filter_js_unmovable', true ) ) { | |
| 458 | 723 | return false; |
| 459 | 724 | } |
| 460 | 725 | |
| 461 | 726 | foreach ( $this->domove as $match ) { |
| 462 | 727 | if ( false !== strpos( $tag, $match ) ) { |
| 463 | - // Matched something | |
| 728 | + // Matched something. | |
| 464 | 729 | return true; |
| 465 | 730 | } |
| 466 | 731 | } |
| 467 | 732 | |
| 468 | - if ( $this->movetolast($tag) ) { | |
| 733 | + if ( $this->movetolast( $tag ) ) { | |
| 469 | 734 | return true; |
| 470 | 735 | } |
| 471 | 736 | |
| 472 | 737 | foreach ( $this->dontmove as $match ) { |
| 473 | 738 | if ( false !== strpos( $tag, $match ) ) { |
| 474 | - // Matched something | |
| 739 | + // Matched something. | |
| 475 | 740 | return false; |
| 476 | 741 | } |
| 477 | 742 | } |
| 478 | 743 | |
| 479 | - // If we're here it's safe to move | |
| 744 | + // If we're here it's safe to move. | |
| 480 | 745 | return true; |
| 481 | 746 | } |
| 482 | 747 | |
| 483 | - private function movetolast($tag) | |
| 748 | + private function movetolast( $tag ) | |
| 484 | 749 | { |
| 750 | + if ( empty( $tag ) ) { | |
| 751 | + return false; | |
| 752 | + } | |
| 753 | + | |
| 485 | 754 | foreach ( $this->domovelast as $match ) { |
| 486 | 755 | if ( false !== strpos( $tag, $match ) ) { |
| 487 | - // Matched, return true | |
| 756 | + // Matched, return true. | |
| 488 | 757 | return true; |
| 489 | 758 | } |
| 490 | 759 | } |
| 491 | 760 | |
| 492 | - // Should be in 'first' | |
| 761 | + // Should be in 'first'. | |
| 493 | 762 | return false; |
| 494 | 763 | } |
| 495 | 764 | |
| 496 | 765 | /** |
| @@ -496,24 +765,24 @@ | ||
| 496 | 765 | /** |
| 497 | 766 | * Determines wheter a <script> $tag can be excluded from minification (as already minified) based on: |
| 498 | 767 | * - inject_min_late being active |
| 499 | 768 | * - filename ending in `min.js` |
| 500 | - * - filename matching `js/jquery/jquery.js` (wordpress core jquery, is minified) | |
| 769 | + * - filename matching `js/jquery/jquery.js` (WordPress core jquery, is minified) | |
| 501 | 770 | * - filename matching one passed in the consider minified filter |
| 502 | 771 | * |
| 503 | - * @param string $jsPath | |
| 772 | + * @param string $js_path Path to JS file. | |
| 504 | 773 | * @return bool |
| 505 | 774 | */ |
| 506 | - private function can_inject_late($jsPath) { | |
| 775 | + private function can_inject_late( $js_path ) { | |
| 507 | 776 | $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); |
| 508 | 777 | if ( true !== $this->inject_min_late ) { |
| 509 | - // late-inject turned off | |
| 778 | + // late-inject turned off. | |
| 510 | 779 | 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 | |
| 780 | + } elseif ( ( false === strpos( $js_path, 'min.js' ) ) && ( false === strpos( $js_path, 'wp-includes/js/jquery/jquery.js' ) ) && ( str_replace( $consider_minified_array, '', $js_path ) === $js_path ) ) { | |
| 781 | + // file not minified based on filename & filter. | |
| 513 | 782 | return false; |
| 514 | 783 | } else { |
| 515 | - // phew, all is safe, we can late-inject | |
| 784 | + // phew, all is safe, we can late-inject. | |
| 516 | 785 | return true; |
| 517 | 786 | } |
| 518 | 787 | } |
| 519 | 788 | |
| @@ -530,9 +799,9 @@ | ||
| 530 | 799 | /** |
| 531 | 800 | * Minifies a single local js file and returns its (cached) url. |
| 532 | 801 | * |
| 533 | 802 | * @param string $filepath Filepath. |
| 534 | - * @param bool $cache_miss Optional. Force a cache miss. Default false. | |
| 803 | + * @param bool $cache_miss Optional. Force a cache miss. Default false. | |
| 535 | 804 | * |
| 536 | 805 | * @return bool|string Url pointing to the minified js file or false. |
| 537 | 806 | */ |
| 538 | 807 | public function minify_single( $filepath, $cache_miss = false ) |
| @@ -549,8 +818,17 @@ | ||
| 549 | 818 | |
| 550 | 819 | // If not in cache already, minify... |
| 551 | 820 | if ( ! $cache->check() || $cache_miss ) { |
| 552 | 821 | $contents = trim( JSMin::minify( $contents ) ); |
| 822 | + | |
| 823 | + // Check if minified cache content is empty. | |
| 824 | + if ( empty( $contents ) ) { | |
| 825 | + return false; | |
| 826 | + } | |
| 827 | + | |
| 828 | + // Filter contents of excluded minified CSS. | |
| 829 | + $contents = apply_filters( 'autoptimize_filter_js_single_after_minify', $contents ); | |
| 830 | + | |
| 553 | 831 | // Store in cache. |
| 554 | 832 | $cache->cache( $contents, 'text/javascript' ); |
| 555 | 833 | } |
| 556 | 834 | |