PluginProbe
Autoptimize / 3.1.12
Autoptimize v3.1.12
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
autoptimize / classes / autoptimizeExtra.php

autoptimizeExtra.php in Autoptimize 3.1.12, at classes/autoptimizeExtra.php

654 lines 33.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Handles autoptimizeExtra frontend features + admin options page
4 */
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit;
8 }
9
10 class autoptimizeExtra
11 {
12 /**
13 * Options
14 *
15 * @var array
16 */
17 protected $options = array();
18
19 /**
20 * Singleton instance.
21 *
22 * @var self|null
23 */
24 protected static $instance = null;
25
26 /**
27 * Creates an instance and calls run().
28 *
29 * @param array $options Optional. Allows overriding options without having to specify them via admin options page.
30 */
31 public function __construct( $options = array() )
32 {
33 if ( empty( $options ) ) {
34 $options = self::fetch_options();
35 }
36
37 $this->options = $options;
38 }
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
57 public function run()
58 {
59 if ( is_admin() ) {
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 }
65 add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_extra_tab' ) );
66 } else {
67 add_action( 'wp', array( $this, 'run_on_frontend' ) );
68 }
69 }
70
71 public function set_options( array $options )
72 {
73 $this->options = $options;
74
75 return $this;
76 }
77
78 public static function fetch_options()
79 {
80 $value = autoptimizeOptionWrapper::get_option( 'autoptimize_extra_settings' );
81 if ( empty( $value ) ) {
82 // Fallback to returning defaults when no stored option exists yet.
83 $value = autoptimizeConfig::get_ao_extra_default_options();
84 }
85
86 return $value;
87 }
88
89 public function disable_emojis()
90 {
91 // Removing all actions related to emojis!
92 remove_action( 'admin_print_styles', 'print_emoji_styles' );
93 remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
94 remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
95 remove_action( 'wp_print_styles', 'print_emoji_styles' );
96 remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
97 remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
98 remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
99
100 // Removes TinyMCE emojis.
101 add_filter( 'tiny_mce_plugins', array( $this, 'filter_disable_emojis_tinymce' ) );
102
103 // Removes emoji dns-preftech.
104 add_filter( 'emoji_svg_url', '__return_false' );
105 }
106
107 public function filter_disable_emojis_tinymce( $plugins )
108 {
109 if ( is_array( $plugins ) ) {
110 return array_diff( $plugins, array( 'wpemoji' ) );
111 } else {
112 return array();
113 }
114 }
115
116 public function filter_remove_qs( $src )
117 {
118 if ( ! empty( $src ) ) {
119 if ( strpos( $src, '?ver=' ) ) {
120 $src = remove_query_arg( 'ver', $src );
121 } elseif ( strpos( $src, '?v=' ) ) {
122 $src = remove_query_arg( 'v', $src );
123 }
124 }
125
126 return $src;
127 }
128
129 public function extra_async_js( $in )
130 {
131 $exclusions = array();
132 if ( ! empty( $in ) ) {
133 $exclusions = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $in ) ) ), '' );
134 }
135
136 $settings = wp_strip_all_tags( $this->options['autoptimize_extra_text_field_3'] );
137 $async = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $settings ) ) ), '' );
138 $attr = apply_filters( 'autoptimize_filter_extra_async', 'async' );
139 foreach ( $async as $k => $v ) {
140 $async[ $k ] = $attr;
141 }
142
143 // Merge exclusions & asyncs in one array and return to AO API.
144 $merged = array_merge( $exclusions, $async );
145
146 return $merged;
147 }
148
149 public function run_on_frontend()
150 {
151 // only run the Extra optimizations on frontend if general conditions
152 // for optimizations are met, this to ensure e.g. removing querystrings
153 // is not done when optimizing for logged in users is off, breaking
154 // some pagebuilders (Divi & Elementor).
155 if ( false === autoptimizeMain::should_buffer() ) {
156 return;
157 }
158
159 $options = $this->options;
160
161 // Disable emojis if specified.
162 if ( ! empty( $options['autoptimize_extra_checkbox_field_1'] ) ) {
163 $this->disable_emojis();
164 }
165
166 // Remove version query parameters.
167 if ( ! empty( $options['autoptimize_extra_checkbox_field_0'] ) ) {
168 add_filter( 'script_loader_src', array( $this, 'filter_remove_qs' ), 15, 1 );
169 add_filter( 'style_loader_src', array( $this, 'filter_remove_qs' ), 15, 1 );
170 }
171
172 // Avoiding conflicts of interest when async-javascript plugin is active!
173 $async_js_plugin_active = autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' );
174 if ( ! empty( $options['autoptimize_extra_text_field_3'] ) && ! $async_js_plugin_active ) {
175 add_filter( 'autoptimize_filter_js_exclude', array( $this, 'extra_async_js' ), 10, 1 );
176 }
177
178 // Optimize google fonts!
179 if ( ! empty( $options['autoptimize_extra_radio_field_4'] ) && ( '1' !== $options['autoptimize_extra_radio_field_4'] ) ) {
180 add_filter( 'wp_resource_hints', array( $this, 'filter_remove_gfonts_dnsprefetch' ), 10, 2 );
181 add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_optimize_google_fonts' ), 10, 1 );
182 add_filter( 'autoptimize_extra_filter_tobepreconn', array( $this, 'filter_preconnect_google_fonts' ), 10, 1 );
183
184 if ( '2' === $options['autoptimize_extra_radio_field_4'] ) {
185 // remove Google Fonts, adding filters to also remove Google Fonts from 3rd party themes/ plugins.
186 // inspired by https://wordpress.org/plugins/disable-remove-google-fonts/.
187 remove_action( 'wp_footer', 'et_builder_print_font' ); // Divi.
188 remove_action( 'wp_footer', array( 'RevSliderFront', 'load_google_fonts' ) ); // Revslider.
189 add_filter( 'elementor/frontend/print_google_fonts', '__return_false' ); // Elementor.
190 add_filter( 'fl_builder_google_fonts_pre_enqueue', '__return_empty_array' ); // Beaver Builder.
191 }
192 }
193
194 // Preconnect!
195 if ( ! empty( $options['autoptimize_extra_text_field_2'] ) || has_filter( 'autoptimize_extra_filter_tobepreconn' ) ) {
196 add_filter( 'wp_resource_hints', array( $this, 'filter_preconnect' ), 10, 2 );
197 }
198
199 // Preload!
200 if ( ! empty( $options['autoptimize_extra_text_field_7'] ) || has_filter( 'autoptimize_filter_extra_tobepreloaded' ) || ! empty( autoptimizeConfig::get_post_meta_ao_settings( 'ao_post_preload' ) ) ) {
201 add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_preload' ), 10, 2 );
202 }
203
204 // Remove global styles.
205 if ( ! empty( $options['autoptimize_extra_checkbox_field_8'] ) ) {
206 $this->disable_global_styles();
207 }
208 }
209
210 public function filter_remove_gfonts_dnsprefetch( $urls, $relation_type )
211 {
212 return $this->filter_remove_dns_prefetch( $urls, $relation_type, 'fonts.googleapis.com' );
213 }
214
215 public function filter_remove_dns_prefetch( $urls, $relation_type, $url_to_remove )
216 {
217 $url_to_remove = (string) $url_to_remove;
218
219 if ( ! empty( $url_to_remove ) && 'dns-prefetch' === $relation_type ) {
220 $cnt = 0;
221 foreach ( $urls as $url ) {
222 if ( false !== strpos( $url, $url_to_remove ) ) {
223 unset( $urls[ $cnt ] );
224 }
225 $cnt++;
226 }
227 }
228
229 return $urls;
230 }
231
232 public function filter_optimize_google_fonts( $in )
233 {
234 // Extract fonts, partly based on wp rocket's extraction code.
235 $markup = preg_replace( '/<!--(.*)-->/Uis', '', $in );
236 preg_match_all( '#<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>#iU', $markup, $matches );
237
238 $fonts_collection = array();
239 if ( ! $matches[2] ) {
240 return $in;
241 }
242
243 // Store them in $fonts array.
244 $i = 0;
245 foreach ( $matches[2] as $font ) {
246 if ( ! preg_match( '/rel=["\']dns-prefetch["\']/', $matches[0][ $i ] ) ) {
247 // Get fonts name.
248 $font = str_replace( array( '%7C', '%7c' ), '|', $font );
249 if ( strpos( $font, 'fonts.googleapis.com/css2' ) !== false ) {
250 // (Somewhat) change Google Fonts APIv2 syntax back to v1.
251 // todo: support for 100..900
252 $font = rawurldecode( $font );
253 $font = str_replace( array( 'css2?', 'ital,wght@', 'wght@', 'ital@', '0,', '1,', ':1', ';', '&family=' ), array( 'css?', '', '', '', '', 'italic', ':italic', ',', '%7C' ), $font );
254 }
255 $font = explode( 'family=', $font );
256 $font = ( isset( $font[1] ) ) ? explode( '&', $font[1] ) : array();
257 // Add font to $fonts[$i] but make sure not to pollute with an empty family!
258 $_thisfont = array_values( array_filter( explode( '|', reset( $font ) ) ) );
259 if ( ! empty( $_thisfont ) ) {
260 $fonts_collection[ $i ]['fonts'] = $_thisfont;
261 // And add subset if any!
262 $subset = ( is_array( $font ) ) ? end( $font ) : '';
263 if ( false !== strpos( $subset, 'subset=' ) ) {
264 $subset = str_replace( array( '%2C', '%2c' ), ',', $subset );
265 $subset = explode( 'subset=', $subset );
266 $fonts_collection[ $i ]['subsets'] = explode( ',', $subset[1] );
267 }
268 }
269 // And remove Google Fonts.
270 $in = str_replace( $matches[0][ $i ], '', $in );
271 }
272 $i++;
273 }
274
275 $options = $this->options;
276 $fonts_markup = '';
277 if ( '2' === $options['autoptimize_extra_radio_field_4'] ) {
278 // Remove Google Fonts.
279 unset( $fonts_collection );
280 return $in;
281 } elseif ( '3' === $options['autoptimize_extra_radio_field_4'] || '5' === $options['autoptimize_extra_radio_field_4'] ) {
282 // Aggregate & link!
283 $fonts_string = '';
284 $subset_string = '';
285 foreach ( $fonts_collection as $font ) {
286 $fonts_string .= '|' . trim( implode( '|', $font['fonts'] ), '|' );
287 if ( ! empty( $font['subsets'] ) ) {
288 $subset_string .= ',' . trim( implode( ',', $font['subsets'] ), ',' );
289 }
290 }
291
292 if ( ! empty( $subset_string ) ) {
293 $subset_string = str_replace( ',', '%2C', ltrim( $subset_string, ',' ) );
294 $fonts_string = $fonts_string . '&#038;subset=' . $subset_string;
295 }
296
297 $fonts_string = apply_filters( 'autoptimize_filter_extra_gfont_fontstring', str_replace( '|', '%7C', ltrim( $fonts_string, '|' ) ) );
298 // only add display parameter if there is none in $fonts_string (by virtue of the filter).
299 if ( strpos( $fonts_string, 'display=' ) === false ) {
300 $fonts_string .= apply_filters( 'autoptimize_filter_extra_gfont_display', '&amp;display=swap' );
301 }
302
303 if ( ! empty( $fonts_string ) ) {
304 if ( '5' === $options['autoptimize_extra_radio_field_4'] ) {
305 $rel_string = 'rel="stylesheet" media="print" onload="' . autoptimizeConfig::get_ao_css_preload_onload() . '"';
306 } else {
307 $rel_string = 'rel="stylesheet"';
308 }
309 $fonts_markup = '<link ' . $rel_string . ' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family=' . $fonts_string . '">';
310 }
311 } elseif ( '4' === $options['autoptimize_extra_radio_field_4'] ) {
312 // Aggregate & load async (webfont.js impl.)!
313 $fonts_array = array();
314 foreach ( $fonts_collection as $_fonts ) {
315 if ( ! empty( $_fonts['subsets'] ) ) {
316 $_subset = implode( ',', $_fonts['subsets'] );
317 foreach ( $_fonts['fonts'] as $key => $_one_font ) {
318 $_one_font = $_one_font . ':' . $_subset;
319 $_fonts['fonts'][ $key ] = $_one_font;
320 }
321 }
322 $fonts_array = array_merge( $fonts_array, $_fonts['fonts'] );
323 }
324
325 $fonts_array = array_map( 'urldecode', $fonts_array );
326 $fonts_array = array_map(
327 function( $_f ) {
328 return trim( $_f, ',' );
329 },
330 $fonts_array
331 );
332
333 // type attrib on <script not added by default.
334 $type_js = '';
335 if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) {
336 $type_js = 'type="text/javascript" ';
337 }
338
339 $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>';
340 $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>';
341 $in = substr_replace( $in, $fonts_library_markup . '</head>', strpos( $in, '</head>' ), strlen( '</head>' ) );
342 }
343
344 // Replace back in markup.
345 $inject_point = apply_filters( 'autoptimize_filter_extra_gfont_injectpoint', '<link' );
346 $out = substr_replace( $in, $fonts_markup . $inject_point, strpos( $in, $inject_point ), strlen( $inject_point ) );
347 unset( $fonts_collection );
348
349 return $out;
350 }
351
352 public function filter_preconnect( $hints, $relation_type )
353 {
354 $options = $this->options;
355 $preconns = array();
356
357 // Get settings and store in array.
358 if ( array_key_exists( 'autoptimize_extra_text_field_2', $options ) ) {
359 $preconns = array_filter( array_map( 'trim', explode( ',', wp_strip_all_tags( $options['autoptimize_extra_text_field_2'] ) ) ) );
360 }
361 $preconns = apply_filters( 'autoptimize_extra_filter_tobepreconn', $preconns );
362
363 // Walk array, extract domain and add to new array with crossorigin attribute.
364 foreach ( $preconns as $preconn ) {
365 $domain = '';
366 $parsed = parse_url( $preconn );
367 if ( is_array( $parsed ) && ! empty( $parsed['host'] ) && empty( $parsed['scheme'] ) ) {
368 $domain = '//' . $parsed['host'];
369 } elseif ( is_array( $parsed ) && ! empty( $parsed['host'] ) ) {
370 $domain = $parsed['scheme'] . '://' . $parsed['host'];
371 }
372
373 if ( ! empty( $domain ) ) {
374 $hint = array( 'href' => $domain );
375 // Fonts don't get preconnected unless crossorigin flag is set, non-fonts don't get preconnected if origin flag is set
376 // so hardcode fonts.gstatic.com to come with crossorigin and have filter to add other domains if needed.
377 $crossorigins = apply_filters( 'autoptimize_extra_filter_preconn_crossorigin', array( 'https://fonts.gstatic.com' ) );
378 if ( in_array( $domain, $crossorigins ) ) {
379 $hint['crossorigin'] = 'anonymous';
380 }
381 $new_hints[] = $hint;
382 }
383 }
384
385 // Merge in WP's preconnect hints.
386 if ( 'preconnect' === $relation_type && ! empty( $new_hints ) ) {
387 $hints = array_merge( $hints, $new_hints );
388 }
389
390 return $hints;
391 }
392
393 public function filter_preconnect_google_fonts( $in )
394 {
395 if ( '2' !== $this->options['autoptimize_extra_radio_field_4'] ) {
396 // Preconnect to fonts.gstatic.com unless we remove gfonts.
397 $in[] = 'https://fonts.gstatic.com';
398 }
399
400 if ( '4' === $this->options['autoptimize_extra_radio_field_4'] ) {
401 // Preconnect even more hosts for webfont.js!
402 $in[] = 'https://ajax.googleapis.com';
403 $in[] = 'https://fonts.googleapis.com';
404 }
405
406 return $in;
407 }
408
409 public function filter_preload( $in ) {
410 // make array from comma separated list.
411 $options = $this->options;
412 $preloads = array();
413 if ( array_key_exists( 'autoptimize_extra_text_field_7', $options ) ) {
414 $preloads = array_filter( array_map( 'trim', explode( ',', wp_strip_all_tags( $options['autoptimize_extra_text_field_7'] ) ) ) );
415 }
416
417 if ( false === autoptimizeImages::imgopt_active() && false === autoptimizeImages::should_lazyload_wrapper() ) {
418 // only do this here if imgopt/ lazyload are not active?
419 $metabox_preloads = array_filter( array_map( 'trim', explode( ',', wp_strip_all_tags( autoptimizeConfig::get_post_meta_ao_settings( 'ao_post_preload' ) ) ) ) );
420 if ( ! empty( $metabox_preloads ) ) {
421 $preloads = array_merge( $preloads, $metabox_preloads );
422 }
423 }
424
425 $preloads = apply_filters( 'autoptimize_filter_extra_tobepreloaded', $preloads );
426
427 // immediately return if nothing to be preloaded.
428 if ( empty( $preloads ) ) {
429 return $in;
430 }
431
432 // iterate through array and add preload link to tmp string.
433 $preload_output = '';
434 foreach ( $preloads as $preload ) {
435 if ( filter_var( $preload, FILTER_VALIDATE_URL ) !== $preload ) {
436 continue;
437 }
438 $preload = esc_url_raw( $preload );
439 $crossorigin = '';
440 $preload_as = '';
441 $mime_type = '';
442 $_preload = strtok( $preload, '?' );
443
444 if ( autoptimizeUtils::str_ends_in( $_preload, '.css' ) ) {
445 $preload_as = 'style';
446 } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.js' ) ) {
447 $preload_as = 'script';
448 } 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' ) ) {
449 $preload_as = 'font';
450 $crossorigin = ' crossorigin';
451 $mime_type = ' type="font/' . pathinfo( $_preload, PATHINFO_EXTENSION ) . '"';
452 if ( ' type="font/eot"' === $mime_type ) {
453 $mime_type = 'application/vnd.ms-fontobject';
454 }
455 } 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' ) ) {
456 $preload_as = 'image';
457 } else {
458 $preload_as = 'other';
459 }
460
461 $preload_output .= '<link rel="preload" href="' . $preload . '" as="' . $preload_as . '"' . $mime_type . $crossorigin . '>';
462 }
463 $preload_output = apply_filters( 'autoptimize_filter_extra_preload_output', $preload_output );
464
465 return $this->inject_preloads( $preload_output, $in );
466 }
467
468 public static function inject_preloads( $preloads, $html ) {
469 // add string to head (before first link node by default).
470 $preload_inject = apply_filters( 'autoptimize_filter_extra_preload_inject', '<link' );
471 $position = autoptimizeUtils::strpos( $html, $preload_inject );
472
473 return autoptimizeUtils::substr_replace( $html, $preloads . $preload_inject, $position, strlen( $preload_inject ) );
474 }
475
476 public function disable_global_styles()
477 {
478 remove_action( 'wp_enqueue_scripts', 'wp_enqueue_global_styles' );
479 remove_action( 'wp_footer', 'wp_enqueue_global_styles', 1 );
480 remove_action( 'wp_body_open', 'wp_global_styles_render_svg_filters' );
481
482 if ( true === apply_filters( 'autoptimize_filter_extra_global_styles_and_block_css', true ) ) {
483 add_action(
484 'wp_enqueue_scripts',
485 function() {
486 wp_dequeue_style( 'wp-block-library' );
487 wp_dequeue_style( 'wp-block-library-theme' );
488 }
489 );
490 }
491
492 if ( true === apply_filters( 'autoptimize_filter_extra_remove_woocommerce_block_css', true ) ) {
493 add_action(
494 'wp_enqueue_scripts',
495 function() {
496 wp_dequeue_style( 'wc-blocks-style' );
497 }
498 );
499 }
500 }
501
502 public function admin_menu()
503 {
504 // no acces if multisite and not network admin and no site config allowed.
505 if ( autoptimizeConfig::should_show_menu_tabs() ) {
506 add_submenu_page(
507 '',
508 'autoptimize_extra',
509 'autoptimize_extra',
510 'manage_options',
511 'autoptimize_extra',
512 array( $this, 'options_page' )
513 );
514 }
515 register_setting( 'autoptimize_extra_settings', 'autoptimize_extra_settings' );
516 }
517
518 public function add_extra_tab( $in )
519 {
520 if ( autoptimizeConfig::should_show_menu_tabs() ) {
521 $in = array_merge( $in, array( 'autoptimize_extra' => esc_html__( 'Extra', 'autoptimize' ) ) );
522 }
523
524 return $in;
525 }
526
527 public function options_page()
528 {
529 // phpcs:disable Squiz.ControlStructures.ControlSignature.NewlineAfterOpenBrace
530
531 // Working with actual option values from the database here.
532 // That way any saves are still processed as expected, but we can still
533 // override behavior by using `new autoptimizeExtra($custom_options)` and not have that custom
534 // behavior being persisted in the DB even if save is done here.
535 $options = $this->fetch_options();
536 $gfonts = $options['autoptimize_extra_radio_field_4'];
537 ?>
538 <style>
539 #ao_settings_form {background: white;border: 1px solid #ccc;padding: 1px 15px;margin: 15px 10px 10px 0;}
540 #ao_settings_form .form-table th {font-weight: normal;}
541 #autoptimize_extra_descr{font-size: 120%;}
542 </style>
543 <script>document.title = "Autoptimize: <?php esc_html_e( 'Extra', 'autoptimize' ); ?> " + document.title;</script>
544 <div class="wrap">
545 <h1><?php apply_filters( 'autoptimize_filter_settings_is_pro', false ) ? esc_html_e( 'Autoptimize Pro Settings', 'autoptimize' ) : esc_html_e( 'Autoptimize Settings', 'autoptimize' ); ?></h1>
546 <?php echo autoptimizeConfig::ao_admin_tabs(); ?>
547 <?php if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_js' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_html' ) && ! autoptimizeImages::imgopt_active() ) { ?>
548 <div class="notice-warning notice"><p>
549 <?php esc_html_e( 'Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize' ); ?>
550 </p></div>
551 <?php } ?>
552
553 <form id='ao_settings_form' action='<?php echo admin_url( 'options.php' ); ?>' method='post'>
554 <?php settings_fields( 'autoptimize_extra_settings' ); ?>
555 <h2><?php esc_html_e( 'Extra Auto-Optimizations', 'autoptimize' ); ?></h2>
556 <span id='autoptimize_extra_descr'><?php esc_html_e( 'The following settings can improve your site\'s performance even more.', 'autoptimize' ); ?></span>
557 <table class="form-table">
558 <tr>
559 <th scope="row"><?php esc_html_e( 'Google Fonts', 'autoptimize' ); ?></th>
560 <td>
561 <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 esc_html_e( 'Leave as is', 'autoptimize' ); ?><br/>
562 <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="2" <?php checked( 2, $gfonts, true ); ?> ><?php esc_html_e( 'Remove Google Fonts', 'autoptimize' ); ?><br/>
563 <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?>
564 <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="3" <?php checked( 3, $gfonts, true ); ?> ><?php echo esc_html__( 'Combine and link in head (fonts load fast but are render-blocking)', 'autoptimize' ) . ', ' . sprintf( esc_html__( 'includes %1$sdisplay:swap%2$s.', 'autoptimize' ), '<code>', '</code>' ); ?><br/>
565 <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?>
566 <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="5" <?php checked( 5, $gfonts, true ); ?> ><?php echo esc_html__( 'Combine and link deferred in head (fonts load late, but are not render-blocking)', 'autoptimize' ) . ', ' . sprintf( esc_html__( 'includes %1$sdisplay:swap%2$s.', 'autoptimize' ), '<code>', '</code>' ); ?>
567 <span <?php if ( '4' !== $gfonts ){ echo "style='display:none;' "; } ?> ><br/><input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="4" <?php checked( 4, $gfonts, true ); ?> ><?php echo sprintf( esc_html__( 'Combine and load fonts asynchronously with %1$swebfont.js%2$s', 'autoptimize' ), '<a href="https://github.com/typekit/webfontloader#readme" target="_blank">', '</a>' ) . ' ' . esc_html__( '(deprecated)', 'autoptimize' ); ?></span><br/>
568 </td>
569 </tr>
570 <tr>
571 <th scope="row"><?php esc_html_e( 'Remove emojis', 'autoptimize' ); ?></th>
572 <td>
573 <label><input type='checkbox' name='autoptimize_extra_settings[autoptimize_extra_checkbox_field_1]' <?php if ( ! empty( $options['autoptimize_extra_checkbox_field_1'] ) && '1' === $options['autoptimize_extra_checkbox_field_1'] ) { echo 'checked="checked"'; } ?> value='1'><?php esc_html_e( 'Removes WordPress\' core emojis\' inline CSS, inline JavaScript, and an otherwise un-autoptimized JavaScript file.', 'autoptimize' ); ?></label>
574 </td>
575 </tr>
576 <tr>
577 <th scope="row"><?php esc_html_e( 'Remove query strings from static resources', 'autoptimize' ); ?></th>
578 <td>
579 <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'>
580 <?php
581 // translators: just a code tag around "ver" which is the parameter added to CSS/ JS URL's by wordpress.
582 printf( esc_html__( 'Removing query strings (or more specifically the %1$sver%2$s parameter) will not improve load time, but might improve performance scores.', 'autoptimize' ), '<code>', '</code>' );
583 ?>
584 </label>
585 </td>
586 </tr>
587 <tr>
588 <th scope="row"><?php esc_html_e( 'Remove WordPress block CSS', 'autoptimize' ); ?></th>
589 <td>
590 <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 esc_html_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>
591 </td>
592 </tr>
593 <tr>
594 <th scope="row"><?php esc_html_e( 'Preconnect to 3rd party domains (advanced users)', 'autoptimize' ); ?></th>
595 <td>
596 <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 />
597 <?php
598 // Translators; link to a page on keycdn blog about preconnecting.
599 printf( esc_html__( 'Add 3rd party domains you want the browser to %1$spreconnect%2$s to, separated by comma\'s. Make sure to include the correct protocol (HTTP or HTTPS).', 'autoptimize' ), '<a href="https://www.keycdn.com/support/preconnect/#primary" target="_blank">', '</a>' );
600 ?>
601 </label>
602 </td>
603 </tr>
604 <tr>
605 <th scope="row"><?php esc_html_e( 'Preload specific requests (advanced users)', 'autoptimize' ); ?></th>
606 <td>
607 <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 esc_html_e( 'Comma-separated list with full URL\'s of to to-be-preloaded resources. To be used sparingly!', 'autoptimize' ); ?></label>
608 </td>
609 </tr>
610 <tr>
611 <th scope="row"><?php esc_html_e( 'Async Javascript-files (advanced users)', 'autoptimize' ); ?></th>
612 <td>
613 <?php
614 if ( autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ) ) {
615 // translators: link points Async Javascript settings page.
616 printf( esc_html__( '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>' );
617 } else {
618 ?>
619 <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'] ); } ?>'>
620 <br />
621 <?php
622 printf( esc_html__( 'Comma-separated list of local or 3rd party JS-files that should loaded with the %1$sasync%2$s flag. JS-files from your own site will be automatically excluded if added here. ', 'autoptimize' ), '<code>', '</code>' );
623 // translators: %s will be replaced by a link to the "async javascript" plugin.
624 echo sprintf( esc_html__( 'Configuration of async javascript is easier and more flexible using the %s plugin.', 'autoptimize' ), '"<a href="https://wordpress.org/plugins/async-javascript" target="_blank">Async Javascript</a>"' );
625 $asj_install_url = network_admin_url() . 'plugin-install.php?s=async+javascript&tab=search&type=term';
626 echo sprintf( ' <a href="' . $asj_install_url . '">%s</a>', esc_html__( 'Click here to install and activate it.', 'autoptimize' ) );
627 }
628 ?>
629 </td>
630 </tr>
631 <tr>
632 <th scope="row"><?php esc_html_e( 'Optimize YouTube videos', 'autoptimize' ); ?></th>
633 <td>
634 <?php
635 if ( autoptimizeUtils::is_plugin_active( 'wp-youtube-lyte/wp-youtube-lyte.php' ) ) {
636 esc_html_e( 'Great, you have WP YouTube Lyte installed.', 'autoptimize' );
637 $lyte_config_url = 'options-general.php?page=lyte_settings_page';
638 echo sprintf( ' <a href="' . $lyte_config_url . '">%s</a>', esc_html__( 'Click here to configure it.', 'autoptimize' ) );
639 } else {
640 // translators: %s will be replaced by a link to "wp youtube lyte" plugin.
641 echo sprintf( esc_html__( '%s allows you to “lazy load” your videos, by inserting responsive “Lite YouTube Embeds". ', 'autoptimize' ), '<a href="https://wordpress.org/plugins/wp-youtube-lyte" target="_blank">WP YouTube Lyte</a>' );
642 $lyte_install_url = network_admin_url() . 'plugin-install.php?s=lyte&tab=search&type=term';
643 echo sprintf( ' <a href="' . $lyte_install_url . '">%s</a>', esc_html__( 'Click here to install and activate it.', 'autoptimize' ) );
644 }
645 ?>
646 </td>
647 </tr>
648 </table>
649 <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php esc_html_e( 'Save Changes', 'autoptimize' ); ?>" /></p>
650 </form>
651 <?php
652 }
653 }
654