PluginProbe
Autoptimize / 3.1.4
Autoptimize v3.1.4
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 +76 -10 2.7.83.1.4 View file →
@@ -57,10 +57,14 @@
57 57 '_stq',
58 58 'nonce',
59 59 'post_id',
60 60 'data-noptimize',
61 + 'data-cfasync',
62 + 'data-pagespeed-no-defer',
61 63 'logHuman',
62 64 'amp-mobile-version-switcher',
65 + 'data-rocketlazyloadscript',
66 + 'rocket-browser-checker-js-after',
63 67 );
64 68
65 69 /**
66 70 * List of to be moved JS.
@@ -109,8 +113,22 @@
109 113 */
110 114 private $aggregate = true;
111 115
112 116 /**
117 + * Setting; if not aggregated, should we defer?
118 + *
119 + * @var bool
120 + */
121 + private $defer_not_aggregate = false;
122 +
123 + /**
124 + * Setting; defer inline JS?
125 + *
126 + * @var bool
127 + */
128 + private $defer_inline = false;
129 +
130 + /**
113 131 * Setting; try/catch wrapping or not.
114 132 *
115 133 * @var bool
116 134 */
@@ -200,9 +218,19 @@
200 218 * @param array $options all options.
201 219 */
202 220 public function read( $options )
203 221 {
204 - $noptimize_js = apply_filters( 'autoptimize_filter_js_noptimize', false, $this->content );
222 + $noptimize_js = false;
223 +
224 + // If page/ post check post_meta to see if optimize is off.
225 + if ( false === autoptimizeConfig::get_post_meta_ao_settings( 'ao_post_js_optimize' ) ) {
226 + $noptimize_js = true;
227 + }
228 +
229 + // And a filter to enforce JS noptimize.
230 + $noptimize_js = apply_filters( 'autoptimize_filter_js_noptimize', $noptimize_js, $this->content );
231 +
232 + // And finally bail if noptimize_js is true.
205 233 if ( $noptimize_js ) {
206 234 return false;
207 235 }
208 236
@@ -207,9 +235,8 @@
207 235 }
208 236
209 237 // only optimize known good JS?
210 238 $allowlist_js = apply_filters( 'autoptimize_filter_js_allowlist', '', $this->content );
211 - $allowlist_js = apply_filters( 'autoptimize_filter_js_whitelist', $allowlist_js, $this->content ); // fixme: to be removed in next version.
212 239 if ( ! empty( $allowlist_js ) ) {
213 240 $this->allowlist = array_filter( array_map( 'trim', explode( ',', $allowlist_js ) ) );
214 241 }
215 242
@@ -233,9 +260,21 @@
233 260 // Returning true for "dontaggregate" turns off aggregation.
234 261 if ( $this->aggregate && apply_filters( 'autoptimize_filter_js_dontaggregate', false ) ) {
235 262 $this->aggregate = false;
236 263 }
264 + // and the filter that should have been there to begin with.
265 + $this->aggregate = apply_filters( 'autoptimize_filter_js_aggregate', $this->aggregate );
237 266
267 + // Defer when not aggregating.
268 + if ( false === $this->aggregate && apply_filters( 'autoptimize_filter_js_defer_not_aggregate', $options['defer_not_aggregate'] ) ) {
269 + $this->defer_not_aggregate = true;
270 + }
271 +
272 + // Defer inline JS?
273 + if ( ( true === $this->defer_not_aggregate && apply_filters( 'autoptimize_js_filter_defer_inline', $options['defer_inline'] ) ) || apply_filters( 'autoptimize_js_filter_force_defer_inline', false ) ) {
274 + $this->defer_inline = true;
275 + }
276 +
238 277 // include inline?
239 278 if ( apply_filters( 'autoptimize_js_include_inline', $options['include_inline'] ) ) {
240 279 $this->include_inline = true;
241 280 }
@@ -336,8 +375,13 @@
336 375 }
337 376 }
338 377 }
339 378
379 + // not aggregating but deferring?
380 + if ( $this->defer_not_aggregate && false === $this->aggregate && ( str_replace( $this->dontmove, '', $path ) === $path || ( apply_filters( 'autoptimize_filter_js_defer_external', true ) && str_replace( $this->dontmove, '', $orig_tag ) === $orig_tag ) ) && strpos( $new_tag, ' defer' ) === false && strpos( $new_tag, ' async' ) === false ) {
381 + $new_tag = str_replace( '<script ', '<script defer ', $new_tag );
382 + }
383 +
340 384 // Should we minify the non-aggregated script?
341 385 // -> if aggregate is on and exclude minify is on
342 386 // -> if aggregate is off and the file is not in dontmove.
343 387 if ( $path && $this->minify_excluded ) {
@@ -354,8 +398,13 @@
354 398 }
355 399 }
356 400 }
357 401
402 + // Check if we still need to CDN (esp. for already minified resources).
403 + if ( ! empty( $this->cdn_url ) || has_filter( 'autoptimize_filter_base_replace_cdn' ) ) {
404 + $new_tag = str_replace( $url, $this->url_replace_cdn( $url ), $new_tag );
405 + }
406 +
358 407 if ( $this->ismovable( $new_tag ) ) {
359 408 // can be moved, flags and all.
360 409 if ( $this->movetolast( $new_tag ) ) {
361 410 $this->move['last'][] = $new_tag;
@@ -386,18 +435,35 @@
386 435 $code = preg_replace( '#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1] );
387 436 $code = preg_replace( '/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code );
388 437 $this->scripts[] = 'INLINE;' . $code;
389 438 } else {
390 - // Can we move this?
391 - $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $tag );
392 - if ( $this->ismovable( $tag ) || '' !== $autoptimize_js_moveable ) {
393 - if ( $this->movetolast( $tag ) || 'last' === $autoptimize_js_moveable ) {
394 - $this->move['last'][] = $tag;
439 + $_inline_deferable = apply_filters( 'autoptimize_filters_js_inline_deferable', array( 'nonce', 'post_id', 'syntaxhighlighter' ) );
440 + $_inline_dontmove = array_values( array_diff( $this->dontmove, $_inline_deferable ) );
441 + if ( false === $this->defer_inline ) {
442 + // Can we move this?
443 + $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $tag );
444 + if ( $this->ismovable( $tag ) || '' !== $autoptimize_js_moveable ) {
445 + if ( $this->movetolast( $tag ) || 'last' === $autoptimize_js_moveable ) {
446 + $this->move['last'][] = $tag;
447 + } else {
448 + $this->move['first'][] = $tag;
449 + }
395 450 } else {
396 - $this->move['first'][] = $tag;
451 + $tag = '';
397 452 }
453 + } else if ( str_replace( $_inline_dontmove, '', $tag ) === $tag && strlen( $tag ) < apply_filters( 'autoptimize_filter_script_defer_inline_maxsize', 200000 ) ) {
454 + // defer inline JS by base64 encoding it but only if string is not ridiculously huge (to avoid issues with below regex mainly).
455 + preg_match( '#<script(?:[^>](?!id=))*\s*(?:id=(["\'])([^"\']+)\1)*+[^>]*+>(.*?)<\/script>#is', $tag, $match );
456 + if ( $match[2] ) {
457 + $_id = 'id="' . $match[2] . '" ';
458 + } else {
459 + $_id = '';
460 + }
461 +
462 + $new_tag = '<script defer ' . $_id . 'src="data:text/javascript;base64,' . base64_encode( $match[3] ) . '"></script>';
463 + $this->content = str_replace( $tag, $new_tag, $this->content );
464 + $tag = '';
398 465 } else {
399 - // We shouldn't touch this.
400 466 $tag = '';
401 467 }
402 468 }
403 469 // Re-hide comments to be able to do the removal based on tag from $this->content.
@@ -429,9 +495,9 @@
429 495 *
430 496 * @param string $tag Script node & child(ren).
431 497 * @return bool
432 498 */
433 - public function should_aggregate( $tag )
499 + public static function should_aggregate( $tag )
434 500 {
435 501 if ( empty( $tag ) ) {
436 502 return false;
437 503 }