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 +75 -20 2.7.33.1.4 View file →
@@ -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;
@@ -175,8 +177,17 @@
175 177 if ( ! empty( $options['autoptimize_extra_radio_field_4'] ) && ( '1' !== $options['autoptimize_extra_radio_field_4'] ) ) {
176 178 add_filter( 'wp_resource_hints', array( $this, 'filter_remove_gfonts_dnsprefetch' ), 10, 2 );
177 179 add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_optimize_google_fonts' ), 10, 1 );
178 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 + }
179 190 }
180 191
181 192 // Preconnect!
182 193 if ( ! empty( $options['autoptimize_extra_text_field_2'] ) || has_filter( 'autoptimize_extra_filter_tobepreconn' ) ) {
@@ -183,11 +194,16 @@
183 194 add_filter( 'wp_resource_hints', array( $this, 'filter_preconnect' ), 10, 2 );
184 195 }
185 196
186 197 // Preload!
187 - if ( ! empty( $options['autoptimize_extra_text_field_7'] ) || has_filter( 'autoptimize_filter_extra_tobepreloaded' ) ) {
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' ) ) ) {
188 199 add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_preload' ), 10, 2 );
189 200 }
201 +
202 + // Remove global styles.
203 + if ( ! empty( $options['autoptimize_extra_checkbox_field_8'] ) ) {
204 + $this->disable_global_styles();
205 + }
190 206 }
191 207
192 208 public function filter_remove_emoji_dns_prefetch( $urls, $relation_type )
193 209 {
@@ -236,9 +252,11 @@
236 252 // Get fonts name.
237 253 $font = str_replace( array( '%7C', '%7c' ), '|', $font );
238 254 if ( strpos( $font, 'fonts.googleapis.com/css2' ) !== false ) {
239 255 // (Somewhat) change Google Fonts APIv2 syntax back to v1.
240 - $font = str_replace( array( 'wght@', 'wght%40', ';', '%3B' ), array( '', '', ',', ',' ), $font );
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 );
241 259 }
242 260 $font = explode( 'family=', $font );
243 261 $font = ( isset( $font[1] ) ) ? explode( '&', $font[1] ) : array();
244 262 // Add font to $fonts[$i] but make sure not to pollute with an empty family!
@@ -288,9 +306,9 @@
288 306 }
289 307
290 308 if ( ! empty( $fonts_string ) ) {
291 309 if ( '5' === $options['autoptimize_extra_radio_field_4'] ) {
292 - $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() . '"';
293 311 } else {
294 312 $rel_string = 'rel="stylesheet"';
295 313 }
296 314 $fonts_markup = '<link ' . $rel_string . ' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family=' . $fonts_string . '" />';
@@ -332,13 +350,8 @@
332 350 $inject_point = apply_filters( 'autoptimize_filter_extra_gfont_injectpoint', '<link' );
333 351 $out = substr_replace( $in, $fonts_markup . $inject_point, strpos( $in, $inject_point ), strlen( $inject_point ) );
334 352 unset( $fonts_collection );
335 353
336 - // and insert preload polyfill if "link preload" and if the polyfill isn't there yet (courtesy of inline&defer).
337 - $preload_polyfill = autoptimizeConfig::get_ao_css_preload_polyfill();
338 - if ( '5' === $options['autoptimize_extra_radio_field_4'] && strpos( $out, $preload_polyfill ) === false ) {
339 - $out = str_replace( '</body>', $preload_polyfill . '</body>', $out );
340 - }
341 354 return $out;
342 355 }
343 356
344 357 public function filter_preconnect( $hints, $relation_type )
@@ -347,9 +360,9 @@
347 360 $preconns = array();
348 361
349 362 // Get settings and store in array.
350 363 if ( array_key_exists( 'autoptimize_extra_text_field_2', $options ) ) {
351 - $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'] ) ) ) );
352 365 }
353 366 $preconns = apply_filters( 'autoptimize_extra_filter_tobepreconn', $preconns );
354 367
355 368 // Walk array, extract domain and add to new array with crossorigin attribute.
@@ -402,10 +415,19 @@
402 415 // make array from comma separated list.
403 416 $options = $this->options;
404 417 $preloads = array();
405 418 if ( array_key_exists( 'autoptimize_extra_text_field_7', $options ) ) {
406 - $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'] ) ) ) );
407 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 +
408 430 $preloads = apply_filters( 'autoptimize_filter_extra_tobepreloaded', $preloads );
409 431
410 432 // immediately return if nothing to be preloaded.
411 433 if ( empty( $preloads ) ) {
@@ -414,8 +436,12 @@
414 436
415 437 // iterate through array and add preload link to tmp string.
416 438 $preload_output = '';
417 439 foreach ( $preloads as $preload ) {
440 + if ( filter_var( $preload, FILTER_VALIDATE_URL ) !== $preload ) {
441 + continue;
442 + }
443 + $preload = esc_url_raw( $preload );
418 444 $crossorigin = '';
419 445 $preload_as = '';
420 446 $mime_type = '';
421 447 $_preload = strtok( $preload, '?' );
@@ -423,9 +449,9 @@
423 449 if ( autoptimizeUtils::str_ends_in( $_preload, '.css' ) ) {
424 450 $preload_as = 'style';
425 451 } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.js' ) ) {
426 452 $preload_as = 'script';
427 - } 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' ) ) {
428 454 $preload_as = 'font';
429 455 $crossorigin = ' crossorigin';
430 456 $mime_type = ' type="font/' . pathinfo( $_preload, PATHINFO_EXTENSION ) . '"';
431 457 if ( ' type="font/eot"' === $mime_type ) {
@@ -430,9 +456,9 @@
430 456 $mime_type = ' type="font/' . pathinfo( $_preload, PATHINFO_EXTENSION ) . '"';
431 457 if ( ' type="font/eot"' === $mime_type ) {
432 458 $mime_type = 'application/vnd.ms-fontobject';
433 459 }
434 - } 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' ) ) {
435 461 $preload_as = 'image';
436 462 } else {
437 463 $preload_as = 'other';
438 464 }
@@ -440,21 +466,42 @@
440 466 $preload_output .= '<link rel="preload" href="' . $preload . '" as="' . $preload_as . '"' . $mime_type . $crossorigin . '>';
441 467 }
442 468 $preload_output = apply_filters( 'autoptimize_filter_extra_preload_output', $preload_output );
443 469
470 + return $this->inject_preloads( $preload_output, $in );
471 + }
472 +
473 + public static function inject_preloads( $preloads, $html ) {
444 474 // add string to head (before first link node by default).
445 475 $preload_inject = apply_filters( 'autoptimize_filter_extra_preload_inject', '<link' );
446 - $position = autoptimizeUtils::strpos( $in, $preload_inject );
476 + $position = autoptimizeUtils::strpos( $html, $preload_inject );
447 477
448 - 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 ) );
449 479 }
450 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 +
451 498 public function admin_menu()
452 499 {
453 500 // no acces if multisite and not network admin and no site config allowed.
454 501 if ( autoptimizeConfig::should_show_menu_tabs() ) {
455 502 add_submenu_page(
456 - null,
503 + '',
457 504 'autoptimize_extra',
458 505 'autoptimize_extra',
459 506 'manage_options',
460 507 'autoptimize_extra',
@@ -474,8 +521,10 @@
474 521 }
475 522
476 523 public function options_page()
477 524 {
525 + // phpcs:disable Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace
526 +
478 527 // Working with actual option values from the database here.
479 528 // That way any saves are still processed as expected, but we can still
480 529 // override behavior by using `new autoptimizeExtra($custom_options)` and not have that custom
481 530 // behavior being persisted in the DB even if save is done here.
@@ -488,9 +537,9 @@
488 537 #autoptimize_extra_descr{font-size: 120%;}
489 538 </style>
490 539 <script>document.title = "Autoptimize: <?php _e( 'Extra', 'autoptimize' ); ?> " + document.title;</script>
491 540 <div class="wrap">
492 - <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>
493 542 <?php echo autoptimizeConfig::ao_admin_tabs(); ?>
494 543 <?php if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_js' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_html' ) && ! autoptimizeImages::imgopt_active() ) { ?>
495 544 <div class="notice-warning notice"><p>
496 545 <?php _e( 'Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize' ); ?>
@@ -509,10 +558,10 @@
509 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/>
510 559 <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?>
511 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/>
512 561 <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?>
513 - <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/>
514 - <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/>
515 564 </td>
516 565 </tr>
517 566 <tr>
518 567 <th scope="row"><?php _e( 'Remove emojis', 'autoptimize' ); ?></th>
@@ -526,8 +575,14 @@
526 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>
527 576 </td>
528 577 </tr>
529 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>
530 585 <th scope="row"><?php _e( 'Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize' ); ?></th>
531 586 <td>
532 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>
533 588 </td>
@@ -545,9 +600,9 @@
545 600 if ( autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ) ) {
546 601 // translators: link points Async Javascript settings page.
547 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>' );
548 603 } else {
549 - ?>
604 + ?>
550 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'] ); } ?>'>
551 606 <br />
552 607 <?php
553 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' );