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