| @@ -16,8 +16,15 @@ | ||
| 16 | 16 | */ |
| 17 | 17 | protected $options = array(); |
| 18 | 18 | |
| 19 | 19 | /** |
| 20 | + * Singleton instance. | |
| 21 | + * | |
| 22 | + * @var self|null | |
| 23 | + */ | |
| 24 | + protected static $instance = null; | |
| 25 | + | |
| 26 | + /** | |
| 20 | 27 | * Creates an instance and calls run(). |
| 21 | 28 | * |
| 22 | 29 | * @param array $options Optional. Allows overriding options without having to specify them via admin options page. |
| 23 | 30 | */ |
| @@ -29,21 +36,49 @@ | ||
| 29 | 36 | |
| 30 | 37 | $this->options = $options; |
| 31 | 38 | } |
| 32 | 39 | |
| 40 | + /** | |
| 41 | + * Helper for getting a singleton instance. While being an | |
| 42 | + * anti-pattern generally, it comes in handy for now from a | |
| 43 | + * readability/maintainability perspective, until we get some | |
| 44 | + * proper dependency injection going. | |
| 45 | + * | |
| 46 | + * @return self | |
| 47 | + */ | |
| 48 | + public static function instance() | |
| 49 | + { | |
| 50 | + if ( null === self::$instance ) { | |
| 51 | + self::$instance = new self(); | |
| 52 | + } | |
| 53 | + | |
| 54 | + return self::$instance; | |
| 55 | + } | |
| 56 | + | |
| 33 | 57 | public function run() |
| 34 | 58 | { |
| 35 | 59 | if ( is_admin() ) { |
| 36 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); | |
| 60 | + if ( is_multisite() && is_network_admin() && autoptimizeOptionWrapper::is_ao_active_for_network() ) { | |
| 61 | + add_action( 'network_admin_menu', array( $this, 'admin_menu' ) ); | |
| 62 | + } else { | |
| 63 | + add_action( 'admin_menu', array( $this, 'admin_menu' ) ); | |
| 64 | + } | |
| 37 | 65 | add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_extra_tab' ) ); |
| 38 | 66 | } else { |
| 39 | - $this->run_on_frontend(); | |
| 67 | + add_action( 'wp', array( $this, 'run_on_frontend' ) ); | |
| 40 | 68 | } |
| 41 | 69 | } |
| 42 | 70 | |
| 71 | + public function set_options( array $options ) | |
| 72 | + { | |
| 73 | + $this->options = $options; | |
| 74 | + | |
| 75 | + return $this; | |
| 76 | + } | |
| 77 | + | |
| 43 | 78 | public static function fetch_options() |
| 44 | 79 | { |
| 45 | - $value = get_option( 'autoptimize_extra_settings' ); | |
| 80 | + $value = autoptimizeOptionWrapper::get_option( 'autoptimize_extra_settings' ); | |
| 46 | 81 | if ( empty( $value ) ) { |
| 47 | 82 | // Fallback to returning defaults when no stored option exists yet. |
| 48 | 83 | $value = autoptimizeConfig::get_ao_extra_default_options(); |
| 49 | 84 | } |
| @@ -81,8 +116,10 @@ | ||
| 81 | 116 | public function filter_remove_qs( $src ) |
| 82 | 117 | { |
| 83 | 118 | if ( strpos( $src, '?ver=' ) ) { |
| 84 | 119 | $src = remove_query_arg( 'ver', $src ); |
| 120 | + } elseif ( strpos( $src, '?v=' ) ) { | |
| 121 | + $src = remove_query_arg( 'v', $src ); | |
| 85 | 122 | } |
| 86 | 123 | |
| 87 | 124 | return $src; |
| 88 | 125 | } |
| @@ -93,9 +130,9 @@ | ||
| 93 | 130 | if ( ! empty( $in ) ) { |
| 94 | 131 | $exclusions = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $in ) ) ), '' ); |
| 95 | 132 | } |
| 96 | 133 | |
| 97 | - $settings = $this->options['autoptimize_extra_text_field_3']; | |
| 134 | + $settings = wp_strip_all_tags( $this->options['autoptimize_extra_text_field_3'] ); | |
| 98 | 135 | $async = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $settings ) ) ), '' ); |
| 99 | 136 | $attr = apply_filters( 'autoptimize_filter_extra_async', 'async' ); |
| 100 | 137 | foreach ( $async as $k => $v ) { |
| 101 | 138 | $async[ $k ] = $attr; |
| @@ -106,10 +143,18 @@ | ||
| 106 | 143 | |
| 107 | 144 | return $merged; |
| 108 | 145 | } |
| 109 | 146 | |
| 110 | - protected function run_on_frontend() | |
| 147 | + public function run_on_frontend() | |
| 111 | 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 | + | |
| 112 | 157 | $options = $this->options; |
| 113 | 158 | |
| 114 | 159 | // Disable emojis if specified. |
| 115 | 160 | if ( ! empty( $options['autoptimize_extra_checkbox_field_1'] ) ) { |
| @@ -132,8 +177,17 @@ | ||
| 132 | 177 | if ( ! empty( $options['autoptimize_extra_radio_field_4'] ) && ( '1' !== $options['autoptimize_extra_radio_field_4'] ) ) { |
| 133 | 178 | add_filter( 'wp_resource_hints', array( $this, 'filter_remove_gfonts_dnsprefetch' ), 10, 2 ); |
| 134 | 179 | add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_optimize_google_fonts' ), 10, 1 ); |
| 135 | 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 | + } | |
| 136 | 190 | } |
| 137 | 191 | |
| 138 | 192 | // Preconnect! |
| 139 | 193 | if ( ! empty( $options['autoptimize_extra_text_field_2'] ) || has_filter( 'autoptimize_extra_filter_tobepreconn' ) ) { |
| @@ -138,8 +192,18 @@ | ||
| 138 | 192 | // Preconnect! |
| 139 | 193 | if ( ! empty( $options['autoptimize_extra_text_field_2'] ) || has_filter( 'autoptimize_extra_filter_tobepreconn' ) ) { |
| 140 | 194 | add_filter( 'wp_resource_hints', array( $this, 'filter_preconnect' ), 10, 2 ); |
| 141 | 195 | } |
| 196 | + | |
| 197 | + // Preload! | |
| 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' ) ) ) { | |
| 199 | + add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_preload' ), 10, 2 ); | |
| 200 | + } | |
| 201 | + | |
| 202 | + // Remove global styles. | |
| 203 | + if ( ! empty( $options['autoptimize_extra_checkbox_field_8'] ) ) { | |
| 204 | + $this->disable_global_styles(); | |
| 205 | + } | |
| 142 | 206 | } |
| 143 | 207 | |
| 144 | 208 | public function filter_remove_emoji_dns_prefetch( $urls, $relation_type ) |
| 145 | 209 | { |
| @@ -186,8 +250,14 @@ | ||
| 186 | 250 | foreach ( $matches[2] as $font ) { |
| 187 | 251 | if ( ! preg_match( '/rel=["\']dns-prefetch["\']/', $matches[0][ $i ] ) ) { |
| 188 | 252 | // Get fonts name. |
| 189 | 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 | + } | |
| 190 | 260 | $font = explode( 'family=', $font ); |
| 191 | 261 | $font = ( isset( $font[1] ) ) ? explode( '&', $font[1] ) : array(); |
| 192 | 262 | // Add font to $fonts[$i] but make sure not to pollute with an empty family! |
| 193 | 263 | $_thisfont = array_values( array_filter( explode( '|', reset( $font ) ) ) ); |
| @@ -229,12 +299,16 @@ | ||
| 229 | 299 | $fonts_string = $fonts_string . '&subset=' . $subset_string; |
| 230 | 300 | } |
| 231 | 301 | |
| 232 | 302 | $fonts_string = apply_filters( 'autoptimize_filter_extra_gfont_fontstring', str_replace( '|', '%7C', ltrim( $fonts_string, '|' ) ) ); |
| 303 | + // only add display parameter if there is none in $fonts_string (by virtue of the filter). | |
| 304 | + if ( strpos( $fonts_string, 'display=' ) === false ) { | |
| 305 | + $fonts_string .= apply_filters( 'autoptimize_filter_extra_gfont_display', '&display=swap' ); | |
| 306 | + } | |
| 233 | 307 | |
| 234 | 308 | if ( ! empty( $fonts_string ) ) { |
| 235 | 309 | if ( '5' === $options['autoptimize_extra_radio_field_4'] ) { |
| 236 | - $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() . '"'; | |
| 237 | 311 | } else { |
| 238 | 312 | $rel_string = 'rel="stylesheet"'; |
| 239 | 313 | } |
| 240 | 314 | $fonts_markup = '<link ' . $rel_string . ' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family=' . $fonts_string . '" />'; |
| @@ -252,11 +326,24 @@ | ||
| 252 | 326 | } |
| 253 | 327 | $fonts_array = array_merge( $fonts_array, $_fonts['fonts'] ); |
| 254 | 328 | } |
| 255 | 329 | |
| 256 | - $fonts_array = array_map( 'urldecode', $fonts_array ); | |
| 257 | - $fonts_markup = '<script data-cfasync="false" id="ao_optimized_gfonts_config" type="text/javascript">WebFontConfig={google:{families:' . wp_json_encode( $fonts_array ) . ' },classes:false, events:false, timeout:1500};</script>'; | |
| 258 | - $fonts_library_markup = '<script data-cfasync="false" id="ao_optimized_gfonts_webfontloader" type="text/javascript">(function() {var wf = document.createElement(\'script\');wf.src=\'https://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js\';wf.type=\'text/javascript\';wf.async=\'true\';var s=document.getElementsByTagName(\'script\')[0];s.parentNode.insertBefore(wf, s);})();</script>'; | |
| 330 | + $fonts_array = array_map( 'urldecode', $fonts_array ); | |
| 331 | + $fonts_array = array_map( | |
| 332 | + function( $_f ) { | |
| 333 | + return trim( $_f, ',' ); | |
| 334 | + }, | |
| 335 | + $fonts_array | |
| 336 | + ); | |
| 337 | + | |
| 338 | + // type attrib on <script not added by default. | |
| 339 | + $type_js = ''; | |
| 340 | + if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { | |
| 341 | + $type_js = 'type="text/javascript" '; | |
| 342 | + } | |
| 343 | + | |
| 344 | + $fonts_markup = '<script ' . $type_js . 'data-cfasync="false" id="ao_optimized_gfonts_config">WebFontConfig={google:{families:' . wp_json_encode( $fonts_array ) . ' },classes:false, events:false, timeout:1500};</script>'; | |
| 345 | + $fonts_library_markup = '<script ' . $type_js . 'data-cfasync="false" id="ao_optimized_gfonts_webfontloader">(function() {var wf = document.createElement(\'script\');wf.src=\'https://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js\';wf.type=\'text/javascript\';wf.async=\'true\';var s=document.getElementsByTagName(\'script\')[0];s.parentNode.insertBefore(wf, s);})();</script>'; | |
| 259 | 346 | $in = substr_replace( $in, $fonts_library_markup . '</head>', strpos( $in, '</head>' ), strlen( '</head>' ) ); |
| 260 | 347 | } |
| 261 | 348 | |
| 262 | 349 | // Replace back in markup. |
| @@ -263,30 +350,29 @@ | ||
| 263 | 350 | $inject_point = apply_filters( 'autoptimize_filter_extra_gfont_injectpoint', '<link' ); |
| 264 | 351 | $out = substr_replace( $in, $fonts_markup . $inject_point, strpos( $in, $inject_point ), strlen( $inject_point ) ); |
| 265 | 352 | unset( $fonts_collection ); |
| 266 | 353 | |
| 267 | - // and insert preload polyfill if "link preload" and if the polyfill isn't there yet (courtesy of inline&defer). | |
| 268 | - $preload_polyfill = autoptimizeConfig::get_ao_css_preload_polyfill(); | |
| 269 | - if ( '5' === $options['autoptimize_extra_radio_field_4'] && strpos( $out, $preload_polyfill ) === false ) { | |
| 270 | - $out = str_replace( '</body>', $preload_polyfill . '</body>', $out ); | |
| 271 | - } | |
| 272 | 354 | return $out; |
| 273 | 355 | } |
| 274 | 356 | |
| 275 | 357 | public function filter_preconnect( $hints, $relation_type ) |
| 276 | 358 | { |
| 277 | - $options = $this->options; | |
| 359 | + $options = $this->options; | |
| 360 | + $preconns = array(); | |
| 278 | 361 | |
| 279 | 362 | // Get settings and store in array. |
| 280 | - $preconns = array_filter( array_map( 'trim', explode( ',', $options['autoptimize_extra_text_field_2'] ) ) ); | |
| 363 | + if ( array_key_exists( 'autoptimize_extra_text_field_2', $options ) ) { | |
| 364 | + $preconns = array_filter( array_map( 'trim', explode( ',', wp_strip_all_tags( $options['autoptimize_extra_text_field_2'] ) ) ) ); | |
| 365 | + } | |
| 281 | 366 | $preconns = apply_filters( 'autoptimize_extra_filter_tobepreconn', $preconns ); |
| 282 | 367 | |
| 283 | 368 | // Walk array, extract domain and add to new array with crossorigin attribute. |
| 284 | 369 | foreach ( $preconns as $preconn ) { |
| 370 | + $domain = ''; | |
| 285 | 371 | $parsed = parse_url( $preconn ); |
| 286 | - if ( is_array( $parsed ) && empty( $parsed['scheme'] ) ) { | |
| 372 | + if ( is_array( $parsed ) && ! empty( $parsed['host'] ) && empty( $parsed['scheme'] ) ) { | |
| 287 | 373 | $domain = '//' . $parsed['host']; |
| 288 | - } elseif ( is_array( $parsed ) ) { | |
| 374 | + } elseif ( is_array( $parsed ) && ! empty( $parsed['host'] ) ) { | |
| 289 | 375 | $domain = $parsed['scheme'] . '://' . $parsed['host']; |
| 290 | 376 | } |
| 291 | 377 | |
| 292 | 378 | if ( ! empty( $domain ) ) { |
| @@ -324,24 +410,113 @@ | ||
| 324 | 410 | |
| 325 | 411 | return $in; |
| 326 | 412 | } |
| 327 | 413 | |
| 414 | + public function filter_preload( $in ) { | |
| 415 | + // make array from comma separated list. | |
| 416 | + $options = $this->options; | |
| 417 | + $preloads = array(); | |
| 418 | + if ( array_key_exists( 'autoptimize_extra_text_field_7', $options ) ) { | |
| 419 | + $preloads = array_filter( array_map( 'trim', explode( ',', wp_strip_all_tags( $options['autoptimize_extra_text_field_7'] ) ) ) ); | |
| 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 | + | |
| 430 | + $preloads = apply_filters( 'autoptimize_filter_extra_tobepreloaded', $preloads ); | |
| 431 | + | |
| 432 | + // immediately return if nothing to be preloaded. | |
| 433 | + if ( empty( $preloads ) ) { | |
| 434 | + return $in; | |
| 435 | + } | |
| 436 | + | |
| 437 | + // iterate through array and add preload link to tmp string. | |
| 438 | + $preload_output = ''; | |
| 439 | + foreach ( $preloads as $preload ) { | |
| 440 | + if ( filter_var( $preload, FILTER_VALIDATE_URL ) !== $preload ) { | |
| 441 | + continue; | |
| 442 | + } | |
| 443 | + $preload = esc_url_raw( $preload ); | |
| 444 | + $crossorigin = ''; | |
| 445 | + $preload_as = ''; | |
| 446 | + $mime_type = ''; | |
| 447 | + $_preload = strtok( $preload, '?' ); | |
| 448 | + | |
| 449 | + if ( autoptimizeUtils::str_ends_in( $_preload, '.css' ) ) { | |
| 450 | + $preload_as = 'style'; | |
| 451 | + } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.js' ) ) { | |
| 452 | + $preload_as = 'script'; | |
| 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' ) ) { | |
| 454 | + $preload_as = 'font'; | |
| 455 | + $crossorigin = ' crossorigin'; | |
| 456 | + $mime_type = ' type="font/' . pathinfo( $_preload, PATHINFO_EXTENSION ) . '"'; | |
| 457 | + if ( ' type="font/eot"' === $mime_type ) { | |
| 458 | + $mime_type = 'application/vnd.ms-fontobject'; | |
| 459 | + } | |
| 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' ) ) { | |
| 461 | + $preload_as = 'image'; | |
| 462 | + } else { | |
| 463 | + $preload_as = 'other'; | |
| 464 | + } | |
| 465 | + | |
| 466 | + $preload_output .= '<link rel="preload" href="' . $preload . '" as="' . $preload_as . '"' . $mime_type . $crossorigin . '>'; | |
| 467 | + } | |
| 468 | + $preload_output = apply_filters( 'autoptimize_filter_extra_preload_output', $preload_output ); | |
| 469 | + | |
| 470 | + return $this->inject_preloads( $preload_output, $in ); | |
| 471 | + } | |
| 472 | + | |
| 473 | + public static function inject_preloads( $preloads, $html ) { | |
| 474 | + // add string to head (before first link node by default). | |
| 475 | + $preload_inject = apply_filters( 'autoptimize_filter_extra_preload_inject', '<link' ); | |
| 476 | + $position = autoptimizeUtils::strpos( $html, $preload_inject ); | |
| 477 | + | |
| 478 | + return autoptimizeUtils::substr_replace( $html, $preloads . $preload_inject, $position, strlen( $preload_inject ) ); | |
| 479 | + } | |
| 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 | + | |
| 328 | 498 | public function admin_menu() |
| 329 | 499 | { |
| 330 | - add_submenu_page( | |
| 331 | - null, | |
| 332 | - 'autoptimize_extra', | |
| 333 | - 'autoptimize_extra', | |
| 334 | - 'manage_options', | |
| 335 | - 'autoptimize_extra', | |
| 336 | - array( $this, 'options_page' ) | |
| 337 | - ); | |
| 500 | + // no acces if multisite and not network admin and no site config allowed. | |
| 501 | + if ( autoptimizeConfig::should_show_menu_tabs() ) { | |
| 502 | + add_submenu_page( | |
| 503 | + '', | |
| 504 | + 'autoptimize_extra', | |
| 505 | + 'autoptimize_extra', | |
| 506 | + 'manage_options', | |
| 507 | + 'autoptimize_extra', | |
| 508 | + array( $this, 'options_page' ) | |
| 509 | + ); | |
| 510 | + } | |
| 338 | 511 | register_setting( 'autoptimize_extra_settings', 'autoptimize_extra_settings' ); |
| 339 | 512 | } |
| 340 | 513 | |
| 341 | 514 | public function add_extra_tab( $in ) |
| 342 | 515 | { |
| 343 | - $in = array_merge( $in, array( 'autoptimize_extra' => __( 'Extra', 'autoptimize' ) ) ); | |
| 516 | + if ( autoptimizeConfig::should_show_menu_tabs() ) { | |
| 517 | + $in = array_merge( $in, array( 'autoptimize_extra' => __( 'Extra', 'autoptimize' ) ) ); | |
| 518 | + } | |
| 344 | 519 | |
| 345 | 520 | return $in; |
| 346 | 521 | } |
| 347 | 522 | |
| @@ -346,8 +521,10 @@ | ||
| 346 | 521 | } |
| 347 | 522 | |
| 348 | 523 | public function options_page() |
| 349 | 524 | { |
| 525 | + // phpcs:disable Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace | |
| 526 | + | |
| 350 | 527 | // Working with actual option values from the database here. |
| 351 | 528 | // That way any saves are still processed as expected, but we can still |
| 352 | 529 | // override behavior by using `new autoptimizeExtra($custom_options)` and not have that custom |
| 353 | 530 | // behavior being persisted in the DB even if save is done here. |
| @@ -358,18 +535,19 @@ | ||
| 358 | 535 | #ao_settings_form {background: white;border: 1px solid #ccc;padding: 1px 15px;margin: 15px 10px 10px 0;} |
| 359 | 536 | #ao_settings_form .form-table th {font-weight: normal;} |
| 360 | 537 | #autoptimize_extra_descr{font-size: 120%;} |
| 361 | 538 | </style> |
| 539 | + <script>document.title = "Autoptimize: <?php _e( 'Extra', 'autoptimize' ); ?> " + document.title;</script> | |
| 362 | 540 | <div class="wrap"> |
| 363 | - <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> | |
| 364 | 542 | <?php echo autoptimizeConfig::ao_admin_tabs(); ?> |
| 365 | - <?php if ( 'on' !== get_option( 'autoptimize_js' ) && 'on' !== get_option( 'autoptimize_css' ) && 'on' !== get_option( 'autoptimize_html' ) && ! autoptimizeImages::imgopt_active() ) { ?> | |
| 543 | + <?php if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_js' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_html' ) && ! autoptimizeImages::imgopt_active() ) { ?> | |
| 366 | 544 | <div class="notice-warning notice"><p> |
| 367 | 545 | <?php _e( 'Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize' ); ?> |
| 368 | 546 | </p></div> |
| 369 | 547 | <?php } ?> |
| 370 | 548 | |
| 371 | - <form id='ao_settings_form' action='options.php' method='post'> | |
| 549 | + <form id='ao_settings_form' action='<?php echo admin_url( 'options.php' ); ?>' method='post'> | |
| 372 | 550 | <?php settings_fields( 'autoptimize_extra_settings' ); ?> |
| 373 | 551 | <h2><?php _e( 'Extra Auto-Optimizations', 'autoptimize' ); ?></h2> |
| 374 | 552 | <span id='autoptimize_extra_descr'><?php _e( 'The following settings can improve your site\'s performance even more.', 'autoptimize' ); ?></span> |
| 375 | 553 | <table class="form-table"> |
| @@ -377,11 +555,13 @@ | ||
| 377 | 555 | <th scope="row"><?php _e( 'Google Fonts', 'autoptimize' ); ?></th> |
| 378 | 556 | <td> |
| 379 | 557 | <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="1" <?php if ( ! in_array( $gfonts, array( 2, 3, 4, 5 ) ) ) { echo 'checked'; } ?> ><?php _e( 'Leave as is', 'autoptimize' ); ?><br/> |
| 380 | 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/> |
| 381 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="3" <?php checked( 3, $gfonts, true ); ?> ><?php _e( 'Combine and link in head (fonts load fast but are render-blocking)', 'autoptimize' ); ?><br/> | |
| 382 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="5" <?php checked( 5, $gfonts, true ); ?> ><?php _e( 'Combine and preload in head (fonts load late, but are not render-blocking)', 'autoptimize' ); ?><br/> | |
| 383 | - <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/> | |
| 559 | + <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?> | |
| 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/> | |
| 561 | + <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?> | |
| 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/> | |
| 384 | 564 | </td> |
| 385 | 565 | </tr> |
| 386 | 566 | <tr> |
| 387 | 567 | <th scope="row"><?php _e( 'Remove emojis', 'autoptimize' ); ?></th> |
| @@ -395,8 +575,14 @@ | ||
| 395 | 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> |
| 396 | 576 | </td> |
| 397 | 577 | </tr> |
| 398 | 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> | |
| 399 | 585 | <th scope="row"><?php _e( 'Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize' ); ?></th> |
| 400 | 586 | <td> |
| 401 | 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> |
| 402 | 588 | </td> |
| @@ -401,8 +587,14 @@ | ||
| 401 | 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> |
| 402 | 588 | </td> |
| 403 | 589 | </tr> |
| 404 | 590 | <tr> |
| 591 | + <th scope="row"><?php _e( 'Preload specific requests <em>(advanced users)</em>', 'autoptimize' ); ?></th> | |
| 592 | + <td> | |
| 593 | + <label><input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_7]' value='<?php if ( array_key_exists( 'autoptimize_extra_text_field_7', $options ) ) { echo esc_attr( $options['autoptimize_extra_text_field_7'] ); } ?>'><br /><?php _e( 'Comma-separated list with full URL\'s of to to-be-preloaded resources. To be used sparingly!', 'autoptimize' ); ?></label> | |
| 594 | + </td> | |
| 595 | + </tr> | |
| 596 | + <tr> | |
| 405 | 597 | <th scope="row"><?php _e( 'Async Javascript-files <em>(advanced users)</em>', 'autoptimize' ); ?></th> |
| 406 | 598 | <td> |
| 407 | 599 | <?php |
| 408 | 600 | if ( autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ) ) { |
| @@ -408,9 +600,9 @@ | ||
| 408 | 600 | if ( autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ) ) { |
| 409 | 601 | // translators: link points Async Javascript settings page. |
| 410 | 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>' ); |
| 411 | 603 | } else { |
| 412 | - ?> | |
| 604 | + ?> | |
| 413 | 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'] ); } ?>'> |
| 414 | 606 | <br /> |
| 415 | 607 | <?php |
| 416 | 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' ); |