| @@ -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; |
| @@ -183,11 +185,16 @@ | ||
| 183 | 185 | add_filter( 'wp_resource_hints', array( $this, 'filter_preconnect' ), 10, 2 ); |
| 184 | 186 | } |
| 185 | 187 | |
| 186 | 188 | // Preload! |
| 187 | - 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' ) ) ) { | |
| 188 | 190 | add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_preload' ), 10, 2 ); |
| 189 | 191 | } |
| 192 | + | |
| 193 | + // Remove global styles. | |
| 194 | + if ( ! empty( $options['autoptimize_extra_checkbox_field_8'] ) ) { | |
| 195 | + $this->disable_global_styles(); | |
| 196 | + } | |
| 190 | 197 | } |
| 191 | 198 | |
| 192 | 199 | public function filter_remove_emoji_dns_prefetch( $urls, $relation_type ) |
| 193 | 200 | { |
| @@ -236,9 +243,11 @@ | ||
| 236 | 243 | // Get fonts name. |
| 237 | 244 | $font = str_replace( array( '%7C', '%7c' ), '|', $font ); |
| 238 | 245 | if ( strpos( $font, 'fonts.googleapis.com/css2' ) !== false ) { |
| 239 | 246 | // (Somewhat) change Google Fonts APIv2 syntax back to v1. |
| 240 | - $font = str_replace( array( 'wght@', 'wght%40', ';', '%3B' ), array( '', '', ',', ',' ), $font ); | |
| 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 ); | |
| 241 | 250 | } |
| 242 | 251 | $font = explode( 'family=', $font ); |
| 243 | 252 | $font = ( isset( $font[1] ) ) ? explode( '&', $font[1] ) : array(); |
| 244 | 253 | // Add font to $fonts[$i] but make sure not to pollute with an empty family! |
| @@ -288,9 +297,9 @@ | ||
| 288 | 297 | } |
| 289 | 298 | |
| 290 | 299 | if ( ! empty( $fonts_string ) ) { |
| 291 | 300 | if ( '5' === $options['autoptimize_extra_radio_field_4'] ) { |
| 292 | - $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() . '"'; | |
| 293 | 302 | } else { |
| 294 | 303 | $rel_string = 'rel="stylesheet"'; |
| 295 | 304 | } |
| 296 | 305 | $fonts_markup = '<link ' . $rel_string . ' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family=' . $fonts_string . '" />'; |
| @@ -332,13 +341,8 @@ | ||
| 332 | 341 | $inject_point = apply_filters( 'autoptimize_filter_extra_gfont_injectpoint', '<link' ); |
| 333 | 342 | $out = substr_replace( $in, $fonts_markup . $inject_point, strpos( $in, $inject_point ), strlen( $inject_point ) ); |
| 334 | 343 | unset( $fonts_collection ); |
| 335 | 344 | |
| 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 | 345 | return $out; |
| 342 | 346 | } |
| 343 | 347 | |
| 344 | 348 | public function filter_preconnect( $hints, $relation_type ) |
| @@ -347,9 +351,9 @@ | ||
| 347 | 351 | $preconns = array(); |
| 348 | 352 | |
| 349 | 353 | // Get settings and store in array. |
| 350 | 354 | if ( array_key_exists( 'autoptimize_extra_text_field_2', $options ) ) { |
| 351 | - $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'] ) ) ) ); | |
| 352 | 356 | } |
| 353 | 357 | $preconns = apply_filters( 'autoptimize_extra_filter_tobepreconn', $preconns ); |
| 354 | 358 | |
| 355 | 359 | // Walk array, extract domain and add to new array with crossorigin attribute. |
| @@ -402,10 +406,19 @@ | ||
| 402 | 406 | // make array from comma separated list. |
| 403 | 407 | $options = $this->options; |
| 404 | 408 | $preloads = array(); |
| 405 | 409 | if ( array_key_exists( 'autoptimize_extra_text_field_7', $options ) ) { |
| 406 | - $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'] ) ) ) ); | |
| 407 | 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 | + | |
| 408 | 421 | $preloads = apply_filters( 'autoptimize_filter_extra_tobepreloaded', $preloads ); |
| 409 | 422 | |
| 410 | 423 | // immediately return if nothing to be preloaded. |
| 411 | 424 | if ( empty( $preloads ) ) { |
| @@ -414,8 +427,12 @@ | ||
| 414 | 427 | |
| 415 | 428 | // iterate through array and add preload link to tmp string. |
| 416 | 429 | $preload_output = ''; |
| 417 | 430 | foreach ( $preloads as $preload ) { |
| 431 | + if ( $preload !== filter_var( $preload, FILTER_VALIDATE_URL ) ) { | |
| 432 | + continue; | |
| 433 | + } | |
| 434 | + $preload = esc_url_raw( $preload ); | |
| 418 | 435 | $crossorigin = ''; |
| 419 | 436 | $preload_as = ''; |
| 420 | 437 | $mime_type = ''; |
| 421 | 438 | $_preload = strtok( $preload, '?' ); |
| @@ -423,9 +440,9 @@ | ||
| 423 | 440 | if ( autoptimizeUtils::str_ends_in( $_preload, '.css' ) ) { |
| 424 | 441 | $preload_as = 'style'; |
| 425 | 442 | } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.js' ) ) { |
| 426 | 443 | $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' ) ) { | |
| 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' ) ) { | |
| 428 | 445 | $preload_as = 'font'; |
| 429 | 446 | $crossorigin = ' crossorigin'; |
| 430 | 447 | $mime_type = ' type="font/' . pathinfo( $_preload, PATHINFO_EXTENSION ) . '"'; |
| 431 | 448 | if ( ' type="font/eot"' === $mime_type ) { |
| @@ -430,9 +447,9 @@ | ||
| 430 | 447 | $mime_type = ' type="font/' . pathinfo( $_preload, PATHINFO_EXTENSION ) . '"'; |
| 431 | 448 | if ( ' type="font/eot"' === $mime_type ) { |
| 432 | 449 | $mime_type = 'application/vnd.ms-fontobject'; |
| 433 | 450 | } |
| 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' ) ) { | |
| 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' ) ) { | |
| 435 | 452 | $preload_as = 'image'; |
| 436 | 453 | } else { |
| 437 | 454 | $preload_as = 'other'; |
| 438 | 455 | } |
| @@ -440,15 +457,33 @@ | ||
| 440 | 457 | $preload_output .= '<link rel="preload" href="' . $preload . '" as="' . $preload_as . '"' . $mime_type . $crossorigin . '>'; |
| 441 | 458 | } |
| 442 | 459 | $preload_output = apply_filters( 'autoptimize_filter_extra_preload_output', $preload_output ); |
| 443 | 460 | |
| 461 | + return $this->inject_preloads( $preload_output, $in ); | |
| 462 | + } | |
| 463 | + | |
| 464 | + public static function inject_preloads( $preloads, $html ) { | |
| 444 | 465 | // add string to head (before first link node by default). |
| 445 | 466 | $preload_inject = apply_filters( 'autoptimize_filter_extra_preload_inject', '<link' ); |
| 446 | - $position = autoptimizeUtils::strpos( $in, $preload_inject ); | |
| 467 | + $position = autoptimizeUtils::strpos( $html, $preload_inject ); | |
| 447 | 468 | |
| 448 | - 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 ) ); | |
| 449 | 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' ); | |
| 450 | 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 | + | |
| 451 | 486 | public function admin_menu() |
| 452 | 487 | { |
| 453 | 488 | // no acces if multisite and not network admin and no site config allowed. |
| 454 | 489 | if ( autoptimizeConfig::should_show_menu_tabs() ) { |
| @@ -488,9 +523,9 @@ | ||
| 488 | 523 | #autoptimize_extra_descr{font-size: 120%;} |
| 489 | 524 | </style> |
| 490 | 525 | <script>document.title = "Autoptimize: <?php _e( 'Extra', 'autoptimize' ); ?> " + document.title;</script> |
| 491 | 526 | <div class="wrap"> |
| 492 | - <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> | |
| 493 | 528 | <?php echo autoptimizeConfig::ao_admin_tabs(); ?> |
| 494 | 529 | <?php if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_js' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_html' ) && ! autoptimizeImages::imgopt_active() ) { ?> |
| 495 | 530 | <div class="notice-warning notice"><p> |
| 496 | 531 | <?php _e( 'Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize' ); ?> |
| @@ -509,10 +544,10 @@ | ||
| 509 | 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/> |
| 510 | 545 | <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?> |
| 511 | 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/> |
| 512 | 547 | <?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/> | |
| 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/> | |
| 515 | 550 | </td> |
| 516 | 551 | </tr> |
| 517 | 552 | <tr> |
| 518 | 553 | <th scope="row"><?php _e( 'Remove emojis', 'autoptimize' ); ?></th> |
| @@ -526,8 +561,14 @@ | ||
| 526 | 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> |
| 527 | 562 | </td> |
| 528 | 563 | </tr> |
| 529 | 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> | |
| 530 | 571 | <th scope="row"><?php _e( 'Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize' ); ?></th> |
| 531 | 572 | <td> |
| 532 | 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> |
| 533 | 574 | </td> |
| @@ -545,9 +586,9 @@ | ||
| 545 | 586 | if ( autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ) ) { |
| 546 | 587 | // translators: link points Async Javascript settings page. |
| 547 | 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>' ); |
| 548 | 589 | } else { |
| 549 | - ?> | |
| 590 | + ?> | |
| 550 | 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'] ); } ?>'> |
| 551 | 592 | <br /> |
| 552 | 593 | <?php |
| 553 | 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' ); |