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