Completed
Push — master ( 74ebb1...0e2a5e )
by frank
02:01
created
classes/autoptimizeExtra.php 1 patch
Spacing   +159 added lines, -159 removed lines patch added patch discarded remove patch
@@ -3,7 +3,7 @@  discard block
 block discarded – undo
3 3
  * Handles autoptimizeExtra frontend features + admin options page
4 4
  */
5 5
 
6
-if ( ! defined( 'ABSPATH' ) ) {
6
+if (!defined('ABSPATH')) {
7 7
     exit;
8 8
 }
9 9
 
@@ -21,9 +21,9 @@  discard block
 block discarded – undo
21 21
      *
22 22
      * @param array $options Optional. Allows overriding options without having to specify them via admin options page.
23 23
      */
24
-    public function __construct( $options = array() )
24
+    public function __construct($options = array())
25 25
     {
26
-        if ( empty( $options ) ) {
26
+        if (empty($options)) {
27 27
             $options = self::fetch_options();
28 28
         }
29 29
 
@@ -32,9 +32,9 @@  discard block
 block discarded – undo
32 32
 
33 33
     public function run()
34 34
     {
35
-        if ( is_admin() ) {
36
-            add_action( 'admin_menu', array( $this, 'admin_menu' ) );
37
-            add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_extra_tab' ) );
35
+        if (is_admin()) {
36
+            add_action('admin_menu', array($this, 'admin_menu'));
37
+            add_filter('autoptimize_filter_settingsscreen_tabs', array($this, 'add_extra_tab'));
38 38
         } else {
39 39
             $this->run_on_frontend();
40 40
         }
@@ -42,8 +42,8 @@  discard block
 block discarded – undo
42 42
 
43 43
     public static function fetch_options()
44 44
     {
45
-        $value = get_option( 'autoptimize_extra_settings' );
46
-        if ( empty( $value ) ) {
45
+        $value = get_option('autoptimize_extra_settings');
46
+        if (empty($value)) {
47 47
             // Fallback to returning defaults when no stored option exists yet.
48 48
             $value = autoptimizeConfig::get_ao_extra_default_options();
49 49
         }
@@ -54,55 +54,55 @@  discard block
 block discarded – undo
54 54
     public function disable_emojis()
55 55
     {
56 56
         // Removing all actions related to emojis!
57
-        remove_action( 'admin_print_styles', 'print_emoji_styles' );
58
-        remove_action( 'wp_head', 'print_emoji_detection_script', 7 );
59
-        remove_action( 'admin_print_scripts', 'print_emoji_detection_script' );
60
-        remove_action( 'wp_print_styles', 'print_emoji_styles' );
61
-        remove_filter( 'wp_mail', 'wp_staticize_emoji_for_email' );
62
-        remove_filter( 'the_content_feed', 'wp_staticize_emoji' );
63
-        remove_filter( 'comment_text_rss', 'wp_staticize_emoji' );
57
+        remove_action('admin_print_styles', 'print_emoji_styles');
58
+        remove_action('wp_head', 'print_emoji_detection_script', 7);
59
+        remove_action('admin_print_scripts', 'print_emoji_detection_script');
60
+        remove_action('wp_print_styles', 'print_emoji_styles');
61
+        remove_filter('wp_mail', 'wp_staticize_emoji_for_email');
62
+        remove_filter('the_content_feed', 'wp_staticize_emoji');
63
+        remove_filter('comment_text_rss', 'wp_staticize_emoji');
64 64
 
65 65
         // Removes TinyMCE emojis.
66
-        add_filter( 'tiny_mce_plugins', array( $this, 'filter_disable_emojis_tinymce' ) );
66
+        add_filter('tiny_mce_plugins', array($this, 'filter_disable_emojis_tinymce'));
67 67
 
68 68
         // Removes emoji dns-preftech.
69
-        add_filter( 'wp_resource_hints', array( $this, 'filter_remove_emoji_dns_prefetch' ), 10, 2 );
69
+        add_filter('wp_resource_hints', array($this, 'filter_remove_emoji_dns_prefetch'), 10, 2);
70 70
     }
71 71
 
72
-    public function filter_disable_emojis_tinymce( $plugins )
72
+    public function filter_disable_emojis_tinymce($plugins)
73 73
     {
74
-        if ( is_array( $plugins ) ) {
75
-            return array_diff( $plugins, array( 'wpemoji' ) );
74
+        if (is_array($plugins)) {
75
+            return array_diff($plugins, array('wpemoji'));
76 76
         } else {
77 77
             return array();
78 78
         }
79 79
     }
80 80
 
81
-    public function filter_remove_qs( $src )
81
+    public function filter_remove_qs($src)
82 82
     {
83
-        if ( strpos( $src, '?ver=' ) ) {
84
-            $src = remove_query_arg( 'ver', $src );
83
+        if (strpos($src, '?ver=')) {
84
+            $src = remove_query_arg('ver', $src);
85 85
         }
86 86
 
87 87
         return $src;
88 88
     }
89 89
 
90
-    public function extra_async_js( $in )
90
+    public function extra_async_js($in)
91 91
     {
92 92
         $exclusions = array();
93
-        if ( ! empty( $in ) ) {
94
-            $exclusions = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $in ) ) ), '' );
93
+        if (!empty($in)) {
94
+            $exclusions = array_fill_keys(array_filter(array_map('trim', explode(',', $in))), '');
95 95
         }
96 96
 
97 97
         $settings = $this->options['autoptimize_extra_text_field_3'];
98
-        $async    = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $settings ) ) ), '' );
99
-        $attr     = apply_filters( 'autoptimize_filter_extra_async', 'async' );
100
-        foreach ( $async as $k => $v ) {
101
-            $async[ $k ] = $attr;
98
+        $async    = array_fill_keys(array_filter(array_map('trim', explode(',', $settings))), '');
99
+        $attr     = apply_filters('autoptimize_filter_extra_async', 'async');
100
+        foreach ($async as $k => $v) {
101
+            $async[$k] = $attr;
102 102
         }
103 103
 
104 104
         // Merge exclusions & asyncs in one array and return to AO API.
105
-        $merged = array_merge( $exclusions, $async );
105
+        $merged = array_merge($exclusions, $async);
106 106
 
107 107
         return $merged;
108 108
     }
@@ -112,56 +112,56 @@  discard block
 block discarded – undo
112 112
         $options = $this->options;
113 113
 
114 114
         // Disable emojis if specified.
115
-        if ( ! empty( $options['autoptimize_extra_checkbox_field_1'] ) ) {
115
+        if (!empty($options['autoptimize_extra_checkbox_field_1'])) {
116 116
             $this->disable_emojis();
117 117
         }
118 118
 
119 119
         // Remove version query parameters.
120
-        if ( ! empty( $options['autoptimize_extra_checkbox_field_0'] ) ) {
121
-            add_filter( 'script_loader_src', array( $this, 'filter_remove_qs' ), 15, 1 );
122
-            add_filter( 'style_loader_src', array( $this, 'filter_remove_qs' ), 15, 1 );
120
+        if (!empty($options['autoptimize_extra_checkbox_field_0'])) {
121
+            add_filter('script_loader_src', array($this, 'filter_remove_qs'), 15, 1);
122
+            add_filter('style_loader_src', array($this, 'filter_remove_qs'), 15, 1);
123 123
         }
124 124
 
125 125
         // Avoiding conflicts of interest when async-javascript plugin is active!
126
-        $async_js_plugin_active = autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' );
127
-        if ( ! empty( $options['autoptimize_extra_text_field_3'] ) && ! $async_js_plugin_active ) {
128
-            add_filter( 'autoptimize_filter_js_exclude', array( $this, 'extra_async_js' ), 10, 1 );
126
+        $async_js_plugin_active = autoptimizeUtils::is_plugin_active('async-javascript/async-javascript.php');
127
+        if (!empty($options['autoptimize_extra_text_field_3']) && !$async_js_plugin_active) {
128
+            add_filter('autoptimize_filter_js_exclude', array($this, 'extra_async_js'), 10, 1);
129 129
         }
130 130
 
131 131
         // Optimize google fonts!
132
-        if ( ! empty( $options['autoptimize_extra_radio_field_4'] ) && ( '1' !== $options['autoptimize_extra_radio_field_4'] ) ) {
133
-            add_filter( 'wp_resource_hints', array( $this, 'filter_remove_gfonts_dnsprefetch' ), 10, 2 );
134
-            add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_optimize_google_fonts' ), 10, 1 );
135
-            add_filter( 'autoptimize_extra_filter_tobepreconn', array( $this, 'filter_preconnect_google_fonts' ), 10, 1 );
132
+        if (!empty($options['autoptimize_extra_radio_field_4']) && ('1' !== $options['autoptimize_extra_radio_field_4'])) {
133
+            add_filter('wp_resource_hints', array($this, 'filter_remove_gfonts_dnsprefetch'), 10, 2);
134
+            add_filter('autoptimize_html_after_minify', array($this, 'filter_optimize_google_fonts'), 10, 1);
135
+            add_filter('autoptimize_extra_filter_tobepreconn', array($this, 'filter_preconnect_google_fonts'), 10, 1);
136 136
         }
137 137
 
138 138
         // Preconnect!
139
-        if ( ! empty( $options['autoptimize_extra_text_field_2'] ) || has_filter( 'autoptimize_extra_filter_tobepreconn' ) ) {
140
-            add_filter( 'wp_resource_hints', array( $this, 'filter_preconnect' ), 10, 2 );
139
+        if (!empty($options['autoptimize_extra_text_field_2']) || has_filter('autoptimize_extra_filter_tobepreconn')) {
140
+            add_filter('wp_resource_hints', array($this, 'filter_preconnect'), 10, 2);
141 141
         }
142 142
     }
143 143
 
144
-    public function filter_remove_emoji_dns_prefetch( $urls, $relation_type )
144
+    public function filter_remove_emoji_dns_prefetch($urls, $relation_type)
145 145
     {
146
-        $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/' );
146
+        $emoji_svg_url = apply_filters('emoji_svg_url', 'https://s.w.org/images/core/emoji/');
147 147
 
148
-        return $this->filter_remove_dns_prefetch( $urls, $relation_type, $emoji_svg_url );
148
+        return $this->filter_remove_dns_prefetch($urls, $relation_type, $emoji_svg_url);
149 149
     }
150 150
 
151
-    public function filter_remove_gfonts_dnsprefetch( $urls, $relation_type )
151
+    public function filter_remove_gfonts_dnsprefetch($urls, $relation_type)
152 152
     {
153
-        return $this->filter_remove_dns_prefetch( $urls, $relation_type, 'fonts.googleapis.com' );
153
+        return $this->filter_remove_dns_prefetch($urls, $relation_type, 'fonts.googleapis.com');
154 154
     }
155 155
 
156
-    public function filter_remove_dns_prefetch( $urls, $relation_type, $url_to_remove )
156
+    public function filter_remove_dns_prefetch($urls, $relation_type, $url_to_remove)
157 157
     {
158 158
         $url_to_remove = (string) $url_to_remove;
159 159
 
160
-        if ( ! empty( $url_to_remove ) && 'dns-prefetch' === $relation_type ) {
160
+        if (!empty($url_to_remove) && 'dns-prefetch' === $relation_type) {
161 161
             $cnt = 0;
162
-            foreach ( $urls as $url ) {
163
-                if ( false !== strpos( $url, $url_to_remove ) ) {
164
-                    unset( $urls[ $cnt ] );
162
+            foreach ($urls as $url) {
163
+                if (false !== strpos($url, $url_to_remove)) {
164
+                    unset($urls[$cnt]);
165 165
                 }
166 166
                 $cnt++;
167 167
             }
@@ -170,131 +170,131 @@  discard block
 block discarded – undo
170 170
         return $urls;
171 171
     }
172 172
 
173
-    public function filter_optimize_google_fonts( $in )
173
+    public function filter_optimize_google_fonts($in)
174 174
     {
175 175
         // Extract fonts, partly based on wp rocket's extraction code.
176
-        $markup = preg_replace( '/<!--(.*)-->/Uis', '', $in );
177
-        preg_match_all( '#<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>#iU', $markup, $matches );
176
+        $markup = preg_replace('/<!--(.*)-->/Uis', '', $in);
177
+        preg_match_all('#<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>#iU', $markup, $matches);
178 178
 
179 179
         $fonts_collection = array();
180
-        if ( ! $matches[2] ) {
180
+        if (!$matches[2]) {
181 181
             return $in;
182 182
         }
183 183
 
184 184
         // Store them in $fonts array.
185 185
         $i = 0;
186
-        foreach ( $matches[2] as $font ) {
187
-            if ( ! preg_match( '/rel=["\']dns-prefetch["\']/', $matches[0][ $i ] ) ) {
186
+        foreach ($matches[2] as $font) {
187
+            if (!preg_match('/rel=["\']dns-prefetch["\']/', $matches[0][$i])) {
188 188
                 // Get fonts name.
189
-                $font = str_replace( array( '%7C', '%7c' ), '|', $font );
190
-                $font = explode( 'family=', $font );
191
-                $font = ( isset( $font[1] ) ) ? explode( '&', $font[1] ) : array();
189
+                $font = str_replace(array('%7C', '%7c'), '|', $font);
190
+                $font = explode('family=', $font);
191
+                $font = (isset($font[1])) ? explode('&', $font[1]) : array();
192 192
                 // Add font to $fonts[$i] but make sure not to pollute with an empty family!
193
-                $_thisfont = array_values( array_filter( explode( '|', reset( $font ) ) ) );
194
-                if ( ! empty( $_thisfont ) ) {
195
-                    $fonts_collection[ $i ]['fonts'] = $_thisfont;
193
+                $_thisfont = array_values(array_filter(explode('|', reset($font))));
194
+                if (!empty($_thisfont)) {
195
+                    $fonts_collection[$i]['fonts'] = $_thisfont;
196 196
                     // And add subset if any!
197
-                    $subset = ( is_array( $font ) ) ? end( $font ) : '';
198
-                    if ( false !== strpos( $subset, 'subset=' ) ) {
199
-                        $subset                            = str_replace( array( '%2C', '%2c' ), ',', $subset );
200
-                        $subset                            = explode( 'subset=', $subset );
201
-                        $fonts_collection[ $i ]['subsets'] = explode( ',', $subset[1] );
197
+                    $subset = (is_array($font)) ? end($font) : '';
198
+                    if (false !== strpos($subset, 'subset=')) {
199
+                        $subset                            = str_replace(array('%2C', '%2c'), ',', $subset);
200
+                        $subset                            = explode('subset=', $subset);
201
+                        $fonts_collection[$i]['subsets'] = explode(',', $subset[1]);
202 202
                     }
203 203
                 }
204 204
                 // And remove Google Fonts.
205
-                $in = str_replace( $matches[0][ $i ], '', $in );
205
+                $in = str_replace($matches[0][$i], '', $in);
206 206
             }
207 207
             $i++;
208 208
         }
209 209
 
210 210
         $options      = $this->options;
211 211
         $fonts_markup = '';
212
-        if ( '2' === $options['autoptimize_extra_radio_field_4'] ) {
212
+        if ('2' === $options['autoptimize_extra_radio_field_4']) {
213 213
             // Remove Google Fonts.
214
-            unset( $fonts_collection );
214
+            unset($fonts_collection);
215 215
             return $in;
216
-        } elseif ( '3' === $options['autoptimize_extra_radio_field_4'] || '5' === $options['autoptimize_extra_radio_field_4'] ) {
216
+        } elseif ('3' === $options['autoptimize_extra_radio_field_4'] || '5' === $options['autoptimize_extra_radio_field_4']) {
217 217
             // Aggregate & link!
218 218
             $fonts_string  = '';
219 219
             $subset_string = '';
220
-            foreach ( $fonts_collection as $font ) {
221
-                $fonts_string .= '|' . trim( implode( '|', $font['fonts'] ), '|' );
222
-                if ( ! empty( $font['subsets'] ) ) {
223
-                    $subset_string .= ',' . trim( implode( ',', $font['subsets'] ), ',' );
220
+            foreach ($fonts_collection as $font) {
221
+                $fonts_string .= '|'.trim(implode('|', $font['fonts']), '|');
222
+                if (!empty($font['subsets'])) {
223
+                    $subset_string .= ','.trim(implode(',', $font['subsets']), ',');
224 224
                 }
225 225
             }
226 226
 
227
-            if ( ! empty( $subset_string ) ) {
228
-                $subset_string = str_replace( ',', '%2C', ltrim( $subset_string, ',' ) );
229
-                $fonts_string  = $fonts_string . '&#038;subset=' . $subset_string;
227
+            if (!empty($subset_string)) {
228
+                $subset_string = str_replace(',', '%2C', ltrim($subset_string, ','));
229
+                $fonts_string  = $fonts_string.'&#038;subset='.$subset_string;
230 230
             }
231 231
 
232
-            $fonts_string = apply_filters( 'autoptimize_filter_extra_gfont_fontstring', str_replace( '|', '%7C', ltrim( $fonts_string, '|' ) ) );
232
+            $fonts_string = apply_filters('autoptimize_filter_extra_gfont_fontstring', str_replace('|', '%7C', ltrim($fonts_string, '|')));
233 233
 
234
-            if ( ! empty( $fonts_string ) ) {
235
-                if ( '5' === $options['autoptimize_extra_radio_field_4'] ) {
236
-                    $rel_string = 'rel="preload" as="style" onload="' . autoptimizeConfig::get_ao_css_preload_onload() . '"';
234
+            if (!empty($fonts_string)) {
235
+                if ('5' === $options['autoptimize_extra_radio_field_4']) {
236
+                    $rel_string = 'rel="preload" as="style" onload="'.autoptimizeConfig::get_ao_css_preload_onload().'"';
237 237
                 } else {
238 238
                     $rel_string = 'rel="stylesheet"';
239 239
                 }
240
-                $fonts_markup = '<link ' . $rel_string . ' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family=' . $fonts_string . '" />';
240
+                $fonts_markup = '<link '.$rel_string.' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family='.$fonts_string.'" />';
241 241
             }
242
-        } elseif ( '4' === $options['autoptimize_extra_radio_field_4'] ) {
242
+        } elseif ('4' === $options['autoptimize_extra_radio_field_4']) {
243 243
             // Aggregate & load async (webfont.js impl.)!
244 244
             $fonts_array = array();
245
-            foreach ( $fonts_collection as $_fonts ) {
246
-                if ( ! empty( $_fonts['subsets'] ) ) {
247
-                    $_subset = implode( ',', $_fonts['subsets'] );
248
-                    foreach ( $_fonts['fonts'] as $key => $_one_font ) {
249
-                        $_one_font               = $_one_font . ':' . $_subset;
250
-                        $_fonts['fonts'][ $key ] = $_one_font;
245
+            foreach ($fonts_collection as $_fonts) {
246
+                if (!empty($_fonts['subsets'])) {
247
+                    $_subset = implode(',', $_fonts['subsets']);
248
+                    foreach ($_fonts['fonts'] as $key => $_one_font) {
249
+                        $_one_font               = $_one_font.':'.$_subset;
250
+                        $_fonts['fonts'][$key] = $_one_font;
251 251
                     }
252 252
                 }
253
-                $fonts_array = array_merge( $fonts_array, $_fonts['fonts'] );
253
+                $fonts_array = array_merge($fonts_array, $_fonts['fonts']);
254 254
             }
255 255
 
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>';
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 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>';
259
-            $in                   = substr_replace( $in, $fonts_library_markup . '</head>', strpos( $in, '</head>' ), strlen( '</head>' ) );
259
+            $in                   = substr_replace($in, $fonts_library_markup.'</head>', strpos($in, '</head>'), strlen('</head>'));
260 260
         }
261 261
 
262 262
         // Replace back in markup.
263
-        $inject_point = apply_filters( 'autoptimize_filter_extra_gfont_injectpoint', '<link' );
264
-        $out          = substr_replace( $in, $fonts_markup . $inject_point, strpos( $in, $inject_point ), strlen( $inject_point ) );
265
-        unset( $fonts_collection );
263
+        $inject_point = apply_filters('autoptimize_filter_extra_gfont_injectpoint', '<link');
264
+        $out          = substr_replace($in, $fonts_markup.$inject_point, strpos($in, $inject_point), strlen($inject_point));
265
+        unset($fonts_collection);
266 266
 
267 267
         // and insert preload polyfill if "link preload" and if the polyfill isn't there yet (courtesy of inline&defer).
268 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 );
269
+        if ('5' === $options['autoptimize_extra_radio_field_4'] && strpos($out, $preload_polyfill) === false) {
270
+            $out = str_replace('</body>', $preload_polyfill.'</body>', $out);
271 271
         }
272 272
         return $out;
273 273
     }
274 274
 
275
-    public function filter_preconnect( $hints, $relation_type )
275
+    public function filter_preconnect($hints, $relation_type)
276 276
     {
277 277
         $options = $this->options;
278 278
 
279 279
         // Get settings and store in array.
280
-        $preconns = array_filter( array_map( 'trim', explode( ',', $options['autoptimize_extra_text_field_2'] ) ) );
281
-        $preconns = apply_filters( 'autoptimize_extra_filter_tobepreconn', $preconns );
280
+        $preconns = array_filter(array_map('trim', explode(',', $options['autoptimize_extra_text_field_2'])));
281
+        $preconns = apply_filters('autoptimize_extra_filter_tobepreconn', $preconns);
282 282
 
283 283
         // Walk array, extract domain and add to new array with crossorigin attribute.
284
-        foreach ( $preconns as $preconn ) {
285
-            $parsed = parse_url( $preconn );
286
-            if ( is_array( $parsed ) && empty( $parsed['scheme'] ) ) {
287
-                $domain = '//' . $parsed['host'];
288
-            } elseif ( is_array( $parsed ) ) {
289
-                $domain = $parsed['scheme'] . '://' . $parsed['host'];
284
+        foreach ($preconns as $preconn) {
285
+            $parsed = parse_url($preconn);
286
+            if (is_array($parsed) && empty($parsed['scheme'])) {
287
+                $domain = '//'.$parsed['host'];
288
+            } elseif (is_array($parsed)) {
289
+                $domain = $parsed['scheme'].'://'.$parsed['host'];
290 290
             }
291 291
 
292
-            if ( ! empty( $domain ) ) {
293
-                $hint = array( 'href' => $domain );
292
+            if (!empty($domain)) {
293
+                $hint = array('href' => $domain);
294 294
                 // Fonts don't get preconnected unless crossorigin flag is set, non-fonts don't get preconnected if origin flag is set
295 295
                 // so hardcode fonts.gstatic.com to come with crossorigin and have filter to add other domains if needed.
296
-                $crossorigins = apply_filters( 'autoptimize_extra_filter_preconn_crossorigin', array( 'https://fonts.gstatic.com' ) );
297
-                if ( in_array( $domain, $crossorigins ) ) {
296
+                $crossorigins = apply_filters('autoptimize_extra_filter_preconn_crossorigin', array('https://fonts.gstatic.com'));
297
+                if (in_array($domain, $crossorigins)) {
298 298
                     $hint['crossorigin'] = 'anonymous';
299 299
                 }
300 300
                 $new_hints[] = $hint;
@@ -302,21 +302,21 @@  discard block
 block discarded – undo
302 302
         }
303 303
 
304 304
         // Merge in WP's preconnect hints.
305
-        if ( 'preconnect' === $relation_type && ! empty( $new_hints ) ) {
306
-            $hints = array_merge( $hints, $new_hints );
305
+        if ('preconnect' === $relation_type && !empty($new_hints)) {
306
+            $hints = array_merge($hints, $new_hints);
307 307
         }
308 308
 
309 309
         return $hints;
310 310
     }
311 311
 
312
-    public function filter_preconnect_google_fonts( $in )
312
+    public function filter_preconnect_google_fonts($in)
313 313
     {
314
-        if ( '2' !== $this->options['autoptimize_extra_radio_field_4'] ) {
314
+        if ('2' !== $this->options['autoptimize_extra_radio_field_4']) {
315 315
             // Preconnect to fonts.gstatic.com unless we remove gfonts.
316 316
             $in[] = 'https://fonts.gstatic.com';
317 317
         }
318 318
 
319
-        if ( '4' === $this->options['autoptimize_extra_radio_field_4'] ) {
319
+        if ('4' === $this->options['autoptimize_extra_radio_field_4']) {
320 320
             // Preconnect even more hosts for webfont.js!
321 321
             $in[] = 'https://ajax.googleapis.com';
322 322
             $in[] = 'https://fonts.googleapis.com';
@@ -333,14 +333,14 @@  discard block
 block discarded – undo
333 333
             'autoptimize_extra',
334 334
             'manage_options',
335 335
             'autoptimize_extra',
336
-            array( $this, 'options_page' )
336
+            array($this, 'options_page')
337 337
         );
338
-        register_setting( 'autoptimize_extra_settings', 'autoptimize_extra_settings' );
338
+        register_setting('autoptimize_extra_settings', 'autoptimize_extra_settings');
339 339
     }
340 340
 
341
-    public function add_extra_tab( $in )
341
+    public function add_extra_tab($in)
342 342
     {
343
-        $in = array_merge( $in, array( 'autoptimize_extra' => __( 'Extra', 'autoptimize' ) ) );
343
+        $in = array_merge($in, array('autoptimize_extra' => __('Extra', 'autoptimize')));
344 344
 
345 345
         return $in;
346 346
     }
@@ -360,87 +360,87 @@  discard block
 block discarded – undo
360 360
         #autoptimize_extra_descr{font-size: 120%;}
361 361
     </style>
362 362
     <div class="wrap">
363
-    <h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1>
363
+    <h1><?php _e('Autoptimize Settings', 'autoptimize'); ?></h1>
364 364
         <?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() ) { ?>
365
+        <?php if ('on' !== get_option('autoptimize_js') && 'on' !== get_option('autoptimize_css') && 'on' !== get_option('autoptimize_html') && !autoptimizeImages::imgopt_active()) { ?>
366 366
             <div class="notice-warning notice"><p>
367
-            <?php _e( 'Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize' ); ?>
367
+            <?php _e('Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize'); ?>
368 368
             </p></div>
369 369
         <?php } ?>
370 370
 
371 371
     <form id='ao_settings_form' action='options.php' method='post'>
372
-        <?php settings_fields( 'autoptimize_extra_settings' ); ?>
373
-        <h2><?php _e( 'Extra Auto-Optimizations', 'autoptimize' ); ?></h2>
374
-        <span id='autoptimize_extra_descr'><?php _e( 'The following settings can improve your site\'s performance even more.', 'autoptimize' ); ?></span>
372
+        <?php settings_fields('autoptimize_extra_settings'); ?>
373
+        <h2><?php _e('Extra Auto-Optimizations', 'autoptimize'); ?></h2>
374
+        <span id='autoptimize_extra_descr'><?php _e('The following settings can improve your site\'s performance even more.', 'autoptimize'); ?></span>
375 375
         <table class="form-table">
376 376
             <tr>
377
-                <th scope="row"><?php _e( 'Google Fonts', 'autoptimize' ); ?></th>
377
+                <th scope="row"><?php _e('Google Fonts', 'autoptimize'); ?></th>
378 378
                 <td>
379
-                    <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
-                    <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/>
379
+                    <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
+                    <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/>
384 384
                 </td>
385 385
             </tr>
386 386
             <tr>
387
-                <th scope="row"><?php _e( 'Remove emojis', 'autoptimize' ); ?></th>
387
+                <th scope="row"><?php _e('Remove emojis', 'autoptimize'); ?></th>
388 388
                 <td>
389
-                    <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 _e( 'Removes WordPress\' core emojis\' inline CSS, inline JavaScript, and an otherwise un-autoptimized JavaScript file.', 'autoptimize' ); ?></label>
389
+                    <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 _e('Removes WordPress\' core emojis\' inline CSS, inline JavaScript, and an otherwise un-autoptimized JavaScript file.', 'autoptimize'); ?></label>
390 390
                 </td>
391 391
             </tr>
392 392
             <tr>
393
-                <th scope="row"><?php _e( 'Remove query strings from static resources', 'autoptimize' ); ?></th>
393
+                <th scope="row"><?php _e('Remove query strings from static resources', 'autoptimize'); ?></th>
394 394
                 <td>
395
-                    <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>
395
+                    <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 396
                 </td>
397 397
             </tr>
398 398
             <tr>
399
-                <th scope="row"><?php _e( 'Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize' ); ?></th>
399
+                <th scope="row"><?php _e('Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize'); ?></th>
400 400
                 <td>
401
-                    <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>
401
+                    <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 402
                 </td>
403 403
             </tr>
404 404
             <tr>
405
-                <th scope="row"><?php _e( 'Async Javascript-files <em>(advanced users)</em>', 'autoptimize' ); ?></th>
405
+                <th scope="row"><?php _e('Async Javascript-files <em>(advanced users)</em>', 'autoptimize'); ?></th>
406 406
                 <td>
407 407
                     <?php
408
-                    if ( autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ) ) {
408
+                    if (autoptimizeUtils::is_plugin_active('async-javascript/async-javascript.php')) {
409 409
                         // translators: link points Async Javascript settings page.
410
-                        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>' );
410
+                        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 411
                     } else {
412 412
                     ?>
413
-                        <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'] ); } ?>'>
413
+                        <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 414
                         <br />
415 415
                         <?php
416
-                            _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' );
416
+                            _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');
417 417
                             // translators: %s will be replaced by a link to the "async javascript" plugin.
418
-                            echo sprintf( __( '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>"' );
419
-                            $asj_install_url = network_admin_url() . 'plugin-install.php?s=async+javascript&tab=search&type=term';
420
-                            echo sprintf( ' <a href="' . $asj_install_url . '">%s</a>', __( 'Click here to install and activate it.', 'autoptimize' ) );
418
+                            echo sprintf(__('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>"');
419
+                            $asj_install_url = network_admin_url().'plugin-install.php?s=async+javascript&tab=search&type=term';
420
+                            echo sprintf(' <a href="'.$asj_install_url.'">%s</a>', __('Click here to install and activate it.', 'autoptimize'));
421 421
                     }
422 422
                     ?>
423 423
                 </td>
424 424
             </tr>
425 425
             <tr>
426
-                <th scope="row"><?php _e( 'Optimize YouTube videos', 'autoptimize' ); ?></th>
426
+                <th scope="row"><?php _e('Optimize YouTube videos', 'autoptimize'); ?></th>
427 427
                 <td>
428 428
                     <?php
429
-                    if ( autoptimizeUtils::is_plugin_active( 'wp-youtube-lyte/wp-youtube-lyte.php' ) ) {
430
-                        _e( 'Great, you have WP YouTube Lyte installed.', 'autoptimize' );
429
+                    if (autoptimizeUtils::is_plugin_active('wp-youtube-lyte/wp-youtube-lyte.php')) {
430
+                        _e('Great, you have WP YouTube Lyte installed.', 'autoptimize');
431 431
                         $lyte_config_url = 'options-general.php?page=lyte_settings_page';
432
-                        echo sprintf( ' <a href="' . $lyte_config_url . '">%s</a>', __( 'Click here to configure it.', 'autoptimize' ) );
432
+                        echo sprintf(' <a href="'.$lyte_config_url.'">%s</a>', __('Click here to configure it.', 'autoptimize'));
433 433
                     } else {
434 434
                         // translators: %s will be replaced by a link to "wp youtube lyte" plugin.
435
-                        echo sprintf( __( '%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>' );
436
-                        $lyte_install_url = network_admin_url() . 'plugin-install.php?s=lyte&tab=search&type=term';
437
-                        echo sprintf( ' <a href="' . $lyte_install_url . '">%s</a>', __( 'Click here to install and activate it.', 'autoptimize' ) );
435
+                        echo sprintf(__('%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>');
436
+                        $lyte_install_url = network_admin_url().'plugin-install.php?s=lyte&tab=search&type=term';
437
+                        echo sprintf(' <a href="'.$lyte_install_url.'">%s</a>', __('Click here to install and activate it.', 'autoptimize'));
438 438
                     }
439 439
                     ?>
440 440
                 </td>
441 441
             </tr>
442 442
         </table>
443
-        <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Save Changes', 'autoptimize' ); ?>" /></p>
443
+        <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes', 'autoptimize'); ?>" /></p>
444 444
     </form>
445 445
         <?php
446 446
     }
Please login to merge, or discard this patch.