| @@ -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 | + } else if ( 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'] ) ) { |
| @@ -175,11 +185,16 @@ | ||
| 175 | 185 | add_filter( 'wp_resource_hints', array( $this, 'filter_preconnect' ), 10, 2 ); |
| 176 | 186 | } |
| 177 | 187 | |
| 178 | 188 | // Preload! |
| 179 | - if ( ! empty( $options['autoptimize_extra_text_field_7'] ) ) { | |
| 189 | + 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 | 190 | add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_preload' ), 10, 2 ); |
| 181 | 191 | } |
| 192 | + | |
| 193 | + // Remove global styles. | |
| 194 | + if ( ! empty( $options['autoptimize_extra_checkbox_field_8'] ) ) { | |
| 195 | + $this->disable_global_styles(); | |
| 196 | + } | |
| 182 | 197 | } |
| 183 | 198 | |
| 184 | 199 | public function filter_remove_emoji_dns_prefetch( $urls, $relation_type ) |
| 185 | 200 | { |
| @@ -226,8 +241,14 @@ | ||
| 226 | 241 | foreach ( $matches[2] as $font ) { |
| 227 | 242 | if ( ! preg_match( '/rel=["\']dns-prefetch["\']/', $matches[0][ $i ] ) ) { |
| 228 | 243 | // Get fonts name. |
| 229 | 244 | $font = str_replace( array( '%7C', '%7c' ), '|', $font ); |
| 245 | + if ( strpos( $font, 'fonts.googleapis.com/css2' ) !== false ) { | |
| 246 | + // (Somewhat) change Google Fonts APIv2 syntax back to v1. | |
| 247 | + // todo: support for 100..900 | |
| 248 | + $font = rawurldecode( $font ); | |
| 249 | + $font = str_replace( array( 'css2?', 'ital,wght@', 'wght@', 'ital@', '0,', '1,', ':1', ';', '&family=' ), array( 'css?', '', '', '', '', 'italic', ':italic', ',', '%7C' ), $font ); | |
| 250 | + } | |
| 230 | 251 | $font = explode( 'family=', $font ); |
| 231 | 252 | $font = ( isset( $font[1] ) ) ? explode( '&', $font[1] ) : array(); |
| 232 | 253 | // Add font to $fonts[$i] but make sure not to pollute with an empty family! |
| 233 | 254 | $_thisfont = array_values( array_filter( explode( '|', reset( $font ) ) ) ); |
| @@ -276,9 +297,9 @@ | ||
| 276 | 297 | } |
| 277 | 298 | |
| 278 | 299 | if ( ! empty( $fonts_string ) ) { |
| 279 | 300 | if ( '5' === $options['autoptimize_extra_radio_field_4'] ) { |
| 280 | - $rel_string = 'rel="preload" as="style" onload="' . autoptimizeConfig::get_ao_css_preload_onload() . '"'; | |
| 301 | + $rel_string = 'rel="stylesheet" media="print" onload="' . autoptimizeConfig::get_ao_css_preload_onload() . '"'; | |
| 281 | 302 | } else { |
| 282 | 303 | $rel_string = 'rel="stylesheet"'; |
| 283 | 304 | } |
| 284 | 305 | $fonts_markup = '<link ' . $rel_string . ' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family=' . $fonts_string . '" />'; |
| @@ -320,13 +341,8 @@ | ||
| 320 | 341 | $inject_point = apply_filters( 'autoptimize_filter_extra_gfont_injectpoint', '<link' ); |
| 321 | 342 | $out = substr_replace( $in, $fonts_markup . $inject_point, strpos( $in, $inject_point ), strlen( $inject_point ) ); |
| 322 | 343 | unset( $fonts_collection ); |
| 323 | 344 | |
| 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 | 345 | return $out; |
| 330 | 346 | } |
| 331 | 347 | |
| 332 | 348 | public function filter_preconnect( $hints, $relation_type ) |
| @@ -335,9 +351,9 @@ | ||
| 335 | 351 | $preconns = array(); |
| 336 | 352 | |
| 337 | 353 | // Get settings and store in array. |
| 338 | 354 | if ( array_key_exists( 'autoptimize_extra_text_field_2', $options ) ) { |
| 339 | - $preconns = array_filter( array_map( 'trim', explode( ',', $options['autoptimize_extra_text_field_2'] ) ) ); | |
| 355 | + $preconns = array_filter( array_map( 'trim', explode( ',', wp_strip_all_tags( $options['autoptimize_extra_text_field_2'] ) ) ) ); | |
| 340 | 356 | } |
| 341 | 357 | $preconns = apply_filters( 'autoptimize_extra_filter_tobepreconn', $preconns ); |
| 342 | 358 | |
| 343 | 359 | // Walk array, extract domain and add to new array with crossorigin attribute. |
| @@ -390,10 +406,19 @@ | ||
| 390 | 406 | // make array from comma separated list. |
| 391 | 407 | $options = $this->options; |
| 392 | 408 | $preloads = array(); |
| 393 | 409 | if ( array_key_exists( 'autoptimize_extra_text_field_7', $options ) ) { |
| 394 | - $preloads = array_filter( array_map( 'trim', explode( ',', $options['autoptimize_extra_text_field_7'] ) ) ); | |
| 410 | + $preloads = array_filter( array_map( 'trim', explode( ',', wp_strip_all_tags( $options['autoptimize_extra_text_field_7'] ) ) ) ); | |
| 395 | 411 | } |
| 412 | + | |
| 413 | + if ( false === autoptimizeImages::imgopt_active() && false === autoptimizeImages::should_lazyload_wrapper() ) { | |
| 414 | + // only do this here if imgopt/ lazyload are not active? | |
| 415 | + $metabox_preloads = array_filter( array_map( 'trim', explode( ',', wp_strip_all_tags( autoptimizeConfig::get_post_meta_ao_settings( 'ao_post_preload' ) ) ) ) ); | |
| 416 | + if ( ! empty( $metabox_preloads ) ) { | |
| 417 | + $preloads = array_merge( $preloads, $metabox_preloads ); | |
| 418 | + } | |
| 419 | + } | |
| 420 | + | |
| 396 | 421 | $preloads = apply_filters( 'autoptimize_filter_extra_tobepreloaded', $preloads ); |
| 397 | 422 | |
| 398 | 423 | // immediately return if nothing to be preloaded. |
| 399 | 424 | if ( empty( $preloads ) ) { |
| @@ -402,24 +427,29 @@ | ||
| 402 | 427 | |
| 403 | 428 | // iterate through array and add preload link to tmp string. |
| 404 | 429 | $preload_output = ''; |
| 405 | 430 | foreach ( $preloads as $preload ) { |
| 431 | + if ( $preload !== filter_var( $preload, FILTER_VALIDATE_URL ) ) { | |
| 432 | + continue; | |
| 433 | + } | |
| 434 | + $preload = esc_url_raw( $preload ); | |
| 406 | 435 | $crossorigin = ''; |
| 407 | 436 | $preload_as = ''; |
| 408 | 437 | $mime_type = ''; |
| 438 | + $_preload = strtok( $preload, '?' ); | |
| 409 | 439 | |
| 410 | - if ( autoptimizeUtils::str_ends_in( $preload, '.css' ) ) { | |
| 440 | + if ( autoptimizeUtils::str_ends_in( $_preload, '.css' ) ) { | |
| 411 | 441 | $preload_as = 'style'; |
| 412 | - } elseif ( autoptimizeUtils::str_ends_in( $preload, '.js' ) ) { | |
| 442 | + } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.js' ) ) { | |
| 413 | 443 | $preload_as = 'script'; |
| 414 | - } 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' ) ) { | |
| 444 | + } 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' ) ) { | |
| 415 | 445 | $preload_as = 'font'; |
| 416 | 446 | $crossorigin = ' crossorigin'; |
| 417 | - $mime_type = ' type="font/' . pathinfo( $preload, PATHINFO_EXTENSION ) . '"'; | |
| 447 | + $mime_type = ' type="font/' . pathinfo( $_preload, PATHINFO_EXTENSION ) . '"'; | |
| 418 | 448 | if ( ' type="font/eot"' === $mime_type ) { |
| 419 | 449 | $mime_type = 'application/vnd.ms-fontobject'; |
| 420 | 450 | } |
| 421 | - } 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' ) ) { | |
| 451 | + } 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' ) ) { | |
| 422 | 452 | $preload_as = 'image'; |
| 423 | 453 | } else { |
| 424 | 454 | $preload_as = 'other'; |
| 425 | 455 | } |
| @@ -427,15 +457,33 @@ | ||
| 427 | 457 | $preload_output .= '<link rel="preload" href="' . $preload . '" as="' . $preload_as . '"' . $mime_type . $crossorigin . '>'; |
| 428 | 458 | } |
| 429 | 459 | $preload_output = apply_filters( 'autoptimize_filter_extra_preload_output', $preload_output ); |
| 430 | 460 | |
| 461 | + return $this->inject_preloads( $preload_output, $in ); | |
| 462 | + } | |
| 463 | + | |
| 464 | + public static function inject_preloads( $preloads, $html ) { | |
| 431 | 465 | // add string to head (before first link node by default). |
| 432 | 466 | $preload_inject = apply_filters( 'autoptimize_filter_extra_preload_inject', '<link' ); |
| 433 | - $position = autoptimizeUtils::strpos( $in, $preload_inject ); | |
| 467 | + $position = autoptimizeUtils::strpos( $html, $preload_inject ); | |
| 434 | 468 | |
| 435 | - return autoptimizeUtils::substr_replace( $in, $preload_output . $preload_inject, $position, strlen( $preload_inject ) ); | |
| 469 | + return autoptimizeUtils::substr_replace( $html, $preloads . $preload_inject, $position, strlen( $preload_inject ) ); | |
| 436 | 470 | } |
| 471 | + | |
| 472 | + public function disable_global_styles() | |
| 473 | + { | |
| 474 | + remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' ); | |
| 475 | + remove_action( 'wp_footer', 'wp_enqueue_global_styles', 1 ); | |
| 476 | + remove_action( 'wp_body_open', 'wp_global_styles_render_svg_filters' ); | |
| 437 | 477 | |
| 478 | + if ( true === apply_filters( 'autoptimize_filter_extra_global_styles_and_block_css', true ) ) { | |
| 479 | + add_action( 'wp_enqueue_scripts', function(){ | |
| 480 | + wp_dequeue_style( 'wp-block-library' ); | |
| 481 | + wp_dequeue_style( 'wp-block-library-theme' ); | |
| 482 | + }); | |
| 483 | + } | |
| 484 | + } | |
| 485 | + | |
| 438 | 486 | public function admin_menu() |
| 439 | 487 | { |
| 440 | 488 | // no acces if multisite and not network admin and no site config allowed. |
| 441 | 489 | if ( autoptimizeConfig::should_show_menu_tabs() ) { |
| @@ -446,10 +494,10 @@ | ||
| 446 | 494 | 'manage_options', |
| 447 | 495 | 'autoptimize_extra', |
| 448 | 496 | array( $this, 'options_page' ) |
| 449 | 497 | ); |
| 450 | - register_setting( 'autoptimize_extra_settings', 'autoptimize_extra_settings' ); | |
| 451 | 498 | } |
| 499 | + register_setting( 'autoptimize_extra_settings', 'autoptimize_extra_settings' ); | |
| 452 | 500 | } |
| 453 | 501 | |
| 454 | 502 | public function add_extra_tab( $in ) |
| 455 | 503 | { |
| @@ -475,9 +523,9 @@ | ||
| 475 | 523 | #autoptimize_extra_descr{font-size: 120%;} |
| 476 | 524 | </style> |
| 477 | 525 | <script>document.title = "Autoptimize: <?php _e( 'Extra', 'autoptimize' ); ?> " + document.title;</script> |
| 478 | 526 | <div class="wrap"> |
| 479 | - <h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1> | |
| 527 | + <h1><?php apply_filters( 'autoptimize_filter_settings_is_pro', false ) ? _e( 'Autoptimize Pro Settings', 'autoptimize' ) : _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1> | |
| 480 | 528 | <?php echo autoptimizeConfig::ao_admin_tabs(); ?> |
| 481 | 529 | <?php if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_js' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_html' ) && ! autoptimizeImages::imgopt_active() ) { ?> |
| 482 | 530 | <div class="notice-warning notice"><p> |
| 483 | 531 | <?php _e( 'Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize' ); ?> |
| @@ -496,10 +544,10 @@ | ||
| 496 | 544 | <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/> |
| 497 | 545 | <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?> |
| 498 | 546 | <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/> |
| 499 | 547 | <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?> |
| 500 | - <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/> | |
| 501 | - <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/> | |
| 548 | + <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/> | |
| 549 | + <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/> | |
| 502 | 550 | </td> |
| 503 | 551 | </tr> |
| 504 | 552 | <tr> |
| 505 | 553 | <th scope="row"><?php _e( 'Remove emojis', 'autoptimize' ); ?></th> |
| @@ -513,8 +561,14 @@ | ||
| 513 | 561 | <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> |
| 514 | 562 | </td> |
| 515 | 563 | </tr> |
| 516 | 564 | <tr> |
| 565 | + <th scope="row"><?php _e( 'Remove WordPress block CSS', 'autoptimize' ); ?></th> | |
| 566 | + <td> | |
| 567 | + <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> | |
| 568 | + </td> | |
| 569 | + </tr> | |
| 570 | + <tr> | |
| 517 | 571 | <th scope="row"><?php _e( 'Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize' ); ?></th> |
| 518 | 572 | <td> |
| 519 | 573 | <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> |
| 520 | 574 | </td> |
| @@ -532,9 +586,9 @@ | ||
| 532 | 586 | if ( autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ) ) { |
| 533 | 587 | // translators: link points Async Javascript settings page. |
| 534 | 588 | 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>' ); |
| 535 | 589 | } else { |
| 536 | - ?> | |
| 590 | + ?> | |
| 537 | 591 | <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'] ); } ?>'> |
| 538 | 592 | <br /> |
| 539 | 593 | <?php |
| 540 | 594 | _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' ); |