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/autoptimizeExtra.php +88 -21 2.6.23.1.4 View file →
@@ -63,9 +63,9 @@
63 63 add_action( 'admin_menu', array( $this, 'admin_menu' ) );
64 64 }
65 65 add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_extra_tab' ) );
66 66 } else {
67 - $this->run_on_frontend();
67 + add_action( 'wp', array( $this, 'run_on_frontend' ) );
68 68 }
69 69 }
70 70
71 71 public function set_options( array $options )
@@ -116,8 +116,10 @@
116 116 public function filter_remove_qs( $src )
117 117 {
118 118 if ( strpos( $src, '?ver=' ) ) {
119 119 $src = remove_query_arg( 'ver', $src );
120 + } elseif ( strpos( $src, '?v=' ) ) {
121 + $src = remove_query_arg( 'v', $src );
120 122 }
121 123
122 124 return $src;
123 125 }
@@ -128,9 +130,9 @@
128 130 if ( ! empty( $in ) ) {
129 131 $exclusions = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $in ) ) ), '' );
130 132 }
131 133
132 - $settings = $this->options['autoptimize_extra_text_field_3'];
134 + $settings = wp_strip_all_tags( $this->options['autoptimize_extra_text_field_3'] );
133 135 $async = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $settings ) ) ), '' );
134 136 $attr = apply_filters( 'autoptimize_filter_extra_async', 'async' );
135 137 foreach ( $async as $k => $v ) {
136 138 $async[ $k ] = $attr;
@@ -141,10 +143,18 @@
141 143
142 144 return $merged;
143 145 }
144 146
145 - protected function run_on_frontend()
147 + public function run_on_frontend()
146 148 {
149 + // only run the Extra optimizations on frontend if general conditions
150 + // for optimizations are met, this to ensure e.g. removing querystrings
151 + // is not done when optimizing for logged in users is off, breaking
152 + // some pagebuilders (Divi & Elementor).
153 + if ( false === autoptimizeMain::should_buffer() ) {
154 + return;
155 + }
156 +
147 157 $options = $this->options;
148 158
149 159 // Disable emojis if specified.
150 160 if ( ! empty( $options['autoptimize_extra_checkbox_field_1'] ) ) {
@@ -167,8 +177,17 @@
167 177 if ( ! empty( $options['autoptimize_extra_radio_field_4'] ) && ( '1' !== $options['autoptimize_extra_radio_field_4'] ) ) {
168 178 add_filter( 'wp_resource_hints', array( $this, 'filter_remove_gfonts_dnsprefetch' ), 10, 2 );
169 179 add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_optimize_google_fonts' ), 10, 1 );
170 180 add_filter( 'autoptimize_extra_filter_tobepreconn', array( $this, 'filter_preconnect_google_fonts' ), 10, 1 );
181 +
182 + if ( '2' === $options['autoptimize_extra_radio_field_4'] ) {
183 + // remove Google Fonts, adding filters to also remove Google Fonts from 3rd party themes/ plugins.
184 + // inspired by https://wordpress.org/plugins/disable-remove-google-fonts/.
185 + remove_action( 'wp_footer', 'et_builder_print_font' ); // Divi.
186 + remove_action( 'wp_footer', array( 'RevSliderFront', 'load_google_fonts' ) ); // Revslider.
187 + add_filter( 'elementor/frontend/print_google_fonts', '__return_false' ); // Elementor.
188 + add_filter( 'fl_builder_google_fonts_pre_enqueue', '__return_empty_array' ); // Beaver Builder.
189 + }
171 190 }
172 191
173 192 // Preconnect!
174 193 if ( ! empty( $options['autoptimize_extra_text_field_2'] ) || has_filter( 'autoptimize_extra_filter_tobepreconn' ) ) {
@@ -175,11 +194,16 @@
175 194 add_filter( 'wp_resource_hints', array( $this, 'filter_preconnect' ), 10, 2 );
176 195 }
177 196
178 197 // Preload!
179 - if ( ! empty( $options['autoptimize_extra_text_field_7'] ) ) {
198 + if ( ! empty( $options['autoptimize_extra_text_field_7'] ) || has_filter( 'autoptimize_filter_extra_tobepreloaded' ) || ! empty( autoptimizeConfig::get_post_meta_ao_settings( 'ao_post_preload' ) ) ) {
180 199 add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_preload' ), 10, 2 );
181 200 }
201 +
202 + // Remove global styles.
203 + if ( ! empty( $options['autoptimize_extra_checkbox_field_8'] ) ) {
204 + $this->disable_global_styles();
205 + }
182 206 }
183 207
184 208 public function filter_remove_emoji_dns_prefetch( $urls, $relation_type )
185 209 {
@@ -226,8 +250,14 @@
226 250 foreach ( $matches[2] as $font ) {
227 251 if ( ! preg_match( '/rel=["\']dns-prefetch["\']/', $matches[0][ $i ] ) ) {
228 252 // Get fonts name.
229 253 $font = str_replace( array( '%7C', '%7c' ), '|', $font );
254 + if ( strpos( $font, 'fonts.googleapis.com/css2' ) !== false ) {
255 + // (Somewhat) change Google Fonts APIv2 syntax back to v1.
256 + // todo: support for 100..900
257 + $font = rawurldecode( $font );
258 + $font = str_replace( array( 'css2?', 'ital,wght@', 'wght@', 'ital@', '0,', '1,', ':1', ';', '&family=' ), array( 'css?', '', '', '', '', 'italic', ':italic', ',', '%7C' ), $font );
259 + }
230 260 $font = explode( 'family=', $font );
231 261 $font = ( isset( $font[1] ) ) ? explode( '&', $font[1] ) : array();
232 262 // Add font to $fonts[$i] but make sure not to pollute with an empty family!
233 263 $_thisfont = array_values( array_filter( explode( '|', reset( $font ) ) ) );
@@ -276,9 +306,9 @@
276 306 }
277 307
278 308 if ( ! empty( $fonts_string ) ) {
279 309 if ( '5' === $options['autoptimize_extra_radio_field_4'] ) {
280 - $rel_string = 'rel="preload" as="style" onload="' . autoptimizeConfig::get_ao_css_preload_onload() . '"';
310 + $rel_string = 'rel="stylesheet" media="print" onload="' . autoptimizeConfig::get_ao_css_preload_onload() . '"';
281 311 } else {
282 312 $rel_string = 'rel="stylesheet"';
283 313 }
284 314 $fonts_markup = '<link ' . $rel_string . ' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family=' . $fonts_string . '" />';
@@ -320,13 +350,8 @@
320 350 $inject_point = apply_filters( 'autoptimize_filter_extra_gfont_injectpoint', '<link' );
321 351 $out = substr_replace( $in, $fonts_markup . $inject_point, strpos( $in, $inject_point ), strlen( $inject_point ) );
322 352 unset( $fonts_collection );
323 353
324 - // and insert preload polyfill if "link preload" and if the polyfill isn't there yet (courtesy of inline&defer).
325 - $preload_polyfill = autoptimizeConfig::get_ao_css_preload_polyfill();
326 - if ( '5' === $options['autoptimize_extra_radio_field_4'] && strpos( $out, $preload_polyfill ) === false ) {
327 - $out = str_replace( '</body>', $preload_polyfill . '</body>', $out );
328 - }
329 354 return $out;
330 355 }
331 356
332 357 public function filter_preconnect( $hints, $relation_type )
@@ -335,9 +360,9 @@
335 360 $preconns = array();
336 361
337 362 // Get settings and store in array.
338 363 if ( array_key_exists( 'autoptimize_extra_text_field_2', $options ) ) {
339 - $preconns = array_filter( array_map( 'trim', explode( ',', $options['autoptimize_extra_text_field_2'] ) ) );
364 + $preconns = array_filter( array_map( 'trim', explode( ',', wp_strip_all_tags( $options['autoptimize_extra_text_field_2'] ) ) ) );
340 365 }
341 366 $preconns = apply_filters( 'autoptimize_extra_filter_tobepreconn', $preconns );
342 367
343 368 // Walk array, extract domain and add to new array with crossorigin attribute.
@@ -390,10 +415,19 @@
390 415 // make array from comma separated list.
391 416 $options = $this->options;
392 417 $preloads = array();
393 418 if ( array_key_exists( 'autoptimize_extra_text_field_7', $options ) ) {
394 - $preloads = array_filter( array_map( 'trim', explode( ',', $options['autoptimize_extra_text_field_7'] ) ) );
419 + $preloads = array_filter( array_map( 'trim', explode( ',', wp_strip_all_tags( $options['autoptimize_extra_text_field_7'] ) ) ) );
395 420 }
421 +
422 + if ( false === autoptimizeImages::imgopt_active() && false === autoptimizeImages::should_lazyload_wrapper() ) {
423 + // only do this here if imgopt/ lazyload are not active?
424 + $metabox_preloads = array_filter( array_map( 'trim', explode( ',', wp_strip_all_tags( autoptimizeConfig::get_post_meta_ao_settings( 'ao_post_preload' ) ) ) ) );
425 + if ( ! empty( $metabox_preloads ) ) {
426 + $preloads = array_merge( $preloads, $metabox_preloads );
427 + }
428 + }
429 +
396 430 $preloads = apply_filters( 'autoptimize_filter_extra_tobepreloaded', $preloads );
397 431
398 432 // immediately return if nothing to be preloaded.
399 433 if ( empty( $preloads ) ) {
@@ -402,8 +436,12 @@
402 436
403 437 // iterate through array and add preload link to tmp string.
404 438 $preload_output = '';
405 439 foreach ( $preloads as $preload ) {
440 + if ( filter_var( $preload, FILTER_VALIDATE_URL ) !== $preload ) {
441 + continue;
442 + }
443 + $preload = esc_url_raw( $preload );
406 444 $crossorigin = '';
407 445 $preload_as = '';
408 446 $mime_type = '';
409 447 $_preload = strtok( $preload, '?' );
@@ -411,9 +449,9 @@
411 449 if ( autoptimizeUtils::str_ends_in( $_preload, '.css' ) ) {
412 450 $preload_as = 'style';
413 451 } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.js' ) ) {
414 452 $preload_as = 'script';
415 - } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.woff' ) || autoptimizeUtils::str_ends_in( $_preload, '.woff2' ) || autoptimizeUtils::str_ends_in( $_preload, '.ttf' ) || autoptimizeUtils::str_ends_in( $_preload, '.eot' ) ) {
453 + } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.woff' ) || autoptimizeUtils::str_ends_in( $_preload, '.woff2' ) || autoptimizeUtils::str_ends_in( $_preload, '.ttf' ) || autoptimizeUtils::str_ends_in( $_preload, '.eot' ) || autoptimizeUtils::str_ends_in( $_preload, '.otf' ) ) {
416 454 $preload_as = 'font';
417 455 $crossorigin = ' crossorigin';
418 456 $mime_type = ' type="font/' . pathinfo( $_preload, PATHINFO_EXTENSION ) . '"';
419 457 if ( ' type="font/eot"' === $mime_type ) {
@@ -418,9 +456,9 @@
418 456 $mime_type = ' type="font/' . pathinfo( $_preload, PATHINFO_EXTENSION ) . '"';
419 457 if ( ' type="font/eot"' === $mime_type ) {
420 458 $mime_type = 'application/vnd.ms-fontobject';
421 459 }
422 - } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.jpeg' ) || autoptimizeUtils::str_ends_in( $_preload, '.jpg' ) || autoptimizeUtils::str_ends_in( $_preload, '.webp' ) || autoptimizeUtils::str_ends_in( $_preload, '.png' ) || autoptimizeUtils::str_ends_in( $_preload, '.gif' ) ) {
460 + } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.jpeg' ) || autoptimizeUtils::str_ends_in( $_preload, '.jpg' ) || autoptimizeUtils::str_ends_in( $_preload, '.webp' ) || autoptimizeUtils::str_ends_in( $_preload, '.png' ) || autoptimizeUtils::str_ends_in( $_preload, '.gif' ) || autoptimizeUtils::str_ends_in( $_preload, '.svg' ) ) {
423 461 $preload_as = 'image';
424 462 } else {
425 463 $preload_as = 'other';
426 464 }
@@ -428,21 +466,42 @@
428 466 $preload_output .= '<link rel="preload" href="' . $preload . '" as="' . $preload_as . '"' . $mime_type . $crossorigin . '>';
429 467 }
430 468 $preload_output = apply_filters( 'autoptimize_filter_extra_preload_output', $preload_output );
431 469
470 + return $this->inject_preloads( $preload_output, $in );
471 + }
472 +
473 + public static function inject_preloads( $preloads, $html ) {
432 474 // add string to head (before first link node by default).
433 475 $preload_inject = apply_filters( 'autoptimize_filter_extra_preload_inject', '<link' );
434 - $position = autoptimizeUtils::strpos( $in, $preload_inject );
476 + $position = autoptimizeUtils::strpos( $html, $preload_inject );
435 477
436 - return autoptimizeUtils::substr_replace( $in, $preload_output . $preload_inject, $position, strlen( $preload_inject ) );
478 + return autoptimizeUtils::substr_replace( $html, $preloads . $preload_inject, $position, strlen( $preload_inject ) );
437 479 }
438 480
481 + public function disable_global_styles()
482 + {
483 + remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
484 + remove_action( 'wp_footer', 'wp_enqueue_global_styles', 1 );
485 + remove_action( 'wp_body_open', 'wp_global_styles_render_svg_filters' );
486 +
487 + if ( true === apply_filters( 'autoptimize_filter_extra_global_styles_and_block_css', true ) ) {
488 + add_action(
489 + 'wp_enqueue_scripts',
490 + function() {
491 + wp_dequeue_style( 'wp-block-library' );
492 + wp_dequeue_style( 'wp-block-library-theme' );
493 + }
494 + );
495 + }
496 + }
497 +
439 498 public function admin_menu()
440 499 {
441 500 // no acces if multisite and not network admin and no site config allowed.
442 501 if ( autoptimizeConfig::should_show_menu_tabs() ) {
443 502 add_submenu_page(
444 - null,
503 + '',
445 504 'autoptimize_extra',
446 505 'autoptimize_extra',
447 506 'manage_options',
448 507 'autoptimize_extra',
@@ -462,8 +521,10 @@
462 521 }
463 522
464 523 public function options_page()
465 524 {
525 + // phpcs:disable Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace
526 +
466 527 // Working with actual option values from the database here.
467 528 // That way any saves are still processed as expected, but we can still
468 529 // override behavior by using `new autoptimizeExtra($custom_options)` and not have that custom
469 530 // behavior being persisted in the DB even if save is done here.
@@ -476,9 +537,9 @@
476 537 #autoptimize_extra_descr{font-size: 120%;}
477 538 </style>
478 539 <script>document.title = "Autoptimize: <?php _e( 'Extra', 'autoptimize' ); ?> " + document.title;</script>
479 540 <div class="wrap">
480 - <h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1>
541 + <h1><?php apply_filters( 'autoptimize_filter_settings_is_pro', false ) ? _e( 'Autoptimize Pro Settings', 'autoptimize' ) : _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1>
481 542 <?php echo autoptimizeConfig::ao_admin_tabs(); ?>
482 543 <?php if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_js' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_html' ) && ! autoptimizeImages::imgopt_active() ) { ?>
483 544 <div class="notice-warning notice"><p>
484 545 <?php _e( 'Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize' ); ?>
@@ -497,10 +558,10 @@
497 558 <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="2" <?php checked( 2, $gfonts, true ); ?> ><?php _e( 'Remove Google Fonts', 'autoptimize' ); ?><br/>
498 559 <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?>
499 560 <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="3" <?php checked( 3, $gfonts, true ); ?> ><?php echo __( 'Combine and link in head (fonts load fast but are render-blocking)', 'autoptimize' ) . ', ' . sprintf( __( 'includes %1$sdisplay:swap%2$s.', 'autoptimize' ), '<code>', '</code>' ); ?><br/>
500 561 <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?>
501 - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="5" <?php checked( 5, $gfonts, true ); ?> ><?php echo __( 'Combine and preload in head (fonts load late, but are not render-blocking)', 'autoptimize' ) . ', ' . sprintf( __( 'includes %1$sdisplay:swap%2$s.', 'autoptimize' ), '<code>', '</code>' ); ?><br/>
502 - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="4" <?php checked( 4, $gfonts, true ); ?> ><?php _e( 'Combine and load fonts asynchronously with <a href="https://github.com/typekit/webfontloader#readme" target="_blank">webfont.js</a>', 'autoptimize' ); ?><br/>
562 + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="5" <?php checked( 5, $gfonts, true ); ?> ><?php echo __( 'Combine and link deferred in head (fonts load late, but are not render-blocking)', 'autoptimize' ) . ', ' . sprintf( __( 'includes %1$sdisplay:swap%2$s.', 'autoptimize' ), '<code>', '</code>' ); ?><br/>
563 + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="4" <?php checked( 4, $gfonts, true ); ?> ><?php echo __( 'Combine and load fonts asynchronously with <a href="https://github.com/typekit/webfontloader#readme" target="_blank">webfont.js</a>', 'autoptimize' ) . ' ' . __( '(deprecated)', 'autoptimize' ); ?><br/>
503 564 </td>
504 565 </tr>
505 566 <tr>
506 567 <th scope="row"><?php _e( 'Remove emojis', 'autoptimize' ); ?></th>
@@ -514,8 +575,14 @@
514 575 <label><input type='checkbox' name='autoptimize_extra_settings[autoptimize_extra_checkbox_field_0]' <?php if ( ! empty( $options['autoptimize_extra_checkbox_field_0'] ) && '1' === $options['autoptimize_extra_checkbox_field_0'] ) { echo 'checked="checked"'; } ?> value='1'><?php _e( 'Removing query strings (or more specifically the <code>ver</code> parameter) will not improve load time, but might improve performance scores.', 'autoptimize' ); ?></label>
515 576 </td>
516 577 </tr>
517 578 <tr>
579 + <th scope="row"><?php _e( 'Remove WordPress block CSS', 'autoptimize' ); ?></th>
580 + <td>
581 + <label><input type='checkbox' name='autoptimize_extra_settings[autoptimize_extra_checkbox_field_8]' <?php if ( ! empty( $options['autoptimize_extra_checkbox_field_8'] ) && '1' === $options['autoptimize_extra_checkbox_field_8'] ) { echo 'checked="checked"'; } ?> value='1'><?php _e( 'WordPress adds block CSS and global styles to improve easy styling of block-based sites, but which can add a significant amount of CSS and SVG. If you are sure your site can do without the block CSS and "global styles", you can disable them here.', 'autoptimize' ); ?></label>
582 + </td>
583 + </tr>
584 + <tr>
518 585 <th scope="row"><?php _e( 'Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize' ); ?></th>
519 586 <td>
520 587 <label><input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_2]' value='<?php if ( array_key_exists( 'autoptimize_extra_text_field_2', $options ) ) { echo esc_attr( $options['autoptimize_extra_text_field_2'] ); } ?>'><br /><?php _e( 'Add 3rd party domains you want the browser to <a href="https://www.keycdn.com/support/preconnect/#primary" target="_blank">preconnect</a> to, separated by comma\'s. Make sure to include the correct protocol (HTTP or HTTPS).', 'autoptimize' ); ?></label>
521 588 </td>
@@ -533,9 +600,9 @@
533 600 if ( autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ) ) {
534 601 // translators: link points Async Javascript settings page.
535 602 printf( __( 'You have "Async JavaScript" installed, %1$sconfiguration of async javascript is best done there%2$s.', 'autoptimize' ), '<a href="' . 'options-general.php?page=async-javascript' . '">', '</a>' );
536 603 } else {
537 - ?>
604 + ?>
538 605 <input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_3]' value='<?php if ( array_key_exists( 'autoptimize_extra_text_field_3', $options ) ) { echo esc_attr( $options['autoptimize_extra_text_field_3'] ); } ?>'>
539 606 <br />
540 607 <?php
541 608 _e( 'Comma-separated list of local or 3rd party JS-files that should loaded with the <code>async</code> flag. JS-files from your own site will be automatically excluded if added here. ', 'autoptimize' );