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