@@ -335,6 +335,9 @@ discard block |
||
335 | 335 | } |
336 | 336 | } |
337 | 337 | |
338 | + /** |
|
339 | + * @param boolean $render |
|
340 | + */ |
|
338 | 341 | public static function ao_ccss_key_status( $render ) { |
339 | 342 | // Provide key status |
340 | 343 | // Get key and key status. |
@@ -539,6 +542,9 @@ discard block |
||
539 | 542 | return true; |
540 | 543 | } |
541 | 544 | |
545 | + /** |
|
546 | + * @param integer $lvl |
|
547 | + */ |
|
542 | 548 | public static function ao_ccss_log( $msg, $lvl ) { |
543 | 549 | // Commom logging facility |
544 | 550 | // Attach debug option. |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * gets called by AO core, checks the rules and if a matching rule is found returns the associated CCSS. |
5 | 5 | */ |
6 | 6 | |
7 | -if ( ! defined( 'ABSPATH' ) ) { |
|
7 | +if (!defined('ABSPATH')) { |
|
8 | 8 | exit; |
9 | 9 | } |
10 | 10 | |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | { |
14 | 14 | // fetch all options at once and populate them individually explicitely as globals. |
15 | 15 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
16 | - foreach ( $all_options as $_option => $_value ) { |
|
16 | + foreach ($all_options as $_option => $_value) { |
|
17 | 17 | global ${$_option}; |
18 | 18 | ${$_option} = $_value; |
19 | 19 | } |
@@ -27,23 +27,23 @@ discard block |
||
27 | 27 | global $ao_ccss_key; |
28 | 28 | |
29 | 29 | // add all filters to do CCSS if key present. |
30 | - if ( $ao_css_defer && isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) ) { |
|
30 | + if ($ao_css_defer && isset($ao_ccss_key) && !empty($ao_ccss_key)) { |
|
31 | 31 | // Set AO behavior: disable minification to avoid double minifying and caching. |
32 | - add_filter( 'autoptimize_filter_css_critcss_minify', '__return_false' ); |
|
33 | - add_filter( 'autoptimize_filter_css_defer_inline', array( $this, 'ao_ccss_frontend' ), 10, 1 ); |
|
32 | + add_filter('autoptimize_filter_css_critcss_minify', '__return_false'); |
|
33 | + add_filter('autoptimize_filter_css_defer_inline', array($this, 'ao_ccss_frontend'), 10, 1); |
|
34 | 34 | |
35 | 35 | // Add the action to enqueue jobs for CriticalCSS cron. |
36 | - add_action( 'autoptimize_action_css_hash', array( 'autoptimizeCriticalCSSEnqueue', 'ao_ccss_enqueue' ), 10, 1 ); |
|
36 | + add_action('autoptimize_action_css_hash', array('autoptimizeCriticalCSSEnqueue', 'ao_ccss_enqueue'), 10, 1); |
|
37 | 37 | |
38 | 38 | // conditionally add the filter to defer jquery and others. |
39 | - if ( $ao_ccss_deferjquery ) { |
|
40 | - add_filter( 'autoptimize_html_after_minify', array( $this, 'ao_ccss_defer_jquery' ), 11, 1 ); |
|
39 | + if ($ao_ccss_deferjquery) { |
|
40 | + add_filter('autoptimize_html_after_minify', array($this, 'ao_ccss_defer_jquery'), 11, 1); |
|
41 | 41 | } |
42 | 42 | |
43 | 43 | // Order paths by length, as longest ones have greater priority in the rules. |
44 | - if ( ! empty( $ao_ccss_rules['paths'] ) ) { |
|
45 | - $keys = array_map( 'strlen', array_keys( $ao_ccss_rules['paths'] ) ); |
|
46 | - array_multisort( $keys, SORT_DESC, $ao_ccss_rules['paths'] ); |
|
44 | + if (!empty($ao_ccss_rules['paths'])) { |
|
45 | + $keys = array_map('strlen', array_keys($ao_ccss_rules['paths'])); |
|
46 | + array_multisort($keys, SORT_DESC, $ao_ccss_rules['paths']); |
|
47 | 47 | } |
48 | 48 | |
49 | 49 | // Add an array with default WordPress's conditional tags |
@@ -52,11 +52,11 @@ discard block |
||
52 | 52 | $ao_ccss_types = $this->get_ao_ccss_core_types(); |
53 | 53 | |
54 | 54 | // Extend conditional tags on plugin initalization. |
55 | - add_action( apply_filters( 'autoptimize_filter_ccss_extend_types_hook', 'init' ), array( $this, 'ao_ccss_extend_types' ) ); |
|
55 | + add_action(apply_filters('autoptimize_filter_ccss_extend_types_hook', 'init'), array($this, 'ao_ccss_extend_types')); |
|
56 | 56 | } |
57 | 57 | } |
58 | 58 | |
59 | - public function ao_ccss_frontend( $inlined ) { |
|
59 | + public function ao_ccss_frontend($inlined) { |
|
60 | 60 | // Apply CriticalCSS to frontend pages |
61 | 61 | // Attach types and settings arrays. |
62 | 62 | global $ao_ccss_types; |
@@ -68,20 +68,20 @@ discard block |
||
68 | 68 | $no_ccss = ''; |
69 | 69 | |
70 | 70 | // Only if keystatus is OK and option to add CCSS for logged on users is on or user is not logged in. |
71 | - if ( ( $ao_ccss_keyst && 2 == $ao_ccss_keyst ) && ( $ao_ccss_loggedin || ! is_user_logged_in() ) ) { |
|
71 | + if (($ao_ccss_keyst && 2 == $ao_ccss_keyst) && ($ao_ccss_loggedin || !is_user_logged_in())) { |
|
72 | 72 | // Check for a valid CriticalCSS based on path to return its contents. |
73 | - $req_path = strtok( urldecode( $_SERVER['REQUEST_URI'] ), '?' ); |
|
74 | - if ( ! empty( $ao_ccss_rules['paths'] ) ) { |
|
75 | - foreach ( $ao_ccss_rules['paths'] as $path => $rule ) { |
|
73 | + $req_path = strtok(urldecode($_SERVER['REQUEST_URI']), '?'); |
|
74 | + if (!empty($ao_ccss_rules['paths'])) { |
|
75 | + foreach ($ao_ccss_rules['paths'] as $path => $rule) { |
|
76 | 76 | // explicit match OR partial match if MANUAL rule. |
77 | - if ( $req_path == $path || ( false == $rule['hash'] && false != $rule['file'] && strpos( $req_path, str_replace( site_url(), '', $path ) ) !== false ) ) { |
|
78 | - if ( file_exists( AO_CCSS_DIR . $rule['file'] ) ) { |
|
79 | - $_ccss_contents = file_get_contents( AO_CCSS_DIR . $rule['file'] ); |
|
80 | - if ( 'none' != $_ccss_contents ) { |
|
81 | - if ( $ao_ccss_debug ) { |
|
82 | - $_ccss_contents = '/* PATH: ' . $path . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
77 | + if ($req_path == $path || (false == $rule['hash'] && false != $rule['file'] && strpos($req_path, str_replace(site_url(), '', $path)) !== false)) { |
|
78 | + if (file_exists(AO_CCSS_DIR.$rule['file'])) { |
|
79 | + $_ccss_contents = file_get_contents(AO_CCSS_DIR.$rule['file']); |
|
80 | + if ('none' != $_ccss_contents) { |
|
81 | + if ($ao_ccss_debug) { |
|
82 | + $_ccss_contents = '/* PATH: '.$path.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
83 | 83 | } |
84 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
84 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
85 | 85 | } else { |
86 | 86 | $no_ccss = 'none'; |
87 | 87 | } |
@@ -91,55 +91,55 @@ discard block |
||
91 | 91 | } |
92 | 92 | |
93 | 93 | // Check for a valid CriticalCSS based on conditional tags to return its contents. |
94 | - if ( ! empty( $ao_ccss_rules['types'] ) && 'none' !== $no_ccss ) { |
|
94 | + if (!empty($ao_ccss_rules['types']) && 'none' !== $no_ccss) { |
|
95 | 95 | // order types-rules by the order of the original $ao_ccss_types array so as not to depend on the order in which rules were added. |
96 | - $ao_ccss_rules['types'] = array_replace( array_intersect_key( array_flip( $ao_ccss_types ), $ao_ccss_rules['types'] ), $ao_ccss_rules['types'] ); |
|
96 | + $ao_ccss_rules['types'] = array_replace(array_intersect_key(array_flip($ao_ccss_types), $ao_ccss_rules['types']), $ao_ccss_rules['types']); |
|
97 | 97 | $is_front_page = is_front_page(); |
98 | 98 | |
99 | - foreach ( $ao_ccss_rules['types'] as $type => $rule ) { |
|
100 | - if ( in_array( $type, $ao_ccss_types ) && file_exists( AO_CCSS_DIR . $rule['file'] ) ) { |
|
101 | - $_ccss_contents = file_get_contents( AO_CCSS_DIR . $rule['file'] ); |
|
102 | - if ( $is_front_page && 'is_front_page' == $type ) { |
|
103 | - if ( 'none' != $_ccss_contents ) { |
|
104 | - if ( $ao_ccss_debug ) { |
|
105 | - $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
99 | + foreach ($ao_ccss_rules['types'] as $type => $rule) { |
|
100 | + if (in_array($type, $ao_ccss_types) && file_exists(AO_CCSS_DIR.$rule['file'])) { |
|
101 | + $_ccss_contents = file_get_contents(AO_CCSS_DIR.$rule['file']); |
|
102 | + if ($is_front_page && 'is_front_page' == $type) { |
|
103 | + if ('none' != $_ccss_contents) { |
|
104 | + if ($ao_ccss_debug) { |
|
105 | + $_ccss_contents = '/* TYPES: '.$type.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
106 | 106 | } |
107 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
107 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
108 | 108 | } else { |
109 | 109 | $no_ccss = 'none'; |
110 | 110 | } |
111 | - } elseif ( strpos( $type, 'custom_post_' ) === 0 && ! $is_front_page ) { |
|
112 | - if ( get_post_type( get_the_ID() ) === substr( $type, 12 ) ) { |
|
113 | - if ( 'none' != $_ccss_contents ) { |
|
114 | - if ( $ao_ccss_debug ) { |
|
115 | - $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
111 | + } elseif (strpos($type, 'custom_post_') === 0 && !$is_front_page) { |
|
112 | + if (get_post_type(get_the_ID()) === substr($type, 12)) { |
|
113 | + if ('none' != $_ccss_contents) { |
|
114 | + if ($ao_ccss_debug) { |
|
115 | + $_ccss_contents = '/* TYPES: '.$type.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
116 | 116 | } |
117 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
117 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
118 | 118 | } else { |
119 | 119 | $no_ccss = 'none'; |
120 | 120 | } |
121 | 121 | } |
122 | - } elseif ( 0 === strpos( $type, 'template_' ) && ! $is_front_page ) { |
|
123 | - if ( is_page_template( substr( $type, 9 ) ) ) { |
|
124 | - if ( 'none' != $_ccss_contents ) { |
|
125 | - if ( $ao_ccss_debug ) { |
|
126 | - $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
122 | + } elseif (0 === strpos($type, 'template_') && !$is_front_page) { |
|
123 | + if (is_page_template(substr($type, 9))) { |
|
124 | + if ('none' != $_ccss_contents) { |
|
125 | + if ($ao_ccss_debug) { |
|
126 | + $_ccss_contents = '/* TYPES: '.$type.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
127 | 127 | } |
128 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
128 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
129 | 129 | } else { |
130 | 130 | $no_ccss = 'none'; |
131 | 131 | } |
132 | 132 | } |
133 | - } elseif ( ! $is_front_page ) { |
|
133 | + } elseif (!$is_front_page) { |
|
134 | 134 | // all "normal" conditional tags, core + woo + buddypress + edd + bbpress |
135 | 135 | // but we have to remove the prefix for the non-core ones for them to function. |
136 | - $type = str_replace( array( 'woo_', 'bp_', 'bbp_', 'edd_' ), '', $type ); |
|
137 | - if ( function_exists( $type ) && call_user_func( $type ) ) { |
|
138 | - if ( 'none' != $_ccss_contents ) { |
|
139 | - if ( $ao_ccss_debug ) { |
|
140 | - $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents; |
|
136 | + $type = str_replace(array('woo_', 'bp_', 'bbp_', 'edd_'), '', $type); |
|
137 | + if (function_exists($type) && call_user_func($type)) { |
|
138 | + if ('none' != $_ccss_contents) { |
|
139 | + if ($ao_ccss_debug) { |
|
140 | + $_ccss_contents = '/* TYPES: '.$type.' hash: '.$rule['hash'].' file: '.$rule['file'].' */ '.$_ccss_contents; |
|
141 | 141 | } |
142 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional ); |
|
142 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $_ccss_contents.$ao_ccss_additional); |
|
143 | 143 | } else { |
144 | 144 | $no_ccss = 'none'; |
145 | 145 | } |
@@ -152,26 +152,26 @@ discard block |
||
152 | 152 | |
153 | 153 | // Finally, inline the default CriticalCSS if any or else the entire CSS for the page |
154 | 154 | // This also applies to logged in users if the option to add CCSS for logged in users has been disabled. |
155 | - if ( ! empty( $inlined ) && 'none' !== $no_ccss ) { |
|
156 | - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $inlined . $ao_ccss_additional ); |
|
155 | + if (!empty($inlined) && 'none' !== $no_ccss) { |
|
156 | + return apply_filters('autoptimize_filter_ccss_core_ccss', $inlined.$ao_ccss_additional); |
|
157 | 157 | } else { |
158 | - add_filter( 'autoptimize_filter_css_inline', '__return_true' ); |
|
158 | + add_filter('autoptimize_filter_css_inline', '__return_true'); |
|
159 | 159 | return; |
160 | 160 | } |
161 | 161 | } |
162 | 162 | |
163 | - public function ao_ccss_defer_jquery( $in ) { |
|
163 | + public function ao_ccss_defer_jquery($in) { |
|
164 | 164 | // try to defer all JS (main goal being jquery.js as AO by default does not aggregate that). |
165 | - if ( ( ! is_user_logged_in() || $ao_ccss_loggedin ) && preg_match_all( '#<script.*>(.*)</script>#Usmi', $in, $matches, PREG_SET_ORDER ) ) { |
|
166 | - foreach ( $matches as $match ) { |
|
167 | - if ( ( ! preg_match( '/<script.* type\s?=.*>/', $match[0] ) || preg_match( '/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $match[0] ) ) && '' !== $match[1] && ( false !== strpos( $match[1], 'jQuery' ) || false !== strpos( $match[1], '$' ) ) ) { |
|
165 | + if ((!is_user_logged_in() || $ao_ccss_loggedin) && preg_match_all('#<script.*>(.*)</script>#Usmi', $in, $matches, PREG_SET_ORDER)) { |
|
166 | + foreach ($matches as $match) { |
|
167 | + if ((!preg_match('/<script.* type\s?=.*>/', $match[0]) || preg_match('/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $match[0])) && '' !== $match[1] && (false !== strpos($match[1], 'jQuery') || false !== strpos($match[1], '$'))) { |
|
168 | 168 | // inline js that requires jquery, wrap deferring JS around it to defer it. |
169 | - $new_match = 'var aoDeferInlineJQuery=function(){' . $match[1] . '}; if (document.readyState === "loading") {document.addEventListener("DOMContentLoaded", aoDeferInlineJQuery);} else {aoDeferInlineJQuery();}'; |
|
170 | - $in = str_replace( $match[1], $new_match, $in ); |
|
171 | - } elseif ( '' === $match[1] && false !== strpos( $match[0], 'src=' ) && false === strpos( $match[0], 'defer' ) ) { |
|
169 | + $new_match = 'var aoDeferInlineJQuery=function(){'.$match[1].'}; if (document.readyState === "loading") {document.addEventListener("DOMContentLoaded", aoDeferInlineJQuery);} else {aoDeferInlineJQuery();}'; |
|
170 | + $in = str_replace($match[1], $new_match, $in); |
|
171 | + } elseif ('' === $match[1] && false !== strpos($match[0], 'src=') && false === strpos($match[0], 'defer')) { |
|
172 | 172 | // linked non-aggregated JS, defer it. |
173 | - $new_match = str_replace( '<script ', '<script defer ', $match[0] ); |
|
174 | - $in = str_replace( $match[0], $new_match, $in ); |
|
173 | + $new_match = str_replace('<script ', '<script defer ', $match[0]); |
|
174 | + $in = str_replace($match[0], $new_match, $in); |
|
175 | 175 | } |
176 | 176 | } |
177 | 177 | } |
@@ -184,9 +184,9 @@ discard block |
||
184 | 184 | global $ao_ccss_types; |
185 | 185 | |
186 | 186 | // in some cases $ao_ccss_types is empty and/or not an array, this should work around that problem. |
187 | - if ( empty( $ao_ccss_types ) || ! is_array( $ao_ccss_types ) ) { |
|
187 | + if (empty($ao_ccss_types) || !is_array($ao_ccss_types)) { |
|
188 | 188 | $ao_ccss_types = get_ao_ccss_core_types(); |
189 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Empty types array in extend, refetching array with core conditionals.', 3 ); |
|
189 | + autoptimizeCriticalCSSCore::ao_ccss_log('Empty types array in extend, refetching array with core conditionals.', 3); |
|
190 | 190 | } |
191 | 191 | |
192 | 192 | // Custom Post Types. |
@@ -198,18 +198,18 @@ discard block |
||
198 | 198 | 'names', |
199 | 199 | 'and' |
200 | 200 | ); |
201 | - foreach ( $cpts as $cpt ) { |
|
202 | - array_unshift( $ao_ccss_types, 'custom_post_' . $cpt ); |
|
201 | + foreach ($cpts as $cpt) { |
|
202 | + array_unshift($ao_ccss_types, 'custom_post_'.$cpt); |
|
203 | 203 | } |
204 | 204 | |
205 | 205 | // Templates. |
206 | 206 | $templates = wp_get_theme()->get_page_templates(); |
207 | - foreach ( $templates as $tplfile => $tplname ) { |
|
208 | - array_unshift( $ao_ccss_types, 'template_' . $tplfile ); |
|
207 | + foreach ($templates as $tplfile => $tplname) { |
|
208 | + array_unshift($ao_ccss_types, 'template_'.$tplfile); |
|
209 | 209 | } |
210 | 210 | |
211 | 211 | // bbPress tags. |
212 | - if ( function_exists( 'is_bbpress' ) ) { |
|
212 | + if (function_exists('is_bbpress')) { |
|
213 | 213 | $ao_ccss_types = array_merge( |
214 | 214 | array( |
215 | 215 | 'bbp_is_bbpress', |
@@ -241,7 +241,7 @@ discard block |
||
241 | 241 | } |
242 | 242 | |
243 | 243 | // BuddyPress tags. |
244 | - if ( function_exists( 'is_buddypress' ) ) { |
|
244 | + if (function_exists('is_buddypress')) { |
|
245 | 245 | $ao_ccss_types = array_merge( |
246 | 246 | array( |
247 | 247 | 'bp_is_activation_page', |
@@ -283,7 +283,7 @@ discard block |
||
283 | 283 | } |
284 | 284 | |
285 | 285 | // Easy Digital Downloads (EDD) tags. |
286 | - if ( function_exists( 'edd_is_checkout' ) ) { |
|
286 | + if (function_exists('edd_is_checkout')) { |
|
287 | 287 | $ao_ccss_types = array_merge( |
288 | 288 | array( |
289 | 289 | 'edd_is_checkout', |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | } |
296 | 296 | |
297 | 297 | // WooCommerce tags. |
298 | - if ( class_exists( 'WooCommerce' ) ) { |
|
298 | + if (class_exists('WooCommerce')) { |
|
299 | 299 | $ao_ccss_types = array_merge( |
300 | 300 | array( |
301 | 301 | 'woo_is_account_page', |
@@ -314,7 +314,7 @@ discard block |
||
314 | 314 | |
315 | 315 | public function get_ao_ccss_core_types() { |
316 | 316 | global $ao_ccss_types; |
317 | - if ( empty( $ao_ccss_types ) || ! is_array( $ao_ccss_types ) ) { |
|
317 | + if (empty($ao_ccss_types) || !is_array($ao_ccss_types)) { |
|
318 | 318 | return array( |
319 | 319 | 'is_404', |
320 | 320 | 'is_archive', |
@@ -335,7 +335,7 @@ discard block |
||
335 | 335 | } |
336 | 336 | } |
337 | 337 | |
338 | - public static function ao_ccss_key_status( $render ) { |
|
338 | + public static function ao_ccss_key_status($render) { |
|
339 | 339 | // Provide key status |
340 | 340 | // Get key and key status. |
341 | 341 | global $ao_ccss_key; |
@@ -348,55 +348,55 @@ discard block |
||
348 | 348 | $key_return = array(); |
349 | 349 | $status = false; |
350 | 350 | |
351 | - if ( $key && 2 == $key_status ) { |
|
351 | + if ($key && 2 == $key_status) { |
|
352 | 352 | // Key exists and its status is valid. |
353 | 353 | // Set valid key status. |
354 | 354 | $status = 'valid'; |
355 | - $status_msg = __( 'Valid' ); |
|
355 | + $status_msg = __('Valid'); |
|
356 | 356 | $color = '#46b450'; // Green. |
357 | 357 | $message = null; |
358 | - } elseif ( $key && 1 == $key_status ) { |
|
358 | + } elseif ($key && 1 == $key_status) { |
|
359 | 359 | // Key exists but its validation has failed. |
360 | 360 | // Set invalid key status. |
361 | 361 | $status = 'invalid'; |
362 | - $status_msg = __( 'Invalid' ); |
|
362 | + $status_msg = __('Invalid'); |
|
363 | 363 | $color = '#dc3232'; // Red. |
364 | - $message = __( 'Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize' ); |
|
365 | - } elseif ( $key && ! $key_status ) { |
|
364 | + $message = __('Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize'); |
|
365 | + } elseif ($key && !$key_status) { |
|
366 | 366 | // Key exists but it has no valid status yet |
367 | 367 | // Perform key validation. |
368 | - $key_check = $self->ao_ccss_key_validation( $key ); |
|
368 | + $key_check = $self->ao_ccss_key_validation($key); |
|
369 | 369 | |
370 | 370 | // Key is valid, set valid status. |
371 | - if ( $key_check ) { |
|
371 | + if ($key_check) { |
|
372 | 372 | $status = 'valid'; |
373 | - $status_msg = __( 'Valid' ); |
|
373 | + $status_msg = __('Valid'); |
|
374 | 374 | $color = '#46b450'; // Green. |
375 | 375 | $message = null; |
376 | 376 | } else { |
377 | 377 | // Key is invalid, set invalid status. |
378 | 378 | $status = 'invalid'; |
379 | - $status_msg = __( 'Invalid' ); |
|
379 | + $status_msg = __('Invalid'); |
|
380 | 380 | $color = '#dc3232'; // Red. |
381 | - if ( get_option( 'autoptimize_ccss_keyst' ) == 1 ) { |
|
382 | - $message = __( 'Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize' ); |
|
381 | + if (get_option('autoptimize_ccss_keyst') == 1) { |
|
382 | + $message = __('Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize'); |
|
383 | 383 | } else { |
384 | - $message = __( 'Something went wrong when checking your API key, make sure you server can communicate with https://criticalcss.com and/ or try again later.', 'autoptimize' ); |
|
384 | + $message = __('Something went wrong when checking your API key, make sure you server can communicate with https://criticalcss.com and/ or try again later.', 'autoptimize'); |
|
385 | 385 | } |
386 | 386 | } |
387 | 387 | } else { |
388 | 388 | // No key nor status |
389 | 389 | // Set no key status. |
390 | 390 | $status = 'nokey'; |
391 | - $status_msg = __( 'None' ); |
|
391 | + $status_msg = __('None'); |
|
392 | 392 | $color = '#ffb900'; // Yellow. |
393 | - $message = __( 'Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> API key to start.', 'autoptimize' ); |
|
393 | + $message = __('Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> API key to start.', 'autoptimize'); |
|
394 | 394 | } |
395 | 395 | |
396 | 396 | // Fill returned values. |
397 | 397 | $key_return['status'] = $status; |
398 | 398 | // Provide rendering information if required. |
399 | - if ( $render ) { |
|
399 | + if ($render) { |
|
400 | 400 | $key_return['stmsg'] = $status_msg; |
401 | 401 | $key_return['color'] = $color; |
402 | 402 | $key_return['msg'] = $message; |
@@ -406,19 +406,19 @@ discard block |
||
406 | 406 | return $key_return; |
407 | 407 | } |
408 | 408 | |
409 | - public function ao_ccss_key_validation( $key ) { |
|
409 | + public function ao_ccss_key_validation($key) { |
|
410 | 410 | // POST a dummy job to criticalcss.com to check for key validation |
411 | 411 | // Prepare home URL for the request. |
412 | 412 | $src_url = get_home_url(); |
413 | - $src_url = apply_filters( 'autoptimize_filter_ccss_cron_srcurl', $src_url ); |
|
413 | + $src_url = apply_filters('autoptimize_filter_ccss_cron_srcurl', $src_url); |
|
414 | 414 | |
415 | 415 | // Prepare the request. |
416 | - $url = esc_url_raw( AO_CCSS_API . 'generate' ); |
|
416 | + $url = esc_url_raw(AO_CCSS_API.'generate'); |
|
417 | 417 | $args = array( |
418 | 418 | 'headers' => array( |
419 | - 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v' . AO_CCSS_VER, |
|
419 | + 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v'.AO_CCSS_VER, |
|
420 | 420 | 'Content-type' => 'application/json; charset=utf-8', |
421 | - 'Authorization' => 'JWT ' . $key, |
|
421 | + 'Authorization' => 'JWT '.$key, |
|
422 | 422 | 'Connection' => 'close', |
423 | 423 | ), |
424 | 424 | // Body must be JSON. |
@@ -432,23 +432,23 @@ discard block |
||
432 | 432 | ); |
433 | 433 | |
434 | 434 | // Dispatch the request and store its response code. |
435 | - $req = wp_safe_remote_post( $url, $args ); |
|
436 | - $code = wp_remote_retrieve_response_code( $req ); |
|
437 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
435 | + $req = wp_safe_remote_post($url, $args); |
|
436 | + $code = wp_remote_retrieve_response_code($req); |
|
437 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
438 | 438 | |
439 | - if ( 200 == $code ) { |
|
439 | + if (200 == $code) { |
|
440 | 440 | // Response is OK. |
441 | 441 | // Set key status as valid and log key check. |
442 | - update_option( 'autoptimize_ccss_keyst', 2 ); |
|
443 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is valid, updating key status', 3 ); |
|
442 | + update_option('autoptimize_ccss_keyst', 2); |
|
443 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is valid, updating key status', 3); |
|
444 | 444 | |
445 | 445 | // extract job-id from $body and put it in the queue as a P job |
446 | 446 | // but only if no jobs and no rules! |
447 | 447 | global $ao_ccss_queue; |
448 | 448 | global $ao_ccss_rules; |
449 | 449 | |
450 | - if ( 0 == count( $ao_ccss_queue ) && 0 == count( $ao_ccss_rules['types'] ) && 0 == count( $ao_ccss_rules['paths'] ) ) { |
|
451 | - if ( 'JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] ) { |
|
450 | + if (0 == count($ao_ccss_queue) && 0 == count($ao_ccss_rules['types']) && 0 == count($ao_ccss_rules['paths'])) { |
|
451 | + if ('JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status']) { |
|
452 | 452 | $jprops['ljid'] = 'firstrun'; |
453 | 453 | $jprops['rtarget'] = 'types|is_front_page'; |
454 | 454 | $jprops['ptype'] = 'is_front_page'; |
@@ -459,30 +459,30 @@ discard block |
||
459 | 459 | $jprops['jqstat'] = $body['job']['status']; |
460 | 460 | $jprops['jrstat'] = null; |
461 | 461 | $jprops['jvstat'] = null; |
462 | - $jprops['jctime'] = microtime( true ); |
|
462 | + $jprops['jctime'] = microtime(true); |
|
463 | 463 | $jprops['jftime'] = null; |
464 | 464 | $ao_ccss_queue['/'] = $jprops; |
465 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
466 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
467 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Created P job for is_front_page based on API key check response.', 3 ); |
|
465 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
466 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
467 | + autoptimizeCriticalCSSCore::ao_ccss_log('Created P job for is_front_page based on API key check response.', 3); |
|
468 | 468 | } |
469 | 469 | } |
470 | 470 | return true; |
471 | - } elseif ( 401 == $code ) { |
|
471 | + } elseif (401 == $code) { |
|
472 | 472 | // Response is unauthorized |
473 | 473 | // Set key status as invalid and log key check. |
474 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
475 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
474 | + update_option('autoptimize_ccss_keyst', 1); |
|
475 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
476 | 476 | return false; |
477 | 477 | } else { |
478 | 478 | // Response unkown |
479 | 479 | // Log key check attempt. |
480 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: could not check API key status, this is a service error, body follows if any...', 2 ); |
|
481 | - if ( ! empty( $body ) ) { |
|
482 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
480 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: could not check API key status, this is a service error, body follows if any...', 2); |
|
481 | + if (!empty($body)) { |
|
482 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
483 | 483 | } |
484 | - if ( is_wp_error( $req ) ) { |
|
485 | - autoptimizeCriticalCSSCore::ao_ccss_log( $req->get_error_message(), 2 ); |
|
484 | + if (is_wp_error($req)) { |
|
485 | + autoptimizeCriticalCSSCore::ao_ccss_log($req->get_error_message(), 2); |
|
486 | 486 | } |
487 | 487 | return false; |
488 | 488 | } |
@@ -497,14 +497,14 @@ discard block |
||
497 | 497 | $viewport = array(); |
498 | 498 | |
499 | 499 | // Viewport Width. |
500 | - if ( ! empty( $ao_ccss_viewport['w'] ) ) { |
|
500 | + if (!empty($ao_ccss_viewport['w'])) { |
|
501 | 501 | $viewport['w'] = $ao_ccss_viewport['w']; |
502 | 502 | } else { |
503 | 503 | $viewport['w'] = ''; |
504 | 504 | } |
505 | 505 | |
506 | 506 | // Viewport Height. |
507 | - if ( ! empty( $ao_ccss_viewport['h'] ) ) { |
|
507 | + if (!empty($ao_ccss_viewport['h'])) { |
|
508 | 508 | $viewport['h'] = $ao_ccss_viewport['h']; |
509 | 509 | } else { |
510 | 510 | $viewport['h'] = ''; |
@@ -513,23 +513,23 @@ discard block |
||
513 | 513 | return $viewport; |
514 | 514 | } |
515 | 515 | |
516 | - public static function ao_ccss_check_contents( $ccss ) { |
|
516 | + public static function ao_ccss_check_contents($ccss) { |
|
517 | 517 | // Perform basic exploit avoidance and CSS validation. |
518 | - if ( ! empty( $ccss ) ) { |
|
518 | + if (!empty($ccss)) { |
|
519 | 519 | // Try to avoid code injection. |
520 | - $blacklist = array( '#!/', 'function(', '<script', '<?php' ); |
|
521 | - foreach ( $blacklist as $blacklisted ) { |
|
522 | - if ( strpos( $ccss, $blacklisted ) !== false ) { |
|
523 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received contained blacklisted content.', 2 ); |
|
520 | + $blacklist = array('#!/', 'function(', '<script', '<?php'); |
|
521 | + foreach ($blacklist as $blacklisted) { |
|
522 | + if (strpos($ccss, $blacklisted) !== false) { |
|
523 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS received contained blacklisted content.', 2); |
|
524 | 524 | return false; |
525 | 525 | } |
526 | 526 | } |
527 | 527 | |
528 | 528 | // Check for most basics CSS structures. |
529 | - $pinklist = array( '{', '}', ':' ); |
|
530 | - foreach ( $pinklist as $needed ) { |
|
531 | - if ( false === strpos( $ccss, $needed ) && 'none' !== $ccss ) { |
|
532 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received did not seem to contain real CSS.', 2 ); |
|
529 | + $pinklist = array('{', '}', ':'); |
|
530 | + foreach ($pinklist as $needed) { |
|
531 | + if (false === strpos($ccss, $needed) && 'none' !== $ccss) { |
|
532 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS received did not seem to contain real CSS.', 2); |
|
533 | 533 | return false; |
534 | 534 | } |
535 | 535 | } |
@@ -539,7 +539,7 @@ discard block |
||
539 | 539 | return true; |
540 | 540 | } |
541 | 541 | |
542 | - public static function ao_ccss_log( $msg, $lvl ) { |
|
542 | + public static function ao_ccss_log($msg, $lvl) { |
|
543 | 543 | // Commom logging facility |
544 | 544 | // Attach debug option. |
545 | 545 | global $ao_ccss_debug; |
@@ -550,7 +550,7 @@ discard block |
||
550 | 550 | // 3: DD (for debug) |
551 | 551 | // Default: UU (for unkown). |
552 | 552 | $level = false; |
553 | - switch ( $lvl ) { |
|
553 | + switch ($lvl) { |
|
554 | 554 | case 1: |
555 | 555 | $level = 'II'; |
556 | 556 | break; |
@@ -559,7 +559,7 @@ discard block |
||
559 | 559 | break; |
560 | 560 | case 3: |
561 | 561 | // Output debug messages only if debug mode is enabled. |
562 | - if ( $ao_ccss_debug ) { |
|
562 | + if ($ao_ccss_debug) { |
|
563 | 563 | $level = 'DD'; |
564 | 564 | } |
565 | 565 | break; |
@@ -568,13 +568,13 @@ discard block |
||
568 | 568 | } |
569 | 569 | |
570 | 570 | // Prepare and write a log message if there's a valid level. |
571 | - if ( $level ) { |
|
571 | + if ($level) { |
|
572 | 572 | |
573 | 573 | // Prepare message. |
574 | - $message = date( 'c' ) . ' - [' . $level . '] ' . htmlentities( $msg ) . '<br>'; |
|
574 | + $message = date('c').' - ['.$level.'] '.htmlentities($msg).'<br>'; |
|
575 | 575 | |
576 | 576 | // Write message to log file. |
577 | - error_log( $message, 3, AO_CCSS_LOG ); |
|
577 | + error_log($message, 3, AO_CCSS_LOG); |
|
578 | 578 | } |
579 | 579 | } |
580 | 580 | } |
@@ -423,6 +423,9 @@ discard block |
||
423 | 423 | } |
424 | 424 | } |
425 | 425 | |
426 | + /** |
|
427 | + * @param boolean|string $debug |
|
428 | + */ |
|
426 | 429 | public function ao_ccss_api_generate( $path, $debug, $dcode ) { |
427 | 430 | // POST jobs to criticalcss.com and return responses |
428 | 431 | // Get key and key status. |
@@ -559,6 +562,9 @@ discard block |
||
559 | 562 | } |
560 | 563 | } |
561 | 564 | |
565 | + /** |
|
566 | + * @param boolean $debug |
|
567 | + */ |
|
562 | 568 | public function ao_ccss_api_results( $jobid, $debug, $dcode ) { |
563 | 569 | // GET jobs from criticalcss.com and return responses |
564 | 570 | // Get key. |
@@ -625,6 +631,9 @@ discard block |
||
625 | 631 | } |
626 | 632 | } |
627 | 633 | |
634 | + /** |
|
635 | + * @param boolean $review |
|
636 | + */ |
|
628 | 637 | public function ao_ccss_save_file( $ccss, $target, $review ) { |
629 | 638 | // Save critical CSS into the filesystem and return its filename |
630 | 639 | // Prepare review mark. |
@@ -4,7 +4,7 @@ discard block |
||
4 | 4 | * processes the queue, submitting jobs to criticalcss.com and retrieving generated CSS from criticalcss.com and saving rules. |
5 | 5 | */ |
6 | 6 | |
7 | -if ( ! defined( 'ABSPATH' ) ) { |
|
7 | +if (!defined('ABSPATH')) { |
|
8 | 8 | exit; |
9 | 9 | } |
10 | 10 | |
@@ -13,23 +13,23 @@ discard block |
||
13 | 13 | { |
14 | 14 | // fetch all options at once and populate them individually explicitely as globals. |
15 | 15 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
16 | - foreach ( $all_options as $_option => $_value ) { |
|
16 | + foreach ($all_options as $_option => $_value) { |
|
17 | 17 | global ${$_option}; |
18 | 18 | ${$_option} = $_value; |
19 | 19 | } |
20 | 20 | |
21 | 21 | // Add queue control to a registered event. |
22 | - add_action( 'ao_ccss_queue', array( $this, 'ao_ccss_queue_control' ) ); |
|
22 | + add_action('ao_ccss_queue', array($this, 'ao_ccss_queue_control')); |
|
23 | 23 | // Add cleaning job to a registered event. |
24 | - add_action( 'ao_ccss_maintenance', array( $this, 'ao_ccss_cleaning' ) ); |
|
24 | + add_action('ao_ccss_maintenance', array($this, 'ao_ccss_cleaning')); |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | public function ao_ccss_queue_control() { |
28 | 28 | // The queue execution backend. |
29 | 29 | global $ao_ccss_key; |
30 | - if ( ! isset( $ao_ccss_key ) || empty( $ao_ccss_key ) ) { |
|
30 | + if (!isset($ao_ccss_key) || empty($ao_ccss_key)) { |
|
31 | 31 | // no key set, not processing the queue! |
32 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'No key set, so not processing queue.', 3 ); |
|
32 | + autoptimizeCriticalCSSCore::ao_ccss_log('No key set, so not processing queue.', 3); |
|
33 | 33 | return; |
34 | 34 | } |
35 | 35 | |
@@ -50,35 +50,35 @@ discard block |
||
50 | 50 | * When properly set, queue will always finish a job with the declared settings above regardless of the real API responses. |
51 | 51 | */ |
52 | 52 | $queue_debug = false; |
53 | - if ( file_exists( AO_CCSS_DEBUG ) ) { |
|
54 | - $qdobj_raw = file_get_contents( AO_CCSS_DEBUG ); |
|
55 | - $qdobj = json_decode( $qdobj_raw, true ); |
|
56 | - if ( $qdobj ) { |
|
57 | - if ( 1 === $qdobj['enable'] ) { |
|
53 | + if (file_exists(AO_CCSS_DEBUG)) { |
|
54 | + $qdobj_raw = file_get_contents(AO_CCSS_DEBUG); |
|
55 | + $qdobj = json_decode($qdobj_raw, true); |
|
56 | + if ($qdobj) { |
|
57 | + if (1 === $qdobj['enable']) { |
|
58 | 58 | $queue_debug = true; |
59 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue operating in debug mode with the following settings: <' . $qdobj_raw . '>', 3 ); |
|
59 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue operating in debug mode with the following settings: <'.$qdobj_raw.'>', 3); |
|
60 | 60 | } |
61 | 61 | } |
62 | 62 | } |
63 | 63 | |
64 | 64 | // Set some default values for $qdobj to avoid function call warnings. |
65 | - if ( ! $queue_debug ) { |
|
65 | + if (!$queue_debug) { |
|
66 | 66 | $qdobj['htcode'] = false; |
67 | 67 | } |
68 | 68 | |
69 | 69 | // Check if queue is already running. |
70 | 70 | $queue_lock = false; |
71 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
71 | + if (file_exists(AO_CCSS_LOCK)) { |
|
72 | 72 | $queue_lock = true; |
73 | 73 | } |
74 | 74 | |
75 | 75 | // Proceed with the queue if it's not already running. |
76 | - if ( ! $queue_lock ) { |
|
76 | + if (!$queue_lock) { |
|
77 | 77 | |
78 | 78 | // Log queue start and create the lock file. |
79 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control started', 3 ); |
|
80 | - if ( touch( AO_CCSS_LOCK ) ) { |
|
81 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control locked', 3 ); |
|
79 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control started', 3); |
|
80 | + if (touch(AO_CCSS_LOCK)) { |
|
81 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control locked', 3); |
|
82 | 82 | } |
83 | 83 | |
84 | 84 | // Attach required variables. |
@@ -88,218 +88,218 @@ discard block |
||
88 | 88 | // Initialize job counters. |
89 | 89 | $jc = 1; |
90 | 90 | $jr = 1; |
91 | - $jt = count( $ao_ccss_queue ); |
|
91 | + $jt = count($ao_ccss_queue); |
|
92 | 92 | |
93 | 93 | // Sort queue by ascending job status (e.g. ERROR, JOB_ONGOING, JOB_QUEUED, NEW...). |
94 | - array_multisort( array_column( $ao_ccss_queue, 'jqstat' ), $ao_ccss_queue ); // @codingStandardsIgnoreLine |
|
94 | + array_multisort(array_column($ao_ccss_queue, 'jqstat'), $ao_ccss_queue); // @codingStandardsIgnoreLine |
|
95 | 95 | |
96 | 96 | // Iterates over the entire queue. |
97 | - foreach ( $ao_ccss_queue as $path => $jprops ) { |
|
97 | + foreach ($ao_ccss_queue as $path => $jprops) { |
|
98 | 98 | // Prepare flags and target rule. |
99 | 99 | $update = false; |
100 | 100 | $deljob = false; |
101 | 101 | $rule_update = false; |
102 | 102 | $oldccssfile = false; |
103 | - $trule = explode( '|', $jprops['rtarget'] ); |
|
103 | + $trule = explode('|', $jprops['rtarget']); |
|
104 | 104 | |
105 | 105 | // Log job count. |
106 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Processing job ' . $jc . ' of ' . $jt . ' with id <' . $jprops['ljid'] . '> and status <' . $jprops['jqstat'] . '>', 3 ); |
|
106 | + autoptimizeCriticalCSSCore::ao_ccss_log('Processing job '.$jc.' of '.$jt.' with id <'.$jprops['ljid'].'> and status <'.$jprops['jqstat'].'>', 3); |
|
107 | 107 | |
108 | 108 | // Process NEW jobs. |
109 | - if ( 'NEW' == $jprops['jqstat'] ) { |
|
109 | + if ('NEW' == $jprops['jqstat']) { |
|
110 | 110 | |
111 | 111 | // Log the new job. |
112 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Found NEW job with local ID <' . $jprops['ljid'] . '>, starting its queue processing', 3 ); |
|
112 | + autoptimizeCriticalCSSCore::ao_ccss_log('Found NEW job with local ID <'.$jprops['ljid'].'>, starting its queue processing', 3); |
|
113 | 113 | |
114 | 114 | // Compare job and rule hashes (if any). |
115 | - $hash = $this->ao_ccss_diff_hashes( $jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget'] ); |
|
115 | + $hash = $this->ao_ccss_diff_hashes($jprops['ljid'], $jprops['hash'], $jprops['hashes'], $jprops['rtarget']); |
|
116 | 116 | |
117 | 117 | // If job hash is new or different of a previous one. |
118 | - if ( $hash ) { |
|
118 | + if ($hash) { |
|
119 | 119 | // Set job hash. |
120 | 120 | $jprops['hash'] = $hash; |
121 | 121 | |
122 | 122 | // If this is not the first job, wait 15 seconds before process next job due criticalcss.com API limits. |
123 | - if ( $jr > 1 ) { |
|
124 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Waiting 15 seconds due to criticalcss.com API limits', 3 ); |
|
125 | - sleep( 15 ); |
|
123 | + if ($jr > 1) { |
|
124 | + autoptimizeCriticalCSSCore::ao_ccss_log('Waiting 15 seconds due to criticalcss.com API limits', 3); |
|
125 | + sleep(15); |
|
126 | 126 | } |
127 | 127 | |
128 | 128 | // Dispatch the job generate request and increment request count. |
129 | - $apireq = $this->ao_ccss_api_generate( $path, $queue_debug, $qdobj['htcode'] ); |
|
129 | + $apireq = $this->ao_ccss_api_generate($path, $queue_debug, $qdobj['htcode']); |
|
130 | 130 | $jr++; |
131 | 131 | |
132 | 132 | // NOTE: All the following conditions maps to the ones in admin_settings_queue.js.php. |
133 | - if ( 'JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status'] ) { |
|
133 | + if ('JOB_QUEUED' == $apireq['job']['status'] || 'JOB_ONGOING' == $apireq['job']['status']) { |
|
134 | 134 | // SUCCESS: request has a valid result. |
135 | 135 | // Update job properties. |
136 | 136 | $jprops['jid'] = $apireq['job']['id']; |
137 | 137 | $jprops['jqstat'] = $apireq['job']['status']; |
138 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> generate request successful, remote id <' . $jprops['jid'] . '>, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
139 | - } elseif ( 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
|
138 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> generate request successful, remote id <'.$jprops['jid'].'>, status now is <'.$jprops['jqstat'].'>', 3); |
|
139 | + } elseif ('STATUS_JOB_BAD' == $apireq['job']['status']) { |
|
140 | 140 | // ERROR: concurrent requests |
141 | 141 | // Update job properties. |
142 | 142 | $jprops['jid'] = $apireq['job']['id']; |
143 | 143 | $jprops['jqstat'] = $apireq['job']['status']; |
144 | 144 | $jprops['jrstat'] = $apireq['error']; |
145 | 145 | $jprops['jvstat'] = 'NONE'; |
146 | - $jprops['jftime'] = microtime( true ); |
|
147 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Concurrent requests when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 ); |
|
148 | - } elseif ( 'INVALID_JWT_TOKEN' == $apireq['errorCode'] ) { |
|
146 | + $jprops['jftime'] = microtime(true); |
|
147 | + autoptimizeCriticalCSSCore::ao_ccss_log('Concurrent requests when processing job id <'.$jprops['ljid'].'>, job status is now <'.$jprops['jqstat'].'>', 3); |
|
148 | + } elseif ('INVALID_JWT_TOKEN' == $apireq['errorCode']) { |
|
149 | 149 | // ERROR: key validation |
150 | 150 | // Update job properties. |
151 | 151 | $jprops['jqstat'] = $apireq['errorCode']; |
152 | 152 | $jprops['jrstat'] = $apireq['error']; |
153 | 153 | $jprops['jvstat'] = 'NONE'; |
154 | - $jprops['jftime'] = microtime( true ); |
|
155 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'API key validation error when processing job id <' . $jprops['ljid'] . '>, job status is now <' . $jprops['jqstat'] . '>', 3 ); |
|
156 | - } elseif ( empty( $apireq ) ) { |
|
154 | + $jprops['jftime'] = microtime(true); |
|
155 | + autoptimizeCriticalCSSCore::ao_ccss_log('API key validation error when processing job id <'.$jprops['ljid'].'>, job status is now <'.$jprops['jqstat'].'>', 3); |
|
156 | + } elseif (empty($apireq)) { |
|
157 | 157 | // ERROR: no response |
158 | 158 | // Update job properties. |
159 | 159 | $jprops['jqstat'] = 'NO_RESPONSE'; |
160 | 160 | $jprops['jrstat'] = 'NONE'; |
161 | 161 | $jprops['jvstat'] = 'NONE'; |
162 | - $jprops['jftime'] = microtime( true ); |
|
163 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
162 | + $jprops['jftime'] = microtime(true); |
|
163 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> request has no response, status now is <'.$jprops['jqstat'].'>', 3); |
|
164 | 164 | } else { |
165 | 165 | // UNKNOWN: unhandled generate exception |
166 | 166 | // Update job properties. |
167 | 167 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
168 | 168 | $jprops['jrstat'] = 'NONE'; |
169 | 169 | $jprops['jvstat'] = 'NONE'; |
170 | - $jprops['jftime'] = microtime( true ); |
|
171 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> generate request has an UNKNOWN condition, status now is <' . $jprops['jqstat'] . '>, check log messages above for more information', 2 ); |
|
172 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
170 | + $jprops['jftime'] = microtime(true); |
|
171 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> generate request has an UNKNOWN condition, status now is <'.$jprops['jqstat'].'>, check log messages above for more information', 2); |
|
172 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
173 | 173 | } |
174 | 174 | } else { |
175 | 175 | // SUCCESS: Job hash is equal to a previous one, so it's done |
176 | 176 | // Update job status and finish time. |
177 | 177 | $jprops['jqstat'] = 'JOB_DONE'; |
178 | - $jprops['jftime'] = microtime( true ); |
|
179 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> requires no further processing, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
178 | + $jprops['jftime'] = microtime(true); |
|
179 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> requires no further processing, status now is <'.$jprops['jqstat'].'>', 3); |
|
180 | 180 | } |
181 | 181 | |
182 | 182 | // Set queue update flag. |
183 | 183 | $update = true; |
184 | 184 | |
185 | - } elseif ( 'JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat'] ) { |
|
185 | + } elseif ('JOB_QUEUED' == $jprops['jqstat'] || 'JOB_ONGOING' == $jprops['jqstat']) { |
|
186 | 186 | // Process QUEUED and ONGOING jobs |
187 | 187 | // Log the pending job. |
188 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Found PENDING job with local ID <' . $jprops['ljid'] . '>, continuing its queue processing', 3 ); |
|
188 | + autoptimizeCriticalCSSCore::ao_ccss_log('Found PENDING job with local ID <'.$jprops['ljid'].'>, continuing its queue processing', 3); |
|
189 | 189 | |
190 | 190 | // If this is not the first job, wait 15 seconds before process next job due criticalcss.com API limits. |
191 | - if ( $jr > 1 ) { |
|
192 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Waiting 15 seconds due to criticalcss.com API limits', 3 ); |
|
193 | - sleep( 15 ); |
|
191 | + if ($jr > 1) { |
|
192 | + autoptimizeCriticalCSSCore::ao_ccss_log('Waiting 15 seconds due to criticalcss.com API limits', 3); |
|
193 | + sleep(15); |
|
194 | 194 | } |
195 | 195 | |
196 | 196 | // Dispatch the job result request and increment request count. |
197 | - $apireq = $this->ao_ccss_api_results( $jprops['jid'], $queue_debug, $qdobj['htcode'] ); |
|
197 | + $apireq = $this->ao_ccss_api_results($jprops['jid'], $queue_debug, $qdobj['htcode']); |
|
198 | 198 | $jr++; |
199 | 199 | |
200 | 200 | // NOTE: All the following condigitons maps to the ones in admin_settings_queue.js.php |
201 | 201 | // Replace API response values if queue debugging is enabled and some value is set. |
202 | - if ( $queue_debug ) { |
|
203 | - if ( $qdobj['status'] ) { |
|
202 | + if ($queue_debug) { |
|
203 | + if ($qdobj['status']) { |
|
204 | 204 | $apireq['status'] = $qdobj['status']; |
205 | 205 | } |
206 | - if ( $qdobj['resultStatus'] ) { |
|
206 | + if ($qdobj['resultStatus']) { |
|
207 | 207 | $apireq['resultStatus'] = $qdobj['resultStatus']; |
208 | 208 | } |
209 | - if ( $qdobj['validationStatus'] ) { |
|
209 | + if ($qdobj['validationStatus']) { |
|
210 | 210 | $apireq['validationStatus'] = $qdobj['validationStatus']; |
211 | 211 | } |
212 | 212 | } |
213 | 213 | |
214 | - if ( 'JOB_QUEUED' == $apireq['status'] || 'JOB_ONGOING' == $apireq['status'] ) { |
|
214 | + if ('JOB_QUEUED' == $apireq['status'] || 'JOB_ONGOING' == $apireq['status']) { |
|
215 | 215 | // SUCCESS: request has a valid result |
216 | 216 | // Process a PENDING job |
217 | 217 | // Update job properties. |
218 | 218 | $jprops['jqstat'] = $apireq['status']; |
219 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '> unchanged', 3 ); |
|
220 | - } elseif ( 'JOB_DONE' == $apireq['status'] ) { |
|
219 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].'> unchanged', 3); |
|
220 | + } elseif ('JOB_DONE' == $apireq['status']) { |
|
221 | 221 | // Process a DONE job |
222 | 222 | // New resultStatus from ccss.com "HTML_404", consider as "GOOD" for now. |
223 | - if ( 'HTML_404' == $apireq['resultStatus'] ) { |
|
223 | + if ('HTML_404' == $apireq['resultStatus']) { |
|
224 | 224 | $apireq['resultStatus'] = 'GOOD'; |
225 | 225 | } |
226 | 226 | |
227 | - if ( 'GOOD' == $apireq['resultStatus'] && 'GOOD' == $apireq['validationStatus'] ) { |
|
227 | + if ('GOOD' == $apireq['resultStatus'] && 'GOOD' == $apireq['validationStatus']) { |
|
228 | 228 | // SUCCESS: GOOD job with GOOD validation |
229 | 229 | // Update job properties. |
230 | - $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, false ); |
|
230 | + $jprops['file'] = $this->ao_ccss_save_file($apireq['css'], $trule, false); |
|
231 | 231 | $jprops['jqstat'] = $apireq['status']; |
232 | 232 | $jprops['jrstat'] = $apireq['resultStatus']; |
233 | 233 | $jprops['jvstat'] = $apireq['validationStatus']; |
234 | - $jprops['jftime'] = microtime( true ); |
|
234 | + $jprops['jftime'] = microtime(true); |
|
235 | 235 | $rule_update = true; |
236 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . '>, file saved <' . $jprops['file'] . '>', 3 ); |
|
237 | - } elseif ( 'GOOD' == $apireq['resultStatus'] && ( 'WARN' == $apireq['validationStatus'] || 'BAD' == $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' == $apireq['validationStatus'] ) ) { |
|
236 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].'>, file saved <'.$jprops['file'].'>', 3); |
|
237 | + } elseif ('GOOD' == $apireq['resultStatus'] && ('WARN' == $apireq['validationStatus'] || 'BAD' == $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' == $apireq['validationStatus'])) { |
|
238 | 238 | // SUCCESS: GOOD job with WARN or BAD validation |
239 | 239 | // Update job properties. |
240 | - $jprops['file'] = $this->ao_ccss_save_file( $apireq['css'], $trule, true ); |
|
240 | + $jprops['file'] = $this->ao_ccss_save_file($apireq['css'], $trule, true); |
|
241 | 241 | $jprops['jqstat'] = $apireq['status']; |
242 | 242 | $jprops['jrstat'] = $apireq['resultStatus']; |
243 | 243 | $jprops['jvstat'] = $apireq['validationStatus']; |
244 | - $jprops['jftime'] = microtime( true ); |
|
244 | + $jprops['jftime'] = microtime(true); |
|
245 | 245 | $rule_update = true; |
246 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful, remote id <' . $jprops['jid'] . '>, status <' . $jprops['jqstat'] . ', file saved <' . $jprops['file'] . '> but requires REVIEW', 3 ); |
|
247 | - } elseif ( 'GOOD' != $apireq['resultStatus'] && ( 'GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'] ) ) { |
|
246 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful, remote id <'.$jprops['jid'].'>, status <'.$jprops['jqstat'].', file saved <'.$jprops['file'].'> but requires REVIEW', 3); |
|
247 | + } elseif ('GOOD' != $apireq['resultStatus'] && ('GOOD' != $apireq['validationStatus'] || 'WARN' != $apireq['validationStatus'] || 'BAD' != $apireq['validationStatus'] || 'SCREENSHOT_WARN_BLANK' != $apireq['validationStatus'])) { |
|
248 | 248 | // ERROR: no GOOD, WARN or BAD results |
249 | 249 | // Update job properties. |
250 | 250 | $jprops['jqstat'] = $apireq['status']; |
251 | 251 | $jprops['jrstat'] = $apireq['resultStatus']; |
252 | 252 | $jprops['jvstat'] = $apireq['validationStatus']; |
253 | - $jprops['jftime'] = microtime( true ); |
|
254 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
253 | + $jprops['jftime'] = microtime(true); |
|
254 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job FAILED, status now is <'.$jprops['jqstat'].'>', 3); |
|
255 | 255 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
256 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
256 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
257 | 257 | } else { |
258 | 258 | // UNKNOWN: unhandled JOB_DONE exception |
259 | 259 | // Update job properties. |
260 | 260 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
261 | 261 | $jprops['jrstat'] = $apireq['resultStatus']; |
262 | 262 | $jprops['jvstat'] = $apireq['validationStatus']; |
263 | - $jprops['jftime'] = microtime( true ); |
|
264 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job is UNKNOWN, status now is <' . $jprops['jqstat'] . '>', 2 ); |
|
263 | + $jprops['jftime'] = microtime(true); |
|
264 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job is UNKNOWN, status now is <'.$jprops['jqstat'].'>', 2); |
|
265 | 265 | $apireq['css'] = '/* critical css removed for DEBUG logging purposes */'; |
266 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job response was: ' . json_encode( $apireq ), 3 ); |
|
266 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job response was: '.json_encode($apireq), 3); |
|
267 | 267 | } |
268 | - } elseif ( 'JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status'] ) { |
|
268 | + } elseif ('JOB_FAILED' == $apireq['job']['status'] || 'STATUS_JOB_BAD' == $apireq['job']['status']) { |
|
269 | 269 | // ERROR: failed job |
270 | 270 | // Update job properties. |
271 | 271 | $jprops['jqstat'] = $apireq['job']['status']; |
272 | - if ( $apireq['error'] ) { |
|
272 | + if ($apireq['error']) { |
|
273 | 273 | $jprops['jrstat'] = $apireq['job']['error']; |
274 | 274 | } else { |
275 | 275 | } |
276 | 276 | $jprops['jvstat'] = 'NONE'; |
277 | - $jprops['jftime'] = microtime( true ); |
|
278 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
279 | - } elseif ( 'This css no longer exists. Please re-generate it.' == $apireq['error'] ) { |
|
277 | + $jprops['jftime'] = microtime(true); |
|
278 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job FAILED, status now is <'.$jprops['jqstat'].'>', 3); |
|
279 | + } elseif ('This css no longer exists. Please re-generate it.' == $apireq['error']) { |
|
280 | 280 | // ERROR: CSS doesn't exist |
281 | 281 | // Update job properties. |
282 | 282 | $jprops['jqstat'] = 'NO_CSS'; |
283 | 283 | $jprops['jrstat'] = $apireq['error']; |
284 | 284 | $jprops['jvstat'] = 'NONE'; |
285 | - $jprops['jftime'] = microtime( true ); |
|
286 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request successful but job FAILED, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
287 | - } elseif ( empty( $apireq ) ) { |
|
285 | + $jprops['jftime'] = microtime(true); |
|
286 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request successful but job FAILED, status now is <'.$jprops['jqstat'].'>', 3); |
|
287 | + } elseif (empty($apireq)) { |
|
288 | 288 | // ERROR: no response |
289 | 289 | // Update job properties. |
290 | 290 | $jprops['jqstat'] = 'NO_RESPONSE'; |
291 | 291 | $jprops['jrstat'] = 'NONE'; |
292 | 292 | $jprops['jvstat'] = 'NONE'; |
293 | - $jprops['jftime'] = microtime( true ); |
|
294 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> request has no response, status now is <' . $jprops['jqstat'] . '>', 3 ); |
|
293 | + $jprops['jftime'] = microtime(true); |
|
294 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> request has no response, status now is <'.$jprops['jqstat'].'>', 3); |
|
295 | 295 | } else { |
296 | 296 | // UNKNOWN: unhandled results exception |
297 | 297 | // Update job properties. |
298 | 298 | $jprops['jqstat'] = 'JOB_UNKNOWN'; |
299 | 299 | $jprops['jrstat'] = 'NONE'; |
300 | 300 | $jprops['jvstat'] = 'NONE'; |
301 | - $jprops['jftime'] = microtime( true ); |
|
302 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> result request has an UNKNOWN condition, status now is <' . $jprops['jqstat'] . '>, check log messages above for more information', 2 ); |
|
301 | + $jprops['jftime'] = microtime(true); |
|
302 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> result request has an UNKNOWN condition, status now is <'.$jprops['jqstat'].'>, check log messages above for more information', 2); |
|
303 | 303 | } |
304 | 304 | |
305 | 305 | // Set queue update flag. |
@@ -307,40 +307,40 @@ discard block |
||
307 | 307 | } |
308 | 308 | |
309 | 309 | // Mark DONE jobs for removal. |
310 | - if ( 'JOB_DONE' == $jprops['jqstat'] ) { |
|
310 | + if ('JOB_DONE' == $jprops['jqstat']) { |
|
311 | 311 | $update = true; |
312 | 312 | $deljob = true; |
313 | 313 | } |
314 | 314 | |
315 | 315 | // Persist updated queue object. |
316 | - if ( $update ) { |
|
317 | - if ( ! $deljob ) { |
|
316 | + if ($update) { |
|
317 | + if (!$deljob) { |
|
318 | 318 | // Update properties of a NEW or PENDING job... |
319 | - $ao_ccss_queue[ $path ] = $jprops; |
|
319 | + $ao_ccss_queue[$path] = $jprops; |
|
320 | 320 | } else { |
321 | 321 | // ...or remove the DONE job. |
322 | - unset( $ao_ccss_queue[ $path ] ); |
|
323 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> is DONE and was removed from the queue', 3 ); |
|
322 | + unset($ao_ccss_queue[$path]); |
|
323 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> is DONE and was removed from the queue', 3); |
|
324 | 324 | } |
325 | 325 | |
326 | 326 | // Update queue object. |
327 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
328 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
329 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue updated by job id <' . $jprops['ljid'] . '>', 3 ); |
|
327 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
328 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
329 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue updated by job id <'.$jprops['ljid'].'>', 3); |
|
330 | 330 | |
331 | 331 | // Update target rule. |
332 | - if ( $rule_update ) { |
|
333 | - $this->ao_ccss_rule_update( $jprops['ljid'], $jprops['rtarget'], $jprops['file'], $jprops['hash'] ); |
|
334 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $jprops['ljid'] . '> updated the target rule <' . $jprops['rtarget'] . '>', 3 ); |
|
332 | + if ($rule_update) { |
|
333 | + $this->ao_ccss_rule_update($jprops['ljid'], $jprops['rtarget'], $jprops['file'], $jprops['hash']); |
|
334 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$jprops['ljid'].'> updated the target rule <'.$jprops['rtarget'].'>', 3); |
|
335 | 335 | } |
336 | 336 | } else { |
337 | 337 | // Or log no queue action. |
338 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Nothing to do on this job', 3 ); |
|
338 | + autoptimizeCriticalCSSCore::ao_ccss_log('Nothing to do on this job', 3); |
|
339 | 339 | } |
340 | 340 | |
341 | 341 | // Break the loop if request limit is set and was reached. |
342 | - if ( $ao_ccss_rlimit && $ao_ccss_rlimit == $jr ) { |
|
343 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'The limit of ' . $ao_ccss_rlimit . ' request(s) to criticalcss.com was reached, queue control must finish now', 3 ); |
|
342 | + if ($ao_ccss_rlimit && $ao_ccss_rlimit == $jr) { |
|
343 | + autoptimizeCriticalCSSCore::ao_ccss_log('The limit of '.$ao_ccss_rlimit.' request(s) to criticalcss.com was reached, queue control must finish now', 3); |
|
344 | 344 | break; |
345 | 345 | } |
346 | 346 | |
@@ -349,46 +349,46 @@ discard block |
||
349 | 349 | } |
350 | 350 | |
351 | 351 | // Remove the lock file and log the queue end. |
352 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
353 | - unlink( AO_CCSS_LOCK ); |
|
354 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control unlocked', 3 ); |
|
352 | + if (file_exists(AO_CCSS_LOCK)) { |
|
353 | + unlink(AO_CCSS_LOCK); |
|
354 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control unlocked', 3); |
|
355 | 355 | } |
356 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue control finished', 3 ); |
|
356 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue control finished', 3); |
|
357 | 357 | |
358 | 358 | // Log that queue is locked. |
359 | 359 | } else { |
360 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue is already running, skipping the attempt to run it again', 3 ); |
|
360 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue is already running, skipping the attempt to run it again', 3); |
|
361 | 361 | } |
362 | 362 | } |
363 | 363 | |
364 | - public function ao_ccss_diff_hashes( $ljid, $hash, $hashes, $rule ) { |
|
364 | + public function ao_ccss_diff_hashes($ljid, $hash, $hashes, $rule) { |
|
365 | 365 | // Compare job hashes |
366 | 366 | // STEP 1: update job hashes. |
367 | - if ( 1 == count( $hashes ) ) { |
|
367 | + if (1 == count($hashes)) { |
|
368 | 368 | // Job with a single hash |
369 | 369 | // Set job hash. |
370 | 370 | $hash = $hashes[0]; |
371 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> updated with SINGLE hash <' . $hash . '>', 3 ); |
|
371 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> updated with SINGLE hash <'.$hash.'>', 3); |
|
372 | 372 | } else { |
373 | 373 | // Job with multiple hashes |
374 | 374 | // Loop through hashes to concatenate them. |
375 | 375 | $nhash = ''; |
376 | - foreach ( $hashes as $shash ) { |
|
376 | + foreach ($hashes as $shash) { |
|
377 | 377 | $nhash .= $shash; |
378 | 378 | } |
379 | 379 | |
380 | 380 | // Set job hash. |
381 | - $hash = md5( $nhash ); |
|
382 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> updated with a COMPOSITE hash <' . $hash . '>', 3 ); |
|
381 | + $hash = md5($nhash); |
|
382 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> updated with a COMPOSITE hash <'.$hash.'>', 3); |
|
383 | 383 | } |
384 | 384 | |
385 | 385 | // STEP 2: compare job to existing jobs to prevent double submission for same type+hash. |
386 | 386 | global $ao_ccss_queue; |
387 | 387 | |
388 | - foreach ( $ao_ccss_queue as $queue_item ) { |
|
389 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Comparing <' . $rule . $hash . '> with <' . $queue_item['rtarget'] . $queue_item['hash'] . '>', 3 ); |
|
390 | - if ( $queue_item['hash'] == $hash && $queue_item['rtarget'] == $rule && in_array( $queue_item['jqstat'], array( 'JOB_QUEUED', 'JOB_ONGOING', 'JOB_DONE' ) ) ) { |
|
391 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> matches the already pending job <' . $queue_item['ljid'] . '>', 3 ); |
|
388 | + foreach ($ao_ccss_queue as $queue_item) { |
|
389 | + autoptimizeCriticalCSSCore::ao_ccss_log('Comparing <'.$rule.$hash.'> with <'.$queue_item['rtarget'].$queue_item['hash'].'>', 3); |
|
390 | + if ($queue_item['hash'] == $hash && $queue_item['rtarget'] == $rule && in_array($queue_item['jqstat'], array('JOB_QUEUED', 'JOB_ONGOING', 'JOB_DONE'))) { |
|
391 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> matches the already pending job <'.$queue_item['ljid'].'>', 3); |
|
392 | 392 | return false; |
393 | 393 | } |
394 | 394 | } |
@@ -398,32 +398,32 @@ discard block |
||
398 | 398 | global $ao_ccss_rules; |
399 | 399 | |
400 | 400 | // Prepare rule variables. |
401 | - $trule = explode( '|', $rule ); |
|
402 | - $srule = $ao_ccss_rules[ $trule[0] ][ $trule[1] ]; |
|
401 | + $trule = explode('|', $rule); |
|
402 | + $srule = $ao_ccss_rules[$trule[0]][$trule[1]]; |
|
403 | 403 | |
404 | 404 | // Check if a MANUAL rule exist and return false. |
405 | - if ( ! empty( $srule ) && ( 0 == $srule['hash'] && 0 != $srule['file'] ) ) { |
|
406 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> matches the MANUAL rule <' . $trule[0] . '|' . $trule[1] . '>', 3 ); |
|
405 | + if (!empty($srule) && (0 == $srule['hash'] && 0 != $srule['file'])) { |
|
406 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> matches the MANUAL rule <'.$trule[0].'|'.$trule[1].'>', 3); |
|
407 | 407 | return false; |
408 | - } elseif ( ! empty( $srule ) ) { |
|
408 | + } elseif (!empty($srule)) { |
|
409 | 409 | // Check if an AUTO rule exist. |
410 | - if ( $hash === $srule['hash'] && is_file( AO_CCSS_DIR . $srule['file'] ) && 0 != filesize( AO_CCSS_DIR . $srule['file'] ) ) { |
|
410 | + if ($hash === $srule['hash'] && is_file(AO_CCSS_DIR.$srule['file']) && 0 != filesize(AO_CCSS_DIR.$srule['file'])) { |
|
411 | 411 | // Check if job hash matches rule, if the CCSS file exists said file is not empty and return FALSE is so. |
412 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> MATCH the one in rule <' . $trule[0] . '|' . $trule[1] . '>', 3 ); |
|
412 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> MATCH the one in rule <'.$trule[0].'|'.$trule[1].'>', 3); |
|
413 | 413 | return false; |
414 | 414 | } else { |
415 | 415 | // Or return the new hash if they differ. |
416 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> DOES NOT MATCH the one in rule <' . $trule[0] . '|' . $trule[1] . '> or rule\'s CCSS file was invalid.', 3 ); |
|
416 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> DOES NOT MATCH the one in rule <'.$trule[0].'|'.$trule[1].'> or rule\'s CCSS file was invalid.', 3); |
|
417 | 417 | return $hash; |
418 | 418 | } |
419 | 419 | } else { |
420 | 420 | // Or just return the hash if no rule exist yet. |
421 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job id <' . $ljid . '> with hash <' . $hash . '> has no rule yet', 3 ); |
|
421 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job id <'.$ljid.'> with hash <'.$hash.'> has no rule yet', 3); |
|
422 | 422 | return $hash; |
423 | 423 | } |
424 | 424 | } |
425 | 425 | |
426 | - public function ao_ccss_api_generate( $path, $debug, $dcode ) { |
|
426 | + public function ao_ccss_api_generate($path, $debug, $dcode) { |
|
427 | 427 | // POST jobs to criticalcss.com and return responses |
428 | 428 | // Get key and key status. |
429 | 429 | global $ao_ccss_key; |
@@ -435,35 +435,35 @@ discard block |
||
435 | 435 | global $ao_ccss_noptimize; |
436 | 436 | |
437 | 437 | $site_host = get_site_url(); |
438 | - $site_path = parse_url( $site_host, PHP_URL_PATH ); |
|
438 | + $site_path = parse_url($site_host, PHP_URL_PATH); |
|
439 | 439 | |
440 | - if ( ! empty( $site_path ) ) { |
|
441 | - $site_host = str_replace( $site_path, '', $site_host ); |
|
440 | + if (!empty($site_path)) { |
|
441 | + $site_host = str_replace($site_path, '', $site_host); |
|
442 | 442 | } |
443 | 443 | |
444 | 444 | // Logic to bind to one domain to avoid site clones of sites would |
445 | 445 | // automatically begin spawning requests to criticalcss.com which has |
446 | 446 | // a per domain cost. |
447 | 447 | global $ao_ccss_domain; |
448 | - if ( empty( $ao_ccss_domain ) ) { |
|
448 | + if (empty($ao_ccss_domain)) { |
|
449 | 449 | // first request being done, update option to allow future requests are only allowed if from same domain. |
450 | - update_option( 'autoptimize_ccss_domain', str_rot13( $site_host ) ); |
|
451 | - } elseif ( trim( $ao_ccss_domain, '\'"' ) !== 'none' && parse_url( $site_host, PHP_URL_HOST ) !== parse_url( $ao_ccss_domain, PHP_URL_HOST ) && apply_filters( 'autoptimize_filter_ccss_bind_domain', true ) ) { |
|
450 | + update_option('autoptimize_ccss_domain', str_rot13($site_host)); |
|
451 | + } elseif (trim($ao_ccss_domain, '\'"') !== 'none' && parse_url($site_host, PHP_URL_HOST) !== parse_url($ao_ccss_domain, PHP_URL_HOST) && apply_filters('autoptimize_filter_ccss_bind_domain', true)) { |
|
452 | 452 | // not the same domain, log as error and return without posting to criticalcss.com. |
453 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Request for domain ' . $site_host . ' does not match bound domain ' . $ao_ccss_domain . ' so not proceeding.', 2 ); |
|
453 | + autoptimizeCriticalCSSCore::ao_ccss_log('Request for domain '.$site_host.' does not match bound domain '.$ao_ccss_domain.' so not proceeding.', 2); |
|
454 | 454 | return false; |
455 | 455 | } |
456 | 456 | |
457 | - $src_url = $site_host . $path; |
|
457 | + $src_url = $site_host.$path; |
|
458 | 458 | |
459 | 459 | // Avoid AO optimizations if required by config or avoid lazyload if lazyload is active in AO. |
460 | - if ( ! empty( $ao_ccss_noptimize ) ) { |
|
460 | + if (!empty($ao_ccss_noptimize)) { |
|
461 | 461 | $src_url .= '?ao_noptirocket=1'; |
462 | - } elseif ( class_exists( 'autoptimizeImages', false ) && autoptimizeImages::should_lazyload_wrapper() ) { |
|
462 | + } elseif (class_exists('autoptimizeImages', false) && autoptimizeImages::should_lazyload_wrapper()) { |
|
463 | 463 | $src_url .= '?ao_nolazy=1'; |
464 | 464 | } |
465 | 465 | |
466 | - $src_url = apply_filters( 'autoptimize_filter_ccss_cron_srcurl', $src_url ); |
|
466 | + $src_url = apply_filters('autoptimize_filter_ccss_cron_srcurl', $src_url); |
|
467 | 467 | |
468 | 468 | // Initialize request body. |
469 | 469 | $body = array(); |
@@ -473,85 +473,85 @@ discard block |
||
473 | 473 | |
474 | 474 | // Prepare and add viewport size to the body if available. |
475 | 475 | $viewport = autoptimizeCriticalCSSCore::ao_ccss_viewport(); |
476 | - if ( ! empty( $viewport['w'] ) && ! empty( $viewport['h'] ) ) { |
|
476 | + if (!empty($viewport['w']) && !empty($viewport['h'])) { |
|
477 | 477 | $body['width'] = $viewport['w']; |
478 | 478 | $body['height'] = $viewport['h']; |
479 | 479 | } |
480 | 480 | |
481 | 481 | // Prepare and add forceInclude to the body if available. |
482 | 482 | global $ao_ccss_finclude; |
483 | - $finclude = $this->ao_ccss_finclude( $ao_ccss_finclude ); |
|
484 | - if ( ! empty( $finclude ) ) { |
|
483 | + $finclude = $this->ao_ccss_finclude($ao_ccss_finclude); |
|
484 | + if (!empty($finclude)) { |
|
485 | 485 | $body['forceInclude'] = $finclude; |
486 | 486 | } |
487 | 487 | |
488 | 488 | // Body must be json and log it. |
489 | - $body = json_encode( $body ); |
|
490 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request body is ' . $body, 3 ); |
|
489 | + $body = json_encode($body); |
|
490 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request body is '.$body, 3); |
|
491 | 491 | |
492 | 492 | // Prepare the request. |
493 | - $url = esc_url_raw( AO_CCSS_API . 'generate?aover=' . AO_CCSS_VER ); |
|
493 | + $url = esc_url_raw(AO_CCSS_API.'generate?aover='.AO_CCSS_VER); |
|
494 | 494 | $args = array( |
495 | 495 | 'headers' => array( |
496 | - 'User-Agent' => 'Autoptimize v' . AO_CCSS_VER, |
|
496 | + 'User-Agent' => 'Autoptimize v'.AO_CCSS_VER, |
|
497 | 497 | 'Content-type' => 'application/json; charset=utf-8', |
498 | - 'Authorization' => 'JWT ' . $key, |
|
498 | + 'Authorization' => 'JWT '.$key, |
|
499 | 499 | 'Connection' => 'close', |
500 | 500 | ), |
501 | 501 | 'body' => $body, |
502 | 502 | ); |
503 | 503 | |
504 | 504 | // Dispatch the request and store its response code. |
505 | - $req = wp_safe_remote_post( $url, $args ); |
|
506 | - $code = wp_remote_retrieve_response_code( $req ); |
|
507 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
505 | + $req = wp_safe_remote_post($url, $args); |
|
506 | + $code = wp_remote_retrieve_response_code($req); |
|
507 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
508 | 508 | |
509 | - if ( $debug && $dcode ) { |
|
509 | + if ($debug && $dcode) { |
|
510 | 510 | // If queue debug is active, change response code. |
511 | 511 | $code = $dcode; |
512 | 512 | } |
513 | 513 | |
514 | - if ( 200 == $code ) { |
|
514 | + if (200 == $code) { |
|
515 | 515 | // Response code is OK. |
516 | 516 | // Workaround criticalcss.com non-RESTful reponses. |
517 | - if ( 'JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] || 'STATUS_JOB_BAD' == $body['job']['status'] ) { |
|
517 | + if ('JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] || 'STATUS_JOB_BAD' == $body['job']['status']) { |
|
518 | 518 | // Log successful and return encoded request body. |
519 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied successfully', 3 ); |
|
519 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied successfully', 3); |
|
520 | 520 | |
521 | 521 | // This code also means the key is valid, so cache key status for 24h if not already cached. |
522 | - if ( ( ! $key_status || 2 != $key_status ) && $key ) { |
|
523 | - update_option( 'autoptimize_ccss_keyst', 2 ); |
|
524 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is valid, updating key status', 3 ); |
|
522 | + if ((!$key_status || 2 != $key_status) && $key) { |
|
523 | + update_option('autoptimize_ccss_keyst', 2); |
|
524 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is valid, updating key status', 3); |
|
525 | 525 | } |
526 | 526 | |
527 | 527 | // Return the request body. |
528 | 528 | return $body; |
529 | 529 | } else { |
530 | 530 | // Log successful requests with invalid reponses. |
531 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied with code <' . $code . '> and an UNKNOWN error condition, body follows...', 2 ); |
|
532 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
531 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied with code <'.$code.'> and an UNKNOWN error condition, body follows...', 2); |
|
532 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
533 | 533 | return $body; |
534 | 534 | } |
535 | 535 | } else { |
536 | 536 | // Response code is anything else. |
537 | 537 | // Log failed request with a valid response code and return body. |
538 | - if ( $code ) { |
|
539 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> replied with error code <' . $code . '>, body follows...', 2 ); |
|
540 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
538 | + if ($code) { |
|
539 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> replied with error code <'.$code.'>, body follows...', 2); |
|
540 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
541 | 541 | |
542 | - if ( 401 == $code ) { |
|
542 | + if (401 == $code) { |
|
543 | 543 | // If request is unauthorized, also clear key status. |
544 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
545 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
544 | + update_option('autoptimize_ccss_keyst', 1); |
|
545 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
546 | 546 | } |
547 | 547 | |
548 | 548 | // Return the request body. |
549 | 549 | return $body; |
550 | 550 | } else { |
551 | 551 | // Log failed request with no response and return false. |
552 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: POST generate request for path <' . $src_url . '> has no response, this could be a service timeout', 2 ); |
|
553 | - if ( is_wp_error( $req ) ) { |
|
554 | - autoptimizeCriticalCSSCore::ao_ccss_log( $req->get_error_message(), 2 ); |
|
552 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: POST generate request for path <'.$src_url.'> has no response, this could be a service timeout', 2); |
|
553 | + if (is_wp_error($req)) { |
|
554 | + autoptimizeCriticalCSSCore::ao_ccss_log($req->get_error_message(), 2); |
|
555 | 555 | } |
556 | 556 | |
557 | 557 | return false; |
@@ -559,76 +559,76 @@ discard block |
||
559 | 559 | } |
560 | 560 | } |
561 | 561 | |
562 | - public function ao_ccss_api_results( $jobid, $debug, $dcode ) { |
|
562 | + public function ao_ccss_api_results($jobid, $debug, $dcode) { |
|
563 | 563 | // GET jobs from criticalcss.com and return responses |
564 | 564 | // Get key. |
565 | 565 | global $ao_ccss_key; |
566 | 566 | $key = $ao_ccss_key; |
567 | 567 | |
568 | 568 | // Prepare the request. |
569 | - $url = AO_CCSS_API . 'results?resultId=' . $jobid; |
|
569 | + $url = AO_CCSS_API.'results?resultId='.$jobid; |
|
570 | 570 | $args = array( |
571 | 571 | 'headers' => array( |
572 | - 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v' . AO_CCSS_VER, |
|
573 | - 'Authorization' => 'JWT ' . $key, |
|
572 | + 'User-Agent' => 'Autoptimize CriticalCSS Power-Up v'.AO_CCSS_VER, |
|
573 | + 'Authorization' => 'JWT '.$key, |
|
574 | 574 | 'Connection' => 'close', |
575 | 575 | ), |
576 | 576 | ); |
577 | 577 | |
578 | 578 | // Dispatch the request and store its response code. |
579 | - $req = wp_safe_remote_get( $url, $args ); |
|
580 | - $code = wp_remote_retrieve_response_code( $req ); |
|
581 | - $body = json_decode( wp_remote_retrieve_body( $req ), true ); |
|
579 | + $req = wp_safe_remote_get($url, $args); |
|
580 | + $code = wp_remote_retrieve_response_code($req); |
|
581 | + $body = json_decode(wp_remote_retrieve_body($req), true); |
|
582 | 582 | |
583 | - if ( $debug && $dcode ) { |
|
583 | + if ($debug && $dcode) { |
|
584 | 584 | // If queue debug is active, change response code. |
585 | 585 | $code = $dcode; |
586 | 586 | } |
587 | 587 | |
588 | - if ( 200 == $code ) { |
|
588 | + if (200 == $code) { |
|
589 | 589 | // Response code is OK. |
590 | - if ( is_array( $body ) && ( array_key_exists( 'status', $body ) || array_key_exists( 'job', $body ) ) && ( 'JOB_QUEUED' == $body['status'] || 'JOB_ONGOING' == $body['status'] || 'JOB_DONE' == $body['status'] || 'JOB_FAILED' == $body['status'] || 'JOB_UNKNOWN' == $body['status'] || 'STATUS_JOB_BAD' == $body['job']['status'] ) ) { |
|
590 | + if (is_array($body) && (array_key_exists('status', $body) || array_key_exists('job', $body)) && ('JOB_QUEUED' == $body['status'] || 'JOB_ONGOING' == $body['status'] || 'JOB_DONE' == $body['status'] || 'JOB_FAILED' == $body['status'] || 'JOB_UNKNOWN' == $body['status'] || 'STATUS_JOB_BAD' == $body['job']['status'])) { |
|
591 | 591 | // Workaround criticalcss.com non-RESTful reponses |
592 | 592 | // Log successful and return encoded request body. |
593 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied successfully', 3 ); |
|
593 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied successfully', 3); |
|
594 | 594 | return $body; |
595 | - } elseif ( is_array( $body ) && ( array_key_exists( 'error', $body ) && 'This css no longer exists. Please re-generate it.' == $body['error'] ) ) { |
|
595 | + } elseif (is_array($body) && (array_key_exists('error', $body) && 'This css no longer exists. Please re-generate it.' == $body['error'])) { |
|
596 | 596 | // Handle no CSS reply |
597 | 597 | // Log no CSS error and return encoded request body. |
598 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied successfully but the CSS for it does not exist anymore', 3 ); |
|
598 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied successfully but the CSS for it does not exist anymore', 3); |
|
599 | 599 | return $body; |
600 | 600 | } else { |
601 | 601 | // Log failed request and return false. |
602 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied with code <' . $code . '> and an UNKNOWN error condition, body follows...', 2 ); |
|
603 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
602 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied with code <'.$code.'> and an UNKNOWN error condition, body follows...', 2); |
|
603 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
604 | 604 | return false; |
605 | 605 | } |
606 | 606 | } else { |
607 | 607 | // Response code is anything else |
608 | 608 | // Log failed request with a valid response code and return body. |
609 | - if ( $code ) { |
|
610 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> replied with error code <' . $code . '>, body follows...', 2 ); |
|
611 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); |
|
612 | - if ( 401 == $code ) { |
|
609 | + if ($code) { |
|
610 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> replied with error code <'.$code.'>, body follows...', 2); |
|
611 | + autoptimizeCriticalCSSCore::ao_ccss_log(print_r($body, true), 2); |
|
612 | + if (401 == $code) { |
|
613 | 613 | // If request is unauthorized, also clear key status. |
614 | - update_option( 'autoptimize_ccss_keyst', 1 ); |
|
615 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 ); |
|
614 | + update_option('autoptimize_ccss_keyst', 1); |
|
615 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: API key is invalid, updating key status', 3); |
|
616 | 616 | } |
617 | 617 | |
618 | 618 | // Return the request body. |
619 | 619 | return $body; |
620 | 620 | } else { |
621 | 621 | // Log failed request with no response and return false. |
622 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: GET results request for remote job id <' . $jobid . '> has no response, this could be a service timeout', 2 ); |
|
622 | + autoptimizeCriticalCSSCore::ao_ccss_log('criticalcss.com: GET results request for remote job id <'.$jobid.'> has no response, this could be a service timeout', 2); |
|
623 | 623 | return false; |
624 | 624 | } |
625 | 625 | } |
626 | 626 | } |
627 | 627 | |
628 | - public function ao_ccss_save_file( $ccss, $target, $review ) { |
|
628 | + public function ao_ccss_save_file($ccss, $target, $review) { |
|
629 | 629 | // Save critical CSS into the filesystem and return its filename |
630 | 630 | // Prepare review mark. |
631 | - if ( $review ) { |
|
631 | + if ($review) { |
|
632 | 632 | $rmark = '_R'; |
633 | 633 | } else { |
634 | 634 | $rmark = ''; |
@@ -638,21 +638,21 @@ discard block |
||
638 | 638 | $filename = false; |
639 | 639 | $content = $ccss; |
640 | 640 | |
641 | - if ( autoptimizeCriticalCSSCore::ao_ccss_check_contents( $content ) ) { |
|
641 | + if (autoptimizeCriticalCSSCore::ao_ccss_check_contents($content)) { |
|
642 | 642 | // Sanitize content, set filename and try to save file. |
643 | - $file = AO_CCSS_DIR . 'ccss_' . md5( $ccss . $target[1] ) . $rmark . '.css'; |
|
644 | - $status = file_put_contents( $file, $content, LOCK_EX ); |
|
645 | - $filename = pathinfo( $file, PATHINFO_BASENAME ); |
|
646 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS file for the rule <' . $target[0] . '|' . $target[1] . '> was saved as <' . $filename . '>, size in bytes is <' . $status . '>', 3 ); |
|
643 | + $file = AO_CCSS_DIR.'ccss_'.md5($ccss.$target[1]).$rmark.'.css'; |
|
644 | + $status = file_put_contents($file, $content, LOCK_EX); |
|
645 | + $filename = pathinfo($file, PATHINFO_BASENAME); |
|
646 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS file for the rule <'.$target[0].'|'.$target[1].'> was saved as <'.$filename.'>, size in bytes is <'.$status.'>', 3); |
|
647 | 647 | |
648 | - if ( ! $status ) { |
|
648 | + if (!$status) { |
|
649 | 649 | // If file has not been saved, reset filename. |
650 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS file <' . $filename . '> could not be not saved', 2 ); |
|
650 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS file <'.$filename.'> could not be not saved', 2); |
|
651 | 651 | $filename = false; |
652 | 652 | return $filename; |
653 | 653 | } |
654 | 654 | } else { |
655 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received did not pass content check', 2 ); |
|
655 | + autoptimizeCriticalCSSCore::ao_ccss_log('Critical CSS received did not pass content check', 2); |
|
656 | 656 | return $filename; |
657 | 657 | } |
658 | 658 | |
@@ -662,15 +662,15 @@ discard block |
||
662 | 662 | global $ao_ccss_rules; |
663 | 663 | |
664 | 664 | // Prepare rule variables. |
665 | - $srule = $ao_ccss_rules[ $target[0] ][ $target[1] ]; |
|
665 | + $srule = $ao_ccss_rules[$target[0]][$target[1]]; |
|
666 | 666 | $oldfile = $srule['file']; |
667 | 667 | |
668 | - if ( $oldfile && $oldfile !== $filename ) { |
|
669 | - $delfile = AO_CCSS_DIR . $oldfile; |
|
670 | - if ( file_exists( $delfile ) ) { |
|
671 | - $unlinkst = unlink( $delfile ); |
|
672 | - if ( $unlinkst ) { |
|
673 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'A previous critical CSS file <' . $oldfile . '> was removed for the rule <' . $target[0] . '|' . $target[1] . '>', 3 ); |
|
668 | + if ($oldfile && $oldfile !== $filename) { |
|
669 | + $delfile = AO_CCSS_DIR.$oldfile; |
|
670 | + if (file_exists($delfile)) { |
|
671 | + $unlinkst = unlink($delfile); |
|
672 | + if ($unlinkst) { |
|
673 | + autoptimizeCriticalCSSCore::ao_ccss_log('A previous critical CSS file <'.$oldfile.'> was removed for the rule <'.$target[0].'|'.$target[1].'>', 3); |
|
674 | 674 | } |
675 | 675 | } |
676 | 676 | } |
@@ -679,29 +679,29 @@ discard block |
||
679 | 679 | return $filename; |
680 | 680 | } |
681 | 681 | |
682 | - public function ao_ccss_rule_update( $ljid, $srule, $file, $hash ) { |
|
682 | + public function ao_ccss_rule_update($ljid, $srule, $file, $hash) { |
|
683 | 683 | // Update or create a rule |
684 | 684 | // Attach required arrays. |
685 | 685 | global $ao_ccss_rules; |
686 | 686 | |
687 | 687 | // Prepare rule variables. |
688 | - $trule = explode( '|', $srule ); |
|
689 | - $rule = $ao_ccss_rules[ $trule[0] ][ $trule[1] ]; |
|
688 | + $trule = explode('|', $srule); |
|
689 | + $rule = $ao_ccss_rules[$trule[0]][$trule[1]]; |
|
690 | 690 | $action = false; |
691 | 691 | $rtype = ''; |
692 | 692 | |
693 | - if ( 0 === $rule['hash'] && 0 !== $rule['file'] ) { |
|
693 | + if (0 === $rule['hash'] && 0 !== $rule['file']) { |
|
694 | 694 | // manual rule, don't ever overwrite. |
695 | 695 | $action = 'NOT UPDATED'; |
696 | 696 | $rtype = 'MANUAL'; |
697 | - } elseif ( 0 === $rule['hash'] && 0 === $rule['file'] ) { |
|
697 | + } elseif (0 === $rule['hash'] && 0 === $rule['file']) { |
|
698 | 698 | // If this is an user created AUTO rule with no hash and file yet, update its hash and filename |
699 | 699 | // Set rule hash, file and action flag. |
700 | 700 | $rule['hash'] = $hash; |
701 | 701 | $rule['file'] = $file; |
702 | 702 | $action = 'UPDATED'; |
703 | 703 | $rtype = 'AUTO'; |
704 | - } elseif ( 0 !== $rule['hash'] && ctype_alnum( $rule['hash'] ) ) { |
|
704 | + } elseif (0 !== $rule['hash'] && ctype_alnum($rule['hash'])) { |
|
705 | 705 | // If this is an genuine AUTO rule, update its hash and filename |
706 | 706 | // Set rule hash, file and action flag. |
707 | 707 | $rule['hash'] = $hash; |
@@ -711,7 +711,7 @@ discard block |
||
711 | 711 | } else { |
712 | 712 | // If rule doesn't exist, create an AUTO rule |
713 | 713 | // AUTO rules were only for types, but will now also work for paths. |
714 | - if ( 'types' == $trule[0] || 'paths' == $trule[0] ) { |
|
714 | + if ('types' == $trule[0] || 'paths' == $trule[0]) { |
|
715 | 715 | // Set rule hash and file and action flag. |
716 | 716 | $rule['hash'] = $hash; |
717 | 717 | $rule['file'] = $file; |
@@ -719,47 +719,47 @@ discard block |
||
719 | 719 | $rtype = 'AUTO'; |
720 | 720 | } else { |
721 | 721 | // Log that no rule was created. |
722 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Exception, no AUTO rule created', 3 ); |
|
722 | + autoptimizeCriticalCSSCore::ao_ccss_log('Exception, no AUTO rule created', 3); |
|
723 | 723 | } |
724 | 724 | } |
725 | 725 | |
726 | - if ( $action ) { |
|
726 | + if ($action) { |
|
727 | 727 | // If a rule creation/update is required, persist updated rules object. |
728 | - $ao_ccss_rules[ $trule[0] ][ $trule[1] ] = $rule; |
|
729 | - $ao_ccss_rules_raw = json_encode( $ao_ccss_rules ); |
|
730 | - update_option( 'autoptimize_ccss_rules', $ao_ccss_rules_raw ); |
|
731 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Target rule <' . $srule . '> of type <' . $rtype . '> was ' . $action . ' for job id <' . $ljid . '>', 3 ); |
|
728 | + $ao_ccss_rules[$trule[0]][$trule[1]] = $rule; |
|
729 | + $ao_ccss_rules_raw = json_encode($ao_ccss_rules); |
|
730 | + update_option('autoptimize_ccss_rules', $ao_ccss_rules_raw); |
|
731 | + autoptimizeCriticalCSSCore::ao_ccss_log('Target rule <'.$srule.'> of type <'.$rtype.'> was '.$action.' for job id <'.$ljid.'>', 3); |
|
732 | 732 | } else { |
733 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'No rule action required', 3 ); |
|
733 | + autoptimizeCriticalCSSCore::ao_ccss_log('No rule action required', 3); |
|
734 | 734 | } |
735 | 735 | } |
736 | 736 | |
737 | - function ao_ccss_finclude( $finclude_raw ) { |
|
737 | + function ao_ccss_finclude($finclude_raw) { |
|
738 | 738 | // Prepare forceInclude object. |
739 | - if ( ! empty( $finclude_raw ) ) { |
|
739 | + if (!empty($finclude_raw)) { |
|
740 | 740 | // If there are any content |
741 | 741 | // Convert raw string into arra and initialize the returning object. |
742 | - $fincludes = explode( ',', $finclude_raw ); |
|
742 | + $fincludes = explode(',', $finclude_raw); |
|
743 | 743 | $finclude = array(); |
744 | 744 | |
745 | 745 | // Interacts over every rule. |
746 | 746 | $i = 0; |
747 | - foreach ( $fincludes as $include ) { |
|
747 | + foreach ($fincludes as $include) { |
|
748 | 748 | // Trim leading and trailing whitespaces. |
749 | - $include = trim( $include ); |
|
749 | + $include = trim($include); |
|
750 | 750 | |
751 | - if ( substr( $include, 0, 2 ) === '//' ) { |
|
751 | + if (substr($include, 0, 2) === '//') { |
|
752 | 752 | // Regex rule |
753 | 753 | // Format value as required. |
754 | - $include = str_replace( '//', '/', $include ); |
|
755 | - $include = $include . '/i'; |
|
754 | + $include = str_replace('//', '/', $include); |
|
755 | + $include = $include.'/i'; |
|
756 | 756 | |
757 | 757 | // Store regex object. |
758 | - $finclude[ $i ]['type'] = 'RegExp'; |
|
759 | - $finclude[ $i ]['value'] = $include; |
|
758 | + $finclude[$i]['type'] = 'RegExp'; |
|
759 | + $finclude[$i]['value'] = $include; |
|
760 | 760 | } else { |
761 | 761 | // Simple value rule. |
762 | - $finclude[ $i ]['value'] = $include; |
|
762 | + $finclude[$i]['value'] = $include; |
|
763 | 763 | } |
764 | 764 | |
765 | 765 | $i++; |
@@ -776,54 +776,54 @@ discard block |
||
776 | 776 | public static function ao_ccss_cleaning() { |
777 | 777 | // Perform plugin maintenance |
778 | 778 | // Truncate log file >= 1MB . |
779 | - if ( file_exists( AO_CCSS_LOG ) ) { |
|
780 | - if ( filesize( AO_CCSS_LOG ) >= 1048576 ) { |
|
781 | - $logfile = fopen( AO_CCSS_LOG, 'w' ); |
|
782 | - fclose( $logfile ); |
|
779 | + if (file_exists(AO_CCSS_LOG)) { |
|
780 | + if (filesize(AO_CCSS_LOG) >= 1048576) { |
|
781 | + $logfile = fopen(AO_CCSS_LOG, 'w'); |
|
782 | + fclose($logfile); |
|
783 | 783 | } |
784 | 784 | } |
785 | 785 | |
786 | 786 | // Remove lock file. |
787 | - if ( file_exists( AO_CCSS_LOCK ) ) { |
|
788 | - unlink( AO_CCSS_LOCK ); |
|
787 | + if (file_exists(AO_CCSS_LOCK)) { |
|
788 | + unlink(AO_CCSS_LOCK); |
|
789 | 789 | } |
790 | 790 | |
791 | 791 | // Make sure queue processing is scheduled, recreate if not. |
792 | - if ( ! wp_next_scheduled( 'ao_ccss_queue' ) ) { |
|
793 | - wp_schedule_event( time(), apply_filters( 'ao_ccss_queue_schedule', 'ao_ccss' ), 'ao_ccss_queue' ); |
|
792 | + if (!wp_next_scheduled('ao_ccss_queue')) { |
|
793 | + wp_schedule_event(time(), apply_filters('ao_ccss_queue_schedule', 'ao_ccss'), 'ao_ccss_queue'); |
|
794 | 794 | } |
795 | 795 | |
796 | 796 | // Queue cleaning. |
797 | 797 | global $ao_ccss_queue; |
798 | 798 | $queue_purge_threshold = 100; |
799 | - $queue_purge_age = 24 * 60 * 60; |
|
800 | - $queue_length = count( $ao_ccss_queue ); |
|
801 | - $timestamp_yesterday = microtime( true ) - $queue_purge_age; |
|
799 | + $queue_purge_age = 24*60*60; |
|
800 | + $queue_length = count($ao_ccss_queue); |
|
801 | + $timestamp_yesterday = microtime(true) - $queue_purge_age; |
|
802 | 802 | $remove_old_new = false; |
803 | 803 | $queue_altered = false; |
804 | 804 | |
805 | - if ( $queue_length > $queue_purge_threshold ) { |
|
805 | + if ($queue_length > $queue_purge_threshold) { |
|
806 | 806 | $remove_old_new = true; |
807 | 807 | } |
808 | 808 | |
809 | - foreach ( $ao_ccss_queue as $path => $job ) { |
|
810 | - if ( ( $remove_old_new && 'NEW' == $job['jqstat'] && $job['jctime'] < $timestamp_yesterday ) || in_array( $job['jqstat'], array( 'JOB_FAILED', 'STATUS_JOB_BAD', 'NO_CSS', 'NO_RESPONSE' ) ) ) { |
|
811 | - unset( $ao_ccss_queue[ $path ] ); |
|
809 | + foreach ($ao_ccss_queue as $path => $job) { |
|
810 | + if (($remove_old_new && 'NEW' == $job['jqstat'] && $job['jctime'] < $timestamp_yesterday) || in_array($job['jqstat'], array('JOB_FAILED', 'STATUS_JOB_BAD', 'NO_CSS', 'NO_RESPONSE'))) { |
|
811 | + unset($ao_ccss_queue[$path]); |
|
812 | 812 | $queue_altered = true; |
813 | 813 | } |
814 | 814 | } |
815 | 815 | |
816 | 816 | // save queue to options! |
817 | - if ( $queue_altered ) { |
|
818 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
819 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
820 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Queue cleaning done.', 3 ); |
|
817 | + if ($queue_altered) { |
|
818 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
819 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
820 | + autoptimizeCriticalCSSCore::ao_ccss_log('Queue cleaning done.', 3); |
|
821 | 821 | } |
822 | 822 | |
823 | 823 | // re-check key if invalid. |
824 | 824 | global $ao_ccss_keyst; |
825 | - if ( 1 == $ao_ccss_keyst ) { |
|
826 | - $this->ao_ccss_api_generate( '', '', '' ); |
|
825 | + if (1 == $ao_ccss_keyst) { |
|
826 | + $this->ao_ccss_api_generate('', '', ''); |
|
827 | 827 | } |
828 | 828 | } |
829 | 829 | } |
@@ -221,6 +221,11 @@ |
||
221 | 221 | return $page_type; |
222 | 222 | } |
223 | 223 | |
224 | + /** |
|
225 | + * @param string $path |
|
226 | + * @param false|string $target |
|
227 | + * @param boolean $create |
|
228 | + */ |
|
224 | 229 | public function ao_ccss_define_job( $path, $target, $type, $hash, $file, $jid, $jrstat, $jvstat, $create ) { |
225 | 230 | // Define a job entry to be created or updated |
226 | 231 | // Define commom job properties. |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Critical CSS job enqueue logic. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -12,27 +12,27 @@ discard block |
||
12 | 12 | { |
13 | 13 | // fetch all options at once and populate them individually explicitely as globals. |
14 | 14 | $all_options = autoptimizeCriticalCSSBase::fetch_options(); |
15 | - foreach ( $all_options as $_option => $_value ) { |
|
15 | + foreach ($all_options as $_option => $_value) { |
|
16 | 16 | global ${$_option}; |
17 | 17 | ${$_option} = $_value; |
18 | 18 | } |
19 | 19 | } |
20 | 20 | |
21 | - public static function ao_ccss_enqueue( $hash ) { |
|
21 | + public static function ao_ccss_enqueue($hash) { |
|
22 | 22 | $self = new self(); |
23 | 23 | // Get key status. |
24 | - $key = autoptimizeCriticalCSSCore::ao_ccss_key_status( false ); |
|
24 | + $key = autoptimizeCriticalCSSCore::ao_ccss_key_status(false); |
|
25 | 25 | |
26 | 26 | // Queue is available to anyone... |
27 | 27 | $enqueue = true; |
28 | 28 | |
29 | 29 | // ... which are not the ones below. |
30 | - if ( is_user_logged_in() || is_feed() || is_404() || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || $self->ao_ccss_ua() || 'nokey' == $key['status'] || 'invalid' == $key['status'] ) { |
|
30 | + if (is_user_logged_in() || is_feed() || is_404() || (defined('DOING_AJAX') && DOING_AJAX) || $self->ao_ccss_ua() || 'nokey' == $key['status'] || 'invalid' == $key['status']) { |
|
31 | 31 | $enqueue = false; |
32 | - autoptimizeCriticalCSSCore::ao_ccss_log( "Job queuing is not available for WordPress's logged in users, feeds, error pages, ajax calls, to criticalcss.com itself or when a valid API key is not found", 3 ); |
|
32 | + autoptimizeCriticalCSSCore::ao_ccss_log("Job queuing is not available for WordPress's logged in users, feeds, error pages, ajax calls, to criticalcss.com itself or when a valid API key is not found", 3); |
|
33 | 33 | } |
34 | 34 | |
35 | - if ( $enqueue ) { |
|
35 | + if ($enqueue) { |
|
36 | 36 | // Continue if queue is available |
37 | 37 | // Attach required arrays/ vars. |
38 | 38 | global $ao_ccss_rules; |
@@ -41,7 +41,7 @@ discard block |
||
41 | 41 | global $ao_ccss_forcepath; |
42 | 42 | |
43 | 43 | // Get request path and page type, and initialize the queue update flag. |
44 | - $req_path = strtok( $_SERVER['REQUEST_URI'], '?' ); |
|
44 | + $req_path = strtok($_SERVER['REQUEST_URI'], '?'); |
|
45 | 45 | $req_type = $self->ao_ccss_get_type(); |
46 | 46 | $job_qualify = false; |
47 | 47 | $target_rule = false; |
@@ -49,21 +49,21 @@ discard block |
||
49 | 49 | $queue_update = false; |
50 | 50 | |
51 | 51 | // Match for paths in rules. |
52 | - foreach ( $ao_ccss_rules['paths'] as $path => $props ) { |
|
52 | + foreach ($ao_ccss_rules['paths'] as $path => $props) { |
|
53 | 53 | |
54 | 54 | // Prepare rule target and log. |
55 | - $target_rule = 'paths|' . $path; |
|
56 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Qualifying path <' . $req_path . '> for job submission by rule <' . $target_rule . '>', 3 ); |
|
55 | + $target_rule = 'paths|'.$path; |
|
56 | + autoptimizeCriticalCSSCore::ao_ccss_log('Qualifying path <'.$req_path.'> for job submission by rule <'.$target_rule.'>', 3); |
|
57 | 57 | |
58 | 58 | // Path match |
59 | 59 | // -> exact match needed for AUTO rules |
60 | 60 | // -> partial match OK for MANUAL rules (which have empty hash and a file with CCSS). |
61 | - if ( $path === $req_path || ( false == $props['hash'] && false != $props['file'] && preg_match( '|' . $path . '|', $req_path ) ) ) { |
|
61 | + if ($path === $req_path || (false == $props['hash'] && false != $props['file'] && preg_match('|'.$path.'|', $req_path))) { |
|
62 | 62 | |
63 | 63 | // There's a path match in the rule, so job QUALIFIES with a path rule match. |
64 | 64 | $job_qualify = true; |
65 | 65 | $rule_properties = $props; |
66 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Path <' . $req_path . '> QUALIFIED for job submission by rule <' . $target_rule . '>', 3 ); |
|
66 | + autoptimizeCriticalCSSCore::ao_ccss_log('Path <'.$req_path.'> QUALIFIED for job submission by rule <'.$target_rule.'>', 3); |
|
67 | 67 | |
68 | 68 | // Stop processing other path rules. |
69 | 69 | break; |
@@ -71,19 +71,19 @@ discard block |
||
71 | 71 | } |
72 | 72 | |
73 | 73 | // Match for types in rules if no path rule matches and if we're not enforcing paths. |
74 | - if ( ! $job_qualify && ( ! $ao_ccss_forcepath || ! in_array( $req_type, apply_filters( 'autoptimize_filter_ccss_coreenqueue_forcepathfortype', array( 'is_page' ) ) ) ) ) { |
|
75 | - foreach ( $ao_ccss_rules['types'] as $type => $props ) { |
|
74 | + if (!$job_qualify && (!$ao_ccss_forcepath || !in_array($req_type, apply_filters('autoptimize_filter_ccss_coreenqueue_forcepathfortype', array('is_page'))))) { |
|
75 | + foreach ($ao_ccss_rules['types'] as $type => $props) { |
|
76 | 76 | |
77 | 77 | // Prepare rule target and log. |
78 | - $target_rule = 'types|' . $type; |
|
79 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Qualifying page type <' . $req_type . '> on path <' . $req_path . '> for job submission by rule <' . $target_rule . '>', 3 ); |
|
78 | + $target_rule = 'types|'.$type; |
|
79 | + autoptimizeCriticalCSSCore::ao_ccss_log('Qualifying page type <'.$req_type.'> on path <'.$req_path.'> for job submission by rule <'.$target_rule.'>', 3); |
|
80 | 80 | |
81 | - if ( $req_type == $type ) { |
|
81 | + if ($req_type == $type) { |
|
82 | 82 | // Type match. |
83 | 83 | // There's a type match in the rule, so job QUALIFIES with a type rule match. |
84 | 84 | $job_qualify = true; |
85 | 85 | $rule_properties = $props; |
86 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Page type <' . $req_type . '> on path <' . $req_path . '> QUALIFIED for job submission by rule <' . $target_rule . '>', 3 ); |
|
86 | + autoptimizeCriticalCSSCore::ao_ccss_log('Page type <'.$req_type.'> on path <'.$req_path.'> QUALIFIED for job submission by rule <'.$target_rule.'>', 3); |
|
87 | 87 | |
88 | 88 | // Stop processing other type rules. |
89 | 89 | break; |
@@ -91,11 +91,11 @@ discard block |
||
91 | 91 | } |
92 | 92 | } |
93 | 93 | |
94 | - if ( $job_qualify && false == $rule_properties['hash'] && false != $rule_properties['file'] ) { |
|
94 | + if ($job_qualify && false == $rule_properties['hash'] && false != $rule_properties['file']) { |
|
95 | 95 | // If job qualifies but rule hash is false and file isn't false (MANUAL rule), job does not qualify despite what previous evaluations says. |
96 | 96 | $job_qualify = false; |
97 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job submission DISQUALIFIED by MANUAL rule <' . $target_rule . '> with hash <' . $rule_properties['hash'] . '> and file <' . $rule_properties['file'] . '>', 3 ); |
|
98 | - } elseif ( ! $job_qualify && empty( $rule_properties ) ) { |
|
97 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job submission DISQUALIFIED by MANUAL rule <'.$target_rule.'> with hash <'.$rule_properties['hash'].'> and file <'.$rule_properties['file'].'>', 3); |
|
98 | + } elseif (!$job_qualify && empty($rule_properties)) { |
|
99 | 99 | // But if job does not qualify and rule properties are set, job qualifies as there is no matching rule for it yet |
100 | 100 | // Fill-in the new target rule. |
101 | 101 | $job_qualify = true; |
@@ -103,28 +103,28 @@ discard block |
||
103 | 103 | // Should we switch to path-base AUTO-rules? Conditions: |
104 | 104 | // 1. forcepath option has to be enabled (off by default) |
105 | 105 | // 2. request type should be (by default, but filterable) one of is_page (removed for now: woo_is_product or woo_is_product_category). |
106 | - if ( $ao_ccss_forcepath && in_array( $req_type, apply_filters( 'autoptimize_filter_ccss_coreenqueue_forcepathfortype', array( 'is_page' ) ) ) ) { |
|
107 | - if ( '/' !== $req_path ) { |
|
108 | - $target_rule = 'paths|' . $req_path; |
|
106 | + if ($ao_ccss_forcepath && in_array($req_type, apply_filters('autoptimize_filter_ccss_coreenqueue_forcepathfortype', array('is_page')))) { |
|
107 | + if ('/' !== $req_path) { |
|
108 | + $target_rule = 'paths|'.$req_path; |
|
109 | 109 | } else { |
110 | 110 | // Exception; we don't want a path-based rule for "/" as that messes things up, hard-switch this to a type-based is_front_page rule. |
111 | - $target_rule = 'types|' . 'is_front_page'; |
|
111 | + $target_rule = 'types|'.'is_front_page'; |
|
112 | 112 | } |
113 | 113 | } else { |
114 | - $target_rule = 'types|' . $req_type; |
|
114 | + $target_rule = 'types|'.$req_type; |
|
115 | 115 | } |
116 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job submission QUALIFIED by MISSING rule for page type <' . $req_type . '> on path <' . $req_path . '>, new rule target is <' . $target_rule . '>', 3 ); |
|
116 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job submission QUALIFIED by MISSING rule for page type <'.$req_type.'> on path <'.$req_path.'>, new rule target is <'.$target_rule.'>', 3); |
|
117 | 117 | } else { |
118 | 118 | // Or just log a job qualified by a matching rule. |
119 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job submission QUALIFIED by AUTO rule <' . $target_rule . '> with hash <' . $rule_properties['hash'] . '> and file <' . $rule_properties['file'] . '>', 3 ); |
|
119 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job submission QUALIFIED by AUTO rule <'.$target_rule.'> with hash <'.$rule_properties['hash'].'> and file <'.$rule_properties['file'].'>', 3); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | // Submit job. |
123 | - if ( $job_qualify ) { |
|
124 | - if ( ! array_key_exists( $req_path, $ao_ccss_queue ) ) { |
|
123 | + if ($job_qualify) { |
|
124 | + if (!array_key_exists($req_path, $ao_ccss_queue)) { |
|
125 | 125 | // This is a NEW job |
126 | 126 | // Merge job into the queue. |
127 | - $ao_ccss_queue[ $req_path ] = $self->ao_ccss_define_job( |
|
127 | + $ao_ccss_queue[$req_path] = $self->ao_ccss_define_job( |
|
128 | 128 | $req_path, |
129 | 129 | $target_rule, |
130 | 130 | $req_type, |
@@ -140,30 +140,30 @@ discard block |
||
140 | 140 | } else { |
141 | 141 | // This is an existing job |
142 | 142 | // The job is still NEW, most likely this is extra CSS file for the same page that needs a hash. |
143 | - if ( 'NEW' == $ao_ccss_queue[ $req_path ]['jqstat'] ) { |
|
143 | + if ('NEW' == $ao_ccss_queue[$req_path]['jqstat']) { |
|
144 | 144 | // Add hash if it's not already in the job. |
145 | - if ( ! in_array( $hash, $ao_ccss_queue[ $req_path ]['hashes'] ) ) { |
|
145 | + if (!in_array($hash, $ao_ccss_queue[$req_path]['hashes'])) { |
|
146 | 146 | // Push new hash to its array and update flag. |
147 | - $queue_update = array_push( $ao_ccss_queue[ $req_path ]['hashes'], $hash ); |
|
147 | + $queue_update = array_push($ao_ccss_queue[$req_path]['hashes'], $hash); |
|
148 | 148 | |
149 | 149 | // Log job update. |
150 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Hashes UPDATED on local job id <' . $ao_ccss_queue[ $req_path ]['ljid'] . '>, job status NEW, target rule <' . $ao_ccss_queue[ $req_path ]['rtarget'] . '>, hash added: ' . $hash, 3 ); |
|
150 | + autoptimizeCriticalCSSCore::ao_ccss_log('Hashes UPDATED on local job id <'.$ao_ccss_queue[$req_path]['ljid'].'>, job status NEW, target rule <'.$ao_ccss_queue[$req_path]['rtarget'].'>, hash added: '.$hash, 3); |
|
151 | 151 | |
152 | 152 | // Return from here as the hash array is already updated. |
153 | 153 | return true; |
154 | 154 | } |
155 | - } elseif ( 'NEW' != $ao_ccss_queue[ $req_path ]['jqstat'] && 'JOB_QUEUED' != $ao_ccss_queue[ $req_path ]['jqstat'] && 'JOB_ONGOING' != $ao_ccss_queue[ $req_path ]['jqstat'] ) { |
|
155 | + } elseif ('NEW' != $ao_ccss_queue[$req_path]['jqstat'] && 'JOB_QUEUED' != $ao_ccss_queue[$req_path]['jqstat'] && 'JOB_ONGOING' != $ao_ccss_queue[$req_path]['jqstat']) { |
|
156 | 156 | // Allow requeuing jobs that are not NEW, JOB_QUEUED or JOB_ONGOING |
157 | 157 | // Merge new job keeping some previous job values. |
158 | - $ao_ccss_queue[ $req_path ] = $self->ao_ccss_define_job( |
|
158 | + $ao_ccss_queue[$req_path] = $self->ao_ccss_define_job( |
|
159 | 159 | $req_path, |
160 | 160 | $target_rule, |
161 | 161 | $req_type, |
162 | 162 | $hash, |
163 | - $ao_ccss_queue[ $req_path ]['file'], |
|
164 | - $ao_ccss_queue[ $req_path ]['jid'], |
|
165 | - $ao_ccss_queue[ $req_path ]['jrstat'], |
|
166 | - $ao_ccss_queue[ $req_path ]['jvstat'], |
|
163 | + $ao_ccss_queue[$req_path]['file'], |
|
164 | + $ao_ccss_queue[$req_path]['jid'], |
|
165 | + $ao_ccss_queue[$req_path]['jrstat'], |
|
166 | + $ao_ccss_queue[$req_path]['jvstat'], |
|
167 | 167 | false |
168 | 168 | ); |
169 | 169 | // Set update flag. |
@@ -171,14 +171,14 @@ discard block |
||
171 | 171 | } |
172 | 172 | } |
173 | 173 | |
174 | - if ( $queue_update ) { |
|
174 | + if ($queue_update) { |
|
175 | 175 | // Persist the job to the queue and return. |
176 | - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue ); |
|
177 | - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false ); |
|
176 | + $ao_ccss_queue_raw = json_encode($ao_ccss_queue); |
|
177 | + update_option('autoptimize_ccss_queue', $ao_ccss_queue_raw, false); |
|
178 | 178 | return true; |
179 | 179 | } else { |
180 | 180 | // Or just return false if no job was added. |
181 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'A job for path <' . $req_path . '> already exist with NEW or PENDING status, skipping job creation', 3 ); |
|
181 | + autoptimizeCriticalCSSCore::ao_ccss_log('A job for path <'.$req_path.'> already exist with NEW or PENDING status, skipping job creation', 3); |
|
182 | 182 | return false; |
183 | 183 | } |
184 | 184 | } |
@@ -194,22 +194,22 @@ discard block |
||
194 | 194 | $page_type = false; |
195 | 195 | |
196 | 196 | // Iterates over the array to match a type. |
197 | - foreach ( $ao_ccss_types as $type ) { |
|
198 | - if ( strpos( $type, 'custom_post_' ) !== false ) { |
|
197 | + foreach ($ao_ccss_types as $type) { |
|
198 | + if (strpos($type, 'custom_post_') !== false) { |
|
199 | 199 | // Match custom post types. |
200 | - if ( get_post_type( get_the_ID() ) === substr( $type, 12 ) ) { |
|
200 | + if (get_post_type(get_the_ID()) === substr($type, 12)) { |
|
201 | 201 | $page_type = $type; |
202 | 202 | break; |
203 | 203 | } |
204 | - } elseif ( strpos( $type, 'template_' ) !== false ) { |
|
204 | + } elseif (strpos($type, 'template_') !== false) { |
|
205 | 205 | // If templates; don't break, templates become manual-only rules. |
206 | 206 | } else { |
207 | 207 | // Match all other existing types |
208 | 208 | // but remove prefix to be able to check if the function exists & returns true. |
209 | - $_type = str_replace( array( 'woo_', 'bp_', 'bbp_', 'edd_' ), '', $type ); |
|
210 | - if ( function_exists( $_type ) && call_user_func( $_type ) ) { |
|
209 | + $_type = str_replace(array('woo_', 'bp_', 'bbp_', 'edd_'), '', $type); |
|
210 | + if (function_exists($_type) && call_user_func($_type)) { |
|
211 | 211 | // Make sure we only return is_front_page (and is_home) for one page, not for the "paged frontpage" (/page/2 ..). |
212 | - if ( ( 'is_front_page' !== $_type && 'is_home' !== $_type ) || ! is_paged() ) { |
|
212 | + if (('is_front_page' !== $_type && 'is_home' !== $_type) || !is_paged()) { |
|
213 | 213 | $page_type = $type; |
214 | 214 | break; |
215 | 215 | } |
@@ -221,44 +221,44 @@ discard block |
||
221 | 221 | return $page_type; |
222 | 222 | } |
223 | 223 | |
224 | - public function ao_ccss_define_job( $path, $target, $type, $hash, $file, $jid, $jrstat, $jvstat, $create ) { |
|
224 | + public function ao_ccss_define_job($path, $target, $type, $hash, $file, $jid, $jrstat, $jvstat, $create) { |
|
225 | 225 | // Define a job entry to be created or updated |
226 | 226 | // Define commom job properties. |
227 | 227 | $path = array(); |
228 | 228 | $path['ljid'] = $this->ao_ccss_job_id(); |
229 | 229 | $path['rtarget'] = $target; |
230 | 230 | $path['ptype'] = $type; |
231 | - $path['hashes'] = array( $hash ); |
|
231 | + $path['hashes'] = array($hash); |
|
232 | 232 | $path['hash'] = $hash; |
233 | 233 | $path['file'] = $file; |
234 | 234 | $path['jid'] = $jid; |
235 | 235 | $path['jqstat'] = 'NEW'; |
236 | 236 | $path['jrstat'] = $jrstat; |
237 | 237 | $path['jvstat'] = $jvstat; |
238 | - $path['jctime'] = microtime( true ); |
|
238 | + $path['jctime'] = microtime(true); |
|
239 | 239 | $path['jftime'] = null; |
240 | 240 | |
241 | 241 | // Set operation requested. |
242 | - if ( $create ) { |
|
242 | + if ($create) { |
|
243 | 243 | $operation = 'CREATED'; |
244 | 244 | } else { |
245 | 245 | $operation = 'UPDATED'; |
246 | 246 | } |
247 | 247 | |
248 | 248 | // Log job creation. |
249 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Job ' . $operation . ' with local job id <' . $path['ljid'] . '> for target rule <' . $target . '>', 3 ); |
|
249 | + autoptimizeCriticalCSSCore::ao_ccss_log('Job '.$operation.' with local job id <'.$path['ljid'].'> for target rule <'.$target.'>', 3); |
|
250 | 250 | |
251 | 251 | return $path; |
252 | 252 | } |
253 | 253 | |
254 | - public function ao_ccss_job_id( $length = 6 ) { |
|
254 | + public function ao_ccss_job_id($length = 6) { |
|
255 | 255 | // Generate random strings for the local job ID |
256 | 256 | // Based on https://stackoverflow.com/a/4356295 . |
257 | 257 | $characters = '0123456789abcdefghijklmnopqrstuvwxyz'; |
258 | - $characters_length = strlen( $characters ); |
|
258 | + $characters_length = strlen($characters); |
|
259 | 259 | $random_string = 'j-'; |
260 | - for ( $i = 0; $i < $length; $i++ ) { |
|
261 | - $random_string .= $characters[ rand( 0, $characters_length - 1 ) ]; |
|
260 | + for ($i = 0; $i < $length; $i++) { |
|
261 | + $random_string .= $characters[rand(0, $characters_length - 1)]; |
|
262 | 262 | } |
263 | 263 | return $random_string; |
264 | 264 | } |
@@ -266,17 +266,17 @@ discard block |
||
266 | 266 | public function ao_ccss_ua() { |
267 | 267 | // Check for criticalcss.com user agent. |
268 | 268 | $agent = ''; |
269 | - if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) { |
|
269 | + if (isset($_SERVER['HTTP_USER_AGENT'])) { |
|
270 | 270 | $agent = $_SERVER['HTTP_USER_AGENT']; |
271 | 271 | } |
272 | 272 | |
273 | 273 | // Check for UA and return TRUE when criticalcss.com is the detected UA, false when not. |
274 | - $rtn = strpos( $agent, AO_CCSS_URL ); |
|
275 | - if ( 0 === $rtn ) { |
|
274 | + $rtn = strpos($agent, AO_CCSS_URL); |
|
275 | + if (0 === $rtn) { |
|
276 | 276 | $rtn = true; |
277 | 277 | } else { |
278 | 278 | $rtn = false; |
279 | 279 | } |
280 | - return ( $rtn ); |
|
280 | + return ($rtn); |
|
281 | 281 | } |
282 | 282 | } |
@@ -676,6 +676,9 @@ discard block |
||
676 | 676 | return true; |
677 | 677 | } |
678 | 678 | |
679 | + /** |
|
680 | + * @param string $tag |
|
681 | + */ |
|
679 | 682 | private function movetolast( $tag ) |
680 | 683 | { |
681 | 684 | if ( empty( $tag ) ) { |
@@ -733,7 +736,7 @@ discard block |
||
733 | 736 | * @param string $filepath Filepath. |
734 | 737 | * @param bool $cache_miss Optional. Force a cache miss. Default false. |
735 | 738 | * |
736 | - * @return bool|string Url pointing to the minified js file or false. |
|
739 | + * @return false|string Url pointing to the minified js file or false. |
|
737 | 740 | */ |
738 | 741 | public function minify_single( $filepath, $cache_miss = false ) |
739 | 742 | { |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Class for JS optimization. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -198,127 +198,127 @@ discard block |
||
198 | 198 | * |
199 | 199 | * @param array $options all options. |
200 | 200 | */ |
201 | - public function read( $options ) |
|
201 | + public function read($options) |
|
202 | 202 | { |
203 | - $noptimize_js = apply_filters( 'autoptimize_filter_js_noptimize', false, $this->content ); |
|
204 | - if ( $noptimize_js ) { |
|
203 | + $noptimize_js = apply_filters('autoptimize_filter_js_noptimize', false, $this->content); |
|
204 | + if ($noptimize_js) { |
|
205 | 205 | return false; |
206 | 206 | } |
207 | 207 | |
208 | 208 | // only optimize known good JS? |
209 | - $whitelist_js = apply_filters( 'autoptimize_filter_js_whitelist', '', $this->content ); |
|
210 | - if ( ! empty( $whitelist_js ) ) { |
|
211 | - $this->whitelist = array_filter( array_map( 'trim', explode( ', ', $whitelist_js ) ) ); |
|
209 | + $whitelist_js = apply_filters('autoptimize_filter_js_whitelist', '', $this->content); |
|
210 | + if (!empty($whitelist_js)) { |
|
211 | + $this->whitelist = array_filter(array_map('trim', explode(', ', $whitelist_js))); |
|
212 | 212 | } |
213 | 213 | |
214 | 214 | // is there JS we should simply remove? |
215 | - $removable_js = apply_filters( 'autoptimize_filter_js_removables', '', $this->content ); |
|
216 | - if ( ! empty( $removable_js ) ) { |
|
217 | - $this->jsremovables = array_filter( array_map( 'trim', explode( ', ', $removable_js ) ) ); |
|
215 | + $removable_js = apply_filters('autoptimize_filter_js_removables', '', $this->content); |
|
216 | + if (!empty($removable_js)) { |
|
217 | + $this->jsremovables = array_filter(array_map('trim', explode(', ', $removable_js))); |
|
218 | 218 | } |
219 | 219 | |
220 | 220 | // only header? |
221 | - if ( apply_filters( 'autoptimize_filter_js_justhead', $options['justhead'] ) ) { |
|
222 | - $content = explode( '</head>', $this->content, 2 ); |
|
223 | - $this->content = $content[0] . '</head>'; |
|
221 | + if (apply_filters('autoptimize_filter_js_justhead', $options['justhead'])) { |
|
222 | + $content = explode('</head>', $this->content, 2); |
|
223 | + $this->content = $content[0].'</head>'; |
|
224 | 224 | $this->restofcontent = $content[1]; |
225 | 225 | } |
226 | 226 | |
227 | 227 | // Determine whether we're doing JS-files aggregation or not. |
228 | - if ( ! $options['aggregate'] ) { |
|
228 | + if (!$options['aggregate']) { |
|
229 | 229 | $this->aggregate = false; |
230 | 230 | } |
231 | 231 | // Returning true for "dontaggregate" turns off aggregation. |
232 | - if ( $this->aggregate && apply_filters( 'autoptimize_filter_js_dontaggregate', false ) ) { |
|
232 | + if ($this->aggregate && apply_filters('autoptimize_filter_js_dontaggregate', false)) { |
|
233 | 233 | $this->aggregate = false; |
234 | 234 | } |
235 | 235 | |
236 | 236 | // include inline? |
237 | - if ( apply_filters( 'autoptimize_js_include_inline', $options['include_inline'] ) ) { |
|
237 | + if (apply_filters('autoptimize_js_include_inline', $options['include_inline'])) { |
|
238 | 238 | $this->include_inline = true; |
239 | 239 | } |
240 | 240 | |
241 | 241 | // filter to "late inject minified JS", default to true for now (it is faster). |
242 | - $this->inject_min_late = apply_filters( 'autoptimize_filter_js_inject_min_late', true ); |
|
242 | + $this->inject_min_late = apply_filters('autoptimize_filter_js_inject_min_late', true); |
|
243 | 243 | |
244 | 244 | // filters to override hardcoded do(nt)move(last) array contents (array in, array out!). |
245 | - $this->dontmove = apply_filters( 'autoptimize_filter_js_dontmove', $this->dontmove ); |
|
246 | - $this->domovelast = apply_filters( 'autoptimize_filter_js_movelast', $this->domovelast ); |
|
247 | - $this->domove = apply_filters( 'autoptimize_filter_js_domove', $this->domove ); |
|
245 | + $this->dontmove = apply_filters('autoptimize_filter_js_dontmove', $this->dontmove); |
|
246 | + $this->domovelast = apply_filters('autoptimize_filter_js_movelast', $this->domovelast); |
|
247 | + $this->domove = apply_filters('autoptimize_filter_js_domove', $this->domove); |
|
248 | 248 | |
249 | 249 | // Determine whether excluded files should be minified if not yet so. |
250 | - if ( ! $options['minify_excluded'] && $options['aggregate'] ) { |
|
250 | + if (!$options['minify_excluded'] && $options['aggregate']) { |
|
251 | 251 | $this->minify_excluded = false; |
252 | 252 | } |
253 | - $this->minify_excluded = apply_filters( 'autoptimize_filter_js_minify_excluded', $this->minify_excluded, '' ); |
|
253 | + $this->minify_excluded = apply_filters('autoptimize_filter_js_minify_excluded', $this->minify_excluded, ''); |
|
254 | 254 | |
255 | 255 | // get extra exclusions settings or filter. |
256 | 256 | $exclude_js = $options['js_exclude']; |
257 | - $exclude_js = apply_filters( 'autoptimize_filter_js_exclude', $exclude_js, $this->content ); |
|
258 | - |
|
259 | - if ( '' !== $exclude_js ) { |
|
260 | - if ( is_array( $exclude_js ) ) { |
|
261 | - $remove_keys = array_keys( $exclude_js, 'remove' ); |
|
262 | - if ( false !== $remove_keys ) { |
|
263 | - foreach ( $remove_keys as $remove_key ) { |
|
264 | - unset( $exclude_js[ $remove_key ] ); |
|
257 | + $exclude_js = apply_filters('autoptimize_filter_js_exclude', $exclude_js, $this->content); |
|
258 | + |
|
259 | + if ('' !== $exclude_js) { |
|
260 | + if (is_array($exclude_js)) { |
|
261 | + $remove_keys = array_keys($exclude_js, 'remove'); |
|
262 | + if (false !== $remove_keys) { |
|
263 | + foreach ($remove_keys as $remove_key) { |
|
264 | + unset($exclude_js[$remove_key]); |
|
265 | 265 | $this->jsremovables[] = $remove_key; |
266 | 266 | } |
267 | 267 | } |
268 | - $excl_js_arr = array_keys( $exclude_js ); |
|
268 | + $excl_js_arr = array_keys($exclude_js); |
|
269 | 269 | } else { |
270 | - $excl_js_arr = array_filter( array_map( 'trim', explode( ', ', $exclude_js ) ) ); |
|
270 | + $excl_js_arr = array_filter(array_map('trim', explode(', ', $exclude_js))); |
|
271 | 271 | } |
272 | - $this->dontmove = array_merge( $excl_js_arr, $this->dontmove ); |
|
272 | + $this->dontmove = array_merge($excl_js_arr, $this->dontmove); |
|
273 | 273 | } |
274 | 274 | |
275 | 275 | // Should we add try-catch? |
276 | - if ( $options['trycatch'] ) { |
|
276 | + if ($options['trycatch']) { |
|
277 | 277 | $this->trycatch = true; |
278 | 278 | } |
279 | 279 | |
280 | 280 | // force js in head? |
281 | - if ( $options['forcehead'] ) { |
|
281 | + if ($options['forcehead']) { |
|
282 | 282 | $this->forcehead = true; |
283 | 283 | } else { |
284 | 284 | $this->forcehead = false; |
285 | 285 | } |
286 | 286 | |
287 | - $this->forcehead = apply_filters( 'autoptimize_filter_js_forcehead', $this->forcehead ); |
|
287 | + $this->forcehead = apply_filters('autoptimize_filter_js_forcehead', $this->forcehead); |
|
288 | 288 | |
289 | 289 | // get cdn url. |
290 | 290 | $this->cdn_url = $options['cdn_url']; |
291 | 291 | |
292 | 292 | // noptimize me. |
293 | - $this->content = $this->hide_noptimize( $this->content ); |
|
293 | + $this->content = $this->hide_noptimize($this->content); |
|
294 | 294 | |
295 | 295 | // Save IE hacks. |
296 | - $this->content = $this->hide_iehacks( $this->content ); |
|
296 | + $this->content = $this->hide_iehacks($this->content); |
|
297 | 297 | |
298 | 298 | // comments. |
299 | - $this->content = $this->hide_comments( $this->content ); |
|
299 | + $this->content = $this->hide_comments($this->content); |
|
300 | 300 | |
301 | 301 | // Get script files. |
302 | - if ( preg_match_all( '#<script.*</script>#Usmi', $this->content, $matches ) ) { |
|
303 | - foreach ( $matches[0] as $tag ) { |
|
302 | + if (preg_match_all('#<script.*</script>#Usmi', $this->content, $matches)) { |
|
303 | + foreach ($matches[0] as $tag) { |
|
304 | 304 | // only consider script aggregation for types whitelisted in should_aggregate-function. |
305 | - $should_aggregate = $this->should_aggregate( $tag ); |
|
306 | - if ( ! $should_aggregate ) { |
|
305 | + $should_aggregate = $this->should_aggregate($tag); |
|
306 | + if (!$should_aggregate) { |
|
307 | 307 | $tag = ''; |
308 | 308 | continue; |
309 | 309 | } |
310 | 310 | |
311 | - if ( preg_match( '#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source ) ) { |
|
311 | + if (preg_match('#<script[^>]*src=("|\')([^>]*)("|\')#Usmi', $tag, $source)) { |
|
312 | 312 | // non-inline script. |
313 | - if ( $this->isremovable( $tag, $this->jsremovables ) ) { |
|
314 | - $this->content = str_replace( $tag, '', $this->content ); |
|
313 | + if ($this->isremovable($tag, $this->jsremovables)) { |
|
314 | + $this->content = str_replace($tag, '', $this->content); |
|
315 | 315 | continue; |
316 | 316 | } |
317 | 317 | |
318 | 318 | $orig_tag = null; |
319 | - $url = current( explode( '?', $source[2], 2 ) ); |
|
320 | - $path = $this->getpath( $url ); |
|
321 | - if ( false !== $path && preg_match( '#\.js$#', $path ) && $this->ismergeable( $tag ) ) { |
|
319 | + $url = current(explode('?', $source[2], 2)); |
|
320 | + $path = $this->getpath($url); |
|
321 | + if (false !== $path && preg_match('#\.js$#', $path) && $this->ismergeable($tag)) { |
|
322 | 322 | // ok to optimize, add to array. |
323 | 323 | $this->scripts[] = $path; |
324 | 324 | } else { |
@@ -326,11 +326,11 @@ discard block |
||
326 | 326 | $new_tag = $tag; |
327 | 327 | |
328 | 328 | // non-mergeable script (excluded or dynamic or external). |
329 | - if ( is_array( $exclude_js ) ) { |
|
329 | + if (is_array($exclude_js)) { |
|
330 | 330 | // should we add flags? |
331 | - foreach ( $exclude_js as $excl_tag => $excl_flags ) { |
|
332 | - if ( false !== strpos( $orig_tag, $excl_tag ) && in_array( $excl_flags, array( 'async', 'defer' ) ) ) { |
|
333 | - $new_tag = str_replace( '<script ', '<script ' . $excl_flags . ' ', $new_tag ); |
|
331 | + foreach ($exclude_js as $excl_tag => $excl_flags) { |
|
332 | + if (false !== strpos($orig_tag, $excl_tag) && in_array($excl_flags, array('async', 'defer'))) { |
|
333 | + $new_tag = str_replace('<script ', '<script '.$excl_flags.' ', $new_tag); |
|
334 | 334 | } |
335 | 335 | } |
336 | 336 | } |
@@ -338,13 +338,13 @@ discard block |
||
338 | 338 | // Should we minify the non-aggregated script? |
339 | 339 | // -> if aggregate is on and exclude minify is on |
340 | 340 | // -> if aggregate is off and the file is not in dontmove. |
341 | - if ( $path && $this->minify_excluded ) { |
|
342 | - $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); |
|
343 | - if ( ( false === $this->aggregate && str_replace( $this->dontmove, '', $path ) === $path ) || ( true === $this->aggregate && ( false === $consider_minified_array || str_replace( $consider_minified_array, '', $path ) === $path ) ) ) { |
|
344 | - $minified_url = $this->minify_single( $path ); |
|
345 | - if ( ! empty( $minified_url ) ) { |
|
341 | + if ($path && $this->minify_excluded) { |
|
342 | + $consider_minified_array = apply_filters('autoptimize_filter_js_consider_minified', false); |
|
343 | + if ((false === $this->aggregate && str_replace($this->dontmove, '', $path) === $path) || (true === $this->aggregate && (false === $consider_minified_array || str_replace($consider_minified_array, '', $path) === $path))) { |
|
344 | + $minified_url = $this->minify_single($path); |
|
345 | + if (!empty($minified_url)) { |
|
346 | 346 | // Replace original URL with minified URL from cache. |
347 | - $new_tag = str_replace( $url, $minified_url, $new_tag ); |
|
347 | + $new_tag = str_replace($url, $minified_url, $new_tag); |
|
348 | 348 | } else { |
349 | 349 | // Remove the original script tag, because cache content is empty. |
350 | 350 | $new_tag = ''; |
@@ -352,17 +352,17 @@ discard block |
||
352 | 352 | } |
353 | 353 | } |
354 | 354 | |
355 | - if ( $this->ismovable( $new_tag ) ) { |
|
355 | + if ($this->ismovable($new_tag)) { |
|
356 | 356 | // can be moved, flags and all. |
357 | - if ( $this->movetolast( $new_tag ) ) { |
|
357 | + if ($this->movetolast($new_tag)) { |
|
358 | 358 | $this->move['last'][] = $new_tag; |
359 | 359 | } else { |
360 | 360 | $this->move['first'][] = $new_tag; |
361 | 361 | } |
362 | 362 | } else { |
363 | 363 | // cannot be moved, so if flag was added re-inject altered tag immediately. |
364 | - if ( ( '' !== $new_tag && $orig_tag !== $new_tag ) || ( '' === $new_tag && apply_filters( 'autoptimize_filter_js_remove_empty_files', false ) ) ) { |
|
365 | - $this->content = str_replace( $orig_tag, $new_tag, $this->content ); |
|
364 | + if (('' !== $new_tag && $orig_tag !== $new_tag) || ('' === $new_tag && apply_filters('autoptimize_filter_js_remove_empty_files', false))) { |
|
365 | + $this->content = str_replace($orig_tag, $new_tag, $this->content); |
|
366 | 366 | $orig_tag = ''; |
367 | 367 | } |
368 | 368 | // and forget about the $tag (not to be touched any more). |
@@ -371,23 +371,23 @@ discard block |
||
371 | 371 | } |
372 | 372 | } else { |
373 | 373 | // Inline script. |
374 | - if ( $this->isremovable( $tag, $this->jsremovables ) ) { |
|
375 | - $this->content = str_replace( $tag, '', $this->content ); |
|
374 | + if ($this->isremovable($tag, $this->jsremovables)) { |
|
375 | + $this->content = str_replace($tag, '', $this->content); |
|
376 | 376 | continue; |
377 | 377 | } |
378 | 378 | |
379 | 379 | // unhide comments, as javascript may be wrapped in comment-tags for old times' sake. |
380 | - $tag = $this->restore_comments( $tag ); |
|
381 | - if ( $this->ismergeable( $tag ) && $this->include_inline ) { |
|
382 | - preg_match( '#<script.*>(.*)</script>#Usmi', $tag, $code ); |
|
383 | - $code = preg_replace( '#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1] ); |
|
384 | - $code = preg_replace( '/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code ); |
|
385 | - $this->scripts[] = 'INLINE;' . $code; |
|
380 | + $tag = $this->restore_comments($tag); |
|
381 | + if ($this->ismergeable($tag) && $this->include_inline) { |
|
382 | + preg_match('#<script.*>(.*)</script>#Usmi', $tag, $code); |
|
383 | + $code = preg_replace('#.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*#sm', '$1', $code[1]); |
|
384 | + $code = preg_replace('/(?:^\\s*<!--\\s*|\\s*(?:\\/\\/)?\\s*-->\\s*$)/', '', $code); |
|
385 | + $this->scripts[] = 'INLINE;'.$code; |
|
386 | 386 | } else { |
387 | 387 | // Can we move this? |
388 | - $autoptimize_js_moveable = apply_filters( 'autoptimize_js_moveable', '', $tag ); |
|
389 | - if ( $this->ismovable( $tag ) || '' !== $autoptimize_js_moveable ) { |
|
390 | - if ( $this->movetolast( $tag ) || 'last' === $autoptimize_js_moveable ) { |
|
388 | + $autoptimize_js_moveable = apply_filters('autoptimize_js_moveable', '', $tag); |
|
389 | + if ($this->ismovable($tag) || '' !== $autoptimize_js_moveable) { |
|
390 | + if ($this->movetolast($tag) || 'last' === $autoptimize_js_moveable) { |
|
391 | 391 | $this->move['last'][] = $tag; |
392 | 392 | } else { |
393 | 393 | $this->move['first'][] = $tag; |
@@ -398,11 +398,11 @@ discard block |
||
398 | 398 | } |
399 | 399 | } |
400 | 400 | // Re-hide comments to be able to do the removal based on tag from $this->content. |
401 | - $tag = $this->hide_comments( $tag ); |
|
401 | + $tag = $this->hide_comments($tag); |
|
402 | 402 | } |
403 | 403 | |
404 | 404 | // Remove the original script tag. |
405 | - $this->content = str_replace( $tag, '', $this->content ); |
|
405 | + $this->content = str_replace($tag, '', $this->content); |
|
406 | 406 | } |
407 | 407 | |
408 | 408 | return true; |
@@ -427,30 +427,30 @@ discard block |
||
427 | 427 | * @param string $tag Script node & child(ren). |
428 | 428 | * @return bool |
429 | 429 | */ |
430 | - public function should_aggregate( $tag ) |
|
430 | + public function should_aggregate($tag) |
|
431 | 431 | { |
432 | - if ( empty( $tag ) ) { |
|
432 | + if (empty($tag)) { |
|
433 | 433 | return false; |
434 | 434 | } |
435 | 435 | |
436 | 436 | // We're only interested in the type attribute of the <script> tag itself, not any possible |
437 | 437 | // inline code that might just contain the 'type=' string... |
438 | 438 | $tag_parts = array(); |
439 | - preg_match( '#<(script[^>]*)>#i', $tag, $tag_parts ); |
|
439 | + preg_match('#<(script[^>]*)>#i', $tag, $tag_parts); |
|
440 | 440 | $tag_without_contents = null; |
441 | - if ( ! empty( $tag_parts[1] ) ) { |
|
441 | + if (!empty($tag_parts[1])) { |
|
442 | 442 | $tag_without_contents = $tag_parts[1]; |
443 | 443 | } |
444 | 444 | |
445 | - $has_type = ( strpos( $tag_without_contents, 'type' ) !== false ); |
|
445 | + $has_type = (strpos($tag_without_contents, 'type') !== false); |
|
446 | 446 | |
447 | 447 | $type_valid = false; |
448 | - if ( $has_type ) { |
|
449 | - $type_valid = (bool) preg_match( '/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents ); |
|
448 | + if ($has_type) { |
|
449 | + $type_valid = (bool) preg_match('/type\s*=\s*[\'"]?(?:text|application)\/(?:javascript|ecmascript)[\'"]?/i', $tag_without_contents); |
|
450 | 450 | } |
451 | 451 | |
452 | 452 | $should_aggregate = false; |
453 | - if ( ! $has_type || $type_valid ) { |
|
453 | + if (!$has_type || $type_valid) { |
|
454 | 454 | $should_aggregate = true; |
455 | 455 | } |
456 | 456 | |
@@ -462,75 +462,75 @@ discard block |
||
462 | 462 | */ |
463 | 463 | public function minify() |
464 | 464 | { |
465 | - foreach ( $this->scripts as $script ) { |
|
466 | - if ( empty( $script ) ) { |
|
465 | + foreach ($this->scripts as $script) { |
|
466 | + if (empty($script)) { |
|
467 | 467 | continue; |
468 | 468 | } |
469 | 469 | |
470 | 470 | // TODO/FIXME: some duplicate code here, can be reduced/simplified. |
471 | - if ( preg_match( '#^INLINE;#', $script ) ) { |
|
471 | + if (preg_match('#^INLINE;#', $script)) { |
|
472 | 472 | // Inline script. |
473 | - $script = preg_replace( '#^INLINE;#', '', $script ); |
|
474 | - $script = rtrim( $script, ";\n\t\r" ) . ';'; |
|
473 | + $script = preg_replace('#^INLINE;#', '', $script); |
|
474 | + $script = rtrim($script, ";\n\t\r").';'; |
|
475 | 475 | // Add try-catch? |
476 | - if ( $this->trycatch ) { |
|
477 | - $script = 'try{' . $script . '}catch(e){}'; |
|
476 | + if ($this->trycatch) { |
|
477 | + $script = 'try{'.$script.'}catch(e){}'; |
|
478 | 478 | } |
479 | - $tmpscript = apply_filters( 'autoptimize_js_individual_script', $script, '' ); |
|
480 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscript ) ) { |
|
479 | + $tmpscript = apply_filters('autoptimize_js_individual_script', $script, ''); |
|
480 | + if (has_filter('autoptimize_js_individual_script') && !empty($tmpscript)) { |
|
481 | 481 | $script = $tmpscript; |
482 | 482 | $this->alreadyminified = true; |
483 | 483 | } |
484 | - $this->jscode .= "\n" . $script; |
|
484 | + $this->jscode .= "\n".$script; |
|
485 | 485 | } else { |
486 | 486 | // External script. |
487 | - if ( false !== $script && file_exists( $script ) && is_readable( $script ) ) { |
|
488 | - $scriptsrc = file_get_contents( $script ); |
|
489 | - $scriptsrc = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc ); |
|
490 | - $scriptsrc = rtrim( $scriptsrc, ";\n\t\r" ) . ';'; |
|
487 | + if (false !== $script && file_exists($script) && is_readable($script)) { |
|
488 | + $scriptsrc = file_get_contents($script); |
|
489 | + $scriptsrc = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $scriptsrc); |
|
490 | + $scriptsrc = rtrim($scriptsrc, ";\n\t\r").';'; |
|
491 | 491 | // Add try-catch? |
492 | - if ( $this->trycatch ) { |
|
493 | - $scriptsrc = 'try{' . $scriptsrc . '}catch(e){}'; |
|
492 | + if ($this->trycatch) { |
|
493 | + $scriptsrc = 'try{'.$scriptsrc.'}catch(e){}'; |
|
494 | 494 | } |
495 | - $tmpscriptsrc = apply_filters( 'autoptimize_js_individual_script', $scriptsrc, $script ); |
|
496 | - if ( has_filter( 'autoptimize_js_individual_script' ) && ! empty( $tmpscriptsrc ) ) { |
|
495 | + $tmpscriptsrc = apply_filters('autoptimize_js_individual_script', $scriptsrc, $script); |
|
496 | + if (has_filter('autoptimize_js_individual_script') && !empty($tmpscriptsrc)) { |
|
497 | 497 | $scriptsrc = $tmpscriptsrc; |
498 | 498 | $this->alreadyminified = true; |
499 | - } elseif ( $this->can_inject_late( $script ) ) { |
|
500 | - $scriptsrc = self::build_injectlater_marker( $script, md5( $scriptsrc ) ); |
|
499 | + } elseif ($this->can_inject_late($script)) { |
|
500 | + $scriptsrc = self::build_injectlater_marker($script, md5($scriptsrc)); |
|
501 | 501 | } |
502 | - $this->jscode .= "\n" . $scriptsrc; |
|
502 | + $this->jscode .= "\n".$scriptsrc; |
|
503 | 503 | } |
504 | 504 | } |
505 | 505 | } |
506 | 506 | |
507 | 507 | // Check for already-minified code. |
508 | - $this->md5hash = md5( $this->jscode ); |
|
509 | - $ccheck = new autoptimizeCache( $this->md5hash, 'js' ); |
|
510 | - if ( $ccheck->check() ) { |
|
508 | + $this->md5hash = md5($this->jscode); |
|
509 | + $ccheck = new autoptimizeCache($this->md5hash, 'js'); |
|
510 | + if ($ccheck->check()) { |
|
511 | 511 | $this->jscode = $ccheck->retrieve(); |
512 | 512 | return true; |
513 | 513 | } |
514 | - unset( $ccheck ); |
|
514 | + unset($ccheck); |
|
515 | 515 | |
516 | 516 | // $this->jscode has all the uncompressed code now. |
517 | - if ( true !== $this->alreadyminified ) { |
|
518 | - if ( apply_filters( 'autoptimize_js_do_minify', true ) ) { |
|
519 | - $tmp_jscode = trim( JSMin::minify( $this->jscode ) ); |
|
520 | - if ( ! empty( $tmp_jscode ) ) { |
|
517 | + if (true !== $this->alreadyminified) { |
|
518 | + if (apply_filters('autoptimize_js_do_minify', true)) { |
|
519 | + $tmp_jscode = trim(JSMin::minify($this->jscode)); |
|
520 | + if (!empty($tmp_jscode)) { |
|
521 | 521 | $this->jscode = $tmp_jscode; |
522 | - unset( $tmp_jscode ); |
|
522 | + unset($tmp_jscode); |
|
523 | 523 | } |
524 | - $this->jscode = $this->inject_minified( $this->jscode ); |
|
525 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
|
524 | + $this->jscode = $this->inject_minified($this->jscode); |
|
525 | + $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode); |
|
526 | 526 | return true; |
527 | 527 | } else { |
528 | - $this->jscode = $this->inject_minified( $this->jscode ); |
|
528 | + $this->jscode = $this->inject_minified($this->jscode); |
|
529 | 529 | return false; |
530 | 530 | } |
531 | 531 | } |
532 | 532 | |
533 | - $this->jscode = apply_filters( 'autoptimize_js_after_minify', $this->jscode ); |
|
533 | + $this->jscode = apply_filters('autoptimize_js_after_minify', $this->jscode); |
|
534 | 534 | return true; |
535 | 535 | } |
536 | 536 | |
@@ -539,13 +539,13 @@ discard block |
||
539 | 539 | */ |
540 | 540 | public function cache() |
541 | 541 | { |
542 | - $cache = new autoptimizeCache( $this->md5hash, 'js' ); |
|
543 | - if ( ! $cache->check() ) { |
|
542 | + $cache = new autoptimizeCache($this->md5hash, 'js'); |
|
543 | + if (!$cache->check()) { |
|
544 | 544 | // Cache our code. |
545 | - $cache->cache( $this->jscode, 'text/javascript' ); |
|
545 | + $cache->cache($this->jscode, 'text/javascript'); |
|
546 | 546 | } |
547 | - $this->url = AUTOPTIMIZE_CACHE_URL . $cache->getname(); |
|
548 | - $this->url = $this->url_replace_cdn( $this->url ); |
|
547 | + $this->url = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
|
548 | + $this->url = $this->url_replace_cdn($this->url); |
|
549 | 549 | } |
550 | 550 | |
551 | 551 | /** |
@@ -554,47 +554,47 @@ discard block |
||
554 | 554 | public function getcontent() |
555 | 555 | { |
556 | 556 | // Restore the full content. |
557 | - if ( ! empty( $this->restofcontent ) ) { |
|
557 | + if (!empty($this->restofcontent)) { |
|
558 | 558 | $this->content .= $this->restofcontent; |
559 | 559 | $this->restofcontent = ''; |
560 | 560 | } |
561 | 561 | |
562 | 562 | // Add the scripts taking forcehead/ deferred (default) into account. |
563 | - if ( $this->forcehead ) { |
|
564 | - $replace_tag = array( '</head>', 'before' ); |
|
563 | + if ($this->forcehead) { |
|
564 | + $replace_tag = array('</head>', 'before'); |
|
565 | 565 | $defer = ''; |
566 | 566 | } else { |
567 | - $replace_tag = array( '</body>', 'before' ); |
|
567 | + $replace_tag = array('</body>', 'before'); |
|
568 | 568 | $defer = 'defer '; |
569 | 569 | } |
570 | 570 | |
571 | - $defer = apply_filters( 'autoptimize_filter_js_defer', $defer ); |
|
571 | + $defer = apply_filters('autoptimize_filter_js_defer', $defer); |
|
572 | 572 | $type_js = ''; |
573 | - if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { |
|
573 | + if (apply_filters('autoptimize_filter_cssjs_addtype', false)) { |
|
574 | 574 | $type_js = 'type="text/javascript" '; |
575 | 575 | } |
576 | 576 | |
577 | - $bodyreplacementpayload = '<script ' . $type_js . $defer . 'src="' . $this->url . '"></script>'; |
|
578 | - $bodyreplacementpayload = apply_filters( 'autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload ); |
|
577 | + $bodyreplacementpayload = '<script '.$type_js.$defer.'src="'.$this->url.'"></script>'; |
|
578 | + $bodyreplacementpayload = apply_filters('autoptimize_filter_js_bodyreplacementpayload', $bodyreplacementpayload); |
|
579 | 579 | |
580 | - $bodyreplacement = implode( '', $this->move['first'] ); |
|
580 | + $bodyreplacement = implode('', $this->move['first']); |
|
581 | 581 | $bodyreplacement .= $bodyreplacementpayload; |
582 | - $bodyreplacement .= implode( '', $this->move['last'] ); |
|
582 | + $bodyreplacement .= implode('', $this->move['last']); |
|
583 | 583 | |
584 | - $replace_tag = apply_filters( 'autoptimize_filter_js_replacetag', $replace_tag ); |
|
584 | + $replace_tag = apply_filters('autoptimize_filter_js_replacetag', $replace_tag); |
|
585 | 585 | |
586 | - if ( strlen( $this->jscode ) > 0 ) { |
|
587 | - $this->inject_in_html( $bodyreplacement, $replace_tag ); |
|
586 | + if (strlen($this->jscode) > 0) { |
|
587 | + $this->inject_in_html($bodyreplacement, $replace_tag); |
|
588 | 588 | } |
589 | 589 | |
590 | 590 | // Restore comments. |
591 | - $this->content = $this->restore_comments( $this->content ); |
|
591 | + $this->content = $this->restore_comments($this->content); |
|
592 | 592 | |
593 | 593 | // Restore IE hacks. |
594 | - $this->content = $this->restore_iehacks( $this->content ); |
|
594 | + $this->content = $this->restore_iehacks($this->content); |
|
595 | 595 | |
596 | 596 | // Restore noptimize. |
597 | - $this->content = $this->restore_noptimize( $this->content ); |
|
597 | + $this->content = $this->restore_noptimize($this->content); |
|
598 | 598 | |
599 | 599 | // Return the modified HTML. |
600 | 600 | return $this->content; |
@@ -605,34 +605,34 @@ discard block |
||
605 | 605 | * |
606 | 606 | * @param string $tag JS tag. |
607 | 607 | */ |
608 | - private function ismergeable( $tag ) |
|
608 | + private function ismergeable($tag) |
|
609 | 609 | { |
610 | - if ( empty( $tag ) || ! $this->aggregate ) { |
|
610 | + if (empty($tag) || !$this->aggregate) { |
|
611 | 611 | return false; |
612 | 612 | } |
613 | 613 | |
614 | - if ( ! empty( $this->whitelist ) ) { |
|
615 | - foreach ( $this->whitelist as $match ) { |
|
616 | - if ( false !== strpos( $tag, $match ) ) { |
|
614 | + if (!empty($this->whitelist)) { |
|
615 | + foreach ($this->whitelist as $match) { |
|
616 | + if (false !== strpos($tag, $match)) { |
|
617 | 617 | return true; |
618 | 618 | } |
619 | 619 | } |
620 | 620 | // No match with whitelist. |
621 | 621 | return false; |
622 | 622 | } else { |
623 | - foreach ( $this->domove as $match ) { |
|
624 | - if ( false !== strpos( $tag, $match ) ) { |
|
623 | + foreach ($this->domove as $match) { |
|
624 | + if (false !== strpos($tag, $match)) { |
|
625 | 625 | // Matched something. |
626 | 626 | return false; |
627 | 627 | } |
628 | 628 | } |
629 | 629 | |
630 | - if ( $this->movetolast( $tag ) ) { |
|
630 | + if ($this->movetolast($tag)) { |
|
631 | 631 | return false; |
632 | 632 | } |
633 | 633 | |
634 | - foreach ( $this->dontmove as $match ) { |
|
635 | - if ( false !== strpos( $tag, $match ) ) { |
|
634 | + foreach ($this->dontmove as $match) { |
|
635 | + if (false !== strpos($tag, $match)) { |
|
636 | 636 | // Matched something. |
637 | 637 | return false; |
638 | 638 | } |
@@ -648,25 +648,25 @@ discard block |
||
648 | 648 | * |
649 | 649 | * @param string $tag tag to check for blacklist (exclusions). |
650 | 650 | */ |
651 | - private function ismovable( $tag ) |
|
651 | + private function ismovable($tag) |
|
652 | 652 | { |
653 | - if ( empty( $tag ) || true !== $this->include_inline || apply_filters( 'autoptimize_filter_js_unmovable', true ) ) { |
|
653 | + if (empty($tag) || true !== $this->include_inline || apply_filters('autoptimize_filter_js_unmovable', true)) { |
|
654 | 654 | return false; |
655 | 655 | } |
656 | 656 | |
657 | - foreach ( $this->domove as $match ) { |
|
658 | - if ( false !== strpos( $tag, $match ) ) { |
|
657 | + foreach ($this->domove as $match) { |
|
658 | + if (false !== strpos($tag, $match)) { |
|
659 | 659 | // Matched something. |
660 | 660 | return true; |
661 | 661 | } |
662 | 662 | } |
663 | 663 | |
664 | - if ( $this->movetolast( $tag ) ) { |
|
664 | + if ($this->movetolast($tag)) { |
|
665 | 665 | return true; |
666 | 666 | } |
667 | 667 | |
668 | - foreach ( $this->dontmove as $match ) { |
|
669 | - if ( false !== strpos( $tag, $match ) ) { |
|
668 | + foreach ($this->dontmove as $match) { |
|
669 | + if (false !== strpos($tag, $match)) { |
|
670 | 670 | // Matched something. |
671 | 671 | return false; |
672 | 672 | } |
@@ -676,14 +676,14 @@ discard block |
||
676 | 676 | return true; |
677 | 677 | } |
678 | 678 | |
679 | - private function movetolast( $tag ) |
|
679 | + private function movetolast($tag) |
|
680 | 680 | { |
681 | - if ( empty( $tag ) ) { |
|
681 | + if (empty($tag)) { |
|
682 | 682 | return false; |
683 | 683 | } |
684 | 684 | |
685 | - foreach ( $this->domovelast as $match ) { |
|
686 | - if ( false !== strpos( $tag, $match ) ) { |
|
685 | + foreach ($this->domovelast as $match) { |
|
686 | + if (false !== strpos($tag, $match)) { |
|
687 | 687 | // Matched, return true. |
688 | 688 | return true; |
689 | 689 | } |
@@ -703,12 +703,12 @@ discard block |
||
703 | 703 | * @param string $js_path Path to JS file. |
704 | 704 | * @return bool |
705 | 705 | */ |
706 | - private function can_inject_late( $js_path ) { |
|
707 | - $consider_minified_array = apply_filters( 'autoptimize_filter_js_consider_minified', false ); |
|
708 | - if ( true !== $this->inject_min_late ) { |
|
706 | + private function can_inject_late($js_path) { |
|
707 | + $consider_minified_array = apply_filters('autoptimize_filter_js_consider_minified', false); |
|
708 | + if (true !== $this->inject_min_late) { |
|
709 | 709 | // late-inject turned off. |
710 | 710 | return false; |
711 | - } elseif ( ( false === strpos( $js_path, 'min.js' ) ) && ( false === strpos( $js_path, 'wp-includes/js/jquery/jquery.js' ) ) && ( str_replace( $consider_minified_array, '', $js_path ) === $js_path ) ) { |
|
711 | + } elseif ((false === strpos($js_path, 'min.js')) && (false === strpos($js_path, 'wp-includes/js/jquery/jquery.js')) && (str_replace($consider_minified_array, '', $js_path) === $js_path)) { |
|
712 | 712 | // file not minified based on filename & filter. |
713 | 713 | return false; |
714 | 714 | } else { |
@@ -735,32 +735,32 @@ discard block |
||
735 | 735 | * |
736 | 736 | * @return bool|string Url pointing to the minified js file or false. |
737 | 737 | */ |
738 | - public function minify_single( $filepath, $cache_miss = false ) |
|
738 | + public function minify_single($filepath, $cache_miss = false) |
|
739 | 739 | { |
740 | - $contents = $this->prepare_minify_single( $filepath ); |
|
740 | + $contents = $this->prepare_minify_single($filepath); |
|
741 | 741 | |
742 | - if ( empty( $contents ) ) { |
|
742 | + if (empty($contents)) { |
|
743 | 743 | return false; |
744 | 744 | } |
745 | 745 | |
746 | 746 | // Check cache. |
747 | - $hash = 'single_' . md5( $contents ); |
|
748 | - $cache = new autoptimizeCache( $hash, 'js' ); |
|
747 | + $hash = 'single_'.md5($contents); |
|
748 | + $cache = new autoptimizeCache($hash, 'js'); |
|
749 | 749 | |
750 | 750 | // If not in cache already, minify... |
751 | - if ( ! $cache->check() || $cache_miss ) { |
|
752 | - $contents = trim( JSMin::minify( $contents ) ); |
|
751 | + if (!$cache->check() || $cache_miss) { |
|
752 | + $contents = trim(JSMin::minify($contents)); |
|
753 | 753 | |
754 | 754 | // Check if minified cache content is empty. |
755 | - if ( empty( $contents ) ) { |
|
755 | + if (empty($contents)) { |
|
756 | 756 | return false; |
757 | 757 | } |
758 | 758 | |
759 | 759 | // Store in cache. |
760 | - $cache->cache( $contents, 'text/javascript' ); |
|
760 | + $cache->cache($contents, 'text/javascript'); |
|
761 | 761 | } |
762 | 762 | |
763 | - $url = $this->build_minify_single_url( $cache ); |
|
763 | + $url = $this->build_minify_single_url($cache); |
|
764 | 764 | |
765 | 765 | return $url; |
766 | 766 | } |
@@ -439,7 +439,7 @@ discard block |
||
439 | 439 | * be inlined into a data: URI. Defaults to 4096, passed through |
440 | 440 | * `autoptimize_filter_css_datauri_maxsize` filter. |
441 | 441 | * |
442 | - * @return mixed |
|
442 | + * @return integer|null |
|
443 | 443 | */ |
444 | 444 | private function get_datauri_maxsize() |
445 | 445 | { |
@@ -460,6 +460,9 @@ discard block |
||
460 | 460 | return $max_size; |
461 | 461 | } |
462 | 462 | |
463 | + /** |
|
464 | + * @param false|string $url |
|
465 | + */ |
|
463 | 466 | private function check_datauri_exclude_list( $url ) |
464 | 467 | { |
465 | 468 | static $exclude_list = null; |
@@ -485,6 +488,9 @@ discard block |
||
485 | 488 | return $matched; |
486 | 489 | } |
487 | 490 | |
491 | + /** |
|
492 | + * @param false|string $path |
|
493 | + */ |
|
488 | 494 | private function build_or_get_datauri_image( $path ) |
489 | 495 | { |
490 | 496 | /** |
@@ -914,6 +920,11 @@ discard block |
||
914 | 920 | return true; |
915 | 921 | } |
916 | 922 | |
923 | + /** |
|
924 | + * @param string $code |
|
925 | + * |
|
926 | + * @return string |
|
927 | + */ |
|
917 | 928 | public function run_minifier_on( $code ) |
918 | 929 | { |
919 | 930 | if ( ! $this->alreadyminified ) { |
@@ -1135,6 +1146,9 @@ discard block |
||
1135 | 1146 | return $code; |
1136 | 1147 | } |
1137 | 1148 | |
1149 | + /** |
|
1150 | + * @param string $tag |
|
1151 | + */ |
|
1138 | 1152 | private function ismovable( $tag ) |
1139 | 1153 | { |
1140 | 1154 | if ( ! $this->aggregate ) { |
@@ -1164,6 +1178,9 @@ discard block |
||
1164 | 1178 | } |
1165 | 1179 | } |
1166 | 1180 | |
1181 | + /** |
|
1182 | + * @param string $css |
|
1183 | + */ |
|
1167 | 1184 | private function can_inject_late( $css_path, $css ) |
1168 | 1185 | { |
1169 | 1186 | $consider_minified_array = apply_filters( 'autoptimize_filter_css_consider_minified', false, $css_path ); |
@@ -1195,7 +1212,7 @@ discard block |
||
1195 | 1212 | * @param string $filepath Filepath. |
1196 | 1213 | * @param bool $cache_miss Optional. Force a cache miss. Default false. |
1197 | 1214 | * |
1198 | - * @return bool|string Url pointing to the minified css file or false. |
|
1215 | + * @return false|string Url pointing to the minified css file or false. |
|
1199 | 1216 | */ |
1200 | 1217 | public function minify_single( $filepath, $cache_miss = false ) |
1201 | 1218 | { |
@@ -1256,6 +1273,9 @@ discard block |
||
1256 | 1273 | $this->options = $options; |
1257 | 1274 | } |
1258 | 1275 | |
1276 | + /** |
|
1277 | + * @param string $name |
|
1278 | + */ |
|
1259 | 1279 | public function setOption( $name, $value ) |
1260 | 1280 | { |
1261 | 1281 | $this->options[ $name ] = $value; |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Class for CSS optimization. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -161,53 +161,53 @@ discard block |
||
161 | 161 | * |
162 | 162 | * @param array $options all options. |
163 | 163 | */ |
164 | - public function read( $options ) |
|
164 | + public function read($options) |
|
165 | 165 | { |
166 | - $noptimize_css = apply_filters( 'autoptimize_filter_css_noptimize', false, $this->content ); |
|
167 | - if ( $noptimize_css ) { |
|
166 | + $noptimize_css = apply_filters('autoptimize_filter_css_noptimize', false, $this->content); |
|
167 | + if ($noptimize_css) { |
|
168 | 168 | return false; |
169 | 169 | } |
170 | 170 | |
171 | - $whitelist_css = apply_filters( 'autoptimize_filter_css_whitelist', '', $this->content ); |
|
172 | - if ( ! empty( $whitelist_css ) ) { |
|
173 | - $this->whitelist = array_filter( array_map( 'trim', explode( ',', $whitelist_css ) ) ); |
|
171 | + $whitelist_css = apply_filters('autoptimize_filter_css_whitelist', '', $this->content); |
|
172 | + if (!empty($whitelist_css)) { |
|
173 | + $this->whitelist = array_filter(array_map('trim', explode(',', $whitelist_css))); |
|
174 | 174 | } |
175 | 175 | |
176 | - $removable_css = apply_filters( 'autoptimize_filter_css_removables', '' ); |
|
177 | - if ( ! empty( $removable_css ) ) { |
|
178 | - $this->cssremovables = array_filter( array_map( 'trim', explode( ',', $removable_css ) ) ); |
|
176 | + $removable_css = apply_filters('autoptimize_filter_css_removables', ''); |
|
177 | + if (!empty($removable_css)) { |
|
178 | + $this->cssremovables = array_filter(array_map('trim', explode(',', $removable_css))); |
|
179 | 179 | } |
180 | 180 | |
181 | - $this->cssinlinesize = apply_filters( 'autoptimize_filter_css_inlinesize', 256 ); |
|
181 | + $this->cssinlinesize = apply_filters('autoptimize_filter_css_inlinesize', 256); |
|
182 | 182 | |
183 | 183 | // filter to "late inject minified CSS", default to true for now (it is faster). |
184 | - $this->inject_min_late = apply_filters( 'autoptimize_filter_css_inject_min_late', true ); |
|
184 | + $this->inject_min_late = apply_filters('autoptimize_filter_css_inject_min_late', true); |
|
185 | 185 | |
186 | 186 | // Remove everything that's not the header. |
187 | - if ( apply_filters( 'autoptimize_filter_css_justhead', $options['justhead'] ) ) { |
|
188 | - $content = explode( '</head>', $this->content, 2 ); |
|
189 | - $this->content = $content[0] . '</head>'; |
|
187 | + if (apply_filters('autoptimize_filter_css_justhead', $options['justhead'])) { |
|
188 | + $content = explode('</head>', $this->content, 2); |
|
189 | + $this->content = $content[0].'</head>'; |
|
190 | 190 | $this->restofcontent = $content[1]; |
191 | 191 | } |
192 | 192 | |
193 | 193 | // Determine whether we're doing CSS-files aggregation or not. |
194 | - if ( isset( $options['aggregate'] ) && ! $options['aggregate'] ) { |
|
194 | + if (isset($options['aggregate']) && !$options['aggregate']) { |
|
195 | 195 | $this->aggregate = false; |
196 | 196 | } |
197 | 197 | // Returning true for "dontaggregate" turns off aggregation. |
198 | - if ( $this->aggregate && apply_filters( 'autoptimize_filter_css_dontaggregate', false ) ) { |
|
198 | + if ($this->aggregate && apply_filters('autoptimize_filter_css_dontaggregate', false)) { |
|
199 | 199 | $this->aggregate = false; |
200 | 200 | } |
201 | 201 | |
202 | 202 | // include inline? |
203 | - if ( apply_filters( 'autoptimize_css_include_inline', $options['include_inline'] ) ) { |
|
203 | + if (apply_filters('autoptimize_css_include_inline', $options['include_inline'])) { |
|
204 | 204 | $this->include_inline = true; |
205 | 205 | } |
206 | 206 | |
207 | 207 | // List of CSS strings which are excluded from autoptimization. |
208 | - $exclude_css = apply_filters( 'autoptimize_filter_css_exclude', $options['css_exclude'], $this->content ); |
|
209 | - if ( '' !== $exclude_css ) { |
|
210 | - $this->dontmove = array_filter( array_map( 'trim', explode( ',', $exclude_css ) ) ); |
|
208 | + $exclude_css = apply_filters('autoptimize_filter_css_exclude', $options['css_exclude'], $this->content); |
|
209 | + if ('' !== $exclude_css) { |
|
210 | + $this->dontmove = array_filter(array_map('trim', explode(',', $exclude_css))); |
|
211 | 211 | } else { |
212 | 212 | $this->dontmove = array(); |
213 | 213 | } |
@@ -218,16 +218,16 @@ discard block |
||
218 | 218 | // Should we defer css? |
219 | 219 | // value: true / false. |
220 | 220 | $this->defer = $options['defer']; |
221 | - $this->defer = apply_filters( 'autoptimize_filter_css_defer', $this->defer, $this->content ); |
|
221 | + $this->defer = apply_filters('autoptimize_filter_css_defer', $this->defer, $this->content); |
|
222 | 222 | |
223 | 223 | // Should we inline while deferring? |
224 | 224 | // value: inlined CSS. |
225 | - $this->defer_inline = apply_filters( 'autoptimize_filter_css_defer_inline', $options['defer_inline'], $this->content ); |
|
225 | + $this->defer_inline = apply_filters('autoptimize_filter_css_defer_inline', $options['defer_inline'], $this->content); |
|
226 | 226 | |
227 | 227 | // Should we inline? |
228 | 228 | // value: true / false. |
229 | 229 | $this->inline = $options['inline']; |
230 | - $this->inline = apply_filters( 'autoptimize_filter_css_inline', $this->inline, $this->content ); |
|
230 | + $this->inline = apply_filters('autoptimize_filter_css_inline', $this->inline, $this->content); |
|
231 | 231 | |
232 | 232 | // Store cdn url. |
233 | 233 | $this->cdn_url = $options['cdn_url']; |
@@ -236,16 +236,16 @@ discard block |
||
236 | 236 | $this->datauris = $options['datauris']; |
237 | 237 | |
238 | 238 | // Determine whether excluded files should be minified if not yet so. |
239 | - if ( ! $options['minify_excluded'] && $options['aggregate'] ) { |
|
239 | + if (!$options['minify_excluded'] && $options['aggregate']) { |
|
240 | 240 | $this->minify_excluded = false; |
241 | 241 | } |
242 | - $this->minify_excluded = apply_filters( 'autoptimize_filter_css_minify_excluded', $this->minify_excluded, '' ); |
|
242 | + $this->minify_excluded = apply_filters('autoptimize_filter_css_minify_excluded', $this->minify_excluded, ''); |
|
243 | 243 | |
244 | 244 | // should we force all media-attributes to all? |
245 | - $this->media_force_all = apply_filters( 'autoptimize_filter_css_tagmedia_forceall', false ); |
|
245 | + $this->media_force_all = apply_filters('autoptimize_filter_css_tagmedia_forceall', false); |
|
246 | 246 | |
247 | 247 | // noptimize me. |
248 | - $this->content = $this->hide_noptimize( $this->content ); |
|
248 | + $this->content = $this->hide_noptimize($this->content); |
|
249 | 249 | |
250 | 250 | // Exclude (no)script, as those may contain CSS which should be left as is. |
251 | 251 | $this->content = $this->replace_contents_with_marker_if_exists( |
@@ -256,25 +256,25 @@ discard block |
||
256 | 256 | ); |
257 | 257 | |
258 | 258 | // Save IE hacks. |
259 | - $this->content = $this->hide_iehacks( $this->content ); |
|
259 | + $this->content = $this->hide_iehacks($this->content); |
|
260 | 260 | |
261 | 261 | // Hide HTML comments. |
262 | - $this->content = $this->hide_comments( $this->content ); |
|
262 | + $this->content = $this->hide_comments($this->content); |
|
263 | 263 | |
264 | 264 | // Get <style> and <link>. |
265 | - if ( preg_match_all( '#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches ) ) { |
|
265 | + if (preg_match_all('#(<style[^>]*>.*</style>)|(<link[^>]*stylesheet[^>]*>)#Usmi', $this->content, $matches)) { |
|
266 | 266 | |
267 | - foreach ( $matches[0] as $tag ) { |
|
268 | - if ( $this->isremovable( $tag, $this->cssremovables ) ) { |
|
269 | - $this->content = str_replace( $tag, '', $this->content ); |
|
270 | - } elseif ( $this->ismovable( $tag ) ) { |
|
267 | + foreach ($matches[0] as $tag) { |
|
268 | + if ($this->isremovable($tag, $this->cssremovables)) { |
|
269 | + $this->content = str_replace($tag, '', $this->content); |
|
270 | + } elseif ($this->ismovable($tag)) { |
|
271 | 271 | // Get the media. |
272 | - if ( false !== strpos( $tag, 'media=' ) ) { |
|
273 | - preg_match( '#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias ); |
|
274 | - $medias = explode( ',', $medias[1] ); |
|
272 | + if (false !== strpos($tag, 'media=')) { |
|
273 | + preg_match('#media=(?:"|\')([^>]*)(?:"|\')#Ui', $tag, $medias); |
|
274 | + $medias = explode(',', $medias[1]); |
|
275 | 275 | $media = array(); |
276 | - foreach ( $medias as $elem ) { |
|
277 | - if ( empty( $elem ) ) { |
|
276 | + foreach ($medias as $elem) { |
|
277 | + if (empty($elem)) { |
|
278 | 278 | $elem = 'all'; |
279 | 279 | } |
280 | 280 | |
@@ -282,67 +282,67 @@ discard block |
||
282 | 282 | } |
283 | 283 | } else { |
284 | 284 | // No media specified - applies to all. |
285 | - $media = array( 'all' ); |
|
285 | + $media = array('all'); |
|
286 | 286 | } |
287 | 287 | |
288 | 288 | // forcing media attribute to all to merge all in one file. |
289 | - if ( $this->media_force_all ) { |
|
290 | - $media = array( 'all' ); |
|
289 | + if ($this->media_force_all) { |
|
290 | + $media = array('all'); |
|
291 | 291 | } |
292 | 292 | |
293 | - $media = apply_filters( 'autoptimize_filter_css_tagmedia', $media, $tag ); |
|
293 | + $media = apply_filters('autoptimize_filter_css_tagmedia', $media, $tag); |
|
294 | 294 | |
295 | - if ( preg_match( '#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source ) ) { |
|
295 | + if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) { |
|
296 | 296 | // <link>. |
297 | - $url = current( explode( '?', $source[2], 2 ) ); |
|
298 | - $path = $this->getpath( $url ); |
|
297 | + $url = current(explode('?', $source[2], 2)); |
|
298 | + $path = $this->getpath($url); |
|
299 | 299 | |
300 | - if ( false !== $path && preg_match( '#\.css$#', $path ) ) { |
|
300 | + if (false !== $path && preg_match('#\.css$#', $path)) { |
|
301 | 301 | // Good link. |
302 | - $this->css[] = array( $media, $path ); |
|
302 | + $this->css[] = array($media, $path); |
|
303 | 303 | } else { |
304 | 304 | // Link is dynamic (.php etc). |
305 | - $new_tag = $this->optionally_defer_excluded( $tag, 'none' ); |
|
306 | - if ( '' !== $new_tag && $new_tag !== $tag ) { |
|
307 | - $this->content = str_replace( $tag, $new_tag, $this->content ); |
|
305 | + $new_tag = $this->optionally_defer_excluded($tag, 'none'); |
|
306 | + if ('' !== $new_tag && $new_tag !== $tag) { |
|
307 | + $this->content = str_replace($tag, $new_tag, $this->content); |
|
308 | 308 | } |
309 | 309 | $tag = ''; |
310 | 310 | } |
311 | 311 | } else { |
312 | 312 | // Inline css in style tags can be wrapped in comment tags, so restore comments. |
313 | - $tag = $this->restore_comments( $tag ); |
|
314 | - preg_match( '#<style.*>(.*)</style>#Usmi', $tag, $code ); |
|
313 | + $tag = $this->restore_comments($tag); |
|
314 | + preg_match('#<style.*>(.*)</style>#Usmi', $tag, $code); |
|
315 | 315 | |
316 | 316 | // And re-hide them to be able to to the removal based on tag. |
317 | - $tag = $this->hide_comments( $tag ); |
|
317 | + $tag = $this->hide_comments($tag); |
|
318 | 318 | |
319 | - if ( $this->include_inline ) { |
|
320 | - $code = preg_replace( '#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1] ); |
|
321 | - $this->css[] = array( $media, 'INLINE;' . $code ); |
|
319 | + if ($this->include_inline) { |
|
320 | + $code = preg_replace('#^.*<!\[CDATA\[(?:\s*\*/)?(.*)(?://|/\*)\s*?\]\]>.*$#sm', '$1', $code[1]); |
|
321 | + $this->css[] = array($media, 'INLINE;'.$code); |
|
322 | 322 | } else { |
323 | 323 | $tag = ''; |
324 | 324 | } |
325 | 325 | } |
326 | 326 | |
327 | 327 | // Remove the original style tag. |
328 | - $this->content = str_replace( $tag, '', $this->content ); |
|
328 | + $this->content = str_replace($tag, '', $this->content); |
|
329 | 329 | } else { |
330 | - if ( preg_match( '#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source ) ) { |
|
331 | - $exploded_url = explode( '?', $source[2], 2 ); |
|
330 | + if (preg_match('#<link.*href=("|\')(.*)("|\')#Usmi', $tag, $source)) { |
|
331 | + $exploded_url = explode('?', $source[2], 2); |
|
332 | 332 | $url = $exploded_url[0]; |
333 | - $path = $this->getpath( $url ); |
|
333 | + $path = $this->getpath($url); |
|
334 | 334 | $new_tag = $tag; |
335 | 335 | |
336 | 336 | // Excluded CSS, minify that file: |
337 | 337 | // -> if aggregate is on and exclude minify is on |
338 | 338 | // -> if aggregate is off and the file is not in dontmove. |
339 | - if ( $path && $this->minify_excluded ) { |
|
340 | - $consider_minified_array = apply_filters( 'autoptimize_filter_css_consider_minified', false ); |
|
341 | - if ( ( false === $this->aggregate && str_replace( $this->dontmove, '', $path ) === $path ) || ( true === $this->aggregate && ( false === $consider_minified_array || str_replace( $consider_minified_array, '', $path ) === $path ) ) ) { |
|
342 | - $minified_url = $this->minify_single( $path ); |
|
343 | - if ( ! empty( $minified_url ) ) { |
|
339 | + if ($path && $this->minify_excluded) { |
|
340 | + $consider_minified_array = apply_filters('autoptimize_filter_css_consider_minified', false); |
|
341 | + if ((false === $this->aggregate && str_replace($this->dontmove, '', $path) === $path) || (true === $this->aggregate && (false === $consider_minified_array || str_replace($consider_minified_array, '', $path) === $path))) { |
|
342 | + $minified_url = $this->minify_single($path); |
|
343 | + if (!empty($minified_url)) { |
|
344 | 344 | // Replace orig URL with cached minified URL. |
345 | - $new_tag = str_replace( $url, $minified_url, $tag ); |
|
345 | + $new_tag = str_replace($url, $minified_url, $tag); |
|
346 | 346 | } else { |
347 | 347 | // Remove the original style tag, because cache content is empty. |
348 | 348 | $new_tag = ''; |
@@ -350,14 +350,14 @@ discard block |
||
350 | 350 | } |
351 | 351 | } |
352 | 352 | |
353 | - if ( '' !== $new_tag ) { |
|
353 | + if ('' !== $new_tag) { |
|
354 | 354 | // Optionally defer (preload) non-aggregated CSS. |
355 | - $new_tag = $this->optionally_defer_excluded( $new_tag, $url ); |
|
355 | + $new_tag = $this->optionally_defer_excluded($new_tag, $url); |
|
356 | 356 | } |
357 | 357 | |
358 | 358 | // And replace! |
359 | - if ( ( '' !== $new_tag && $new_tag !== $tag ) || ( '' === $new_tag && apply_filters( 'autoptimize_filter_css_remove_empty_files', false ) ) ) { |
|
360 | - $this->content = str_replace( $tag, $new_tag, $this->content ); |
|
359 | + if (('' !== $new_tag && $new_tag !== $tag) || ('' === $new_tag && apply_filters('autoptimize_filter_css_remove_empty_files', false))) { |
|
360 | + $this->content = str_replace($tag, $new_tag, $this->content); |
|
361 | 361 | } |
362 | 362 | } |
363 | 363 | } |
@@ -378,10 +378,10 @@ discard block |
||
378 | 378 | * |
379 | 379 | * @return string $new_tag |
380 | 380 | */ |
381 | - private function optionally_defer_excluded( $tag, $url = '' ) |
|
381 | + private function optionally_defer_excluded($tag, $url = '') |
|
382 | 382 | { |
383 | 383 | // Defer single CSS if "inline & defer" is ON and there is inline CSS. |
384 | - if ( ! empty( $tag ) && $this->defer && ! empty( $this->defer_inline ) && apply_filters( 'autoptimize_filter_css_defer_excluded', true, $tag ) ) { |
|
384 | + if (!empty($tag) && $this->defer && !empty($this->defer_inline) && apply_filters('autoptimize_filter_css_defer_excluded', true, $tag)) { |
|
385 | 385 | // Get/ set (via filter) the JS to be triggers onload of the preloaded CSS. |
386 | 386 | $_preload_onload = apply_filters( |
387 | 387 | 'autoptimize_filter_css_preload_onload', |
@@ -390,12 +390,12 @@ discard block |
||
390 | 390 | ); |
391 | 391 | |
392 | 392 | // Adapt original <link> element for CSS to be preloaded and add <noscript>-version for fallback. |
393 | - $new_tag = '<noscript>' . autoptimizeUtils::remove_id_from_node( $tag ) . '</noscript>' . str_replace( |
|
393 | + $new_tag = '<noscript>'.autoptimizeUtils::remove_id_from_node($tag).'</noscript>'.str_replace( |
|
394 | 394 | array( |
395 | 395 | "rel='stylesheet'", |
396 | 396 | 'rel="stylesheet"', |
397 | 397 | ), |
398 | - "rel='preload' as='style' onload=\"" . $_preload_onload . '"', |
|
398 | + "rel='preload' as='style' onload=\"".$_preload_onload.'"', |
|
399 | 399 | $tag |
400 | 400 | ); |
401 | 401 | |
@@ -413,22 +413,22 @@ discard block |
||
413 | 413 | * @param string $path image path. |
414 | 414 | * @return boolean |
415 | 415 | */ |
416 | - private function is_datauri_candidate( $path ) |
|
416 | + private function is_datauri_candidate($path) |
|
417 | 417 | { |
418 | 418 | // Call only once since it's called from a loop. |
419 | 419 | static $max_size = null; |
420 | - if ( null === $max_size ) { |
|
420 | + if (null === $max_size) { |
|
421 | 421 | $max_size = $this->get_datauri_maxsize(); |
422 | 422 | } |
423 | 423 | |
424 | - if ( $path && preg_match( '#\.(jpe?g|png|gif|webp|bmp)$#i', $path ) && |
|
425 | - file_exists( $path ) && is_readable( $path ) && filesize( $path ) <= $max_size ) { |
|
424 | + if ($path && preg_match('#\.(jpe?g|png|gif|webp|bmp)$#i', $path) && |
|
425 | + file_exists($path) && is_readable($path) && filesize($path) <= $max_size) { |
|
426 | 426 | |
427 | 427 | // Seems we have a candidate. |
428 | 428 | $is_candidate = true; |
429 | 429 | } else { |
430 | 430 | // Filter allows overriding default decision (which checks for local file existence). |
431 | - $is_candidate = apply_filters( 'autoptimize_filter_css_is_datauri_candidate', false, $path ); |
|
431 | + $is_candidate = apply_filters('autoptimize_filter_css_is_datauri_candidate', false, $path); |
|
432 | 432 | } |
433 | 433 | |
434 | 434 | return $is_candidate; |
@@ -453,29 +453,29 @@ discard block |
||
453 | 453 | * of thing you're probably better of building assets completely |
454 | 454 | * outside of WordPress anyway. |
455 | 455 | */ |
456 | - if ( null === $max_size ) { |
|
457 | - $max_size = (int) apply_filters( 'autoptimize_filter_css_datauri_maxsize', 4096 ); |
|
456 | + if (null === $max_size) { |
|
457 | + $max_size = (int) apply_filters('autoptimize_filter_css_datauri_maxsize', 4096); |
|
458 | 458 | } |
459 | 459 | |
460 | 460 | return $max_size; |
461 | 461 | } |
462 | 462 | |
463 | - private function check_datauri_exclude_list( $url ) |
|
463 | + private function check_datauri_exclude_list($url) |
|
464 | 464 | { |
465 | 465 | static $exclude_list = null; |
466 | 466 | $no_datauris = array(); |
467 | 467 | |
468 | 468 | // Again, skip doing certain stuff repeatedly when loop-called. |
469 | - if ( null === $exclude_list ) { |
|
470 | - $exclude_list = apply_filters( 'autoptimize_filter_css_datauri_exclude', '' ); |
|
471 | - $no_datauris = array_filter( array_map( 'trim', explode( ',', $exclude_list ) ) ); |
|
469 | + if (null === $exclude_list) { |
|
470 | + $exclude_list = apply_filters('autoptimize_filter_css_datauri_exclude', ''); |
|
471 | + $no_datauris = array_filter(array_map('trim', explode(',', $exclude_list))); |
|
472 | 472 | } |
473 | 473 | |
474 | 474 | $matched = false; |
475 | 475 | |
476 | - if ( ! empty( $exclude_list ) ) { |
|
477 | - foreach ( $no_datauris as $no_datauri ) { |
|
478 | - if ( false !== strpos( $url, $no_datauri ) ) { |
|
476 | + if (!empty($exclude_list)) { |
|
477 | + foreach ($no_datauris as $no_datauri) { |
|
478 | + if (false !== strpos($url, $no_datauri)) { |
|
479 | 479 | $matched = true; |
480 | 480 | break; |
481 | 481 | } |
@@ -485,7 +485,7 @@ discard block |
||
485 | 485 | return $matched; |
486 | 486 | } |
487 | 487 | |
488 | - private function build_or_get_datauri_image( $path ) |
|
488 | + private function build_or_get_datauri_image($path) |
|
489 | 489 | { |
490 | 490 | /** |
491 | 491 | * TODO/FIXME: document the required return array format, or better yet, |
@@ -494,27 +494,27 @@ discard block |
||
494 | 494 | */ |
495 | 495 | |
496 | 496 | // Allows short-circuiting datauri generation for an image. |
497 | - $result = apply_filters( 'autoptimize_filter_css_datauri_image', array(), $path ); |
|
498 | - if ( ! empty( $result ) ) { |
|
499 | - if ( is_array( $result ) && isset( $result['full'] ) && isset( $result['base64data'] ) ) { |
|
497 | + $result = apply_filters('autoptimize_filter_css_datauri_image', array(), $path); |
|
498 | + if (!empty($result)) { |
|
499 | + if (is_array($result) && isset($result['full']) && isset($result['base64data'])) { |
|
500 | 500 | return $result; |
501 | 501 | } |
502 | 502 | } |
503 | 503 | |
504 | - $hash = md5( $path ); |
|
505 | - $check = new autoptimizeCache( $hash, 'img' ); |
|
506 | - if ( $check->check() ) { |
|
504 | + $hash = md5($path); |
|
505 | + $check = new autoptimizeCache($hash, 'img'); |
|
506 | + if ($check->check()) { |
|
507 | 507 | // we have the base64 image in cache. |
508 | 508 | $head_and_data = $check->retrieve(); |
509 | - $_base64data = explode( ';base64,', $head_and_data ); |
|
509 | + $_base64data = explode(';base64,', $head_and_data); |
|
510 | 510 | $base64data = $_base64data[1]; |
511 | - unset( $_base64data ); |
|
511 | + unset($_base64data); |
|
512 | 512 | } else { |
513 | 513 | // It's an image and we don't have it in cache, get the type by extension. |
514 | - $exploded_path = explode( '.', $path ); |
|
515 | - $type = end( $exploded_path ); |
|
514 | + $exploded_path = explode('.', $path); |
|
515 | + $type = end($exploded_path); |
|
516 | 516 | |
517 | - switch ( $type ) { |
|
517 | + switch ($type) { |
|
518 | 518 | case 'jpg': |
519 | 519 | case 'jpeg': |
520 | 520 | $dataurihead = 'data:image/jpeg;base64,'; |
@@ -536,13 +536,13 @@ discard block |
||
536 | 536 | } |
537 | 537 | |
538 | 538 | // Encode the data. |
539 | - $base64data = base64_encode( file_get_contents( $path ) ); |
|
540 | - $head_and_data = $dataurihead . $base64data; |
|
539 | + $base64data = base64_encode(file_get_contents($path)); |
|
540 | + $head_and_data = $dataurihead.$base64data; |
|
541 | 541 | |
542 | 542 | // Save in cache. |
543 | - $check->cache( $head_and_data, 'text/plain' ); |
|
543 | + $check->cache($head_and_data, 'text/plain'); |
|
544 | 544 | } |
545 | - unset( $check ); |
|
545 | + unset($check); |
|
546 | 546 | |
547 | 547 | return array( |
548 | 548 | 'full' => $head_and_data, |
@@ -559,13 +559,13 @@ discard block |
||
559 | 559 | * |
560 | 560 | * @return string |
561 | 561 | */ |
562 | - protected static function replace_longest_matches_first( $string, $replacements = array() ) |
|
562 | + protected static function replace_longest_matches_first($string, $replacements = array()) |
|
563 | 563 | { |
564 | - if ( ! empty( $replacements ) ) { |
|
564 | + if (!empty($replacements)) { |
|
565 | 565 | // Sort the replacements array by key length in desc order (so that the longest strings are replaced first). |
566 | - $keys = array_map( 'strlen', array_keys( $replacements ) ); |
|
567 | - array_multisort( $keys, SORT_DESC, $replacements ); |
|
568 | - $string = str_replace( array_keys( $replacements ), array_values( $replacements ), $string ); |
|
566 | + $keys = array_map('strlen', array_keys($replacements)); |
|
567 | + array_multisort($keys, SORT_DESC, $replacements); |
|
568 | + $string = str_replace(array_keys($replacements), array_values($replacements), $string); |
|
569 | 569 | } |
570 | 570 | |
571 | 571 | return $string; |
@@ -581,15 +581,15 @@ discard block |
||
581 | 581 | * |
582 | 582 | * @return string |
583 | 583 | */ |
584 | - public function replace_urls( $code = '' ) |
|
584 | + public function replace_urls($code = '') |
|
585 | 585 | { |
586 | 586 | $replacements = array(); |
587 | 587 | |
588 | - preg_match_all( self::ASSETS_REGEX, $code, $url_src_matches ); |
|
589 | - if ( is_array( $url_src_matches ) && ! empty( $url_src_matches ) ) { |
|
590 | - foreach ( $url_src_matches[1] as $count => $original_url ) { |
|
588 | + preg_match_all(self::ASSETS_REGEX, $code, $url_src_matches); |
|
589 | + if (is_array($url_src_matches) && !empty($url_src_matches)) { |
|
590 | + foreach ($url_src_matches[1] as $count => $original_url) { |
|
591 | 591 | // Removes quotes and other cruft. |
592 | - $url = trim( $original_url, " \t\n\r\0\x0B\"'" ); |
|
592 | + $url = trim($original_url, " \t\n\r\0\x0B\"'"); |
|
593 | 593 | |
594 | 594 | /** |
595 | 595 | * TODO/FIXME: Add a way for other code / callable to be called here |
@@ -607,17 +607,17 @@ discard block |
||
607 | 607 | */ |
608 | 608 | |
609 | 609 | // Do CDN replacement if needed. |
610 | - if ( ! empty( $this->cdn_url ) ) { |
|
611 | - $replacement_url = $this->url_replace_cdn( $url ); |
|
610 | + if (!empty($this->cdn_url)) { |
|
611 | + $replacement_url = $this->url_replace_cdn($url); |
|
612 | 612 | // Prepare replacements array. |
613 | - $replacements[ $url_src_matches[1][ $count ] ] = str_replace( |
|
614 | - $original_url, $replacement_url, $url_src_matches[1][ $count ] |
|
613 | + $replacements[$url_src_matches[1][$count]] = str_replace( |
|
614 | + $original_url, $replacement_url, $url_src_matches[1][$count] |
|
615 | 615 | ); |
616 | 616 | } |
617 | 617 | } |
618 | 618 | } |
619 | 619 | |
620 | - $code = self::replace_longest_matches_first( $code, $replacements ); |
|
620 | + $code = self::replace_longest_matches_first($code, $replacements); |
|
621 | 621 | |
622 | 622 | return $code; |
623 | 623 | } |
@@ -630,26 +630,26 @@ discard block |
||
630 | 630 | * @param string $code HTML being processed to hide fonts. |
631 | 631 | * @return string |
632 | 632 | */ |
633 | - public function hide_fontface_and_maybe_cdn( $code ) |
|
633 | + public function hide_fontface_and_maybe_cdn($code) |
|
634 | 634 | { |
635 | 635 | // Proceed only if @font-face declarations exist within $code. |
636 | - preg_match_all( self::FONT_FACE_REGEX, $code, $fontfaces ); |
|
637 | - if ( isset( $fontfaces[0] ) ) { |
|
636 | + preg_match_all(self::FONT_FACE_REGEX, $code, $fontfaces); |
|
637 | + if (isset($fontfaces[0])) { |
|
638 | 638 | // Check if we need to cdn fonts or not. |
639 | - $do_font_cdn = apply_filters( 'autoptimize_filter_css_fonts_cdn', false ); |
|
639 | + $do_font_cdn = apply_filters('autoptimize_filter_css_fonts_cdn', false); |
|
640 | 640 | |
641 | - foreach ( $fontfaces[0] as $full_match ) { |
|
641 | + foreach ($fontfaces[0] as $full_match) { |
|
642 | 642 | // Keep original match so we can search/replace it. |
643 | 643 | $match_search = $full_match; |
644 | 644 | |
645 | 645 | // Do font cdn if needed. |
646 | - if ( $do_font_cdn ) { |
|
647 | - $full_match = $this->replace_urls( $full_match ); |
|
646 | + if ($do_font_cdn) { |
|
647 | + $full_match = $this->replace_urls($full_match); |
|
648 | 648 | } |
649 | 649 | |
650 | 650 | // Replace declaration with its base64 encoded string. |
651 | - $replacement = self::build_marker( 'FONTFACE', $full_match ); |
|
652 | - $code = str_replace( $match_search, $replacement, $code ); |
|
651 | + $replacement = self::build_marker('FONTFACE', $full_match); |
|
652 | + $code = str_replace($match_search, $replacement, $code); |
|
653 | 653 | } |
654 | 654 | } |
655 | 655 | |
@@ -663,9 +663,9 @@ discard block |
||
663 | 663 | * @param string $code HTML being processed to unhide fonts. |
664 | 664 | * @return string |
665 | 665 | */ |
666 | - public function restore_fontface( $code ) |
|
666 | + public function restore_fontface($code) |
|
667 | 667 | { |
668 | - return $this->restore_marked_content( 'FONTFACE', $code ); |
|
668 | + return $this->restore_marked_content('FONTFACE', $code); |
|
669 | 669 | } |
670 | 670 | |
671 | 671 | /** |
@@ -674,10 +674,10 @@ discard block |
||
674 | 674 | * @param string $code HTML being processed rewrite assets. |
675 | 675 | * @return string |
676 | 676 | */ |
677 | - public function rewrite_assets( $code ) |
|
677 | + public function rewrite_assets($code) |
|
678 | 678 | { |
679 | 679 | // Handle @font-face rules by hiding and processing them separately. |
680 | - $code = $this->hide_fontface_and_maybe_cdn( $code ); |
|
680 | + $code = $this->hide_fontface_and_maybe_cdn($code); |
|
681 | 681 | |
682 | 682 | /** |
683 | 683 | * TODO/FIXME: |
@@ -694,35 +694,35 @@ discard block |
||
694 | 694 | $imgreplace = array(); |
695 | 695 | |
696 | 696 | // Matches and captures anything specified within the literal `url()` and excludes those containing data: URIs. |
697 | - preg_match_all( self::ASSETS_REGEX, $code, $url_src_matches ); |
|
698 | - if ( is_array( $url_src_matches ) && ! empty( $url_src_matches ) ) { |
|
699 | - foreach ( $url_src_matches[1] as $count => $original_url ) { |
|
697 | + preg_match_all(self::ASSETS_REGEX, $code, $url_src_matches); |
|
698 | + if (is_array($url_src_matches) && !empty($url_src_matches)) { |
|
699 | + foreach ($url_src_matches[1] as $count => $original_url) { |
|
700 | 700 | // Removes quotes and other cruft. |
701 | - $url = trim( $original_url, " \t\n\r\0\x0B\"'" ); |
|
701 | + $url = trim($original_url, " \t\n\r\0\x0B\"'"); |
|
702 | 702 | |
703 | 703 | // If datauri inlining is turned on, do it. |
704 | 704 | $inlined = false; |
705 | - if ( $this->datauris ) { |
|
705 | + if ($this->datauris) { |
|
706 | 706 | $iurl = $url; |
707 | - if ( false !== strpos( $iurl, '?' ) ) { |
|
708 | - $iurl = strtok( $iurl, '?' ); |
|
707 | + if (false !== strpos($iurl, '?')) { |
|
708 | + $iurl = strtok($iurl, '?'); |
|
709 | 709 | } |
710 | 710 | |
711 | - $ipath = $this->getpath( $iurl ); |
|
711 | + $ipath = $this->getpath($iurl); |
|
712 | 712 | |
713 | - $excluded = $this->check_datauri_exclude_list( $ipath ); |
|
714 | - if ( ! $excluded ) { |
|
715 | - $is_datauri_candidate = $this->is_datauri_candidate( $ipath ); |
|
716 | - if ( $is_datauri_candidate ) { |
|
717 | - $datauri = $this->build_or_get_datauri_image( $ipath ); |
|
713 | + $excluded = $this->check_datauri_exclude_list($ipath); |
|
714 | + if (!$excluded) { |
|
715 | + $is_datauri_candidate = $this->is_datauri_candidate($ipath); |
|
716 | + if ($is_datauri_candidate) { |
|
717 | + $datauri = $this->build_or_get_datauri_image($ipath); |
|
718 | 718 | $base64data = $datauri['base64data']; |
719 | 719 | // Add it to the list for replacement. |
720 | - $imgreplace[ $url_src_matches[1][ $count ] ] = str_replace( |
|
720 | + $imgreplace[$url_src_matches[1][$count]] = str_replace( |
|
721 | 721 | $original_url, |
722 | 722 | $datauri['full'], |
723 | - $url_src_matches[1][ $count ] |
|
723 | + $url_src_matches[1][$count] |
|
724 | 724 | ); |
725 | - $inlined = true; |
|
725 | + $inlined = true; |
|
726 | 726 | } |
727 | 727 | } |
728 | 728 | } |
@@ -733,20 +733,20 @@ discard block |
||
733 | 733 | * inlining isn't turned on, or if a resource is skipped from |
734 | 734 | * being inlined for whatever reason above. |
735 | 735 | */ |
736 | - if ( ! $inlined && ( ! empty( $this->cdn_url ) || has_filter( 'autoptimize_filter_base_replace_cdn' ) ) ) { |
|
736 | + if (!$inlined && (!empty($this->cdn_url) || has_filter('autoptimize_filter_base_replace_cdn'))) { |
|
737 | 737 | // Just do the "simple" CDN replacement. |
738 | - $replacement_url = $this->url_replace_cdn( $url ); |
|
739 | - $imgreplace[ $url_src_matches[1][ $count ] ] = str_replace( |
|
740 | - $original_url, $replacement_url, $url_src_matches[1][ $count ] |
|
738 | + $replacement_url = $this->url_replace_cdn($url); |
|
739 | + $imgreplace[$url_src_matches[1][$count]] = str_replace( |
|
740 | + $original_url, $replacement_url, $url_src_matches[1][$count] |
|
741 | 741 | ); |
742 | 742 | } |
743 | 743 | } |
744 | 744 | } |
745 | 745 | |
746 | - $code = self::replace_longest_matches_first( $code, $imgreplace ); |
|
746 | + $code = self::replace_longest_matches_first($code, $imgreplace); |
|
747 | 747 | |
748 | 748 | // Replace back font-face markers with actual font-face declarations. |
749 | - $code = $this->restore_fontface( $code ); |
|
749 | + $code = $this->restore_fontface($code); |
|
750 | 750 | |
751 | 751 | return $code; |
752 | 752 | } |
@@ -756,29 +756,29 @@ discard block |
||
756 | 756 | */ |
757 | 757 | public function minify() |
758 | 758 | { |
759 | - foreach ( $this->css as $group ) { |
|
760 | - list( $media, $css ) = $group; |
|
761 | - if ( preg_match( '#^INLINE;#', $css ) ) { |
|
759 | + foreach ($this->css as $group) { |
|
760 | + list($media, $css) = $group; |
|
761 | + if (preg_match('#^INLINE;#', $css)) { |
|
762 | 762 | // <style>. |
763 | - $css = preg_replace( '#^INLINE;#', '', $css ); |
|
764 | - $css = self::fixurls( ABSPATH . 'index.php', $css ); // ABSPATH already contains a trailing slash. |
|
765 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, '' ); |
|
766 | - if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) { |
|
763 | + $css = preg_replace('#^INLINE;#', '', $css); |
|
764 | + $css = self::fixurls(ABSPATH.'index.php', $css); // ABSPATH already contains a trailing slash. |
|
765 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, ''); |
|
766 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
767 | 767 | $css = $tmpstyle; |
768 | 768 | $this->alreadyminified = true; |
769 | 769 | } |
770 | 770 | } else { |
771 | 771 | // <link> |
772 | - if ( false !== $css && file_exists( $css ) && is_readable( $css ) ) { |
|
772 | + if (false !== $css && file_exists($css) && is_readable($css)) { |
|
773 | 773 | $css_path = $css; |
774 | - $css = self::fixurls( $css_path, file_get_contents( $css_path ) ); |
|
775 | - $css = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $css ); |
|
776 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $css, $css_path ); |
|
777 | - if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) { |
|
774 | + $css = self::fixurls($css_path, file_get_contents($css_path)); |
|
775 | + $css = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $css); |
|
776 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $css, $css_path); |
|
777 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
778 | 778 | $css = $tmpstyle; |
779 | 779 | $this->alreadyminified = true; |
780 | - } elseif ( $this->can_inject_late( $css_path, $css ) ) { |
|
781 | - $css = self::build_injectlater_marker( $css_path, md5( $css ) ); |
|
780 | + } elseif ($this->can_inject_late($css_path, $css)) { |
|
781 | + $css = self::build_injectlater_marker($css_path, md5($css)); |
|
782 | 782 | } |
783 | 783 | } else { |
784 | 784 | // Couldn't read CSS. Maybe getpath isn't working? |
@@ -786,12 +786,12 @@ discard block |
||
786 | 786 | } |
787 | 787 | } |
788 | 788 | |
789 | - foreach ( $media as $elem ) { |
|
790 | - if ( ! empty( $css ) ) { |
|
791 | - if ( ! isset( $this->csscode[ $elem ] ) ) { |
|
792 | - $this->csscode[ $elem ] = ''; |
|
789 | + foreach ($media as $elem) { |
|
790 | + if (!empty($css)) { |
|
791 | + if (!isset($this->csscode[$elem])) { |
|
792 | + $this->csscode[$elem] = ''; |
|
793 | 793 | } |
794 | - $this->csscode[ $elem ] .= "\n/*FILESTART*/" . $css; |
|
794 | + $this->csscode[$elem] .= "\n/*FILESTART*/".$css; |
|
795 | 795 | } |
796 | 796 | } |
797 | 797 | } |
@@ -799,133 +799,133 @@ discard block |
||
799 | 799 | // Check for duplicate code. |
800 | 800 | $md5list = array(); |
801 | 801 | $tmpcss = $this->csscode; |
802 | - foreach ( $tmpcss as $media => $code ) { |
|
803 | - $md5sum = md5( $code ); |
|
802 | + foreach ($tmpcss as $media => $code) { |
|
803 | + $md5sum = md5($code); |
|
804 | 804 | $medianame = $media; |
805 | - foreach ( $md5list as $med => $sum ) { |
|
805 | + foreach ($md5list as $med => $sum) { |
|
806 | 806 | // If same code. |
807 | - if ( $sum === $md5sum ) { |
|
807 | + if ($sum === $md5sum) { |
|
808 | 808 | // Add the merged code. |
809 | - $medianame = $med . ', ' . $media; |
|
810 | - $this->csscode[ $medianame ] = $code; |
|
811 | - $md5list[ $medianame ] = $md5list[ $med ]; |
|
812 | - unset( $this->csscode[ $med ], $this->csscode[ $media ], $md5list[ $med ] ); |
|
809 | + $medianame = $med.', '.$media; |
|
810 | + $this->csscode[$medianame] = $code; |
|
811 | + $md5list[$medianame] = $md5list[$med]; |
|
812 | + unset($this->csscode[$med], $this->csscode[$media], $md5list[$med]); |
|
813 | 813 | } |
814 | 814 | } |
815 | - $md5list[ $medianame ] = $md5sum; |
|
815 | + $md5list[$medianame] = $md5sum; |
|
816 | 816 | } |
817 | - unset( $tmpcss ); |
|
817 | + unset($tmpcss); |
|
818 | 818 | |
819 | 819 | // Manage @imports, while is for recursive import management. |
820 | - foreach ( $this->csscode as &$thiscss ) { |
|
820 | + foreach ($this->csscode as &$thiscss) { |
|
821 | 821 | // Flag to trigger import reconstitution and var to hold external imports. |
822 | 822 | $fiximports = false; |
823 | 823 | $external_imports = ''; |
824 | 824 | |
825 | 825 | // remove comments to avoid importing commented-out imports. |
826 | - $thiscss_nocomments = preg_replace( '#/\*.*\*/#Us', '', $thiscss ); |
|
827 | - while ( preg_match_all( '#@import +(?:url)?(?:(?:\((["\']?)(?:[^"\')]+)\1\)|(["\'])(?:[^"\']+)\2)(?:[^,;"\']+(?:,[^,;"\']+)*)?)(?:;)#mi', $thiscss_nocomments, $matches ) ) { |
|
828 | - foreach ( $matches[0] as $import ) { |
|
829 | - if ( $this->isremovable( $import, $this->cssremovables ) ) { |
|
830 | - $thiscss = str_replace( $import, '', $thiscss ); |
|
826 | + $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss); |
|
827 | + while (preg_match_all('#@import +(?:url)?(?:(?:\((["\']?)(?:[^"\')]+)\1\)|(["\'])(?:[^"\']+)\2)(?:[^,;"\']+(?:,[^,;"\']+)*)?)(?:;)#mi', $thiscss_nocomments, $matches)) { |
|
828 | + foreach ($matches[0] as $import) { |
|
829 | + if ($this->isremovable($import, $this->cssremovables)) { |
|
830 | + $thiscss = str_replace($import, '', $thiscss); |
|
831 | 831 | $import_ok = true; |
832 | 832 | } else { |
833 | - $url = trim( preg_replace( '#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim( $import ) ), " \t\n\r\0\x0B\"'" ); |
|
834 | - $path = $this->getpath( $url ); |
|
833 | + $url = trim(preg_replace('#^.*((?:https?:|ftp:)?//.*\.css).*$#', '$1', trim($import)), " \t\n\r\0\x0B\"'"); |
|
834 | + $path = $this->getpath($url); |
|
835 | 835 | $import_ok = false; |
836 | - if ( file_exists( $path ) && is_readable( $path ) ) { |
|
837 | - $code = addcslashes( self::fixurls( $path, file_get_contents( $path ) ), '\\' ); |
|
838 | - $code = preg_replace( '/\x{EF}\x{BB}\x{BF}/', '', $code ); |
|
839 | - $tmpstyle = apply_filters( 'autoptimize_css_individual_style', $code, '' ); |
|
840 | - if ( has_filter( 'autoptimize_css_individual_style' ) && ! empty( $tmpstyle ) ) { |
|
836 | + if (file_exists($path) && is_readable($path)) { |
|
837 | + $code = addcslashes(self::fixurls($path, file_get_contents($path)), '\\'); |
|
838 | + $code = preg_replace('/\x{EF}\x{BB}\x{BF}/', '', $code); |
|
839 | + $tmpstyle = apply_filters('autoptimize_css_individual_style', $code, ''); |
|
840 | + if (has_filter('autoptimize_css_individual_style') && !empty($tmpstyle)) { |
|
841 | 841 | $code = $tmpstyle; |
842 | 842 | $this->alreadyminified = true; |
843 | - } elseif ( $this->can_inject_late( $path, $code ) ) { |
|
844 | - $code = self::build_injectlater_marker( $path, md5( $code ) ); |
|
843 | + } elseif ($this->can_inject_late($path, $code)) { |
|
844 | + $code = self::build_injectlater_marker($path, md5($code)); |
|
845 | 845 | } |
846 | 846 | |
847 | - if ( ! empty( $code ) ) { |
|
848 | - $tmp_thiscss = preg_replace( '#(/\*FILESTART\*/.*)' . preg_quote( $import, '#' ) . '#Us', '/*FILESTART2*/' . $code . '$1', $thiscss ); |
|
849 | - if ( ! empty( $tmp_thiscss ) ) { |
|
847 | + if (!empty($code)) { |
|
848 | + $tmp_thiscss = preg_replace('#(/\*FILESTART\*/.*)'.preg_quote($import, '#').'#Us', '/*FILESTART2*/'.$code.'$1', $thiscss); |
|
849 | + if (!empty($tmp_thiscss)) { |
|
850 | 850 | $thiscss = $tmp_thiscss; |
851 | 851 | $import_ok = true; |
852 | - unset( $tmp_thiscss ); |
|
852 | + unset($tmp_thiscss); |
|
853 | 853 | } |
854 | 854 | } |
855 | - unset( $code ); |
|
855 | + unset($code); |
|
856 | 856 | } |
857 | 857 | } |
858 | - if ( ! $import_ok ) { |
|
858 | + if (!$import_ok) { |
|
859 | 859 | // External imports and general fall-back. |
860 | 860 | $external_imports .= $import; |
861 | 861 | |
862 | - $thiscss = str_replace( $import, '', $thiscss ); |
|
862 | + $thiscss = str_replace($import, '', $thiscss); |
|
863 | 863 | $fiximports = true; |
864 | 864 | } |
865 | 865 | } |
866 | - $thiscss = preg_replace( '#/\*FILESTART\*/#', '', $thiscss ); |
|
867 | - $thiscss = preg_replace( '#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss ); |
|
866 | + $thiscss = preg_replace('#/\*FILESTART\*/#', '', $thiscss); |
|
867 | + $thiscss = preg_replace('#/\*FILESTART2\*/#', '/*FILESTART*/', $thiscss); |
|
868 | 868 | |
869 | 869 | // and update $thiscss_nocomments before going into next iteration in while loop. |
870 | - $thiscss_nocomments = preg_replace( '#/\*.*\*/#Us', '', $thiscss ); |
|
870 | + $thiscss_nocomments = preg_replace('#/\*.*\*/#Us', '', $thiscss); |
|
871 | 871 | } |
872 | - unset( $thiscss_nocomments ); |
|
872 | + unset($thiscss_nocomments); |
|
873 | 873 | |
874 | 874 | // Add external imports to top of aggregated CSS. |
875 | - if ( $fiximports ) { |
|
876 | - $thiscss = $external_imports . $thiscss; |
|
875 | + if ($fiximports) { |
|
876 | + $thiscss = $external_imports.$thiscss; |
|
877 | 877 | } |
878 | 878 | } |
879 | - unset( $thiscss ); |
|
879 | + unset($thiscss); |
|
880 | 880 | |
881 | 881 | // $this->csscode has all the uncompressed code now. |
882 | - foreach ( $this->csscode as &$code ) { |
|
882 | + foreach ($this->csscode as &$code) { |
|
883 | 883 | // Check for already-minified code. |
884 | - $hash = md5( $code ); |
|
885 | - do_action( 'autoptimize_action_css_hash', $hash ); |
|
886 | - $ccheck = new autoptimizeCache( $hash, 'css' ); |
|
887 | - if ( $ccheck->check() ) { |
|
884 | + $hash = md5($code); |
|
885 | + do_action('autoptimize_action_css_hash', $hash); |
|
886 | + $ccheck = new autoptimizeCache($hash, 'css'); |
|
887 | + if ($ccheck->check()) { |
|
888 | 888 | $code = $ccheck->retrieve(); |
889 | - $this->hashmap[ md5( $code ) ] = $hash; |
|
889 | + $this->hashmap[md5($code)] = $hash; |
|
890 | 890 | continue; |
891 | 891 | } |
892 | - unset( $ccheck ); |
|
892 | + unset($ccheck); |
|
893 | 893 | |
894 | 894 | // Rewrite and/or inline referenced assets. |
895 | - $code = $this->rewrite_assets( $code ); |
|
895 | + $code = $this->rewrite_assets($code); |
|
896 | 896 | |
897 | 897 | // Minify. |
898 | - $code = $this->run_minifier_on( $code ); |
|
898 | + $code = $this->run_minifier_on($code); |
|
899 | 899 | |
900 | 900 | // Bring back INJECTLATER stuff. |
901 | - $code = $this->inject_minified( $code ); |
|
901 | + $code = $this->inject_minified($code); |
|
902 | 902 | |
903 | 903 | // Filter results. |
904 | - $tmp_code = apply_filters( 'autoptimize_css_after_minify', $code ); |
|
905 | - if ( ! empty( $tmp_code ) ) { |
|
904 | + $tmp_code = apply_filters('autoptimize_css_after_minify', $code); |
|
905 | + if (!empty($tmp_code)) { |
|
906 | 906 | $code = $tmp_code; |
907 | - unset( $tmp_code ); |
|
907 | + unset($tmp_code); |
|
908 | 908 | } |
909 | 909 | |
910 | - $this->hashmap[ md5( $code ) ] = $hash; |
|
910 | + $this->hashmap[md5($code)] = $hash; |
|
911 | 911 | } |
912 | 912 | |
913 | - unset( $code ); |
|
913 | + unset($code); |
|
914 | 914 | return true; |
915 | 915 | } |
916 | 916 | |
917 | - public function run_minifier_on( $code ) |
|
917 | + public function run_minifier_on($code) |
|
918 | 918 | { |
919 | - if ( ! $this->alreadyminified ) { |
|
920 | - $do_minify = apply_filters( 'autoptimize_css_do_minify', true ); |
|
919 | + if (!$this->alreadyminified) { |
|
920 | + $do_minify = apply_filters('autoptimize_css_do_minify', true); |
|
921 | 921 | |
922 | - if ( $do_minify ) { |
|
922 | + if ($do_minify) { |
|
923 | 923 | $cssmin = new autoptimizeCSSmin(); |
924 | - $tmp_code = trim( $cssmin->run( $code ) ); |
|
924 | + $tmp_code = trim($cssmin->run($code)); |
|
925 | 925 | |
926 | - if ( ! empty( $tmp_code ) ) { |
|
926 | + if (!empty($tmp_code)) { |
|
927 | 927 | $code = $tmp_code; |
928 | - unset( $tmp_code ); |
|
928 | + unset($tmp_code); |
|
929 | 929 | } |
930 | 930 | } |
931 | 931 | } |
@@ -939,18 +939,18 @@ discard block |
||
939 | 939 | public function cache() |
940 | 940 | { |
941 | 941 | // CSS cache. |
942 | - foreach ( $this->csscode as $media => $code ) { |
|
943 | - if ( empty( $code ) ) { |
|
942 | + foreach ($this->csscode as $media => $code) { |
|
943 | + if (empty($code)) { |
|
944 | 944 | continue; |
945 | 945 | } |
946 | 946 | |
947 | - $md5 = $this->hashmap[ md5( $code ) ]; |
|
948 | - $cache = new autoptimizeCache( $md5, 'css' ); |
|
949 | - if ( ! $cache->check() ) { |
|
947 | + $md5 = $this->hashmap[md5($code)]; |
|
948 | + $cache = new autoptimizeCache($md5, 'css'); |
|
949 | + if (!$cache->check()) { |
|
950 | 950 | // Cache our code. |
951 | - $cache->cache( $code, 'text/css' ); |
|
951 | + $cache->cache($code, 'text/css'); |
|
952 | 952 | } |
953 | - $this->url[ $media ] = AUTOPTIMIZE_CACHE_URL . $cache->getname(); |
|
953 | + $this->url[$media] = AUTOPTIMIZE_CACHE_URL.$cache->getname(); |
|
954 | 954 | } |
955 | 955 | } |
956 | 956 | |
@@ -960,99 +960,99 @@ discard block |
||
960 | 960 | public function getcontent() |
961 | 961 | { |
962 | 962 | // Restore the full content (only applies when "autoptimize_filter_css_justhead" filter is true). |
963 | - if ( ! empty( $this->restofcontent ) ) { |
|
963 | + if (!empty($this->restofcontent)) { |
|
964 | 964 | $this->content .= $this->restofcontent; |
965 | 965 | $this->restofcontent = ''; |
966 | 966 | } |
967 | 967 | |
968 | 968 | // type is not added by default. |
969 | 969 | $type_css = ''; |
970 | - if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { |
|
970 | + if (apply_filters('autoptimize_filter_cssjs_addtype', false)) { |
|
971 | 971 | $type_css = 'type="text/css" '; |
972 | 972 | } |
973 | 973 | |
974 | 974 | // Inject the new stylesheets. |
975 | - $replace_tag = array( '<title', 'before' ); |
|
976 | - $replace_tag = apply_filters( 'autoptimize_filter_css_replacetag', $replace_tag, $this->content ); |
|
975 | + $replace_tag = array('<title', 'before'); |
|
976 | + $replace_tag = apply_filters('autoptimize_filter_css_replacetag', $replace_tag, $this->content); |
|
977 | 977 | |
978 | - if ( $this->inline ) { |
|
979 | - foreach ( $this->csscode as $media => $code ) { |
|
980 | - $this->inject_in_html( '<style ' . $type_css . 'media="' . $media . '">' . $code . '</style>', $replace_tag ); |
|
978 | + if ($this->inline) { |
|
979 | + foreach ($this->csscode as $media => $code) { |
|
980 | + $this->inject_in_html('<style '.$type_css.'media="'.$media.'">'.$code.'</style>', $replace_tag); |
|
981 | 981 | } |
982 | 982 | } else { |
983 | - if ( $this->defer ) { |
|
983 | + if ($this->defer) { |
|
984 | 984 | $preload_css_block = ''; |
985 | 985 | $inlined_ccss_block = ''; |
986 | 986 | $noscript_css_block = '<noscript id="aonoscrcss">'; |
987 | 987 | |
988 | 988 | $defer_inline_code = $this->defer_inline; |
989 | - if ( ! empty( $defer_inline_code ) ) { |
|
990 | - if ( apply_filters( 'autoptimize_filter_css_critcss_minify', true ) ) { |
|
991 | - $icss_hash = md5( $defer_inline_code ); |
|
992 | - $icss_cache = new autoptimizeCache( $icss_hash, 'css' ); |
|
993 | - if ( $icss_cache->check() ) { |
|
989 | + if (!empty($defer_inline_code)) { |
|
990 | + if (apply_filters('autoptimize_filter_css_critcss_minify', true)) { |
|
991 | + $icss_hash = md5($defer_inline_code); |
|
992 | + $icss_cache = new autoptimizeCache($icss_hash, 'css'); |
|
993 | + if ($icss_cache->check()) { |
|
994 | 994 | // we have the optimized inline CSS in cache. |
995 | 995 | $defer_inline_code = $icss_cache->retrieve(); |
996 | 996 | } else { |
997 | 997 | $cssmin = new autoptimizeCSSmin(); |
998 | - $tmp_code = trim( $cssmin->run( $defer_inline_code ) ); |
|
998 | + $tmp_code = trim($cssmin->run($defer_inline_code)); |
|
999 | 999 | |
1000 | - if ( ! empty( $tmp_code ) ) { |
|
1000 | + if (!empty($tmp_code)) { |
|
1001 | 1001 | $defer_inline_code = $tmp_code; |
1002 | - $icss_cache->cache( $defer_inline_code, 'text/css' ); |
|
1003 | - unset( $tmp_code ); |
|
1002 | + $icss_cache->cache($defer_inline_code, 'text/css'); |
|
1003 | + unset($tmp_code); |
|
1004 | 1004 | } |
1005 | 1005 | } |
1006 | 1006 | } |
1007 | 1007 | // inlined critical css set here, but injected when full CSS is injected |
1008 | 1008 | // to avoid CSS containing SVG with <title tag receiving the full CSS link. |
1009 | - $inlined_ccss_block = '<style ' . $type_css . 'id="aoatfcss" media="all">' . $defer_inline_code . '</style>'; |
|
1009 | + $inlined_ccss_block = '<style '.$type_css.'id="aoatfcss" media="all">'.$defer_inline_code.'</style>'; |
|
1010 | 1010 | } |
1011 | 1011 | } |
1012 | 1012 | |
1013 | - foreach ( $this->url as $media => $url ) { |
|
1014 | - $url = $this->url_replace_cdn( $url ); |
|
1013 | + foreach ($this->url as $media => $url) { |
|
1014 | + $url = $this->url_replace_cdn($url); |
|
1015 | 1015 | |
1016 | 1016 | // Add the stylesheet either deferred (import at bottom) or normal links in head. |
1017 | - if ( $this->defer ) { |
|
1017 | + if ($this->defer) { |
|
1018 | 1018 | $preload_onload = autoptimizeConfig::get_ao_css_preload_onload(); |
1019 | 1019 | |
1020 | - $preload_css_block .= '<link rel="preload" as="style" media="' . $media . '" href="' . $url . '" onload="' . $preload_onload . '" />'; |
|
1021 | - $noscript_css_block .= '<link ' . $type_css . 'media="' . $media . '" href="' . $url . '" rel="stylesheet" />'; |
|
1020 | + $preload_css_block .= '<link rel="preload" as="style" media="'.$media.'" href="'.$url.'" onload="'.$preload_onload.'" />'; |
|
1021 | + $noscript_css_block .= '<link '.$type_css.'media="'.$media.'" href="'.$url.'" rel="stylesheet" />'; |
|
1022 | 1022 | } else { |
1023 | - if ( strlen( $this->csscode[ $media ] ) > $this->cssinlinesize ) { |
|
1024 | - $this->inject_in_html( '<link ' . $type_css . 'media="' . $media . '" href="' . $url . '" rel="stylesheet" />', $replace_tag ); |
|
1025 | - } elseif ( strlen( $this->csscode[ $media ] ) > 0 ) { |
|
1026 | - $this->inject_in_html( '<style ' . $type_css . 'media="' . $media . '">' . $this->csscode[ $media ] . '</style>', $replace_tag ); |
|
1023 | + if (strlen($this->csscode[$media]) > $this->cssinlinesize) { |
|
1024 | + $this->inject_in_html('<link '.$type_css.'media="'.$media.'" href="'.$url.'" rel="stylesheet" />', $replace_tag); |
|
1025 | + } elseif (strlen($this->csscode[$media]) > 0) { |
|
1026 | + $this->inject_in_html('<style '.$type_css.'media="'.$media.'">'.$this->csscode[$media].'</style>', $replace_tag); |
|
1027 | 1027 | } |
1028 | 1028 | } |
1029 | 1029 | } |
1030 | 1030 | |
1031 | - if ( $this->defer ) { |
|
1031 | + if ($this->defer) { |
|
1032 | 1032 | $preload_polyfill = autoptimizeConfig::get_ao_css_preload_polyfill(); |
1033 | 1033 | $noscript_css_block .= '</noscript>'; |
1034 | 1034 | // Inject inline critical CSS, the preloaded full CSS and the noscript-CSS. |
1035 | - $this->inject_in_html( $inlined_ccss_block . $preload_css_block . $noscript_css_block, $replace_tag ); |
|
1035 | + $this->inject_in_html($inlined_ccss_block.$preload_css_block.$noscript_css_block, $replace_tag); |
|
1036 | 1036 | |
1037 | 1037 | // Adds preload polyfill at end of body tag. |
1038 | 1038 | $this->inject_in_html( |
1039 | - apply_filters( 'autoptimize_css_preload_polyfill', $preload_polyfill ), |
|
1040 | - apply_filters( 'autoptimize_css_preload_polyfill_injectat', array( '</body>', 'before' ) ) |
|
1039 | + apply_filters('autoptimize_css_preload_polyfill', $preload_polyfill), |
|
1040 | + apply_filters('autoptimize_css_preload_polyfill_injectat', array('</body>', 'before')) |
|
1041 | 1041 | ); |
1042 | 1042 | } |
1043 | 1043 | } |
1044 | 1044 | |
1045 | 1045 | // restore comments. |
1046 | - $this->content = $this->restore_comments( $this->content ); |
|
1046 | + $this->content = $this->restore_comments($this->content); |
|
1047 | 1047 | |
1048 | 1048 | // restore IE hacks. |
1049 | - $this->content = $this->restore_iehacks( $this->content ); |
|
1049 | + $this->content = $this->restore_iehacks($this->content); |
|
1050 | 1050 | |
1051 | 1051 | // restore (no)script. |
1052 | - $this->content = $this->restore_marked_content( 'SCRIPT', $this->content ); |
|
1052 | + $this->content = $this->restore_marked_content('SCRIPT', $this->content); |
|
1053 | 1053 | |
1054 | 1054 | // Restore noptimize. |
1055 | - $this->content = $this->restore_noptimize( $this->content ); |
|
1055 | + $this->content = $this->restore_noptimize($this->content); |
|
1056 | 1056 | |
1057 | 1057 | // Return the modified stylesheet. |
1058 | 1058 | return $this->content; |
@@ -1064,44 +1064,44 @@ discard block |
||
1064 | 1064 | * @param string $file filename of optimized CSS-file. |
1065 | 1065 | * @param string $code CSS-code in which to fix URL's. |
1066 | 1066 | */ |
1067 | - static function fixurls( $file, $code ) |
|
1067 | + static function fixurls($file, $code) |
|
1068 | 1068 | { |
1069 | 1069 | // Switch all imports to the url() syntax. |
1070 | - $code = preg_replace( '#@import ("|\')(.+?)\.css.*?("|\')#', '@import url("${2}.css")', $code ); |
|
1070 | + $code = preg_replace('#@import ("|\')(.+?)\.css.*?("|\')#', '@import url("${2}.css")', $code); |
|
1071 | 1071 | |
1072 | - if ( preg_match_all( self::ASSETS_REGEX, $code, $matches ) ) { |
|
1073 | - $file = str_replace( WP_ROOT_DIR, '/', $file ); |
|
1072 | + if (preg_match_all(self::ASSETS_REGEX, $code, $matches)) { |
|
1073 | + $file = str_replace(WP_ROOT_DIR, '/', $file); |
|
1074 | 1074 | /** |
1075 | 1075 | * Rollback as per https://github.com/futtta/autoptimize/issues/94 |
1076 | 1076 | * $file = str_replace( AUTOPTIMIZE_WP_CONTENT_NAME, '', $file ); |
1077 | 1077 | */ |
1078 | - $dir = dirname( $file ); // Like /themes/expound/css. |
|
1078 | + $dir = dirname($file); // Like /themes/expound/css. |
|
1079 | 1079 | |
1080 | 1080 | /** |
1081 | 1081 | * $dir should not contain backslashes, since it's used to replace |
1082 | 1082 | * urls, but it can contain them when running on Windows because |
1083 | 1083 | * fixurls() is sometimes called with `ABSPATH . 'index.php'` |
1084 | 1084 | */ |
1085 | - $dir = str_replace( '\\', '/', $dir ); |
|
1086 | - unset( $file ); // not used below at all. |
|
1085 | + $dir = str_replace('\\', '/', $dir); |
|
1086 | + unset($file); // not used below at all. |
|
1087 | 1087 | |
1088 | 1088 | $replace = array(); |
1089 | - foreach ( $matches[1] as $k => $url ) { |
|
1089 | + foreach ($matches[1] as $k => $url) { |
|
1090 | 1090 | // Remove quotes. |
1091 | - $url = trim( $url, " \t\n\r\0\x0B\"'" ); |
|
1092 | - $no_q_url = trim( $url, "\"'" ); |
|
1093 | - if ( $url !== $no_q_url ) { |
|
1091 | + $url = trim($url, " \t\n\r\0\x0B\"'"); |
|
1092 | + $no_q_url = trim($url, "\"'"); |
|
1093 | + if ($url !== $no_q_url) { |
|
1094 | 1094 | $removed_quotes = true; |
1095 | 1095 | } else { |
1096 | 1096 | $removed_quotes = false; |
1097 | 1097 | } |
1098 | 1098 | |
1099 | - if ( '' === $no_q_url ) { |
|
1099 | + if ('' === $no_q_url) { |
|
1100 | 1100 | continue; |
1101 | 1101 | } |
1102 | 1102 | |
1103 | 1103 | $url = $no_q_url; |
1104 | - if ( '/' === $url[0] || preg_match( '#^(https?://|ftp://|data:)#i', $url ) ) { |
|
1104 | + if ('/' === $url[0] || preg_match('#^(https?://|ftp://|data:)#i', $url)) { |
|
1105 | 1105 | // URL is protocol-relative, host-relative or something we don't touch. |
1106 | 1106 | continue; |
1107 | 1107 | } else { // Relative URL. |
@@ -1110,49 +1110,49 @@ discard block |
||
1110 | 1110 | * rollback as per https://github.com/futtta/autoptimize/issues/94 |
1111 | 1111 | * $newurl = preg_replace( '/https?:/', '', str_replace( ' ', '%20', AUTOPTIMIZE_WP_CONTENT_URL . str_replace( '//', '/', $dir . '/' . $url ) ) ); |
1112 | 1112 | */ |
1113 | - $newurl = preg_replace( '/https?:/', '', str_replace( ' ', '%20', AUTOPTIMIZE_WP_ROOT_URL . str_replace( '//', '/', $dir . '/' . $url ) ) ); |
|
1114 | - $newurl = apply_filters( 'autoptimize_filter_css_fixurl_newurl', $newurl ); |
|
1113 | + $newurl = preg_replace('/https?:/', '', str_replace(' ', '%20', AUTOPTIMIZE_WP_ROOT_URL.str_replace('//', '/', $dir.'/'.$url))); |
|
1114 | + $newurl = apply_filters('autoptimize_filter_css_fixurl_newurl', $newurl); |
|
1115 | 1115 | |
1116 | 1116 | /** |
1117 | 1117 | * Hash the url + whatever was behind potentially for replacement |
1118 | 1118 | * We must do this, or different css classes referencing the same bg image (but |
1119 | 1119 | * different parts of it, say, in sprites and such) loose their stuff... |
1120 | 1120 | */ |
1121 | - $hash = md5( $url . $matches[2][ $k ] ); |
|
1122 | - $code = str_replace( $matches[0][ $k ], $hash, $code ); |
|
1121 | + $hash = md5($url.$matches[2][$k]); |
|
1122 | + $code = str_replace($matches[0][$k], $hash, $code); |
|
1123 | 1123 | |
1124 | - if ( $removed_quotes ) { |
|
1125 | - $replace[ $hash ] = "url('" . $newurl . "')" . $matches[2][ $k ]; |
|
1124 | + if ($removed_quotes) { |
|
1125 | + $replace[$hash] = "url('".$newurl."')".$matches[2][$k]; |
|
1126 | 1126 | } else { |
1127 | - $replace[ $hash ] = 'url(' . $newurl . ')' . $matches[2][ $k ]; |
|
1127 | + $replace[$hash] = 'url('.$newurl.')'.$matches[2][$k]; |
|
1128 | 1128 | } |
1129 | 1129 | } |
1130 | 1130 | } |
1131 | 1131 | |
1132 | - $code = self::replace_longest_matches_first( $code, $replace ); |
|
1132 | + $code = self::replace_longest_matches_first($code, $replace); |
|
1133 | 1133 | } |
1134 | 1134 | |
1135 | 1135 | return $code; |
1136 | 1136 | } |
1137 | 1137 | |
1138 | - private function ismovable( $tag ) |
|
1138 | + private function ismovable($tag) |
|
1139 | 1139 | { |
1140 | - if ( ! $this->aggregate ) { |
|
1140 | + if (!$this->aggregate) { |
|
1141 | 1141 | return false; |
1142 | 1142 | } |
1143 | 1143 | |
1144 | - if ( ! empty( $this->whitelist ) ) { |
|
1145 | - foreach ( $this->whitelist as $match ) { |
|
1146 | - if ( false !== strpos( $tag, $match ) ) { |
|
1144 | + if (!empty($this->whitelist)) { |
|
1145 | + foreach ($this->whitelist as $match) { |
|
1146 | + if (false !== strpos($tag, $match)) { |
|
1147 | 1147 | return true; |
1148 | 1148 | } |
1149 | 1149 | } |
1150 | 1150 | // no match with whitelist. |
1151 | 1151 | return false; |
1152 | 1152 | } else { |
1153 | - if ( is_array( $this->dontmove ) && ! empty( $this->dontmove ) ) { |
|
1154 | - foreach ( $this->dontmove as $match ) { |
|
1155 | - if ( false !== strpos( $tag, $match ) ) { |
|
1153 | + if (is_array($this->dontmove) && !empty($this->dontmove)) { |
|
1154 | + foreach ($this->dontmove as $match) { |
|
1155 | + if (false !== strpos($tag, $match)) { |
|
1156 | 1156 | // Matched something. |
1157 | 1157 | return false; |
1158 | 1158 | } |
@@ -1164,22 +1164,22 @@ discard block |
||
1164 | 1164 | } |
1165 | 1165 | } |
1166 | 1166 | |
1167 | - private function can_inject_late( $css_path, $css ) |
|
1167 | + private function can_inject_late($css_path, $css) |
|
1168 | 1168 | { |
1169 | - $consider_minified_array = apply_filters( 'autoptimize_filter_css_consider_minified', false, $css_path ); |
|
1170 | - if ( true !== $this->inject_min_late ) { |
|
1169 | + $consider_minified_array = apply_filters('autoptimize_filter_css_consider_minified', false, $css_path); |
|
1170 | + if (true !== $this->inject_min_late) { |
|
1171 | 1171 | // late-inject turned off. |
1172 | 1172 | return false; |
1173 | - } elseif ( ( false === strpos( $css_path, 'min.css' ) ) && ( str_replace( $consider_minified_array, '', $css_path ) === $css_path ) ) { |
|
1173 | + } elseif ((false === strpos($css_path, 'min.css')) && (str_replace($consider_minified_array, '', $css_path) === $css_path)) { |
|
1174 | 1174 | // file not minified based on filename & filter. |
1175 | 1175 | return false; |
1176 | - } elseif ( false !== strpos( $css, '@import' ) ) { |
|
1176 | + } elseif (false !== strpos($css, '@import')) { |
|
1177 | 1177 | // can't late-inject files with imports as those need to be aggregated. |
1178 | 1178 | return false; |
1179 | - } elseif ( ( false !== strpos( $css, '@font-face' ) ) && ( apply_filters( 'autoptimize_filter_css_fonts_cdn', false ) === true ) && ( ! empty( $this->cdn_url ) ) ) { |
|
1179 | + } elseif ((false !== strpos($css, '@font-face')) && (apply_filters('autoptimize_filter_css_fonts_cdn', false) === true) && (!empty($this->cdn_url))) { |
|
1180 | 1180 | // don't late-inject CSS with font-src's if fonts are set to be CDN'ed. |
1181 | 1181 | return false; |
1182 | - } elseif ( ( ( true == $this->datauris ) || ( ! empty( $this->cdn_url ) ) ) && preg_match( '#background[^;}]*url\(#Ui', $css ) ) { |
|
1182 | + } elseif (((true == $this->datauris) || (!empty($this->cdn_url))) && preg_match('#background[^;}]*url\(#Ui', $css)) { |
|
1183 | 1183 | // don't late-inject CSS with images if CDN is set OR if image inlining is on. |
1184 | 1184 | return false; |
1185 | 1185 | } else { |
@@ -1197,41 +1197,41 @@ discard block |
||
1197 | 1197 | * |
1198 | 1198 | * @return bool|string Url pointing to the minified css file or false. |
1199 | 1199 | */ |
1200 | - public function minify_single( $filepath, $cache_miss = false ) |
|
1200 | + public function minify_single($filepath, $cache_miss = false) |
|
1201 | 1201 | { |
1202 | - $contents = $this->prepare_minify_single( $filepath ); |
|
1202 | + $contents = $this->prepare_minify_single($filepath); |
|
1203 | 1203 | |
1204 | - if ( empty( $contents ) ) { |
|
1204 | + if (empty($contents)) { |
|
1205 | 1205 | return false; |
1206 | 1206 | } |
1207 | 1207 | |
1208 | 1208 | // Check cache. |
1209 | - $hash = 'single_' . md5( $contents ); |
|
1210 | - $cache = new autoptimizeCache( $hash, 'css' ); |
|
1211 | - do_action( 'autoptimize_action_css_hash', $hash ); |
|
1209 | + $hash = 'single_'.md5($contents); |
|
1210 | + $cache = new autoptimizeCache($hash, 'css'); |
|
1211 | + do_action('autoptimize_action_css_hash', $hash); |
|
1212 | 1212 | |
1213 | 1213 | // If not in cache already, minify... |
1214 | - if ( ! $cache->check() || $cache_miss ) { |
|
1214 | + if (!$cache->check() || $cache_miss) { |
|
1215 | 1215 | // Fixurls... |
1216 | - $contents = self::fixurls( $filepath, $contents ); |
|
1216 | + $contents = self::fixurls($filepath, $contents); |
|
1217 | 1217 | // CDN-replace any referenced assets if needed... |
1218 | - $contents = $this->hide_fontface_and_maybe_cdn( $contents ); |
|
1219 | - $contents = $this->replace_urls( $contents ); |
|
1220 | - $contents = $this->restore_fontface( $contents ); |
|
1218 | + $contents = $this->hide_fontface_and_maybe_cdn($contents); |
|
1219 | + $contents = $this->replace_urls($contents); |
|
1220 | + $contents = $this->restore_fontface($contents); |
|
1221 | 1221 | // Now minify... |
1222 | 1222 | $cssmin = new autoptimizeCSSmin(); |
1223 | - $contents = trim( $cssmin->run( $contents ) ); |
|
1223 | + $contents = trim($cssmin->run($contents)); |
|
1224 | 1224 | |
1225 | 1225 | // Check if minified cache content is empty. |
1226 | - if ( empty( $contents ) ) { |
|
1226 | + if (empty($contents)) { |
|
1227 | 1227 | return false; |
1228 | 1228 | } |
1229 | 1229 | |
1230 | 1230 | // Store in cache. |
1231 | - $cache->cache( $contents, 'text/css' ); |
|
1231 | + $cache->cache($contents, 'text/css'); |
|
1232 | 1232 | } |
1233 | 1233 | |
1234 | - $url = $this->build_minify_single_url( $cache ); |
|
1234 | + $url = $this->build_minify_single_url($cache); |
|
1235 | 1235 | |
1236 | 1236 | return $url; |
1237 | 1237 | } |
@@ -1251,19 +1251,19 @@ discard block |
||
1251 | 1251 | return $this->options; |
1252 | 1252 | } |
1253 | 1253 | |
1254 | - public function replaceOptions( $options ) |
|
1254 | + public function replaceOptions($options) |
|
1255 | 1255 | { |
1256 | 1256 | $this->options = $options; |
1257 | 1257 | } |
1258 | 1258 | |
1259 | - public function setOption( $name, $value ) |
|
1259 | + public function setOption($name, $value) |
|
1260 | 1260 | { |
1261 | - $this->options[ $name ] = $value; |
|
1261 | + $this->options[$name] = $value; |
|
1262 | 1262 | $this->$name = $value; |
1263 | 1263 | } |
1264 | 1264 | |
1265 | - public function getOption( $name ) |
|
1265 | + public function getOption($name) |
|
1266 | 1266 | { |
1267 | - return $this->options[ $name ]; |
|
1267 | + return $this->options[$name]; |
|
1268 | 1268 | } |
1269 | 1269 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Wraps base plugin logic/hooks and handles activation/deactivation/uninstall. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -33,7 +33,7 @@ discard block |
||
33 | 33 | * @param string $version Version. |
34 | 34 | * @param string $filepath Filepath. Needed for activation/deactivation/uninstall hooks. |
35 | 35 | */ |
36 | - public function __construct( $version, $filepath ) |
|
36 | + public function __construct($version, $filepath) |
|
37 | 37 | { |
38 | 38 | $this->version = $version; |
39 | 39 | $this->filepath = $filepath; |
@@ -50,102 +50,102 @@ discard block |
||
50 | 50 | |
51 | 51 | protected function add_hooks() |
52 | 52 | { |
53 | - if ( ! defined( 'AUTOPTIMIZE_SETUP_INITHOOK' ) ) { |
|
54 | - define( 'AUTOPTIMIZE_SETUP_INITHOOK', 'plugins_loaded' ); |
|
53 | + if (!defined('AUTOPTIMIZE_SETUP_INITHOOK')) { |
|
54 | + define('AUTOPTIMIZE_SETUP_INITHOOK', 'plugins_loaded'); |
|
55 | 55 | } |
56 | 56 | |
57 | - add_action( AUTOPTIMIZE_SETUP_INITHOOK, array( $this, 'setup' ) ); |
|
58 | - add_action( AUTOPTIMIZE_SETUP_INITHOOK, array( $this, 'hook_page_cache_purge' ) ); |
|
57 | + add_action(AUTOPTIMIZE_SETUP_INITHOOK, array($this, 'setup')); |
|
58 | + add_action(AUTOPTIMIZE_SETUP_INITHOOK, array($this, 'hook_page_cache_purge')); |
|
59 | 59 | |
60 | - add_action( 'autoptimize_setup_done', array( $this, 'version_upgrades_check' ) ); |
|
61 | - add_action( 'autoptimize_setup_done', array( $this, 'check_cache_and_run' ) ); |
|
62 | - add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_ao_extra' ), 15 ); |
|
63 | - add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_partners_tab' ), 20 ); |
|
64 | - add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_criticalcss' ), 11 ); |
|
65 | - add_action( 'autoptimize_setup_done', array( $this, 'maybe_run_notfound_fallback' ), 10 ); |
|
60 | + add_action('autoptimize_setup_done', array($this, 'version_upgrades_check')); |
|
61 | + add_action('autoptimize_setup_done', array($this, 'check_cache_and_run')); |
|
62 | + add_action('autoptimize_setup_done', array($this, 'maybe_run_ao_extra'), 15); |
|
63 | + add_action('autoptimize_setup_done', array($this, 'maybe_run_partners_tab'), 20); |
|
64 | + add_action('autoptimize_setup_done', array($this, 'maybe_run_criticalcss'), 11); |
|
65 | + add_action('autoptimize_setup_done', array($this, 'maybe_run_notfound_fallback'), 10); |
|
66 | 66 | |
67 | - add_action( 'init', array( $this, 'load_textdomain' ) ); |
|
68 | - add_action( 'admin_init', array( 'PAnD', 'init' ) ); |
|
67 | + add_action('init', array($this, 'load_textdomain')); |
|
68 | + add_action('admin_init', array('PAnD', 'init')); |
|
69 | 69 | |
70 | - if ( is_multisite() && is_admin() ) { |
|
70 | + if (is_multisite() && is_admin()) { |
|
71 | 71 | // Only if multisite and if in admin we want to check if we need to save options on network level. |
72 | - add_action( 'init', 'autoptimizeOptionWrapper::check_multisite_on_saving_options' ); |
|
72 | + add_action('init', 'autoptimizeOptionWrapper::check_multisite_on_saving_options'); |
|
73 | 73 | } |
74 | 74 | |
75 | 75 | // register uninstall & deactivation hooks. |
76 | - register_uninstall_hook( $this->filepath, 'autoptimizeMain::on_uninstall' ); |
|
77 | - register_deactivation_hook( $this->filepath, 'autoptimizeMain::on_deactivation' ); |
|
76 | + register_uninstall_hook($this->filepath, 'autoptimizeMain::on_uninstall'); |
|
77 | + register_deactivation_hook($this->filepath, 'autoptimizeMain::on_deactivation'); |
|
78 | 78 | } |
79 | 79 | |
80 | 80 | public function load_textdomain() |
81 | 81 | { |
82 | - load_plugin_textdomain( 'autoptimize' ); |
|
82 | + load_plugin_textdomain('autoptimize'); |
|
83 | 83 | } |
84 | 84 | |
85 | 85 | public function setup() |
86 | 86 | { |
87 | 87 | // Do we gzip in php when caching or is the webserver doing it? |
88 | - define( 'AUTOPTIMIZE_CACHE_NOGZIP', (bool) autoptimizeOptionWrapper::get_option( 'autoptimize_cache_nogzip' ) ); |
|
88 | + define('AUTOPTIMIZE_CACHE_NOGZIP', (bool) autoptimizeOptionWrapper::get_option('autoptimize_cache_nogzip')); |
|
89 | 89 | |
90 | 90 | // These can be overridden by specifying them in wp-config.php or such. |
91 | - if ( ! defined( 'AUTOPTIMIZE_WP_CONTENT_NAME' ) ) { |
|
92 | - define( 'AUTOPTIMIZE_WP_CONTENT_NAME', '/' . wp_basename( WP_CONTENT_DIR ) ); |
|
91 | + if (!defined('AUTOPTIMIZE_WP_CONTENT_NAME')) { |
|
92 | + define('AUTOPTIMIZE_WP_CONTENT_NAME', '/'.wp_basename(WP_CONTENT_DIR)); |
|
93 | 93 | } |
94 | - if ( ! defined( 'AUTOPTIMIZE_CACHE_CHILD_DIR' ) ) { |
|
95 | - define( 'AUTOPTIMIZE_CACHE_CHILD_DIR', '/cache/autoptimize/' ); |
|
94 | + if (!defined('AUTOPTIMIZE_CACHE_CHILD_DIR')) { |
|
95 | + define('AUTOPTIMIZE_CACHE_CHILD_DIR', '/cache/autoptimize/'); |
|
96 | 96 | } |
97 | - if ( ! defined( 'AUTOPTIMIZE_CACHEFILE_PREFIX' ) ) { |
|
98 | - define( 'AUTOPTIMIZE_CACHEFILE_PREFIX', 'autoptimize_' ); |
|
97 | + if (!defined('AUTOPTIMIZE_CACHEFILE_PREFIX')) { |
|
98 | + define('AUTOPTIMIZE_CACHEFILE_PREFIX', 'autoptimize_'); |
|
99 | 99 | } |
100 | 100 | // Note: trailing slash is not optional! |
101 | - if ( ! defined( 'AUTOPTIMIZE_CACHE_DIR' ) ) { |
|
102 | - define( 'AUTOPTIMIZE_CACHE_DIR', autoptimizeCache::get_pathname() ); |
|
101 | + if (!defined('AUTOPTIMIZE_CACHE_DIR')) { |
|
102 | + define('AUTOPTIMIZE_CACHE_DIR', autoptimizeCache::get_pathname()); |
|
103 | 103 | } |
104 | 104 | |
105 | - define( 'WP_ROOT_DIR', substr( WP_CONTENT_DIR, 0, strlen( WP_CONTENT_DIR ) - strlen( AUTOPTIMIZE_WP_CONTENT_NAME ) ) ); |
|
105 | + define('WP_ROOT_DIR', substr(WP_CONTENT_DIR, 0, strlen(WP_CONTENT_DIR) - strlen(AUTOPTIMIZE_WP_CONTENT_NAME))); |
|
106 | 106 | |
107 | - if ( ! defined( 'AUTOPTIMIZE_WP_SITE_URL' ) ) { |
|
108 | - if ( function_exists( 'domain_mapping_siteurl' ) ) { |
|
109 | - define( 'AUTOPTIMIZE_WP_SITE_URL', domain_mapping_siteurl( get_current_blog_id() ) ); |
|
107 | + if (!defined('AUTOPTIMIZE_WP_SITE_URL')) { |
|
108 | + if (function_exists('domain_mapping_siteurl')) { |
|
109 | + define('AUTOPTIMIZE_WP_SITE_URL', domain_mapping_siteurl(get_current_blog_id())); |
|
110 | 110 | } else { |
111 | - define( 'AUTOPTIMIZE_WP_SITE_URL', site_url() ); |
|
111 | + define('AUTOPTIMIZE_WP_SITE_URL', site_url()); |
|
112 | 112 | } |
113 | 113 | } |
114 | - if ( ! defined( 'AUTOPTIMIZE_WP_CONTENT_URL' ) ) { |
|
115 | - if ( function_exists( 'get_original_url' ) ) { |
|
116 | - define( 'AUTOPTIMIZE_WP_CONTENT_URL', str_replace( get_original_url( AUTOPTIMIZE_WP_SITE_URL ), AUTOPTIMIZE_WP_SITE_URL, content_url() ) ); |
|
114 | + if (!defined('AUTOPTIMIZE_WP_CONTENT_URL')) { |
|
115 | + if (function_exists('get_original_url')) { |
|
116 | + define('AUTOPTIMIZE_WP_CONTENT_URL', str_replace(get_original_url(AUTOPTIMIZE_WP_SITE_URL), AUTOPTIMIZE_WP_SITE_URL, content_url())); |
|
117 | 117 | } else { |
118 | - define( 'AUTOPTIMIZE_WP_CONTENT_URL', content_url() ); |
|
118 | + define('AUTOPTIMIZE_WP_CONTENT_URL', content_url()); |
|
119 | 119 | } |
120 | 120 | } |
121 | - if ( ! defined( 'AUTOPTIMIZE_CACHE_URL' ) ) { |
|
122 | - if ( is_multisite() && apply_filters( 'autoptimize_separate_blog_caches', true ) ) { |
|
121 | + if (!defined('AUTOPTIMIZE_CACHE_URL')) { |
|
122 | + if (is_multisite() && apply_filters('autoptimize_separate_blog_caches', true)) { |
|
123 | 123 | $blog_id = get_current_blog_id(); |
124 | - define( 'AUTOPTIMIZE_CACHE_URL', AUTOPTIMIZE_WP_CONTENT_URL . AUTOPTIMIZE_CACHE_CHILD_DIR . $blog_id . '/' ); |
|
124 | + define('AUTOPTIMIZE_CACHE_URL', AUTOPTIMIZE_WP_CONTENT_URL.AUTOPTIMIZE_CACHE_CHILD_DIR.$blog_id.'/'); |
|
125 | 125 | } else { |
126 | - define( 'AUTOPTIMIZE_CACHE_URL', AUTOPTIMIZE_WP_CONTENT_URL . AUTOPTIMIZE_CACHE_CHILD_DIR ); |
|
126 | + define('AUTOPTIMIZE_CACHE_URL', AUTOPTIMIZE_WP_CONTENT_URL.AUTOPTIMIZE_CACHE_CHILD_DIR); |
|
127 | 127 | } |
128 | 128 | } |
129 | - if ( ! defined( 'AUTOPTIMIZE_WP_ROOT_URL' ) ) { |
|
130 | - define( 'AUTOPTIMIZE_WP_ROOT_URL', str_replace( AUTOPTIMIZE_WP_CONTENT_NAME, '', AUTOPTIMIZE_WP_CONTENT_URL ) ); |
|
129 | + if (!defined('AUTOPTIMIZE_WP_ROOT_URL')) { |
|
130 | + define('AUTOPTIMIZE_WP_ROOT_URL', str_replace(AUTOPTIMIZE_WP_CONTENT_NAME, '', AUTOPTIMIZE_WP_CONTENT_URL)); |
|
131 | 131 | } |
132 | - if ( ! defined( 'AUTOPTIMIZE_HASH' ) ) { |
|
133 | - define( 'AUTOPTIMIZE_HASH', wp_hash( AUTOPTIMIZE_CACHE_URL ) ); |
|
132 | + if (!defined('AUTOPTIMIZE_HASH')) { |
|
133 | + define('AUTOPTIMIZE_HASH', wp_hash(AUTOPTIMIZE_CACHE_URL)); |
|
134 | 134 | } |
135 | - if ( ! defined( 'AUTOPTIMIZE_SITE_DOMAIN' ) ) { |
|
136 | - define( 'AUTOPTIMIZE_SITE_DOMAIN', parse_url( AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST ) ); |
|
135 | + if (!defined('AUTOPTIMIZE_SITE_DOMAIN')) { |
|
136 | + define('AUTOPTIMIZE_SITE_DOMAIN', parse_url(AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST)); |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | // Multibyte-capable string replacements are available with a filter. |
140 | 140 | // Also requires 'mbstring' extension. |
141 | - $with_mbstring = apply_filters( 'autoptimize_filter_main_use_mbstring', false ); |
|
142 | - if ( $with_mbstring ) { |
|
143 | - autoptimizeUtils::mbstring_available( \extension_loaded( 'mbstring' ) ); |
|
141 | + $with_mbstring = apply_filters('autoptimize_filter_main_use_mbstring', false); |
|
142 | + if ($with_mbstring) { |
|
143 | + autoptimizeUtils::mbstring_available(\extension_loaded('mbstring')); |
|
144 | 144 | } else { |
145 | - autoptimizeUtils::mbstring_available( false ); |
|
145 | + autoptimizeUtils::mbstring_available(false); |
|
146 | 146 | } |
147 | 147 | |
148 | - do_action( 'autoptimize_setup_done' ); |
|
148 | + do_action('autoptimize_setup_done'); |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | /** |
@@ -155,61 +155,61 @@ discard block |
||
155 | 155 | */ |
156 | 156 | public function version_upgrades_check() |
157 | 157 | { |
158 | - autoptimizeVersionUpdatesHandler::check_installed_and_update( $this->version ); |
|
158 | + autoptimizeVersionUpdatesHandler::check_installed_and_update($this->version); |
|
159 | 159 | } |
160 | 160 | |
161 | 161 | public function check_cache_and_run() |
162 | 162 | { |
163 | - if ( autoptimizeCache::cacheavail() ) { |
|
163 | + if (autoptimizeCache::cacheavail()) { |
|
164 | 164 | $conf = autoptimizeConfig::instance(); |
165 | - if ( $conf->get( 'autoptimize_html' ) || $conf->get( 'autoptimize_js' ) || $conf->get( 'autoptimize_css' ) || autoptimizeImages::imgopt_active() || autoptimizeImages::should_lazyload_wrapper() ) { |
|
166 | - if ( ! defined( 'AUTOPTIMIZE_NOBUFFER_OPTIMIZE' ) ) { |
|
165 | + if ($conf->get('autoptimize_html') || $conf->get('autoptimize_js') || $conf->get('autoptimize_css') || autoptimizeImages::imgopt_active() || autoptimizeImages::should_lazyload_wrapper()) { |
|
166 | + if (!defined('AUTOPTIMIZE_NOBUFFER_OPTIMIZE')) { |
|
167 | 167 | // Hook into WordPress frontend. |
168 | - if ( defined( 'AUTOPTIMIZE_INIT_EARLIER' ) ) { |
|
168 | + if (defined('AUTOPTIMIZE_INIT_EARLIER')) { |
|
169 | 169 | add_action( |
170 | 170 | 'init', |
171 | - array( $this, 'start_buffering' ), |
|
171 | + array($this, 'start_buffering'), |
|
172 | 172 | self::INIT_EARLIER_PRIORITY |
173 | 173 | ); |
174 | 174 | } else { |
175 | - if ( ! defined( 'AUTOPTIMIZE_HOOK_INTO' ) ) { |
|
176 | - define( 'AUTOPTIMIZE_HOOK_INTO', 'template_redirect' ); |
|
175 | + if (!defined('AUTOPTIMIZE_HOOK_INTO')) { |
|
176 | + define('AUTOPTIMIZE_HOOK_INTO', 'template_redirect'); |
|
177 | 177 | } |
178 | 178 | add_action( |
179 | - constant( 'AUTOPTIMIZE_HOOK_INTO' ), |
|
180 | - array( $this, 'start_buffering' ), |
|
179 | + constant('AUTOPTIMIZE_HOOK_INTO'), |
|
180 | + array($this, 'start_buffering'), |
|
181 | 181 | self::DEFAULT_HOOK_PRIORITY |
182 | 182 | ); |
183 | 183 | } |
184 | 184 | } |
185 | 185 | |
186 | 186 | // And disable Jetpack's site accelerator if JS or CSS opt. are active. |
187 | - if ( class_exists( 'Jetpack' ) && apply_filters( 'autoptimize_filter_main_disable_jetpack_cdn', true ) && ( $conf->get( 'autoptimize_js' ) || $conf->get( 'autoptimize_css' ) ) ) { |
|
188 | - add_filter( 'jetpack_force_disable_site_accelerator', '__return_true' ); |
|
187 | + if (class_exists('Jetpack') && apply_filters('autoptimize_filter_main_disable_jetpack_cdn', true) && ($conf->get('autoptimize_js') || $conf->get('autoptimize_css'))) { |
|
188 | + add_filter('jetpack_force_disable_site_accelerator', '__return_true'); |
|
189 | 189 | } |
190 | 190 | } |
191 | 191 | } else { |
192 | - add_action( 'admin_notices', 'autoptimizeMain::notice_cache_unavailable' ); |
|
192 | + add_action('admin_notices', 'autoptimizeMain::notice_cache_unavailable'); |
|
193 | 193 | } |
194 | 194 | } |
195 | 195 | |
196 | 196 | public function maybe_run_ao_extra() |
197 | 197 | { |
198 | - if ( apply_filters( 'autoptimize_filter_extra_activate', true ) ) { |
|
198 | + if (apply_filters('autoptimize_filter_extra_activate', true)) { |
|
199 | 199 | $ao_imgopt = new autoptimizeImages(); |
200 | 200 | $ao_imgopt->run(); |
201 | 201 | $ao_extra = new autoptimizeExtra(); |
202 | 202 | $ao_extra->run(); |
203 | 203 | |
204 | 204 | // And show the imgopt notice. |
205 | - add_action( 'admin_notices', 'autoptimizeMain::notice_plug_imgopt' ); |
|
205 | + add_action('admin_notices', 'autoptimizeMain::notice_plug_imgopt'); |
|
206 | 206 | } |
207 | 207 | } |
208 | 208 | |
209 | 209 | public function maybe_run_partners_tab() |
210 | 210 | { |
211 | 211 | // Loads partners tab code if in admin (and not in admin-ajax.php)! |
212 | - if ( autoptimizeConfig::is_admin_and_not_ajax() ) { |
|
212 | + if (autoptimizeConfig::is_admin_and_not_ajax()) { |
|
213 | 213 | new autoptimizePartners(); |
214 | 214 | } |
215 | 215 | } |
@@ -217,22 +217,22 @@ discard block |
||
217 | 217 | public function maybe_run_criticalcss() |
218 | 218 | { |
219 | 219 | // Loads criticalcss if the power-up is not active and if the filter returns true. |
220 | - if ( apply_filters( 'autoptimize_filter_criticalcss_active', true ) && ! autoptimizeUtils::is_plugin_active( 'autoptimize-criticalcss/ao_criticss_aas.php' ) ) { |
|
220 | + if (apply_filters('autoptimize_filter_criticalcss_active', true) && !autoptimizeUtils::is_plugin_active('autoptimize-criticalcss/ao_criticss_aas.php')) { |
|
221 | 221 | new autoptimizeCriticalCSSBase(); |
222 | 222 | } |
223 | 223 | } |
224 | 224 | |
225 | 225 | public function maybe_run_notfound_fallback() |
226 | 226 | { |
227 | - if ( autoptimizeCache::do_fallback() ) { |
|
228 | - add_action( 'template_redirect', array( 'autoptimizeCache', 'wordpress_notfound_fallback' ) ); |
|
227 | + if (autoptimizeCache::do_fallback()) { |
|
228 | + add_action('template_redirect', array('autoptimizeCache', 'wordpress_notfound_fallback')); |
|
229 | 229 | } |
230 | 230 | } |
231 | 231 | |
232 | 232 | public function hook_page_cache_purge() |
233 | 233 | { |
234 | 234 | // hook into a collection of page cache purge actions if filter allows. |
235 | - if ( apply_filters( 'autoptimize_filter_main_hookpagecachepurge', true ) ) { |
|
235 | + if (apply_filters('autoptimize_filter_main_hookpagecachepurge', true)) { |
|
236 | 236 | $page_cache_purge_actions = array( |
237 | 237 | 'after_rocket_clean_domain', // exists. |
238 | 238 | 'hyper_cache_purged', // Stefano confirmed this will be added. |
@@ -247,9 +247,9 @@ discard block |
||
247 | 247 | 'wpo_cache_flush', // wp-optimize. |
248 | 248 | 'rt_nginx_helper_after_fastcgi_purge_all', // nginx helper. |
249 | 249 | ); |
250 | - $page_cache_purge_actions = apply_filters( 'autoptimize_filter_main_pagecachepurgeactions', $page_cache_purge_actions ); |
|
251 | - foreach ( $page_cache_purge_actions as $purge_action ) { |
|
252 | - add_action( $purge_action, 'autoptimizeCache::clearall_actionless' ); |
|
250 | + $page_cache_purge_actions = apply_filters('autoptimize_filter_main_pagecachepurgeactions', $page_cache_purge_actions); |
|
251 | + foreach ($page_cache_purge_actions as $purge_action) { |
|
252 | + add_action($purge_action, 'autoptimizeCache::clearall_actionless'); |
|
253 | 253 | } |
254 | 254 | } |
255 | 255 | } |
@@ -261,38 +261,38 @@ discard block |
||
261 | 261 | */ |
262 | 262 | public function start_buffering() |
263 | 263 | { |
264 | - if ( $this->should_buffer() ) { |
|
264 | + if ($this->should_buffer()) { |
|
265 | 265 | |
266 | 266 | // Load speedupper conditionally (true by default). |
267 | - if ( apply_filters( 'autoptimize_filter_speedupper', true ) ) { |
|
267 | + if (apply_filters('autoptimize_filter_speedupper', true)) { |
|
268 | 268 | $ao_speedupper = new autoptimizeSpeedupper(); |
269 | 269 | } |
270 | 270 | |
271 | 271 | $conf = autoptimizeConfig::instance(); |
272 | 272 | |
273 | - if ( $conf->get( 'autoptimize_js' ) ) { |
|
274 | - if ( ! defined( 'CONCATENATE_SCRIPTS' ) ) { |
|
275 | - define( 'CONCATENATE_SCRIPTS', false ); |
|
273 | + if ($conf->get('autoptimize_js')) { |
|
274 | + if (!defined('CONCATENATE_SCRIPTS')) { |
|
275 | + define('CONCATENATE_SCRIPTS', false); |
|
276 | 276 | } |
277 | - if ( ! defined( 'COMPRESS_SCRIPTS' ) ) { |
|
278 | - define( 'COMPRESS_SCRIPTS', false ); |
|
277 | + if (!defined('COMPRESS_SCRIPTS')) { |
|
278 | + define('COMPRESS_SCRIPTS', false); |
|
279 | 279 | } |
280 | 280 | } |
281 | 281 | |
282 | - if ( $conf->get( 'autoptimize_css' ) ) { |
|
283 | - if ( ! defined( 'COMPRESS_CSS' ) ) { |
|
284 | - define( 'COMPRESS_CSS', false ); |
|
282 | + if ($conf->get('autoptimize_css')) { |
|
283 | + if (!defined('COMPRESS_CSS')) { |
|
284 | + define('COMPRESS_CSS', false); |
|
285 | 285 | } |
286 | 286 | } |
287 | 287 | |
288 | - if ( apply_filters( 'autoptimize_filter_obkiller', false ) ) { |
|
289 | - while ( ob_get_level() > 0 ) { |
|
288 | + if (apply_filters('autoptimize_filter_obkiller', false)) { |
|
289 | + while (ob_get_level() > 0) { |
|
290 | 290 | ob_end_clean(); |
291 | 291 | } |
292 | 292 | } |
293 | 293 | |
294 | 294 | // Now, start the real thing! |
295 | - ob_start( array( $this, 'end_buffering' ) ); |
|
295 | + ob_start(array($this, 'end_buffering')); |
|
296 | 296 | } |
297 | 297 | } |
298 | 298 | |
@@ -303,31 +303,31 @@ discard block |
||
303 | 303 | * deciding once per request (for use in tests). |
304 | 304 | * @return bool |
305 | 305 | */ |
306 | - public static function should_buffer( $doing_tests = false ) |
|
306 | + public static function should_buffer($doing_tests = false) |
|
307 | 307 | { |
308 | 308 | static $do_buffering = null; |
309 | 309 | |
310 | 310 | // Only check once in case we're called multiple times by others but |
311 | 311 | // still allows multiple calls when doing tests. |
312 | - if ( null === $do_buffering || $doing_tests ) { |
|
312 | + if (null === $do_buffering || $doing_tests) { |
|
313 | 313 | |
314 | 314 | $ao_noptimize = false; |
315 | 315 | |
316 | 316 | // Checking for DONOTMINIFY constant as used by e.g. WooCommerce POS. |
317 | - if ( defined( 'DONOTMINIFY' ) && ( constant( 'DONOTMINIFY' ) === true || constant( 'DONOTMINIFY' ) === 'true' ) ) { |
|
317 | + if (defined('DONOTMINIFY') && (constant('DONOTMINIFY') === true || constant('DONOTMINIFY') === 'true')) { |
|
318 | 318 | $ao_noptimize = true; |
319 | 319 | } |
320 | 320 | |
321 | 321 | // Skip checking query strings if they're disabled. |
322 | - if ( apply_filters( 'autoptimize_filter_honor_qs_noptimize', true ) ) { |
|
322 | + if (apply_filters('autoptimize_filter_honor_qs_noptimize', true)) { |
|
323 | 323 | // Check for `ao_noptimize` (and other) keys in the query string |
324 | 324 | // to get non-optimized page for debugging. |
325 | 325 | $keys = array( |
326 | 326 | 'ao_noptimize', |
327 | 327 | 'ao_noptirocket', |
328 | 328 | ); |
329 | - foreach ( $keys as $key ) { |
|
330 | - if ( array_key_exists( $key, $_GET ) && '1' === $_GET[ $key ] ) { |
|
329 | + foreach ($keys as $key) { |
|
330 | + if (array_key_exists($key, $_GET) && '1' === $_GET[$key]) { |
|
331 | 331 | $ao_noptimize = true; |
332 | 332 | break; |
333 | 333 | } |
@@ -336,25 +336,25 @@ discard block |
||
336 | 336 | |
337 | 337 | // also honor PageSpeed=off parameter as used by mod_pagespeed, in use by some pagebuilders, |
338 | 338 | // see https://www.modpagespeed.com/doc/experiment#ModPagespeed for info on that. |
339 | - if ( false === $ao_noptimize && array_key_exists( 'PageSpeed', $_GET ) && 'off' === $_GET['PageSpeed'] ) { |
|
339 | + if (false === $ao_noptimize && array_key_exists('PageSpeed', $_GET) && 'off' === $_GET['PageSpeed']) { |
|
340 | 340 | $ao_noptimize = true; |
341 | 341 | } |
342 | 342 | |
343 | 343 | // and make sure Thrive editor doesn't get optimized HTML. |
344 | - if ( false === $ao_noptimize && array_key_exists( 'tve', $_GET ) && 'true' === $_GET['tve'] ) { |
|
344 | + if (false === $ao_noptimize && array_key_exists('tve', $_GET) && 'true' === $_GET['tve']) { |
|
345 | 345 | $ao_noptimize = true; |
346 | 346 | } |
347 | 347 | |
348 | 348 | // If setting says not to optimize logged in user and user is logged in... |
349 | - if ( false === $ao_noptimize && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_optimize_logged', 'on' ) && is_user_logged_in() && current_user_can( 'edit_posts' ) ) { |
|
349 | + if (false === $ao_noptimize && 'on' !== autoptimizeOptionWrapper::get_option('autoptimize_optimize_logged', 'on') && is_user_logged_in() && current_user_can('edit_posts')) { |
|
350 | 350 | $ao_noptimize = true; |
351 | 351 | } |
352 | 352 | |
353 | 353 | // If setting says not to optimize cart/checkout. |
354 | - if ( false === $ao_noptimize && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_optimize_checkout', 'off' ) ) { |
|
354 | + if (false === $ao_noptimize && 'on' !== autoptimizeOptionWrapper::get_option('autoptimize_optimize_checkout', 'off')) { |
|
355 | 355 | // Checking for woocommerce, easy digital downloads and wp ecommerce... |
356 | - foreach ( array( 'is_checkout', 'is_cart', 'is_account_page', 'edd_is_checkout', 'wpsc_is_cart', 'wpsc_is_checkout' ) as $func ) { |
|
357 | - if ( function_exists( $func ) && $func() ) { |
|
356 | + foreach (array('is_checkout', 'is_cart', 'is_account_page', 'edd_is_checkout', 'wpsc_is_cart', 'wpsc_is_checkout') as $func) { |
|
357 | + if (function_exists($func) && $func()) { |
|
358 | 358 | $ao_noptimize = true; |
359 | 359 | break; |
360 | 360 | } |
@@ -362,11 +362,11 @@ discard block |
||
362 | 362 | } |
363 | 363 | |
364 | 364 | // Allows blocking of autoptimization on your own terms regardless of above decisions. |
365 | - $ao_noptimize = (bool) apply_filters( 'autoptimize_filter_noptimize', $ao_noptimize ); |
|
365 | + $ao_noptimize = (bool) apply_filters('autoptimize_filter_noptimize', $ao_noptimize); |
|
366 | 366 | |
367 | 367 | // Check for site being previewed in the Customizer (available since WP 4.0). |
368 | 368 | $is_customize_preview = false; |
369 | - if ( function_exists( 'is_customize_preview' ) && is_customize_preview() ) { |
|
369 | + if (function_exists('is_customize_preview') && is_customize_preview()) { |
|
370 | 370 | $is_customize_preview = is_customize_preview(); |
371 | 371 | } |
372 | 372 | |
@@ -377,7 +377,7 @@ discard block |
||
377 | 377 | * while the main query hasn't been ran yet. Thats why we use |
378 | 378 | * AUTOPTIMIZE_INIT_EARLIER in tests. |
379 | 379 | */ |
380 | - $do_buffering = ( ! is_admin() && ! is_feed() && ! is_embed() && ! $ao_noptimize && ! $is_customize_preview ); |
|
380 | + $do_buffering = (!is_admin() && !is_feed() && !is_embed() && !$ao_noptimize && !$is_customize_preview); |
|
381 | 381 | } |
382 | 382 | |
383 | 383 | return $do_buffering; |
@@ -390,25 +390,25 @@ discard block |
||
390 | 390 | * |
391 | 391 | * @return bool |
392 | 392 | */ |
393 | - public function is_valid_buffer( $content ) |
|
393 | + public function is_valid_buffer($content) |
|
394 | 394 | { |
395 | 395 | // Defaults to true. |
396 | 396 | $valid = true; |
397 | 397 | |
398 | - $has_no_html_tag = ( false === stripos( $content, '<html' ) ); |
|
399 | - $has_xsl_stylesheet = ( false !== stripos( $content, '<xsl:stylesheet' ) || false !== stripos( $content, '<?xml-stylesheet' ) ); |
|
400 | - $has_html5_doctype = ( preg_match( '/^<!DOCTYPE.+html>/i', ltrim( $content ) ) > 0 ); |
|
401 | - $has_noptimize_page = ( false !== stripos( $content, '<!-- noptimize-page -->' ) ); |
|
398 | + $has_no_html_tag = (false === stripos($content, '<html')); |
|
399 | + $has_xsl_stylesheet = (false !== stripos($content, '<xsl:stylesheet') || false !== stripos($content, '<?xml-stylesheet')); |
|
400 | + $has_html5_doctype = (preg_match('/^<!DOCTYPE.+html>/i', ltrim($content)) > 0); |
|
401 | + $has_noptimize_page = (false !== stripos($content, '<!-- noptimize-page -->')); |
|
402 | 402 | |
403 | - if ( $has_no_html_tag ) { |
|
403 | + if ($has_no_html_tag) { |
|
404 | 404 | // Can't be valid amp markup without an html tag preceding it. |
405 | 405 | $is_amp_markup = false; |
406 | 406 | } else { |
407 | - $is_amp_markup = self::is_amp_markup( $content ); |
|
407 | + $is_amp_markup = self::is_amp_markup($content); |
|
408 | 408 | } |
409 | 409 | |
410 | 410 | // If it's not html, or if it's amp or contains xsl stylesheets we don't touch it. |
411 | - if ( $has_no_html_tag && ! $has_html5_doctype || $is_amp_markup || $has_xsl_stylesheet || $has_noptimize_page ) { |
|
411 | + if ($has_no_html_tag && !$has_html5_doctype || $is_amp_markup || $has_xsl_stylesheet || $has_noptimize_page) { |
|
412 | 412 | $valid = false; |
413 | 413 | } |
414 | 414 | |
@@ -423,14 +423,14 @@ discard block |
||
423 | 423 | * |
424 | 424 | * @return bool |
425 | 425 | */ |
426 | - public static function is_amp_markup( $content ) |
|
426 | + public static function is_amp_markup($content) |
|
427 | 427 | { |
428 | 428 | // Short-circuit when a function is available to determine whether the response is (or will be) an AMP page. |
429 | - if ( function_exists( 'is_amp_endpoint' ) ) { |
|
429 | + if (function_exists('is_amp_endpoint')) { |
|
430 | 430 | return is_amp_endpoint(); |
431 | 431 | } |
432 | 432 | |
433 | - $is_amp_markup = preg_match( '/<html[^>]*(?:amp|⚡)/i', $content ); |
|
433 | + $is_amp_markup = preg_match('/<html[^>]*(?:amp|⚡)/i', $content); |
|
434 | 434 | |
435 | 435 | return (bool) $is_amp_markup; |
436 | 436 | } |
@@ -443,10 +443,10 @@ discard block |
||
443 | 443 | * |
444 | 444 | * @return string |
445 | 445 | */ |
446 | - public function end_buffering( $content ) |
|
446 | + public function end_buffering($content) |
|
447 | 447 | { |
448 | 448 | // Bail early without modifying anything if we can't handle the content. |
449 | - if ( ! $this->is_valid_buffer( $content ) ) { |
|
449 | + if (!$this->is_valid_buffer($content)) { |
|
450 | 450 | return $content; |
451 | 451 | } |
452 | 452 | |
@@ -454,73 +454,73 @@ discard block |
||
454 | 454 | |
455 | 455 | // Determine what needs to be ran. |
456 | 456 | $classes = array(); |
457 | - if ( $conf->get( 'autoptimize_js' ) ) { |
|
457 | + if ($conf->get('autoptimize_js')) { |
|
458 | 458 | $classes[] = 'autoptimizeScripts'; |
459 | 459 | } |
460 | - if ( $conf->get( 'autoptimize_css' ) ) { |
|
460 | + if ($conf->get('autoptimize_css')) { |
|
461 | 461 | $classes[] = 'autoptimizeStyles'; |
462 | 462 | } |
463 | - if ( $conf->get( 'autoptimize_html' ) ) { |
|
463 | + if ($conf->get('autoptimize_html')) { |
|
464 | 464 | $classes[] = 'autoptimizeHTML'; |
465 | 465 | } |
466 | 466 | |
467 | 467 | $classoptions = array( |
468 | 468 | 'autoptimizeScripts' => array( |
469 | - 'aggregate' => $conf->get( 'autoptimize_js_aggregate' ), |
|
470 | - 'justhead' => $conf->get( 'autoptimize_js_justhead' ), |
|
471 | - 'forcehead' => $conf->get( 'autoptimize_js_forcehead' ), |
|
472 | - 'trycatch' => $conf->get( 'autoptimize_js_trycatch' ), |
|
473 | - 'js_exclude' => $conf->get( 'autoptimize_js_exclude' ), |
|
474 | - 'cdn_url' => $conf->get( 'autoptimize_cdn_url' ), |
|
475 | - 'include_inline' => $conf->get( 'autoptimize_js_include_inline' ), |
|
476 | - 'minify_excluded' => $conf->get( 'autoptimize_minify_excluded' ), |
|
469 | + 'aggregate' => $conf->get('autoptimize_js_aggregate'), |
|
470 | + 'justhead' => $conf->get('autoptimize_js_justhead'), |
|
471 | + 'forcehead' => $conf->get('autoptimize_js_forcehead'), |
|
472 | + 'trycatch' => $conf->get('autoptimize_js_trycatch'), |
|
473 | + 'js_exclude' => $conf->get('autoptimize_js_exclude'), |
|
474 | + 'cdn_url' => $conf->get('autoptimize_cdn_url'), |
|
475 | + 'include_inline' => $conf->get('autoptimize_js_include_inline'), |
|
476 | + 'minify_excluded' => $conf->get('autoptimize_minify_excluded'), |
|
477 | 477 | ), |
478 | 478 | 'autoptimizeStyles' => array( |
479 | - 'aggregate' => $conf->get( 'autoptimize_css_aggregate' ), |
|
480 | - 'justhead' => $conf->get( 'autoptimize_css_justhead' ), |
|
481 | - 'datauris' => $conf->get( 'autoptimize_css_datauris' ), |
|
482 | - 'defer' => $conf->get( 'autoptimize_css_defer' ), |
|
483 | - 'defer_inline' => $conf->get( 'autoptimize_css_defer_inline' ), |
|
484 | - 'inline' => $conf->get( 'autoptimize_css_inline' ), |
|
485 | - 'css_exclude' => $conf->get( 'autoptimize_css_exclude' ), |
|
486 | - 'cdn_url' => $conf->get( 'autoptimize_cdn_url' ), |
|
487 | - 'include_inline' => $conf->get( 'autoptimize_css_include_inline' ), |
|
488 | - 'nogooglefont' => $conf->get( 'autoptimize_css_nogooglefont' ), |
|
489 | - 'minify_excluded' => $conf->get( 'autoptimize_minify_excluded' ), |
|
479 | + 'aggregate' => $conf->get('autoptimize_css_aggregate'), |
|
480 | + 'justhead' => $conf->get('autoptimize_css_justhead'), |
|
481 | + 'datauris' => $conf->get('autoptimize_css_datauris'), |
|
482 | + 'defer' => $conf->get('autoptimize_css_defer'), |
|
483 | + 'defer_inline' => $conf->get('autoptimize_css_defer_inline'), |
|
484 | + 'inline' => $conf->get('autoptimize_css_inline'), |
|
485 | + 'css_exclude' => $conf->get('autoptimize_css_exclude'), |
|
486 | + 'cdn_url' => $conf->get('autoptimize_cdn_url'), |
|
487 | + 'include_inline' => $conf->get('autoptimize_css_include_inline'), |
|
488 | + 'nogooglefont' => $conf->get('autoptimize_css_nogooglefont'), |
|
489 | + 'minify_excluded' => $conf->get('autoptimize_minify_excluded'), |
|
490 | 490 | ), |
491 | 491 | 'autoptimizeHTML' => array( |
492 | - 'keepcomments' => $conf->get( 'autoptimize_html_keepcomments' ), |
|
492 | + 'keepcomments' => $conf->get('autoptimize_html_keepcomments'), |
|
493 | 493 | ), |
494 | 494 | ); |
495 | 495 | |
496 | - $content = apply_filters( 'autoptimize_filter_html_before_minify', $content ); |
|
496 | + $content = apply_filters('autoptimize_filter_html_before_minify', $content); |
|
497 | 497 | |
498 | 498 | // Run the classes! |
499 | - foreach ( $classes as $name ) { |
|
500 | - $instance = new $name( $content ); |
|
501 | - if ( $instance->read( $classoptions[ $name ] ) ) { |
|
499 | + foreach ($classes as $name) { |
|
500 | + $instance = new $name($content); |
|
501 | + if ($instance->read($classoptions[$name])) { |
|
502 | 502 | $instance->minify(); |
503 | 503 | $instance->cache(); |
504 | 504 | $content = $instance->getcontent(); |
505 | 505 | } |
506 | - unset( $instance ); |
|
506 | + unset($instance); |
|
507 | 507 | } |
508 | 508 | |
509 | - $content = apply_filters( 'autoptimize_html_after_minify', $content ); |
|
509 | + $content = apply_filters('autoptimize_html_after_minify', $content); |
|
510 | 510 | |
511 | 511 | return $content; |
512 | 512 | } |
513 | 513 | |
514 | - public static function autoptimize_nobuffer_optimize( $html_in ) { |
|
514 | + public static function autoptimize_nobuffer_optimize($html_in) { |
|
515 | 515 | $html_out = $html_in; |
516 | 516 | |
517 | - if ( apply_filters( 'autoptimize_filter_speedupper', true ) ) { |
|
517 | + if (apply_filters('autoptimize_filter_speedupper', true)) { |
|
518 | 518 | $ao_speedupper = new autoptimizeSpeedupper(); |
519 | 519 | } |
520 | 520 | |
521 | - $self = new self( AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE ); |
|
522 | - if ( $self->should_buffer() ) { |
|
523 | - $html_out = $self->end_buffering( $html_in ); |
|
521 | + $self = new self(AUTOPTIMIZE_PLUGIN_VERSION, AUTOPTIMIZE_PLUGIN_FILE); |
|
522 | + if ($self->should_buffer()) { |
|
523 | + $html_out = $self->end_buffering($html_in); |
|
524 | 524 | } |
525 | 525 | return $html_out; |
526 | 526 | } |
@@ -581,45 +581,45 @@ discard block |
||
581 | 581 | 'autoptimize_ccss_domain', |
582 | 582 | ); |
583 | 583 | |
584 | - if ( ! is_multisite() ) { |
|
585 | - foreach ( $delete_options as $del_opt ) { |
|
586 | - delete_option( $del_opt ); |
|
584 | + if (!is_multisite()) { |
|
585 | + foreach ($delete_options as $del_opt) { |
|
586 | + delete_option($del_opt); |
|
587 | 587 | } |
588 | 588 | autoptimizeMain::remove_cronjobs(); |
589 | 589 | } else { |
590 | 590 | global $wpdb; |
591 | - $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
|
591 | + $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
|
592 | 592 | $original_blog_id = get_current_blog_id(); |
593 | - foreach ( $blog_ids as $blog_id ) { |
|
594 | - switch_to_blog( $blog_id ); |
|
595 | - foreach ( $delete_options as $del_opt ) { |
|
596 | - delete_option( $del_opt ); |
|
593 | + foreach ($blog_ids as $blog_id) { |
|
594 | + switch_to_blog($blog_id); |
|
595 | + foreach ($delete_options as $del_opt) { |
|
596 | + delete_option($del_opt); |
|
597 | 597 | } |
598 | 598 | autoptimizeMain::remove_cronjobs(); |
599 | 599 | } |
600 | - switch_to_blog( $original_blog_id ); |
|
600 | + switch_to_blog($original_blog_id); |
|
601 | 601 | } |
602 | 602 | |
603 | 603 | // Remove AO CCSS cached files and directory. |
604 | - $ao_ccss_dir = WP_CONTENT_DIR . '/uploads/ao_ccss/'; |
|
605 | - if ( file_exists( $ao_ccss_dir ) && is_dir( $ao_ccss_dir ) ) { |
|
604 | + $ao_ccss_dir = WP_CONTENT_DIR.'/uploads/ao_ccss/'; |
|
605 | + if (file_exists($ao_ccss_dir) && is_dir($ao_ccss_dir)) { |
|
606 | 606 | // fixme: should check for subdirs when in multisite and remove contents of those as well. |
607 | - array_map( 'unlink', glob( AO_CCSS_DIR . '*.{css,html,json,log,zip,lock}', GLOB_BRACE ) ); |
|
608 | - rmdir( AO_CCSS_DIR ); |
|
607 | + array_map('unlink', glob(AO_CCSS_DIR.'*.{css,html,json,log,zip,lock}', GLOB_BRACE)); |
|
608 | + rmdir(AO_CCSS_DIR); |
|
609 | 609 | } |
610 | 610 | } |
611 | 611 | |
612 | 612 | public static function on_deactivation() |
613 | 613 | { |
614 | - if ( is_multisite() && is_network_admin() ) { |
|
614 | + if (is_multisite() && is_network_admin()) { |
|
615 | 615 | global $wpdb; |
616 | - $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
|
616 | + $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
|
617 | 617 | $original_blog_id = get_current_blog_id(); |
618 | - foreach ( $blog_ids as $blog_id ) { |
|
619 | - switch_to_blog( $blog_id ); |
|
618 | + foreach ($blog_ids as $blog_id) { |
|
619 | + switch_to_blog($blog_id); |
|
620 | 620 | autoptimizeMain::remove_cronjobs(); |
621 | 621 | } |
622 | - switch_to_blog( $original_blog_id ); |
|
622 | + switch_to_blog($original_blog_id); |
|
623 | 623 | } else { |
624 | 624 | autoptimizeMain::remove_cronjobs(); |
625 | 625 | } |
@@ -628,9 +628,9 @@ discard block |
||
628 | 628 | |
629 | 629 | public static function remove_cronjobs() { |
630 | 630 | // Remove scheduled events. |
631 | - foreach ( array( 'ao_cachechecker', 'ao_ccss_queue', 'ao_ccss_maintenance' ) as $_event ) { |
|
632 | - if ( wp_get_schedule( $_event ) ) { |
|
633 | - wp_clear_scheduled_hook( $_event ); |
|
631 | + foreach (array('ao_cachechecker', 'ao_ccss_queue', 'ao_ccss_maintenance') as $_event) { |
|
632 | + if (wp_get_schedule($_event)) { |
|
633 | + wp_clear_scheduled_hook($_event); |
|
634 | 634 | } |
635 | 635 | } |
636 | 636 | } |
@@ -639,35 +639,35 @@ discard block |
||
639 | 639 | { |
640 | 640 | echo '<div class="error"><p>'; |
641 | 641 | // Translators: %s is the cache directory location. |
642 | - printf( __( 'Autoptimize cannot write to the cache directory (%s), please fix to enable CSS/ JS optimization!', 'autoptimize' ), AUTOPTIMIZE_CACHE_DIR ); |
|
642 | + printf(__('Autoptimize cannot write to the cache directory (%s), please fix to enable CSS/ JS optimization!', 'autoptimize'), AUTOPTIMIZE_CACHE_DIR); |
|
643 | 643 | echo '</p></div>'; |
644 | 644 | } |
645 | 645 | |
646 | 646 | public static function notice_installed() |
647 | 647 | { |
648 | 648 | echo '<div class="updated"><p>'; |
649 | - _e( 'Thank you for installing and activating Autoptimize. Please configure it under "Settings" -> "Autoptimize" to start improving your site\'s performance.', 'autoptimize' ); |
|
649 | + _e('Thank you for installing and activating Autoptimize. Please configure it under "Settings" -> "Autoptimize" to start improving your site\'s performance.', 'autoptimize'); |
|
650 | 650 | echo '</p></div>'; |
651 | 651 | } |
652 | 652 | |
653 | 653 | public static function notice_updated() |
654 | 654 | { |
655 | 655 | echo '<div class="updated"><p>'; |
656 | - _e( 'Autoptimize has just been updated. Please <strong>test your site now</strong> and adapt Autoptimize config if needed.', 'autoptimize' ); |
|
656 | + _e('Autoptimize has just been updated. Please <strong>test your site now</strong> and adapt Autoptimize config if needed.', 'autoptimize'); |
|
657 | 657 | echo '</p></div>'; |
658 | 658 | } |
659 | 659 | |
660 | 660 | public static function notice_plug_imgopt() |
661 | 661 | { |
662 | 662 | // Translators: the URL added points to the Autopmize Extra settings. |
663 | - $_ao_imgopt_plug_notice = sprintf( __( 'Did you know Autoptimize includes on-the-fly image optimization (with support for WebP) and CDN via ShortPixel? Check out the %1$sAutoptimize Image settings%2$s to activate this option.', 'autoptimize' ), '<a href="options-general.php?page=autoptimize_imgopt">', '</a>' ); |
|
664 | - $_ao_imgopt_plug_notice = apply_filters( 'autoptimize_filter_main_imgopt_plug_notice', $_ao_imgopt_plug_notice ); |
|
663 | + $_ao_imgopt_plug_notice = sprintf(__('Did you know Autoptimize includes on-the-fly image optimization (with support for WebP) and CDN via ShortPixel? Check out the %1$sAutoptimize Image settings%2$s to activate this option.', 'autoptimize'), '<a href="options-general.php?page=autoptimize_imgopt">', '</a>'); |
|
664 | + $_ao_imgopt_plug_notice = apply_filters('autoptimize_filter_main_imgopt_plug_notice', $_ao_imgopt_plug_notice); |
|
665 | 665 | $_ao_imgopt_launch_ok = autoptimizeImages::launch_ok_wrapper(); |
666 | 666 | $_ao_imgopt_plug_dismissible = 'ao-img-opt-plug-123'; |
667 | 667 | $_ao_imgopt_active = autoptimizeImages::imgopt_active(); |
668 | 668 | |
669 | - if ( current_user_can( 'manage_options' ) && '' !== $_ao_imgopt_plug_notice && ! $_ao_imgopt_active && $_ao_imgopt_launch_ok && PAnD::is_admin_notice_active( $_ao_imgopt_plug_dismissible ) ) { |
|
670 | - echo '<div class="notice notice-info is-dismissible" data-dismissible="' . $_ao_imgopt_plug_dismissible . '"><p>'; |
|
669 | + if (current_user_can('manage_options') && '' !== $_ao_imgopt_plug_notice && !$_ao_imgopt_active && $_ao_imgopt_launch_ok && PAnD::is_admin_notice_active($_ao_imgopt_plug_dismissible)) { |
|
670 | + echo '<div class="notice notice-info is-dismissible" data-dismissible="'.$_ao_imgopt_plug_dismissible.'"><p>'; |
|
671 | 671 | echo $_ao_imgopt_plug_notice; |
672 | 672 | echo '</p></div>'; |
673 | 673 | } |
@@ -3,7 +3,7 @@ discard block |
||
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 | |
@@ -28,9 +28,9 @@ discard block |
||
28 | 28 | * |
29 | 29 | * @param array $options Optional. Allows overriding options without having to specify them via admin options page. |
30 | 30 | */ |
31 | - public function __construct( $options = array() ) |
|
31 | + public function __construct($options = array()) |
|
32 | 32 | { |
33 | - if ( empty( $options ) ) { |
|
33 | + if (empty($options)) { |
|
34 | 34 | $options = self::fetch_options(); |
35 | 35 | } |
36 | 36 | |
@@ -47,7 +47,7 @@ discard block |
||
47 | 47 | */ |
48 | 48 | public static function instance() |
49 | 49 | { |
50 | - if ( null === self::$instance ) { |
|
50 | + if (null === self::$instance) { |
|
51 | 51 | self::$instance = new self(); |
52 | 52 | } |
53 | 53 | |
@@ -56,19 +56,19 @@ discard block |
||
56 | 56 | |
57 | 57 | public function run() |
58 | 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' ) ); |
|
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 | 62 | } else { |
63 | - add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
|
63 | + add_action('admin_menu', array($this, 'admin_menu')); |
|
64 | 64 | } |
65 | - add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_extra_tab' ) ); |
|
65 | + add_filter('autoptimize_filter_settingsscreen_tabs', array($this, 'add_extra_tab')); |
|
66 | 66 | } else { |
67 | - add_action( 'wp', array( $this, 'run_on_frontend' ) ); |
|
67 | + add_action('wp', array($this, 'run_on_frontend')); |
|
68 | 68 | } |
69 | 69 | } |
70 | 70 | |
71 | - public function set_options( array $options ) |
|
71 | + public function set_options(array $options) |
|
72 | 72 | { |
73 | 73 | $this->options = $options; |
74 | 74 | |
@@ -77,8 +77,8 @@ discard block |
||
77 | 77 | |
78 | 78 | public static function fetch_options() |
79 | 79 | { |
80 | - $value = autoptimizeOptionWrapper::get_option( 'autoptimize_extra_settings' ); |
|
81 | - if ( empty( $value ) ) { |
|
80 | + $value = autoptimizeOptionWrapper::get_option('autoptimize_extra_settings'); |
|
81 | + if (empty($value)) { |
|
82 | 82 | // Fallback to returning defaults when no stored option exists yet. |
83 | 83 | $value = autoptimizeConfig::get_ao_extra_default_options(); |
84 | 84 | } |
@@ -89,55 +89,55 @@ discard block |
||
89 | 89 | public function disable_emojis() |
90 | 90 | { |
91 | 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' ); |
|
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 | 99 | |
100 | 100 | // Removes TinyMCE emojis. |
101 | - add_filter( 'tiny_mce_plugins', array( $this, 'filter_disable_emojis_tinymce' ) ); |
|
101 | + add_filter('tiny_mce_plugins', array($this, 'filter_disable_emojis_tinymce')); |
|
102 | 102 | |
103 | 103 | // Removes emoji dns-preftech. |
104 | - add_filter( 'wp_resource_hints', array( $this, 'filter_remove_emoji_dns_prefetch' ), 10, 2 ); |
|
104 | + add_filter('wp_resource_hints', array($this, 'filter_remove_emoji_dns_prefetch'), 10, 2); |
|
105 | 105 | } |
106 | 106 | |
107 | - public function filter_disable_emojis_tinymce( $plugins ) |
|
107 | + public function filter_disable_emojis_tinymce($plugins) |
|
108 | 108 | { |
109 | - if ( is_array( $plugins ) ) { |
|
110 | - return array_diff( $plugins, array( 'wpemoji' ) ); |
|
109 | + if (is_array($plugins)) { |
|
110 | + return array_diff($plugins, array('wpemoji')); |
|
111 | 111 | } else { |
112 | 112 | return array(); |
113 | 113 | } |
114 | 114 | } |
115 | 115 | |
116 | - public function filter_remove_qs( $src ) |
|
116 | + public function filter_remove_qs($src) |
|
117 | 117 | { |
118 | - if ( strpos( $src, '?ver=' ) ) { |
|
119 | - $src = remove_query_arg( 'ver', $src ); |
|
118 | + if (strpos($src, '?ver=')) { |
|
119 | + $src = remove_query_arg('ver', $src); |
|
120 | 120 | } |
121 | 121 | |
122 | 122 | return $src; |
123 | 123 | } |
124 | 124 | |
125 | - public function extra_async_js( $in ) |
|
125 | + public function extra_async_js($in) |
|
126 | 126 | { |
127 | 127 | $exclusions = array(); |
128 | - if ( ! empty( $in ) ) { |
|
129 | - $exclusions = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $in ) ) ), '' ); |
|
128 | + if (!empty($in)) { |
|
129 | + $exclusions = array_fill_keys(array_filter(array_map('trim', explode(',', $in))), ''); |
|
130 | 130 | } |
131 | 131 | |
132 | 132 | $settings = $this->options['autoptimize_extra_text_field_3']; |
133 | - $async = array_fill_keys( array_filter( array_map( 'trim', explode( ',', $settings ) ) ), '' ); |
|
134 | - $attr = apply_filters( 'autoptimize_filter_extra_async', 'async' ); |
|
135 | - foreach ( $async as $k => $v ) { |
|
136 | - $async[ $k ] = $attr; |
|
133 | + $async = array_fill_keys(array_filter(array_map('trim', explode(',', $settings))), ''); |
|
134 | + $attr = apply_filters('autoptimize_filter_extra_async', 'async'); |
|
135 | + foreach ($async as $k => $v) { |
|
136 | + $async[$k] = $attr; |
|
137 | 137 | } |
138 | 138 | |
139 | 139 | // Merge exclusions & asyncs in one array and return to AO API. |
140 | - $merged = array_merge( $exclusions, $async ); |
|
140 | + $merged = array_merge($exclusions, $async); |
|
141 | 141 | |
142 | 142 | return $merged; |
143 | 143 | } |
@@ -148,68 +148,68 @@ discard block |
||
148 | 148 | // for optimizations are met, this to ensure e.g. removing querystrings |
149 | 149 | // is not done when optimizing for logged in users is off, breaking |
150 | 150 | // some pagebuilders (Divi & Elementor). |
151 | - if ( false === autoptimizeMain::should_buffer() ) { |
|
151 | + if (false === autoptimizeMain::should_buffer()) { |
|
152 | 152 | return; |
153 | 153 | } |
154 | 154 | |
155 | 155 | $options = $this->options; |
156 | 156 | |
157 | 157 | // Disable emojis if specified. |
158 | - if ( ! empty( $options['autoptimize_extra_checkbox_field_1'] ) ) { |
|
158 | + if (!empty($options['autoptimize_extra_checkbox_field_1'])) { |
|
159 | 159 | $this->disable_emojis(); |
160 | 160 | } |
161 | 161 | |
162 | 162 | // Remove version query parameters. |
163 | - if ( ! empty( $options['autoptimize_extra_checkbox_field_0'] ) ) { |
|
164 | - add_filter( 'script_loader_src', array( $this, 'filter_remove_qs' ), 15, 1 ); |
|
165 | - add_filter( 'style_loader_src', array( $this, 'filter_remove_qs' ), 15, 1 ); |
|
163 | + if (!empty($options['autoptimize_extra_checkbox_field_0'])) { |
|
164 | + add_filter('script_loader_src', array($this, 'filter_remove_qs'), 15, 1); |
|
165 | + add_filter('style_loader_src', array($this, 'filter_remove_qs'), 15, 1); |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | // Avoiding conflicts of interest when async-javascript plugin is active! |
169 | - $async_js_plugin_active = autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ); |
|
170 | - if ( ! empty( $options['autoptimize_extra_text_field_3'] ) && ! $async_js_plugin_active ) { |
|
171 | - add_filter( 'autoptimize_filter_js_exclude', array( $this, 'extra_async_js' ), 10, 1 ); |
|
169 | + $async_js_plugin_active = autoptimizeUtils::is_plugin_active('async-javascript/async-javascript.php'); |
|
170 | + if (!empty($options['autoptimize_extra_text_field_3']) && !$async_js_plugin_active) { |
|
171 | + add_filter('autoptimize_filter_js_exclude', array($this, 'extra_async_js'), 10, 1); |
|
172 | 172 | } |
173 | 173 | |
174 | 174 | // Optimize google fonts! |
175 | - if ( ! empty( $options['autoptimize_extra_radio_field_4'] ) && ( '1' !== $options['autoptimize_extra_radio_field_4'] ) ) { |
|
176 | - add_filter( 'wp_resource_hints', array( $this, 'filter_remove_gfonts_dnsprefetch' ), 10, 2 ); |
|
177 | - add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_optimize_google_fonts' ), 10, 1 ); |
|
178 | - add_filter( 'autoptimize_extra_filter_tobepreconn', array( $this, 'filter_preconnect_google_fonts' ), 10, 1 ); |
|
175 | + if (!empty($options['autoptimize_extra_radio_field_4']) && ('1' !== $options['autoptimize_extra_radio_field_4'])) { |
|
176 | + add_filter('wp_resource_hints', array($this, 'filter_remove_gfonts_dnsprefetch'), 10, 2); |
|
177 | + add_filter('autoptimize_html_after_minify', array($this, 'filter_optimize_google_fonts'), 10, 1); |
|
178 | + add_filter('autoptimize_extra_filter_tobepreconn', array($this, 'filter_preconnect_google_fonts'), 10, 1); |
|
179 | 179 | } |
180 | 180 | |
181 | 181 | // Preconnect! |
182 | - if ( ! empty( $options['autoptimize_extra_text_field_2'] ) || has_filter( 'autoptimize_extra_filter_tobepreconn' ) ) { |
|
183 | - add_filter( 'wp_resource_hints', array( $this, 'filter_preconnect' ), 10, 2 ); |
|
182 | + if (!empty($options['autoptimize_extra_text_field_2']) || has_filter('autoptimize_extra_filter_tobepreconn')) { |
|
183 | + add_filter('wp_resource_hints', array($this, 'filter_preconnect'), 10, 2); |
|
184 | 184 | } |
185 | 185 | |
186 | 186 | // Preload! |
187 | - if ( ! empty( $options['autoptimize_extra_text_field_7'] ) ) { |
|
188 | - add_filter( 'autoptimize_html_after_minify', array( $this, 'filter_preload' ), 10, 2 ); |
|
187 | + if (!empty($options['autoptimize_extra_text_field_7'])) { |
|
188 | + add_filter('autoptimize_html_after_minify', array($this, 'filter_preload'), 10, 2); |
|
189 | 189 | } |
190 | 190 | } |
191 | 191 | |
192 | - public function filter_remove_emoji_dns_prefetch( $urls, $relation_type ) |
|
192 | + public function filter_remove_emoji_dns_prefetch($urls, $relation_type) |
|
193 | 193 | { |
194 | - $emoji_svg_url = apply_filters( 'emoji_svg_url', 'https://s.w.org/images/core/emoji/' ); |
|
194 | + $emoji_svg_url = apply_filters('emoji_svg_url', 'https://s.w.org/images/core/emoji/'); |
|
195 | 195 | |
196 | - return $this->filter_remove_dns_prefetch( $urls, $relation_type, $emoji_svg_url ); |
|
196 | + return $this->filter_remove_dns_prefetch($urls, $relation_type, $emoji_svg_url); |
|
197 | 197 | } |
198 | 198 | |
199 | - public function filter_remove_gfonts_dnsprefetch( $urls, $relation_type ) |
|
199 | + public function filter_remove_gfonts_dnsprefetch($urls, $relation_type) |
|
200 | 200 | { |
201 | - return $this->filter_remove_dns_prefetch( $urls, $relation_type, 'fonts.googleapis.com' ); |
|
201 | + return $this->filter_remove_dns_prefetch($urls, $relation_type, 'fonts.googleapis.com'); |
|
202 | 202 | } |
203 | 203 | |
204 | - public function filter_remove_dns_prefetch( $urls, $relation_type, $url_to_remove ) |
|
204 | + public function filter_remove_dns_prefetch($urls, $relation_type, $url_to_remove) |
|
205 | 205 | { |
206 | 206 | $url_to_remove = (string) $url_to_remove; |
207 | 207 | |
208 | - if ( ! empty( $url_to_remove ) && 'dns-prefetch' === $relation_type ) { |
|
208 | + if (!empty($url_to_remove) && 'dns-prefetch' === $relation_type) { |
|
209 | 209 | $cnt = 0; |
210 | - foreach ( $urls as $url ) { |
|
211 | - if ( false !== strpos( $url, $url_to_remove ) ) { |
|
212 | - unset( $urls[ $cnt ] ); |
|
210 | + foreach ($urls as $url) { |
|
211 | + if (false !== strpos($url, $url_to_remove)) { |
|
212 | + unset($urls[$cnt]); |
|
213 | 213 | } |
214 | 214 | $cnt++; |
215 | 215 | } |
@@ -218,156 +218,156 @@ discard block |
||
218 | 218 | return $urls; |
219 | 219 | } |
220 | 220 | |
221 | - public function filter_optimize_google_fonts( $in ) |
|
221 | + public function filter_optimize_google_fonts($in) |
|
222 | 222 | { |
223 | 223 | // Extract fonts, partly based on wp rocket's extraction code. |
224 | - $markup = preg_replace( '/<!--(.*)-->/Uis', '', $in ); |
|
225 | - preg_match_all( '#<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>#iU', $markup, $matches ); |
|
224 | + $markup = preg_replace('/<!--(.*)-->/Uis', '', $in); |
|
225 | + preg_match_all('#<link(?:\s+(?:(?!href\s*=\s*)[^>])+)?(?:\s+href\s*=\s*([\'"])((?:https?:)?\/\/fonts\.googleapis\.com\/css(?:(?!\1).)+)\1)(?:\s+[^>]*)?>#iU', $markup, $matches); |
|
226 | 226 | |
227 | 227 | $fonts_collection = array(); |
228 | - if ( ! $matches[2] ) { |
|
228 | + if (!$matches[2]) { |
|
229 | 229 | return $in; |
230 | 230 | } |
231 | 231 | |
232 | 232 | // Store them in $fonts array. |
233 | 233 | $i = 0; |
234 | - foreach ( $matches[2] as $font ) { |
|
235 | - if ( ! preg_match( '/rel=["\']dns-prefetch["\']/', $matches[0][ $i ] ) ) { |
|
234 | + foreach ($matches[2] as $font) { |
|
235 | + if (!preg_match('/rel=["\']dns-prefetch["\']/', $matches[0][$i])) { |
|
236 | 236 | // Get fonts name. |
237 | - $font = str_replace( array( '%7C', '%7c' ), '|', $font ); |
|
238 | - if ( strpos( $font, 'fonts.googleapis.com/css2' ) !== false ) { |
|
237 | + $font = str_replace(array('%7C', '%7c'), '|', $font); |
|
238 | + if (strpos($font, 'fonts.googleapis.com/css2') !== false) { |
|
239 | 239 | // (Somewhat) change Google Fonts APIv2 syntax back to v1. |
240 | - $font = str_replace( array( 'wght@', 'wght%40', ';', '%3B' ), array( '', '', ',', ',' ), $font ); |
|
240 | + $font = str_replace(array('wght@', 'wght%40', ';', '%3B'), array('', '', ',', ','), $font); |
|
241 | 241 | } |
242 | - $font = explode( 'family=', $font ); |
|
243 | - $font = ( isset( $font[1] ) ) ? explode( '&', $font[1] ) : array(); |
|
242 | + $font = explode('family=', $font); |
|
243 | + $font = (isset($font[1])) ? explode('&', $font[1]) : array(); |
|
244 | 244 | // Add font to $fonts[$i] but make sure not to pollute with an empty family! |
245 | - $_thisfont = array_values( array_filter( explode( '|', reset( $font ) ) ) ); |
|
246 | - if ( ! empty( $_thisfont ) ) { |
|
247 | - $fonts_collection[ $i ]['fonts'] = $_thisfont; |
|
245 | + $_thisfont = array_values(array_filter(explode('|', reset($font)))); |
|
246 | + if (!empty($_thisfont)) { |
|
247 | + $fonts_collection[$i]['fonts'] = $_thisfont; |
|
248 | 248 | // And add subset if any! |
249 | - $subset = ( is_array( $font ) ) ? end( $font ) : ''; |
|
250 | - if ( false !== strpos( $subset, 'subset=' ) ) { |
|
251 | - $subset = str_replace( array( '%2C', '%2c' ), ',', $subset ); |
|
252 | - $subset = explode( 'subset=', $subset ); |
|
253 | - $fonts_collection[ $i ]['subsets'] = explode( ',', $subset[1] ); |
|
249 | + $subset = (is_array($font)) ? end($font) : ''; |
|
250 | + if (false !== strpos($subset, 'subset=')) { |
|
251 | + $subset = str_replace(array('%2C', '%2c'), ',', $subset); |
|
252 | + $subset = explode('subset=', $subset); |
|
253 | + $fonts_collection[$i]['subsets'] = explode(',', $subset[1]); |
|
254 | 254 | } |
255 | 255 | } |
256 | 256 | // And remove Google Fonts. |
257 | - $in = str_replace( $matches[0][ $i ], '', $in ); |
|
257 | + $in = str_replace($matches[0][$i], '', $in); |
|
258 | 258 | } |
259 | 259 | $i++; |
260 | 260 | } |
261 | 261 | |
262 | 262 | $options = $this->options; |
263 | 263 | $fonts_markup = ''; |
264 | - if ( '2' === $options['autoptimize_extra_radio_field_4'] ) { |
|
264 | + if ('2' === $options['autoptimize_extra_radio_field_4']) { |
|
265 | 265 | // Remove Google Fonts. |
266 | - unset( $fonts_collection ); |
|
266 | + unset($fonts_collection); |
|
267 | 267 | return $in; |
268 | - } elseif ( '3' === $options['autoptimize_extra_radio_field_4'] || '5' === $options['autoptimize_extra_radio_field_4'] ) { |
|
268 | + } elseif ('3' === $options['autoptimize_extra_radio_field_4'] || '5' === $options['autoptimize_extra_radio_field_4']) { |
|
269 | 269 | // Aggregate & link! |
270 | 270 | $fonts_string = ''; |
271 | 271 | $subset_string = ''; |
272 | - foreach ( $fonts_collection as $font ) { |
|
273 | - $fonts_string .= '|' . trim( implode( '|', $font['fonts'] ), '|' ); |
|
274 | - if ( ! empty( $font['subsets'] ) ) { |
|
275 | - $subset_string .= ',' . trim( implode( ',', $font['subsets'] ), ',' ); |
|
272 | + foreach ($fonts_collection as $font) { |
|
273 | + $fonts_string .= '|'.trim(implode('|', $font['fonts']), '|'); |
|
274 | + if (!empty($font['subsets'])) { |
|
275 | + $subset_string .= ','.trim(implode(',', $font['subsets']), ','); |
|
276 | 276 | } |
277 | 277 | } |
278 | 278 | |
279 | - if ( ! empty( $subset_string ) ) { |
|
280 | - $subset_string = str_replace( ',', '%2C', ltrim( $subset_string, ',' ) ); |
|
281 | - $fonts_string = $fonts_string . '&subset=' . $subset_string; |
|
279 | + if (!empty($subset_string)) { |
|
280 | + $subset_string = str_replace(',', '%2C', ltrim($subset_string, ',')); |
|
281 | + $fonts_string = $fonts_string.'&subset='.$subset_string; |
|
282 | 282 | } |
283 | 283 | |
284 | - $fonts_string = apply_filters( 'autoptimize_filter_extra_gfont_fontstring', str_replace( '|', '%7C', ltrim( $fonts_string, '|' ) ) ); |
|
284 | + $fonts_string = apply_filters('autoptimize_filter_extra_gfont_fontstring', str_replace('|', '%7C', ltrim($fonts_string, '|'))); |
|
285 | 285 | // only add display parameter if there is none in $fonts_string (by virtue of the filter). |
286 | - if ( strpos( $fonts_string, 'display=' ) === false ) { |
|
287 | - $fonts_string .= apply_filters( 'autoptimize_filter_extra_gfont_display', '&display=swap' ); |
|
286 | + if (strpos($fonts_string, 'display=') === false) { |
|
287 | + $fonts_string .= apply_filters('autoptimize_filter_extra_gfont_display', '&display=swap'); |
|
288 | 288 | } |
289 | 289 | |
290 | - if ( ! empty( $fonts_string ) ) { |
|
291 | - if ( '5' === $options['autoptimize_extra_radio_field_4'] ) { |
|
292 | - $rel_string = 'rel="preload" as="style" onload="' . autoptimizeConfig::get_ao_css_preload_onload() . '"'; |
|
290 | + if (!empty($fonts_string)) { |
|
291 | + if ('5' === $options['autoptimize_extra_radio_field_4']) { |
|
292 | + $rel_string = 'rel="preload" as="style" onload="'.autoptimizeConfig::get_ao_css_preload_onload().'"'; |
|
293 | 293 | } else { |
294 | 294 | $rel_string = 'rel="stylesheet"'; |
295 | 295 | } |
296 | - $fonts_markup = '<link ' . $rel_string . ' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family=' . $fonts_string . '" />'; |
|
296 | + $fonts_markup = '<link '.$rel_string.' id="ao_optimized_gfonts" href="https://fonts.googleapis.com/css?family='.$fonts_string.'" />'; |
|
297 | 297 | } |
298 | - } elseif ( '4' === $options['autoptimize_extra_radio_field_4'] ) { |
|
298 | + } elseif ('4' === $options['autoptimize_extra_radio_field_4']) { |
|
299 | 299 | // Aggregate & load async (webfont.js impl.)! |
300 | 300 | $fonts_array = array(); |
301 | - foreach ( $fonts_collection as $_fonts ) { |
|
302 | - if ( ! empty( $_fonts['subsets'] ) ) { |
|
303 | - $_subset = implode( ',', $_fonts['subsets'] ); |
|
304 | - foreach ( $_fonts['fonts'] as $key => $_one_font ) { |
|
305 | - $_one_font = $_one_font . ':' . $_subset; |
|
306 | - $_fonts['fonts'][ $key ] = $_one_font; |
|
301 | + foreach ($fonts_collection as $_fonts) { |
|
302 | + if (!empty($_fonts['subsets'])) { |
|
303 | + $_subset = implode(',', $_fonts['subsets']); |
|
304 | + foreach ($_fonts['fonts'] as $key => $_one_font) { |
|
305 | + $_one_font = $_one_font.':'.$_subset; |
|
306 | + $_fonts['fonts'][$key] = $_one_font; |
|
307 | 307 | } |
308 | 308 | } |
309 | - $fonts_array = array_merge( $fonts_array, $_fonts['fonts'] ); |
|
309 | + $fonts_array = array_merge($fonts_array, $_fonts['fonts']); |
|
310 | 310 | } |
311 | 311 | |
312 | - $fonts_array = array_map( 'urldecode', $fonts_array ); |
|
312 | + $fonts_array = array_map('urldecode', $fonts_array); |
|
313 | 313 | $fonts_array = array_map( |
314 | - function( $_f ) { |
|
315 | - return trim( $_f, ',' ); |
|
314 | + function($_f) { |
|
315 | + return trim($_f, ','); |
|
316 | 316 | }, |
317 | 317 | $fonts_array |
318 | 318 | ); |
319 | 319 | |
320 | 320 | // type attrib on <script not added by default. |
321 | 321 | $type_js = ''; |
322 | - if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { |
|
322 | + if (apply_filters('autoptimize_filter_cssjs_addtype', false)) { |
|
323 | 323 | $type_js = 'type="text/javascript" '; |
324 | 324 | } |
325 | 325 | |
326 | - $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>'; |
|
327 | - $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>'; |
|
328 | - $in = substr_replace( $in, $fonts_library_markup . '</head>', strpos( $in, '</head>' ), strlen( '</head>' ) ); |
|
326 | + $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>'; |
|
327 | + $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>'; |
|
328 | + $in = substr_replace($in, $fonts_library_markup.'</head>', strpos($in, '</head>'), strlen('</head>')); |
|
329 | 329 | } |
330 | 330 | |
331 | 331 | // Replace back in markup. |
332 | - $inject_point = apply_filters( 'autoptimize_filter_extra_gfont_injectpoint', '<link' ); |
|
333 | - $out = substr_replace( $in, $fonts_markup . $inject_point, strpos( $in, $inject_point ), strlen( $inject_point ) ); |
|
334 | - unset( $fonts_collection ); |
|
332 | + $inject_point = apply_filters('autoptimize_filter_extra_gfont_injectpoint', '<link'); |
|
333 | + $out = substr_replace($in, $fonts_markup.$inject_point, strpos($in, $inject_point), strlen($inject_point)); |
|
334 | + unset($fonts_collection); |
|
335 | 335 | |
336 | 336 | // and insert preload polyfill if "link preload" and if the polyfill isn't there yet (courtesy of inline&defer). |
337 | 337 | $preload_polyfill = autoptimizeConfig::get_ao_css_preload_polyfill(); |
338 | - if ( '5' === $options['autoptimize_extra_radio_field_4'] && strpos( $out, $preload_polyfill ) === false ) { |
|
339 | - $out = str_replace( '</body>', $preload_polyfill . '</body>', $out ); |
|
338 | + if ('5' === $options['autoptimize_extra_radio_field_4'] && strpos($out, $preload_polyfill) === false) { |
|
339 | + $out = str_replace('</body>', $preload_polyfill.'</body>', $out); |
|
340 | 340 | } |
341 | 341 | return $out; |
342 | 342 | } |
343 | 343 | |
344 | - public function filter_preconnect( $hints, $relation_type ) |
|
344 | + public function filter_preconnect($hints, $relation_type) |
|
345 | 345 | { |
346 | 346 | $options = $this->options; |
347 | 347 | $preconns = array(); |
348 | 348 | |
349 | 349 | // Get settings and store in array. |
350 | - if ( array_key_exists( 'autoptimize_extra_text_field_2', $options ) ) { |
|
351 | - $preconns = array_filter( array_map( 'trim', explode( ',', $options['autoptimize_extra_text_field_2'] ) ) ); |
|
350 | + if (array_key_exists('autoptimize_extra_text_field_2', $options)) { |
|
351 | + $preconns = array_filter(array_map('trim', explode(',', $options['autoptimize_extra_text_field_2']))); |
|
352 | 352 | } |
353 | - $preconns = apply_filters( 'autoptimize_extra_filter_tobepreconn', $preconns ); |
|
353 | + $preconns = apply_filters('autoptimize_extra_filter_tobepreconn', $preconns); |
|
354 | 354 | |
355 | 355 | // Walk array, extract domain and add to new array with crossorigin attribute. |
356 | - foreach ( $preconns as $preconn ) { |
|
356 | + foreach ($preconns as $preconn) { |
|
357 | 357 | $domain = ''; |
358 | - $parsed = parse_url( $preconn ); |
|
359 | - if ( is_array( $parsed ) && ! empty( $parsed['host'] ) && empty( $parsed['scheme'] ) ) { |
|
360 | - $domain = '//' . $parsed['host']; |
|
361 | - } elseif ( is_array( $parsed ) && ! empty( $parsed['host'] ) ) { |
|
362 | - $domain = $parsed['scheme'] . '://' . $parsed['host']; |
|
358 | + $parsed = parse_url($preconn); |
|
359 | + if (is_array($parsed) && !empty($parsed['host']) && empty($parsed['scheme'])) { |
|
360 | + $domain = '//'.$parsed['host']; |
|
361 | + } elseif (is_array($parsed) && !empty($parsed['host'])) { |
|
362 | + $domain = $parsed['scheme'].'://'.$parsed['host']; |
|
363 | 363 | } |
364 | 364 | |
365 | - if ( ! empty( $domain ) ) { |
|
366 | - $hint = array( 'href' => $domain ); |
|
365 | + if (!empty($domain)) { |
|
366 | + $hint = array('href' => $domain); |
|
367 | 367 | // Fonts don't get preconnected unless crossorigin flag is set, non-fonts don't get preconnected if origin flag is set |
368 | 368 | // so hardcode fonts.gstatic.com to come with crossorigin and have filter to add other domains if needed. |
369 | - $crossorigins = apply_filters( 'autoptimize_extra_filter_preconn_crossorigin', array( 'https://fonts.gstatic.com' ) ); |
|
370 | - if ( in_array( $domain, $crossorigins ) ) { |
|
369 | + $crossorigins = apply_filters('autoptimize_extra_filter_preconn_crossorigin', array('https://fonts.gstatic.com')); |
|
370 | + if (in_array($domain, $crossorigins)) { |
|
371 | 371 | $hint['crossorigin'] = 'anonymous'; |
372 | 372 | } |
373 | 373 | $new_hints[] = $hint; |
@@ -375,21 +375,21 @@ discard block |
||
375 | 375 | } |
376 | 376 | |
377 | 377 | // Merge in WP's preconnect hints. |
378 | - if ( 'preconnect' === $relation_type && ! empty( $new_hints ) ) { |
|
379 | - $hints = array_merge( $hints, $new_hints ); |
|
378 | + if ('preconnect' === $relation_type && !empty($new_hints)) { |
|
379 | + $hints = array_merge($hints, $new_hints); |
|
380 | 380 | } |
381 | 381 | |
382 | 382 | return $hints; |
383 | 383 | } |
384 | 384 | |
385 | - public function filter_preconnect_google_fonts( $in ) |
|
385 | + public function filter_preconnect_google_fonts($in) |
|
386 | 386 | { |
387 | - if ( '2' !== $this->options['autoptimize_extra_radio_field_4'] ) { |
|
387 | + if ('2' !== $this->options['autoptimize_extra_radio_field_4']) { |
|
388 | 388 | // Preconnect to fonts.gstatic.com unless we remove gfonts. |
389 | 389 | $in[] = 'https://fonts.gstatic.com'; |
390 | 390 | } |
391 | 391 | |
392 | - if ( '4' === $this->options['autoptimize_extra_radio_field_4'] ) { |
|
392 | + if ('4' === $this->options['autoptimize_extra_radio_field_4']) { |
|
393 | 393 | // Preconnect even more hosts for webfont.js! |
394 | 394 | $in[] = 'https://ajax.googleapis.com'; |
395 | 395 | $in[] = 'https://fonts.googleapis.com'; |
@@ -398,76 +398,76 @@ discard block |
||
398 | 398 | return $in; |
399 | 399 | } |
400 | 400 | |
401 | - public function filter_preload( $in ) { |
|
401 | + public function filter_preload($in) { |
|
402 | 402 | // make array from comma separated list. |
403 | 403 | $options = $this->options; |
404 | 404 | $preloads = array(); |
405 | - if ( array_key_exists( 'autoptimize_extra_text_field_7', $options ) ) { |
|
406 | - $preloads = array_filter( array_map( 'trim', explode( ',', $options['autoptimize_extra_text_field_7'] ) ) ); |
|
405 | + if (array_key_exists('autoptimize_extra_text_field_7', $options)) { |
|
406 | + $preloads = array_filter(array_map('trim', explode(',', $options['autoptimize_extra_text_field_7']))); |
|
407 | 407 | } |
408 | - $preloads = apply_filters( 'autoptimize_filter_extra_tobepreloaded', $preloads ); |
|
408 | + $preloads = apply_filters('autoptimize_filter_extra_tobepreloaded', $preloads); |
|
409 | 409 | |
410 | 410 | // immediately return if nothing to be preloaded. |
411 | - if ( empty( $preloads ) ) { |
|
411 | + if (empty($preloads)) { |
|
412 | 412 | return $in; |
413 | 413 | } |
414 | 414 | |
415 | 415 | // iterate through array and add preload link to tmp string. |
416 | 416 | $preload_output = ''; |
417 | - foreach ( $preloads as $preload ) { |
|
417 | + foreach ($preloads as $preload) { |
|
418 | 418 | $crossorigin = ''; |
419 | 419 | $preload_as = ''; |
420 | 420 | $mime_type = ''; |
421 | - $_preload = strtok( $preload, '?' ); |
|
421 | + $_preload = strtok($preload, '?'); |
|
422 | 422 | |
423 | - if ( autoptimizeUtils::str_ends_in( $_preload, '.css' ) ) { |
|
423 | + if (autoptimizeUtils::str_ends_in($_preload, '.css')) { |
|
424 | 424 | $preload_as = 'style'; |
425 | - } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.js' ) ) { |
|
425 | + } elseif (autoptimizeUtils::str_ends_in($_preload, '.js')) { |
|
426 | 426 | $preload_as = 'script'; |
427 | - } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.woff' ) || autoptimizeUtils::str_ends_in( $_preload, '.woff2' ) || autoptimizeUtils::str_ends_in( $_preload, '.ttf' ) || autoptimizeUtils::str_ends_in( $_preload, '.eot' ) ) { |
|
427 | + } elseif (autoptimizeUtils::str_ends_in($_preload, '.woff') || autoptimizeUtils::str_ends_in($_preload, '.woff2') || autoptimizeUtils::str_ends_in($_preload, '.ttf') || autoptimizeUtils::str_ends_in($_preload, '.eot')) { |
|
428 | 428 | $preload_as = 'font'; |
429 | 429 | $crossorigin = ' crossorigin'; |
430 | - $mime_type = ' type="font/' . pathinfo( $_preload, PATHINFO_EXTENSION ) . '"'; |
|
431 | - if ( ' type="font/eot"' === $mime_type ) { |
|
430 | + $mime_type = ' type="font/'.pathinfo($_preload, PATHINFO_EXTENSION).'"'; |
|
431 | + if (' type="font/eot"' === $mime_type) { |
|
432 | 432 | $mime_type = 'application/vnd.ms-fontobject'; |
433 | 433 | } |
434 | - } elseif ( autoptimizeUtils::str_ends_in( $_preload, '.jpeg' ) || autoptimizeUtils::str_ends_in( $_preload, '.jpg' ) || autoptimizeUtils::str_ends_in( $_preload, '.webp' ) || autoptimizeUtils::str_ends_in( $_preload, '.png' ) || autoptimizeUtils::str_ends_in( $_preload, '.gif' ) ) { |
|
434 | + } elseif (autoptimizeUtils::str_ends_in($_preload, '.jpeg') || autoptimizeUtils::str_ends_in($_preload, '.jpg') || autoptimizeUtils::str_ends_in($_preload, '.webp') || autoptimizeUtils::str_ends_in($_preload, '.png') || autoptimizeUtils::str_ends_in($_preload, '.gif')) { |
|
435 | 435 | $preload_as = 'image'; |
436 | 436 | } else { |
437 | 437 | $preload_as = 'other'; |
438 | 438 | } |
439 | 439 | |
440 | - $preload_output .= '<link rel="preload" href="' . $preload . '" as="' . $preload_as . '"' . $mime_type . $crossorigin . '>'; |
|
440 | + $preload_output .= '<link rel="preload" href="'.$preload.'" as="'.$preload_as.'"'.$mime_type.$crossorigin.'>'; |
|
441 | 441 | } |
442 | - $preload_output = apply_filters( 'autoptimize_filter_extra_preload_output', $preload_output ); |
|
442 | + $preload_output = apply_filters('autoptimize_filter_extra_preload_output', $preload_output); |
|
443 | 443 | |
444 | 444 | // add string to head (before first link node by default). |
445 | - $preload_inject = apply_filters( 'autoptimize_filter_extra_preload_inject', '<link' ); |
|
446 | - $position = autoptimizeUtils::strpos( $in, $preload_inject ); |
|
445 | + $preload_inject = apply_filters('autoptimize_filter_extra_preload_inject', '<link'); |
|
446 | + $position = autoptimizeUtils::strpos($in, $preload_inject); |
|
447 | 447 | |
448 | - return autoptimizeUtils::substr_replace( $in, $preload_output . $preload_inject, $position, strlen( $preload_inject ) ); |
|
448 | + return autoptimizeUtils::substr_replace($in, $preload_output.$preload_inject, $position, strlen($preload_inject)); |
|
449 | 449 | } |
450 | 450 | |
451 | 451 | public function admin_menu() |
452 | 452 | { |
453 | 453 | // no acces if multisite and not network admin and no site config allowed. |
454 | - if ( autoptimizeConfig::should_show_menu_tabs() ) { |
|
454 | + if (autoptimizeConfig::should_show_menu_tabs()) { |
|
455 | 455 | add_submenu_page( |
456 | 456 | null, |
457 | 457 | 'autoptimize_extra', |
458 | 458 | 'autoptimize_extra', |
459 | 459 | 'manage_options', |
460 | 460 | 'autoptimize_extra', |
461 | - array( $this, 'options_page' ) |
|
461 | + array($this, 'options_page') |
|
462 | 462 | ); |
463 | 463 | } |
464 | - register_setting( 'autoptimize_extra_settings', 'autoptimize_extra_settings' ); |
|
464 | + register_setting('autoptimize_extra_settings', 'autoptimize_extra_settings'); |
|
465 | 465 | } |
466 | 466 | |
467 | - public function add_extra_tab( $in ) |
|
467 | + public function add_extra_tab($in) |
|
468 | 468 | { |
469 | - if ( autoptimizeConfig::should_show_menu_tabs() ) { |
|
470 | - $in = array_merge( $in, array( 'autoptimize_extra' => __( 'Extra', 'autoptimize' ) ) ); |
|
469 | + if (autoptimizeConfig::should_show_menu_tabs()) { |
|
470 | + $in = array_merge($in, array('autoptimize_extra' => __('Extra', 'autoptimize'))); |
|
471 | 471 | } |
472 | 472 | |
473 | 473 | return $in; |
@@ -487,97 +487,97 @@ discard block |
||
487 | 487 | #ao_settings_form .form-table th {font-weight: normal;} |
488 | 488 | #autoptimize_extra_descr{font-size: 120%;} |
489 | 489 | </style> |
490 | - <script>document.title = "Autoptimize: <?php _e( 'Extra', 'autoptimize' ); ?> " + document.title;</script> |
|
490 | + <script>document.title = "Autoptimize: <?php _e('Extra', 'autoptimize'); ?> " + document.title;</script> |
|
491 | 491 | <div class="wrap"> |
492 | - <h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1> |
|
492 | + <h1><?php _e('Autoptimize Settings', 'autoptimize'); ?></h1> |
|
493 | 493 | <?php echo autoptimizeConfig::ao_admin_tabs(); ?> |
494 | - <?php if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_js' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css' ) && 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_html' ) && ! autoptimizeImages::imgopt_active() ) { ?> |
|
494 | + <?php if ('on' !== autoptimizeOptionWrapper::get_option('autoptimize_js') && 'on' !== autoptimizeOptionWrapper::get_option('autoptimize_css') && 'on' !== autoptimizeOptionWrapper::get_option('autoptimize_html') && !autoptimizeImages::imgopt_active()) { ?> |
|
495 | 495 | <div class="notice-warning notice"><p> |
496 | - <?php _e( 'Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize' ); ?> |
|
496 | + <?php _e('Most of below Extra optimizations require at least one of HTML, JS, CSS or Image autoptimizations being active.', 'autoptimize'); ?> |
|
497 | 497 | </p></div> |
498 | 498 | <?php } ?> |
499 | 499 | |
500 | - <form id='ao_settings_form' action='<?php echo admin_url( 'options.php' ); ?>' method='post'> |
|
501 | - <?php settings_fields( 'autoptimize_extra_settings' ); ?> |
|
502 | - <h2><?php _e( 'Extra Auto-Optimizations', 'autoptimize' ); ?></h2> |
|
503 | - <span id='autoptimize_extra_descr'><?php _e( 'The following settings can improve your site\'s performance even more.', 'autoptimize' ); ?></span> |
|
500 | + <form id='ao_settings_form' action='<?php echo admin_url('options.php'); ?>' method='post'> |
|
501 | + <?php settings_fields('autoptimize_extra_settings'); ?> |
|
502 | + <h2><?php _e('Extra Auto-Optimizations', 'autoptimize'); ?></h2> |
|
503 | + <span id='autoptimize_extra_descr'><?php _e('The following settings can improve your site\'s performance even more.', 'autoptimize'); ?></span> |
|
504 | 504 | <table class="form-table"> |
505 | 505 | <tr> |
506 | - <th scope="row"><?php _e( 'Google Fonts', 'autoptimize' ); ?></th> |
|
506 | + <th scope="row"><?php _e('Google Fonts', 'autoptimize'); ?></th> |
|
507 | 507 | <td> |
508 | - <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/> |
|
509 | - <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/> |
|
508 | + <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/> |
|
509 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="2" <?php checked(2, $gfonts, true); ?> ><?php _e('Remove Google Fonts', 'autoptimize'); ?><br/> |
|
510 | 510 | <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?> |
511 | - <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/> |
|
511 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="3" <?php checked(3, $gfonts, true); ?> ><?php echo __('Combine and link in head (fonts load fast but are render-blocking)', 'autoptimize').', '.sprintf(__('includes %1$sdisplay:swap%2$s.', 'autoptimize'), '<code>', '</code>'); ?><br/> |
|
512 | 512 | <?php // translators: "display:swap" should remain untranslated, will be shown in code tags. ?> |
513 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="5" <?php checked( 5, $gfonts, true ); ?> ><?php echo __( 'Combine and preload in head (fonts load late, but are not render-blocking)', 'autoptimize' ) . ', ' . sprintf( __( 'includes %1$sdisplay:swap%2$s.', 'autoptimize' ), '<code>', '</code>' ); ?><br/> |
|
514 | - <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="4" <?php checked( 4, $gfonts, true ); ?> ><?php _e( 'Combine and load fonts asynchronously with <a href="https://github.com/typekit/webfontloader#readme" target="_blank">webfont.js</a>', 'autoptimize' ); ?><br/> |
|
513 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="5" <?php checked(5, $gfonts, true); ?> ><?php echo __('Combine and preload in head (fonts load late, but are not render-blocking)', 'autoptimize').', '.sprintf(__('includes %1$sdisplay:swap%2$s.', 'autoptimize'), '<code>', '</code>'); ?><br/> |
|
514 | + <input type="radio" name="autoptimize_extra_settings[autoptimize_extra_radio_field_4]" value="4" <?php checked(4, $gfonts, true); ?> ><?php _e('Combine and load fonts asynchronously with <a href="https://github.com/typekit/webfontloader#readme" target="_blank">webfont.js</a>', 'autoptimize'); ?><br/> |
|
515 | 515 | </td> |
516 | 516 | </tr> |
517 | 517 | <tr> |
518 | - <th scope="row"><?php _e( 'Remove emojis', 'autoptimize' ); ?></th> |
|
518 | + <th scope="row"><?php _e('Remove emojis', 'autoptimize'); ?></th> |
|
519 | 519 | <td> |
520 | - <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> |
|
520 | + <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> |
|
521 | 521 | </td> |
522 | 522 | </tr> |
523 | 523 | <tr> |
524 | - <th scope="row"><?php _e( 'Remove query strings from static resources', 'autoptimize' ); ?></th> |
|
524 | + <th scope="row"><?php _e('Remove query strings from static resources', 'autoptimize'); ?></th> |
|
525 | 525 | <td> |
526 | - <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> |
|
526 | + <label><input type='checkbox' name='autoptimize_extra_settings[autoptimize_extra_checkbox_field_0]' <?php if (!empty($options['autoptimize_extra_checkbox_field_0']) && '1' === $options['autoptimize_extra_checkbox_field_0']) { echo 'checked="checked"'; } ?> value='1'><?php _e('Removing query strings (or more specifically the <code>ver</code> parameter) will not improve load time, but might improve performance scores.', 'autoptimize'); ?></label> |
|
527 | 527 | </td> |
528 | 528 | </tr> |
529 | 529 | <tr> |
530 | - <th scope="row"><?php _e( 'Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize' ); ?></th> |
|
530 | + <th scope="row"><?php _e('Preconnect to 3rd party domains <em>(advanced users)</em>', 'autoptimize'); ?></th> |
|
531 | 531 | <td> |
532 | - <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> |
|
532 | + <label><input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_2]' value='<?php if (array_key_exists('autoptimize_extra_text_field_2', $options)) { echo esc_attr($options['autoptimize_extra_text_field_2']); } ?>'><br /><?php _e('Add 3rd party domains you want the browser to <a href="https://www.keycdn.com/support/preconnect/#primary" target="_blank">preconnect</a> to, separated by comma\'s. Make sure to include the correct protocol (HTTP or HTTPS).', 'autoptimize'); ?></label> |
|
533 | 533 | </td> |
534 | 534 | </tr> |
535 | 535 | <tr> |
536 | - <th scope="row"><?php _e( 'Preload specific requests <em>(advanced users)</em>', 'autoptimize' ); ?></th> |
|
536 | + <th scope="row"><?php _e('Preload specific requests <em>(advanced users)</em>', 'autoptimize'); ?></th> |
|
537 | 537 | <td> |
538 | - <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> |
|
538 | + <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> |
|
539 | 539 | </td> |
540 | 540 | </tr> |
541 | 541 | <tr> |
542 | - <th scope="row"><?php _e( 'Async Javascript-files <em>(advanced users)</em>', 'autoptimize' ); ?></th> |
|
542 | + <th scope="row"><?php _e('Async Javascript-files <em>(advanced users)</em>', 'autoptimize'); ?></th> |
|
543 | 543 | <td> |
544 | 544 | <?php |
545 | - if ( autoptimizeUtils::is_plugin_active( 'async-javascript/async-javascript.php' ) ) { |
|
545 | + if (autoptimizeUtils::is_plugin_active('async-javascript/async-javascript.php')) { |
|
546 | 546 | // translators: link points Async Javascript settings page. |
547 | - 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>' ); |
|
547 | + printf(__('You have "Async JavaScript" installed, %1$sconfiguration of async javascript is best done there%2$s.', 'autoptimize'), '<a href="'.'options-general.php?page=async-javascript'.'">', '</a>'); |
|
548 | 548 | } else { |
549 | 549 | ?> |
550 | - <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'] ); } ?>'> |
|
550 | + <input type='text' style='width:80%' name='autoptimize_extra_settings[autoptimize_extra_text_field_3]' value='<?php if (array_key_exists('autoptimize_extra_text_field_3', $options)) { echo esc_attr($options['autoptimize_extra_text_field_3']); } ?>'> |
|
551 | 551 | <br /> |
552 | 552 | <?php |
553 | - _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' ); |
|
553 | + _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'); |
|
554 | 554 | // translators: %s will be replaced by a link to the "async javascript" plugin. |
555 | - 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>"' ); |
|
556 | - $asj_install_url = network_admin_url() . 'plugin-install.php?s=async+javascript&tab=search&type=term'; |
|
557 | - echo sprintf( ' <a href="' . $asj_install_url . '">%s</a>', __( 'Click here to install and activate it.', 'autoptimize' ) ); |
|
555 | + 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>"'); |
|
556 | + $asj_install_url = network_admin_url().'plugin-install.php?s=async+javascript&tab=search&type=term'; |
|
557 | + echo sprintf(' <a href="'.$asj_install_url.'">%s</a>', __('Click here to install and activate it.', 'autoptimize')); |
|
558 | 558 | } |
559 | 559 | ?> |
560 | 560 | </td> |
561 | 561 | </tr> |
562 | 562 | <tr> |
563 | - <th scope="row"><?php _e( 'Optimize YouTube videos', 'autoptimize' ); ?></th> |
|
563 | + <th scope="row"><?php _e('Optimize YouTube videos', 'autoptimize'); ?></th> |
|
564 | 564 | <td> |
565 | 565 | <?php |
566 | - if ( autoptimizeUtils::is_plugin_active( 'wp-youtube-lyte/wp-youtube-lyte.php' ) ) { |
|
567 | - _e( 'Great, you have WP YouTube Lyte installed.', 'autoptimize' ); |
|
566 | + if (autoptimizeUtils::is_plugin_active('wp-youtube-lyte/wp-youtube-lyte.php')) { |
|
567 | + _e('Great, you have WP YouTube Lyte installed.', 'autoptimize'); |
|
568 | 568 | $lyte_config_url = 'options-general.php?page=lyte_settings_page'; |
569 | - echo sprintf( ' <a href="' . $lyte_config_url . '">%s</a>', __( 'Click here to configure it.', 'autoptimize' ) ); |
|
569 | + echo sprintf(' <a href="'.$lyte_config_url.'">%s</a>', __('Click here to configure it.', 'autoptimize')); |
|
570 | 570 | } else { |
571 | 571 | // translators: %s will be replaced by a link to "wp youtube lyte" plugin. |
572 | - 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>' ); |
|
573 | - $lyte_install_url = network_admin_url() . 'plugin-install.php?s=lyte&tab=search&type=term'; |
|
574 | - echo sprintf( ' <a href="' . $lyte_install_url . '">%s</a>', __( 'Click here to install and activate it.', 'autoptimize' ) ); |
|
572 | + 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>'); |
|
573 | + $lyte_install_url = network_admin_url().'plugin-install.php?s=lyte&tab=search&type=term'; |
|
574 | + echo sprintf(' <a href="'.$lyte_install_url.'">%s</a>', __('Click here to install and activate it.', 'autoptimize')); |
|
575 | 575 | } |
576 | 576 | ?> |
577 | 577 | </td> |
578 | 578 | </tr> |
579 | 579 | </table> |
580 | - <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Save Changes', 'autoptimize' ); ?>" /></p> |
|
580 | + <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes', 'autoptimize'); ?>" /></p> |
|
581 | 581 | </form> |
582 | 582 | <?php |
583 | 583 | } |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Handles optimizing images. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -23,17 +23,17 @@ discard block |
||
23 | 23 | */ |
24 | 24 | protected static $instance = null; |
25 | 25 | |
26 | - public function __construct( array $options = array() ) |
|
26 | + public function __construct(array $options = array()) |
|
27 | 27 | { |
28 | 28 | // If options are not provided, fetch them. |
29 | - if ( empty( $options ) ) { |
|
29 | + if (empty($options)) { |
|
30 | 30 | $options = $this->fetch_options(); |
31 | 31 | } |
32 | 32 | |
33 | - $this->set_options( $options ); |
|
33 | + $this->set_options($options); |
|
34 | 34 | } |
35 | 35 | |
36 | - public function set_options( array $options ) |
|
36 | + public function set_options(array $options) |
|
37 | 37 | { |
38 | 38 | $this->options = $options; |
39 | 39 | |
@@ -42,17 +42,17 @@ discard block |
||
42 | 42 | |
43 | 43 | public static function fetch_options() |
44 | 44 | { |
45 | - $value = autoptimizeOptionWrapper::get_option( 'autoptimize_imgopt_settings' ); |
|
46 | - if ( empty( $value ) ) { |
|
45 | + $value = autoptimizeOptionWrapper::get_option('autoptimize_imgopt_settings'); |
|
46 | + if (empty($value)) { |
|
47 | 47 | // Fallback to returning defaults when no stored option exists yet. |
48 | 48 | $value = autoptimizeConfig::get_ao_imgopt_default_options(); |
49 | 49 | } |
50 | 50 | |
51 | 51 | // get service availability and add it to the options-array. |
52 | - $value['availabilities'] = autoptimizeOptionWrapper::get_option( 'autoptimize_service_availablity' ); |
|
52 | + $value['availabilities'] = autoptimizeOptionWrapper::get_option('autoptimize_service_availablity'); |
|
53 | 53 | |
54 | - if ( empty( $value['availabilities'] ) ) { |
|
55 | - $value['availabilities'] = autoptimizeUtils::check_service_availability( true ); |
|
54 | + if (empty($value['availabilities'])) { |
|
55 | + $value['availabilities'] = autoptimizeUtils::check_service_availability(true); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | return $value; |
@@ -65,9 +65,9 @@ discard block |
||
65 | 65 | // and does not use/ request the availablity data (which could slow things down). |
66 | 66 | static $imgopt_active = null; |
67 | 67 | |
68 | - if ( null === $imgopt_active ) { |
|
69 | - $opts = autoptimizeOptionWrapper::get_option( 'autoptimize_imgopt_settings', '' ); |
|
70 | - if ( ! empty( $opts ) && is_array( $opts ) && array_key_exists( 'autoptimize_imgopt_checkbox_field_1', $opts ) && ! empty( $opts['autoptimize_imgopt_checkbox_field_1'] ) && '1' === $opts['autoptimize_imgopt_checkbox_field_1'] ) { |
|
68 | + if (null === $imgopt_active) { |
|
69 | + $opts = autoptimizeOptionWrapper::get_option('autoptimize_imgopt_settings', ''); |
|
70 | + if (!empty($opts) && is_array($opts) && array_key_exists('autoptimize_imgopt_checkbox_field_1', $opts) && !empty($opts['autoptimize_imgopt_checkbox_field_1']) && '1' === $opts['autoptimize_imgopt_checkbox_field_1']) { |
|
71 | 71 | $imgopt_active = true; |
72 | 72 | } else { |
73 | 73 | $imgopt_active = false; |
@@ -87,7 +87,7 @@ discard block |
||
87 | 87 | */ |
88 | 88 | public static function instance() |
89 | 89 | { |
90 | - if ( null === self::$instance ) { |
|
90 | + if (null === self::$instance) { |
|
91 | 91 | self::$instance = new self(); |
92 | 92 | } |
93 | 93 | |
@@ -96,34 +96,34 @@ discard block |
||
96 | 96 | |
97 | 97 | public function run() |
98 | 98 | { |
99 | - if ( is_admin() ) { |
|
100 | - if ( is_multisite() && is_network_admin() && autoptimizeOptionWrapper::is_ao_active_for_network() ) { |
|
101 | - add_action( 'network_admin_menu', array( $this, 'imgopt_admin_menu' ) ); |
|
99 | + if (is_admin()) { |
|
100 | + if (is_multisite() && is_network_admin() && autoptimizeOptionWrapper::is_ao_active_for_network()) { |
|
101 | + add_action('network_admin_menu', array($this, 'imgopt_admin_menu')); |
|
102 | 102 | } else { |
103 | - add_action( 'admin_menu', array( $this, 'imgopt_admin_menu' ) ); |
|
103 | + add_action('admin_menu', array($this, 'imgopt_admin_menu')); |
|
104 | 104 | } |
105 | - add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_imgopt_tab' ), 9 ); |
|
105 | + add_filter('autoptimize_filter_settingsscreen_tabs', array($this, 'add_imgopt_tab'), 9); |
|
106 | 106 | } else { |
107 | - add_action( 'wp', array( $this, 'run_on_frontend' ) ); |
|
107 | + add_action('wp', array($this, 'run_on_frontend')); |
|
108 | 108 | } |
109 | 109 | } |
110 | 110 | |
111 | 111 | public function run_on_frontend() { |
112 | - if ( ! $this->should_run() ) { |
|
113 | - if ( $this->should_lazyload() ) { |
|
112 | + if (!$this->should_run()) { |
|
113 | + if ($this->should_lazyload()) { |
|
114 | 114 | add_filter( |
115 | 115 | 'wp_lazy_loading_enabled', |
116 | 116 | '__return_false' |
117 | 117 | ); |
118 | 118 | add_filter( |
119 | 119 | 'autoptimize_html_after_minify', |
120 | - array( $this, 'filter_lazyload_images' ), |
|
120 | + array($this, 'filter_lazyload_images'), |
|
121 | 121 | 10, |
122 | 122 | 1 |
123 | 123 | ); |
124 | 124 | add_action( |
125 | 125 | 'wp_footer', |
126 | - array( $this, 'add_lazyload_js_footer' ), |
|
126 | + array($this, 'add_lazyload_js_footer'), |
|
127 | 127 | 10, |
128 | 128 | 0 |
129 | 129 | ); |
@@ -133,43 +133,43 @@ discard block |
||
133 | 133 | |
134 | 134 | $active = false; |
135 | 135 | |
136 | - if ( apply_filters( 'autoptimize_filter_imgopt_do', true ) ) { |
|
136 | + if (apply_filters('autoptimize_filter_imgopt_do', true)) { |
|
137 | 137 | add_filter( |
138 | 138 | 'autoptimize_html_after_minify', |
139 | - array( $this, 'filter_optimize_images' ), |
|
139 | + array($this, 'filter_optimize_images'), |
|
140 | 140 | 10, |
141 | 141 | 1 |
142 | 142 | ); |
143 | 143 | $active = true; |
144 | 144 | } |
145 | 145 | |
146 | - if ( apply_filters( 'autoptimize_filter_imgopt_do_css', true ) ) { |
|
146 | + if (apply_filters('autoptimize_filter_imgopt_do_css', true)) { |
|
147 | 147 | add_filter( |
148 | 148 | 'autoptimize_filter_base_replace_cdn', |
149 | - array( $this, 'filter_optimize_css_images' ), |
|
149 | + array($this, 'filter_optimize_css_images'), |
|
150 | 150 | 10, |
151 | 151 | 1 |
152 | 152 | ); |
153 | 153 | $active = true; |
154 | 154 | } |
155 | 155 | |
156 | - if ( $active ) { |
|
156 | + if ($active) { |
|
157 | 157 | add_filter( |
158 | 158 | 'autoptimize_extra_filter_tobepreconn', |
159 | - array( $this, 'filter_preconnect_imgopt_url' ), |
|
159 | + array($this, 'filter_preconnect_imgopt_url'), |
|
160 | 160 | 10, |
161 | 161 | 1 |
162 | 162 | ); |
163 | 163 | } |
164 | 164 | |
165 | - if ( $this->should_lazyload() ) { |
|
165 | + if ($this->should_lazyload()) { |
|
166 | 166 | add_filter( |
167 | 167 | 'wp_lazy_loading_enabled', |
168 | 168 | '__return_false' |
169 | 169 | ); |
170 | 170 | add_action( |
171 | 171 | 'wp_footer', |
172 | - array( $this, 'add_lazyload_js_footer' ), |
|
172 | + array($this, 'add_lazyload_js_footer'), |
|
173 | 173 | 10, |
174 | 174 | 0 |
175 | 175 | ); |
@@ -184,12 +184,12 @@ discard block |
||
184 | 184 | protected function should_run() |
185 | 185 | { |
186 | 186 | $opts = $this->options; |
187 | - $service_not_down = ( 'down' !== $opts['availabilities']['extra_imgopt']['status'] ); |
|
188 | - $not_launch_status = ( 'launch' !== $opts['availabilities']['extra_imgopt']['status'] ); |
|
187 | + $service_not_down = ('down' !== $opts['availabilities']['extra_imgopt']['status']); |
|
188 | + $not_launch_status = ('launch' !== $opts['availabilities']['extra_imgopt']['status']); |
|
189 | 189 | |
190 | 190 | $do_cdn = true; |
191 | 191 | $_userstatus = $this->get_imgopt_provider_userstatus(); |
192 | - if ( isset( $_userstatus['Status'] ) && ( -2 == $_userstatus['Status'] || -3 == $_userstatus['Status'] ) ) { |
|
192 | + if (isset($_userstatus['Status']) && ( -2 == $_userstatus['Status'] || -3 == $_userstatus['Status'] )) { |
|
193 | 193 | // don't even attempt to put images on CDN if heavily exceeded threshold or if site not reachable. |
194 | 194 | $do_cdn = false; |
195 | 195 | } |
@@ -198,7 +198,7 @@ discard block |
||
198 | 198 | $this->imgopt_active() |
199 | 199 | && $do_cdn |
200 | 200 | && $service_not_down |
201 | - && ( $not_launch_status || $this->launch_ok() ) |
|
201 | + && ($not_launch_status || $this->launch_ok()) |
|
202 | 202 | ) { |
203 | 203 | return true; |
204 | 204 | } |
@@ -209,13 +209,13 @@ discard block |
||
209 | 209 | { |
210 | 210 | static $imgopt_host = null; |
211 | 211 | |
212 | - if ( null === $imgopt_host ) { |
|
212 | + if (null === $imgopt_host) { |
|
213 | 213 | $imgopt_host = 'https://cdn.shortpixel.ai/'; |
214 | 214 | $avail_imgopt = $this->options['availabilities']['extra_imgopt']; |
215 | - if ( ! empty( $avail_imgopt ) && array_key_exists( 'hosts', $avail_imgopt ) && is_array( $avail_imgopt['hosts'] ) ) { |
|
216 | - $imgopt_host = array_rand( array_flip( $avail_imgopt['hosts'] ) ); |
|
215 | + if (!empty($avail_imgopt) && array_key_exists('hosts', $avail_imgopt) && is_array($avail_imgopt['hosts'])) { |
|
216 | + $imgopt_host = array_rand(array_flip($avail_imgopt['hosts'])); |
|
217 | 217 | } |
218 | - $imgopt_host = apply_filters( 'autoptimize_filter_imgopt_host', $imgopt_host ); |
|
218 | + $imgopt_host = apply_filters('autoptimize_filter_imgopt_host', $imgopt_host); |
|
219 | 219 | } |
220 | 220 | |
221 | 221 | return $imgopt_host; |
@@ -230,7 +230,7 @@ discard block |
||
230 | 230 | |
231 | 231 | public static function get_service_url_suffix() |
232 | 232 | { |
233 | - $suffix = '/af/GWRGFLW109483/' . AUTOPTIMIZE_SITE_DOMAIN; |
|
233 | + $suffix = '/af/GWRGFLW109483/'.AUTOPTIMIZE_SITE_DOMAIN; |
|
234 | 234 | |
235 | 235 | return $suffix; |
236 | 236 | } |
@@ -239,12 +239,12 @@ discard block |
||
239 | 239 | { |
240 | 240 | static $quality = null; |
241 | 241 | |
242 | - if ( null === $quality ) { |
|
242 | + if (null === $quality) { |
|
243 | 243 | $q_array = $this->get_img_quality_array(); |
244 | 244 | $setting = $this->get_img_quality_setting(); |
245 | 245 | $quality = apply_filters( |
246 | 246 | 'autoptimize_filter_imgopt_quality', |
247 | - 'q_' . $q_array[ $setting ] |
|
247 | + 'q_'.$q_array[$setting] |
|
248 | 248 | ); |
249 | 249 | } |
250 | 250 | |
@@ -255,7 +255,7 @@ discard block |
||
255 | 255 | { |
256 | 256 | static $map = null; |
257 | 257 | |
258 | - if ( null === $map ) { |
|
258 | + if (null === $map) { |
|
259 | 259 | $map = array( |
260 | 260 | '1' => 'lossy', |
261 | 261 | '2' => 'glossy', |
@@ -274,12 +274,12 @@ discard block |
||
274 | 274 | { |
275 | 275 | static $q = null; |
276 | 276 | |
277 | - if ( null === $q ) { |
|
278 | - if ( is_array( $this->options ) && array_key_exists( 'autoptimize_imgopt_select_field_2', $this->options ) ) { |
|
277 | + if (null === $q) { |
|
278 | + if (is_array($this->options) && array_key_exists('autoptimize_imgopt_select_field_2', $this->options)) { |
|
279 | 279 | $setting = $this->options['autoptimize_imgopt_select_field_2']; |
280 | 280 | } |
281 | 281 | |
282 | - if ( ! isset( $setting ) || empty( $setting ) || ( '1' !== $setting && '3' !== $setting ) ) { |
|
282 | + if (!isset($setting) || empty($setting) || ('1' !== $setting && '3' !== $setting)) { |
|
283 | 283 | // default image opt. value is 2 ("glossy"). |
284 | 284 | $q = '2'; |
285 | 285 | } else { |
@@ -290,10 +290,10 @@ discard block |
||
290 | 290 | return $q; |
291 | 291 | } |
292 | 292 | |
293 | - public function filter_preconnect_imgopt_url( array $in ) |
|
293 | + public function filter_preconnect_imgopt_url(array $in) |
|
294 | 294 | { |
295 | - $url_parts = parse_url( $this->get_imgopt_base_url() ); |
|
296 | - $in[] = $url_parts['scheme'] . '://' . $url_parts['host']; |
|
295 | + $url_parts = parse_url($this->get_imgopt_base_url()); |
|
296 | + $in[] = $url_parts['scheme'].'://'.$url_parts['host']; |
|
297 | 297 | |
298 | 298 | return $in; |
299 | 299 | } |
@@ -306,20 +306,20 @@ discard block |
||
306 | 306 | * |
307 | 307 | * @return string |
308 | 308 | */ |
309 | - private function normalize_img_url( $in ) |
|
309 | + private function normalize_img_url($in) |
|
310 | 310 | { |
311 | 311 | // Only parse the site url once. |
312 | 312 | static $parsed_site_url = null; |
313 | - if ( null === $parsed_site_url ) { |
|
314 | - $parsed_site_url = parse_url( site_url() ); |
|
313 | + if (null === $parsed_site_url) { |
|
314 | + $parsed_site_url = parse_url(site_url()); |
|
315 | 315 | } |
316 | 316 | |
317 | 317 | // get CDN domain once. |
318 | 318 | static $cdn_domain = null; |
319 | - if ( is_null( $cdn_domain ) ) { |
|
319 | + if (is_null($cdn_domain)) { |
|
320 | 320 | $cdn_url = $this->get_cdn_url(); |
321 | - if ( ! empty( $cdn_url ) ) { |
|
322 | - $cdn_domain = parse_url( $cdn_url, PHP_URL_HOST ); |
|
321 | + if (!empty($cdn_url)) { |
|
322 | + $cdn_domain = parse_url($cdn_url, PHP_URL_HOST); |
|
323 | 323 | } else { |
324 | 324 | $cdn_domain = ''; |
325 | 325 | } |
@@ -335,44 +335,44 @@ discard block |
||
335 | 335 | * identical string operations). |
336 | 336 | */ |
337 | 337 | static $cache = null; |
338 | - if ( null === $cache ) { |
|
338 | + if (null === $cache) { |
|
339 | 339 | $cache = array(); |
340 | 340 | } |
341 | 341 | |
342 | 342 | // Do the work on cache miss only. |
343 | - if ( ! isset( $cache[ $in ] ) ) { |
|
343 | + if (!isset($cache[$in])) { |
|
344 | 344 | // Default to (the trimmed version of) what was given to us. |
345 | - $result = trim( $in ); |
|
345 | + $result = trim($in); |
|
346 | 346 | |
347 | 347 | // Some silly plugins wrap background images in html-encoded quotes, so remove those from the img url. |
348 | - if ( strpos( $result, '"' ) !== false ) { |
|
349 | - $result = str_replace( '"', '', $result ); |
|
348 | + if (strpos($result, '"') !== false) { |
|
349 | + $result = str_replace('"', '', $result); |
|
350 | 350 | } |
351 | 351 | |
352 | - if ( autoptimizeUtils::is_protocol_relative( $result ) ) { |
|
353 | - $result = $parsed_site_url['scheme'] . ':' . $result; |
|
354 | - } elseif ( 0 === strpos( $result, '/' ) ) { |
|
352 | + if (autoptimizeUtils::is_protocol_relative($result)) { |
|
353 | + $result = $parsed_site_url['scheme'].':'.$result; |
|
354 | + } elseif (0 === strpos($result, '/')) { |
|
355 | 355 | // Root-relative... |
356 | - $result = $parsed_site_url['scheme'] . '://' . $parsed_site_url['host'] . $result; |
|
357 | - } elseif ( ! empty( $cdn_domain ) && strpos( $result, $cdn_domain ) !== 0 ) { |
|
358 | - $result = str_replace( $cdn_domain, $parsed_site_url['host'], $result ); |
|
356 | + $result = $parsed_site_url['scheme'].'://'.$parsed_site_url['host'].$result; |
|
357 | + } elseif (!empty($cdn_domain) && strpos($result, $cdn_domain) !== 0) { |
|
358 | + $result = str_replace($cdn_domain, $parsed_site_url['host'], $result); |
|
359 | 359 | } |
360 | 360 | |
361 | - $result = apply_filters( 'autoptimize_filter_imgopt_normalized_url', $result ); |
|
361 | + $result = apply_filters('autoptimize_filter_imgopt_normalized_url', $result); |
|
362 | 362 | |
363 | 363 | // Store in cache. |
364 | - $cache[ $in ] = $result; |
|
364 | + $cache[$in] = $result; |
|
365 | 365 | } |
366 | 366 | |
367 | - return $cache[ $in ]; |
|
367 | + return $cache[$in]; |
|
368 | 368 | } |
369 | 369 | |
370 | - public function filter_optimize_css_images( $in ) |
|
370 | + public function filter_optimize_css_images($in) |
|
371 | 371 | { |
372 | - $in = $this->normalize_img_url( $in ); |
|
372 | + $in = $this->normalize_img_url($in); |
|
373 | 373 | |
374 | - if ( $this->can_optimize_image( $in ) ) { |
|
375 | - return $this->build_imgopt_url( $in, '', '' ); |
|
374 | + if ($this->can_optimize_image($in)) { |
|
375 | + return $this->build_imgopt_url($in, '', ''); |
|
376 | 376 | } else { |
377 | 377 | return $in; |
378 | 378 | } |
@@ -382,50 +382,50 @@ discard block |
||
382 | 382 | { |
383 | 383 | static $imgopt_base_url = null; |
384 | 384 | |
385 | - if ( null === $imgopt_base_url ) { |
|
385 | + if (null === $imgopt_base_url) { |
|
386 | 386 | $imgopt_host = $this->get_imgopt_host(); |
387 | 387 | $quality = $this->get_img_quality_string(); |
388 | - $ret_val = apply_filters( 'autoptimize_filter_imgopt_wait', 'ret_img' ); // values: ret_wait, ret_img, ret_json, ret_blank. |
|
389 | - $imgopt_base_url = $imgopt_host . 'client/' . $quality . ',' . $ret_val; |
|
390 | - $imgopt_base_url = apply_filters( 'autoptimize_filter_imgopt_base_url', $imgopt_base_url ); |
|
388 | + $ret_val = apply_filters('autoptimize_filter_imgopt_wait', 'ret_img'); // values: ret_wait, ret_img, ret_json, ret_blank. |
|
389 | + $imgopt_base_url = $imgopt_host.'client/'.$quality.','.$ret_val; |
|
390 | + $imgopt_base_url = apply_filters('autoptimize_filter_imgopt_base_url', $imgopt_base_url); |
|
391 | 391 | } |
392 | 392 | |
393 | 393 | return $imgopt_base_url; |
394 | 394 | } |
395 | 395 | |
396 | - private function can_optimize_image( $url ) |
|
396 | + private function can_optimize_image($url) |
|
397 | 397 | { |
398 | 398 | static $cdn_url = null; |
399 | 399 | static $nopti_images = null; |
400 | 400 | |
401 | - if ( null === $cdn_url ) { |
|
401 | + if (null === $cdn_url) { |
|
402 | 402 | $cdn_url = apply_filters( |
403 | 403 | 'autoptimize_filter_base_cdnurl', |
404 | - autoptimizeOptionWrapper::get_option( 'autoptimize_cdn_url', '' ) |
|
404 | + autoptimizeOptionWrapper::get_option('autoptimize_cdn_url', '') |
|
405 | 405 | ); |
406 | 406 | } |
407 | 407 | |
408 | - if ( null === $nopti_images ) { |
|
409 | - $nopti_images = apply_filters( 'autoptimize_filter_imgopt_noptimize', '' ); |
|
408 | + if (null === $nopti_images) { |
|
409 | + $nopti_images = apply_filters('autoptimize_filter_imgopt_noptimize', ''); |
|
410 | 410 | } |
411 | 411 | |
412 | 412 | $site_host = AUTOPTIMIZE_SITE_DOMAIN; |
413 | - $url = $this->normalize_img_url( $url ); |
|
414 | - $url_parsed = parse_url( $url ); |
|
413 | + $url = $this->normalize_img_url($url); |
|
414 | + $url_parsed = parse_url($url); |
|
415 | 415 | |
416 | - if ( array_key_exists( 'host', $url_parsed ) && $url_parsed['host'] !== $site_host && empty( $cdn_url ) ) { |
|
416 | + if (array_key_exists('host', $url_parsed) && $url_parsed['host'] !== $site_host && empty($cdn_url)) { |
|
417 | 417 | return false; |
418 | - } elseif ( ! empty( $cdn_url ) && strpos( $url, $cdn_url ) === false && array_key_exists( 'host', $url_parsed ) && $url_parsed['host'] !== $site_host ) { |
|
418 | + } elseif (!empty($cdn_url) && strpos($url, $cdn_url) === false && array_key_exists('host', $url_parsed) && $url_parsed['host'] !== $site_host) { |
|
419 | 419 | return false; |
420 | - } elseif ( strpos( $url, '.php' ) !== false ) { |
|
420 | + } elseif (strpos($url, '.php') !== false) { |
|
421 | 421 | return false; |
422 | - } elseif ( str_ireplace( array( '.png', '.gif', '.jpg', '.jpeg', '.webp' ), '', $url_parsed['path'] ) === $url_parsed['path'] ) { |
|
422 | + } elseif (str_ireplace(array('.png', '.gif', '.jpg', '.jpeg', '.webp'), '', $url_parsed['path']) === $url_parsed['path']) { |
|
423 | 423 | // fixme: better check against end of string. |
424 | 424 | return false; |
425 | - } elseif ( ! empty( $nopti_images ) ) { |
|
426 | - $nopti_images_array = array_filter( array_map( 'trim', explode( ',', $nopti_images ) ) ); |
|
427 | - foreach ( $nopti_images_array as $nopti_image ) { |
|
428 | - if ( strpos( $url, $nopti_image ) !== false ) { |
|
425 | + } elseif (!empty($nopti_images)) { |
|
426 | + $nopti_images_array = array_filter(array_map('trim', explode(',', $nopti_images))); |
|
427 | + foreach ($nopti_images_array as $nopti_image) { |
|
428 | + if (strpos($url, $nopti_image) !== false) { |
|
429 | 429 | return false; |
430 | 430 | } |
431 | 431 | } |
@@ -433,13 +433,13 @@ discard block |
||
433 | 433 | return true; |
434 | 434 | } |
435 | 435 | |
436 | - private function build_imgopt_url( $orig_url, $width = 0, $height = 0 ) |
|
436 | + private function build_imgopt_url($orig_url, $width = 0, $height = 0) |
|
437 | 437 | { |
438 | 438 | // sanitize width and height. |
439 | - if ( strpos( $width, '%' ) !== false ) { |
|
439 | + if (strpos($width, '%') !== false) { |
|
440 | 440 | $width = 0; |
441 | 441 | } |
442 | - if ( strpos( $height, '%' ) !== false ) { |
|
442 | + if (strpos($height, '%') !== false) { |
|
443 | 443 | $height = 0; |
444 | 444 | } |
445 | 445 | $width = (int) $width; |
@@ -453,43 +453,43 @@ discard block |
||
453 | 453 | ); |
454 | 454 | |
455 | 455 | // If filter modified the url, return that. |
456 | - if ( $filtered_url !== $orig_url ) { |
|
456 | + if ($filtered_url !== $orig_url) { |
|
457 | 457 | return $filtered_url; |
458 | 458 | } |
459 | 459 | |
460 | - $orig_url = $this->normalize_img_url( $orig_url ); |
|
460 | + $orig_url = $this->normalize_img_url($orig_url); |
|
461 | 461 | $imgopt_base_url = $this->get_imgopt_base_url(); |
462 | 462 | $imgopt_size = ''; |
463 | 463 | |
464 | - if ( $width && 0 !== $width ) { |
|
465 | - $imgopt_size = ',w_' . $width; |
|
464 | + if ($width && 0 !== $width) { |
|
465 | + $imgopt_size = ',w_'.$width; |
|
466 | 466 | } |
467 | 467 | |
468 | - if ( $height && 0 !== $height ) { |
|
469 | - $imgopt_size .= ',h_' . $height; |
|
468 | + if ($height && 0 !== $height) { |
|
469 | + $imgopt_size .= ',h_'.$height; |
|
470 | 470 | } |
471 | 471 | |
472 | - $url = $imgopt_base_url . $imgopt_size . '/' . $orig_url; |
|
472 | + $url = $imgopt_base_url.$imgopt_size.'/'.$orig_url; |
|
473 | 473 | |
474 | 474 | return $url; |
475 | 475 | } |
476 | 476 | |
477 | - public function replace_data_thumbs( $matches ) |
|
477 | + public function replace_data_thumbs($matches) |
|
478 | 478 | { |
479 | - return $this->replace_img_callback( $matches, 150, 150 ); |
|
479 | + return $this->replace_img_callback($matches, 150, 150); |
|
480 | 480 | } |
481 | 481 | |
482 | - public function replace_img_callback( $matches, $width = 0, $height = 0 ) |
|
482 | + public function replace_img_callback($matches, $width = 0, $height = 0) |
|
483 | 483 | { |
484 | - $_normalized_img_url = $this->normalize_img_url( $matches[1] ); |
|
485 | - if ( $this->can_optimize_image( $matches[1] ) ) { |
|
486 | - return str_replace( $matches[1], $this->build_imgopt_url( $_normalized_img_url, $width, $height ), $matches[0] ); |
|
484 | + $_normalized_img_url = $this->normalize_img_url($matches[1]); |
|
485 | + if ($this->can_optimize_image($matches[1])) { |
|
486 | + return str_replace($matches[1], $this->build_imgopt_url($_normalized_img_url, $width, $height), $matches[0]); |
|
487 | 487 | } else { |
488 | 488 | return $matches[0]; |
489 | 489 | } |
490 | 490 | } |
491 | 491 | |
492 | - public function filter_optimize_images( $in ) |
|
492 | + public function filter_optimize_images($in) |
|
493 | 493 | { |
494 | 494 | /* |
495 | 495 | * potential future functional improvements: |
@@ -499,7 +499,7 @@ discard block |
||
499 | 499 | $to_replace = array(); |
500 | 500 | |
501 | 501 | // hide noscript tags to avoid nesting noscript tags (as lazyloaded images add noscript). |
502 | - if ( $this->should_lazyload() ) { |
|
502 | + if ($this->should_lazyload()) { |
|
503 | 503 | $in = autoptimizeBase::replace_contents_with_marker_if_exists( |
504 | 504 | 'SCRIPT', |
505 | 505 | '<script', |
@@ -509,25 +509,25 @@ discard block |
||
509 | 509 | } |
510 | 510 | |
511 | 511 | // extract img tags. |
512 | - if ( preg_match_all( '#<img[^>]*src[^>]*>#Usmi', $in, $matches ) ) { |
|
513 | - foreach ( $matches[0] as $tag ) { |
|
512 | + if (preg_match_all('#<img[^>]*src[^>]*>#Usmi', $in, $matches)) { |
|
513 | + foreach ($matches[0] as $tag) { |
|
514 | 514 | $orig_tag = $tag; |
515 | 515 | $imgopt_w = ''; |
516 | 516 | $imgopt_h = ''; |
517 | 517 | |
518 | 518 | // first do (data-)srcsets. |
519 | - if ( preg_match_all( '#srcset=("|\')(.*)("|\')#Usmi', $tag, $allsrcsets, PREG_SET_ORDER ) ) { |
|
520 | - foreach ( $allsrcsets as $srcset ) { |
|
519 | + if (preg_match_all('#srcset=("|\')(.*)("|\')#Usmi', $tag, $allsrcsets, PREG_SET_ORDER)) { |
|
520 | + foreach ($allsrcsets as $srcset) { |
|
521 | 521 | $srcset = $srcset[2]; |
522 | - $srcsets = explode( ',', $srcset ); |
|
523 | - foreach ( $srcsets as $indiv_srcset ) { |
|
524 | - $indiv_srcset_parts = explode( ' ', trim( $indiv_srcset ) ); |
|
525 | - if ( isset( $indiv_srcset_parts[1] ) && rtrim( $indiv_srcset_parts[1], 'w' ) !== $indiv_srcset_parts[1] ) { |
|
526 | - $imgopt_w = rtrim( $indiv_srcset_parts[1], 'w' ); |
|
522 | + $srcsets = explode(',', $srcset); |
|
523 | + foreach ($srcsets as $indiv_srcset) { |
|
524 | + $indiv_srcset_parts = explode(' ', trim($indiv_srcset)); |
|
525 | + if (isset($indiv_srcset_parts[1]) && rtrim($indiv_srcset_parts[1], 'w') !== $indiv_srcset_parts[1]) { |
|
526 | + $imgopt_w = rtrim($indiv_srcset_parts[1], 'w'); |
|
527 | 527 | } |
528 | - if ( $this->can_optimize_image( $indiv_srcset_parts[0] ) ) { |
|
529 | - $imgopt_url = $this->build_imgopt_url( $indiv_srcset_parts[0], $imgopt_w, '' ); |
|
530 | - $tag = str_replace( $indiv_srcset_parts[0], $imgopt_url, $tag ); |
|
528 | + if ($this->can_optimize_image($indiv_srcset_parts[0])) { |
|
529 | + $imgopt_url = $this->build_imgopt_url($indiv_srcset_parts[0], $imgopt_w, ''); |
|
530 | + $tag = str_replace($indiv_srcset_parts[0], $imgopt_url, $tag); |
|
531 | 531 | } |
532 | 532 | } |
533 | 533 | } |
@@ -535,124 +535,124 @@ discard block |
||
535 | 535 | |
536 | 536 | // proceed with img src. |
537 | 537 | // get width and height and add to $imgopt_size. |
538 | - $_get_size = $this->get_size_from_tag( $tag ); |
|
538 | + $_get_size = $this->get_size_from_tag($tag); |
|
539 | 539 | $imgopt_w = $_get_size['width']; |
540 | 540 | $imgopt_h = $_get_size['height']; |
541 | 541 | |
542 | 542 | // then start replacing images src. |
543 | - if ( preg_match_all( '#src=(?:"|\')(?!data)(.*)(?:"|\')#Usmi', $tag, $urls, PREG_SET_ORDER ) ) { |
|
544 | - foreach ( $urls as $url ) { |
|
543 | + if (preg_match_all('#src=(?:"|\')(?!data)(.*)(?:"|\')#Usmi', $tag, $urls, PREG_SET_ORDER)) { |
|
544 | + foreach ($urls as $url) { |
|
545 | 545 | $full_src_orig = $url[0]; |
546 | 546 | $url = $url[1]; |
547 | - if ( $this->can_optimize_image( $url ) ) { |
|
548 | - $imgopt_url = $this->build_imgopt_url( $url, $imgopt_w, $imgopt_h ); |
|
549 | - $full_imgopt_src = str_replace( $url, $imgopt_url, $full_src_orig ); |
|
550 | - $tag = str_replace( $full_src_orig, $full_imgopt_src, $tag ); |
|
547 | + if ($this->can_optimize_image($url)) { |
|
548 | + $imgopt_url = $this->build_imgopt_url($url, $imgopt_w, $imgopt_h); |
|
549 | + $full_imgopt_src = str_replace($url, $imgopt_url, $full_src_orig); |
|
550 | + $tag = str_replace($full_src_orig, $full_imgopt_src, $tag); |
|
551 | 551 | } |
552 | 552 | } |
553 | 553 | } |
554 | 554 | |
555 | 555 | // do lazyload stuff. |
556 | - if ( $this->should_lazyload( $in ) && ! empty( $url ) ) { |
|
556 | + if ($this->should_lazyload($in) && !empty($url)) { |
|
557 | 557 | // first do lpiq placeholder logic. |
558 | - if ( strpos( $url, $this->get_imgopt_host() ) === 0 ) { |
|
558 | + if (strpos($url, $this->get_imgopt_host()) === 0) { |
|
559 | 559 | // if all img src have been replaced during srcset, we have to extract the |
560 | 560 | // origin url from the imgopt one to be able to set a lqip placeholder. |
561 | - $_url = substr( $url, strpos( $url, '/http' ) + 1 ); |
|
561 | + $_url = substr($url, strpos($url, '/http') + 1); |
|
562 | 562 | } else { |
563 | 563 | $_url = $url; |
564 | 564 | } |
565 | 565 | |
566 | - $_url = $this->normalize_img_url( $_url ); |
|
566 | + $_url = $this->normalize_img_url($_url); |
|
567 | 567 | |
568 | 568 | $placeholder = ''; |
569 | - if ( $this->can_optimize_image( $_url ) && apply_filters( 'autoptimize_filter_imgopt_lazyload_dolqip', true ) ) { |
|
569 | + if ($this->can_optimize_image($_url) && apply_filters('autoptimize_filter_imgopt_lazyload_dolqip', true)) { |
|
570 | 570 | $lqip_w = ''; |
571 | 571 | $lqip_h = ''; |
572 | - if ( isset( $imgopt_w ) && ! empty( $imgopt_w ) ) { |
|
573 | - $lqip_w = ',w_' . $imgopt_w; |
|
572 | + if (isset($imgopt_w) && !empty($imgopt_w)) { |
|
573 | + $lqip_w = ',w_'.$imgopt_w; |
|
574 | 574 | } |
575 | - if ( isset( $imgopt_h ) && ! empty( $imgopt_h ) ) { |
|
576 | - $lqip_h = ',h_' . $imgopt_h; |
|
575 | + if (isset($imgopt_h) && !empty($imgopt_h)) { |
|
576 | + $lqip_h = ',h_'.$imgopt_h; |
|
577 | 577 | } |
578 | - $placeholder = $this->get_imgopt_host() . 'client/q_lqip,ret_wait' . $lqip_w . $lqip_h . '/' . $_url; |
|
578 | + $placeholder = $this->get_imgopt_host().'client/q_lqip,ret_wait'.$lqip_w.$lqip_h.'/'.$_url; |
|
579 | 579 | } |
580 | 580 | // then call add_lazyload-function with lpiq placeholder if set. |
581 | - $tag = $this->add_lazyload( $tag, $placeholder ); |
|
581 | + $tag = $this->add_lazyload($tag, $placeholder); |
|
582 | 582 | } |
583 | 583 | |
584 | 584 | // and add tag to array for later replacement. |
585 | - if ( $tag !== $orig_tag ) { |
|
586 | - $to_replace[ $orig_tag ] = $tag; |
|
585 | + if ($tag !== $orig_tag) { |
|
586 | + $to_replace[$orig_tag] = $tag; |
|
587 | 587 | } |
588 | 588 | } |
589 | 589 | } |
590 | 590 | |
591 | 591 | // and replace all. |
592 | - $out = str_replace( array_keys( $to_replace ), array_values( $to_replace ), $in ); |
|
592 | + $out = str_replace(array_keys($to_replace), array_values($to_replace), $in); |
|
593 | 593 | |
594 | 594 | // img thumbnails in e.g. woocommerce. |
595 | - if ( strpos( $out, 'data-thumb' ) !== false && apply_filters( 'autoptimize_filter_imgopt_datathumbs', true ) ) { |
|
595 | + if (strpos($out, 'data-thumb') !== false && apply_filters('autoptimize_filter_imgopt_datathumbs', true)) { |
|
596 | 596 | $out = preg_replace_callback( |
597 | 597 | '/\<div(?:[^>]?)\sdata-thumb\=(?:\"|\')(.+?)(?:\"|\')(?:[^>]*)?\>/s', |
598 | - array( $this, 'replace_data_thumbs' ), |
|
598 | + array($this, 'replace_data_thumbs'), |
|
599 | 599 | $out |
600 | 600 | ); |
601 | 601 | } |
602 | 602 | |
603 | 603 | // background-image in inline style. |
604 | - if ( strpos( $out, 'background-image:' ) !== false && apply_filters( 'autoptimize_filter_imgopt_backgroundimages', true ) ) { |
|
604 | + if (strpos($out, 'background-image:') !== false && apply_filters('autoptimize_filter_imgopt_backgroundimages', true)) { |
|
605 | 605 | $out = preg_replace_callback( |
606 | 606 | '/style=(?:"|\')[^<>]*?background-image:\s?url\((?:"|\')?([^"\')]*)(?:"|\')?\)/', |
607 | - array( $this, 'replace_img_callback' ), |
|
607 | + array($this, 'replace_img_callback'), |
|
608 | 608 | $out |
609 | 609 | ); |
610 | 610 | } |
611 | 611 | |
612 | 612 | // lazyload: restore noscript tags + lazyload picture source tags and bgimage. |
613 | - if ( $this->should_lazyload() ) { |
|
613 | + if ($this->should_lazyload()) { |
|
614 | 614 | $out = autoptimizeBase::restore_marked_content( |
615 | 615 | 'SCRIPT', |
616 | 616 | $out |
617 | 617 | ); |
618 | 618 | |
619 | - $out = $this->process_picture_tag( $out, true, true ); |
|
620 | - $out = $this->process_bgimage( $out ); |
|
619 | + $out = $this->process_picture_tag($out, true, true); |
|
620 | + $out = $this->process_bgimage($out); |
|
621 | 621 | } else { |
622 | - $out = $this->process_picture_tag( $out, true, false ); |
|
622 | + $out = $this->process_picture_tag($out, true, false); |
|
623 | 623 | } |
624 | 624 | |
625 | 625 | return $out; |
626 | 626 | } |
627 | 627 | |
628 | - public function get_size_from_tag( $tag ) { |
|
628 | + public function get_size_from_tag($tag) { |
|
629 | 629 | // reusable function to extract widht and height from an image tag |
630 | 630 | // enforcing a filterable maximum width and height (default 4999X4999). |
631 | 631 | $width = ''; |
632 | 632 | $height = ''; |
633 | 633 | |
634 | - if ( preg_match( '#width=("|\')(.*)("|\')#Usmi', $tag, $_width ) ) { |
|
635 | - if ( strpos( $_width[2], '%' ) === false ) { |
|
634 | + if (preg_match('#width=("|\')(.*)("|\')#Usmi', $tag, $_width)) { |
|
635 | + if (strpos($_width[2], '%') === false) { |
|
636 | 636 | $width = (int) $_width[2]; |
637 | 637 | } |
638 | 638 | } |
639 | - if ( preg_match( '#height=("|\')(.*)("|\')#Usmi', $tag, $_height ) ) { |
|
640 | - if ( strpos( $_height[2], '%' ) === false ) { |
|
639 | + if (preg_match('#height=("|\')(.*)("|\')#Usmi', $tag, $_height)) { |
|
640 | + if (strpos($_height[2], '%') === false) { |
|
641 | 641 | $height = (int) $_height[2]; |
642 | 642 | } |
643 | 643 | } |
644 | 644 | |
645 | 645 | // check for and enforce (filterable) max sizes. |
646 | - $_max_width = apply_filters( 'autoptimize_filter_imgopt_max_width', 4999 ); |
|
647 | - if ( $width > $_max_width ) { |
|
646 | + $_max_width = apply_filters('autoptimize_filter_imgopt_max_width', 4999); |
|
647 | + if ($width > $_max_width) { |
|
648 | 648 | $_width = $_max_width; |
649 | - $height = $_width / $width * $height; |
|
649 | + $height = $_width/$width*$height; |
|
650 | 650 | $width = $_width; |
651 | 651 | } |
652 | - $_max_height = apply_filters( 'autoptimize_filter_imgopt_max_height', 4999 ); |
|
653 | - if ( $height > $_max_height ) { |
|
652 | + $_max_height = apply_filters('autoptimize_filter_imgopt_max_height', 4999); |
|
653 | + if ($height > $_max_height) { |
|
654 | 654 | $_height = $_max_height; |
655 | - $width = $_height / $height * $width; |
|
655 | + $width = $_height/$height*$width; |
|
656 | 656 | $height = $_height; |
657 | 657 | } |
658 | 658 | |
@@ -671,26 +671,26 @@ discard block |
||
671 | 671 | return $self->should_lazyload(); |
672 | 672 | } |
673 | 673 | |
674 | - public function should_lazyload( $context = '' ) { |
|
675 | - if ( ! empty( $this->options['autoptimize_imgopt_checkbox_field_3'] ) && false === $this->check_nolazy() ) { |
|
674 | + public function should_lazyload($context = '') { |
|
675 | + if (!empty($this->options['autoptimize_imgopt_checkbox_field_3']) && false === $this->check_nolazy()) { |
|
676 | 676 | $lazyload_return = true; |
677 | 677 | } else { |
678 | 678 | $lazyload_return = false; |
679 | 679 | } |
680 | - $lazyload_return = apply_filters( 'autoptimize_filter_imgopt_should_lazyload', $lazyload_return, $context ); |
|
680 | + $lazyload_return = apply_filters('autoptimize_filter_imgopt_should_lazyload', $lazyload_return, $context); |
|
681 | 681 | |
682 | 682 | return $lazyload_return; |
683 | 683 | } |
684 | 684 | |
685 | 685 | public function check_nolazy() { |
686 | - if ( array_key_exists( 'ao_nolazy', $_GET ) && '1' === $_GET['ao_nolazy'] ) { |
|
686 | + if (array_key_exists('ao_nolazy', $_GET) && '1' === $_GET['ao_nolazy']) { |
|
687 | 687 | return true; |
688 | 688 | } else { |
689 | 689 | return false; |
690 | 690 | } |
691 | 691 | } |
692 | 692 | |
693 | - public function filter_lazyload_images( $in ) |
|
693 | + public function filter_lazyload_images($in) |
|
694 | 694 | { |
695 | 695 | // only used is image optimization is NOT active but lazyload is. |
696 | 696 | $to_replace = array(); |
@@ -704,20 +704,20 @@ discard block |
||
704 | 704 | ); |
705 | 705 | |
706 | 706 | // extract img tags and add lazyload attribs. |
707 | - if ( preg_match_all( '#<img[^>]*src[^>]*>#Usmi', $out, $matches ) ) { |
|
708 | - foreach ( $matches[0] as $tag ) { |
|
709 | - if ( $this->should_lazyload( $out ) ) { |
|
710 | - $to_replace[ $tag ] = $this->add_lazyload( $tag ); |
|
707 | + if (preg_match_all('#<img[^>]*src[^>]*>#Usmi', $out, $matches)) { |
|
708 | + foreach ($matches[0] as $tag) { |
|
709 | + if ($this->should_lazyload($out)) { |
|
710 | + $to_replace[$tag] = $this->add_lazyload($tag); |
|
711 | 711 | } |
712 | 712 | } |
713 | - $out = str_replace( array_keys( $to_replace ), array_values( $to_replace ), $out ); |
|
713 | + $out = str_replace(array_keys($to_replace), array_values($to_replace), $out); |
|
714 | 714 | } |
715 | 715 | |
716 | 716 | // and also lazyload picture tag. |
717 | - $out = $this->process_picture_tag( $out, false, true ); |
|
717 | + $out = $this->process_picture_tag($out, false, true); |
|
718 | 718 | |
719 | 719 | // and inline style blocks with background-image. |
720 | - $out = $this->process_bgimage( $out ); |
|
720 | + $out = $this->process_bgimage($out); |
|
721 | 721 | |
722 | 722 | // restore noscript tags. |
723 | 723 | $out = autoptimizeBase::restore_marked_content( |
@@ -728,84 +728,84 @@ discard block |
||
728 | 728 | return $out; |
729 | 729 | } |
730 | 730 | |
731 | - public function add_lazyload( $tag, $placeholder = '' ) { |
|
731 | + public function add_lazyload($tag, $placeholder = '') { |
|
732 | 732 | // adds actual lazyload-attributes to an image node. |
733 | - if ( str_ireplace( $this->get_lazyload_exclusions(), '', $tag ) === $tag ) { |
|
734 | - $tag = $this->maybe_fix_missing_quotes( $tag ); |
|
733 | + if (str_ireplace($this->get_lazyload_exclusions(), '', $tag) === $tag) { |
|
734 | + $tag = $this->maybe_fix_missing_quotes($tag); |
|
735 | 735 | |
736 | 736 | // store original tag for use in noscript version. |
737 | - $noscript_tag = '<noscript>' . autoptimizeUtils::remove_id_from_node( $tag ) . '</noscript>'; |
|
737 | + $noscript_tag = '<noscript>'.autoptimizeUtils::remove_id_from_node($tag).'</noscript>'; |
|
738 | 738 | |
739 | - $lazyload_class = apply_filters( 'autoptimize_filter_imgopt_lazyload_class', 'lazyload' ); |
|
739 | + $lazyload_class = apply_filters('autoptimize_filter_imgopt_lazyload_class', 'lazyload'); |
|
740 | 740 | |
741 | 741 | // insert lazyload class. |
742 | - $tag = $this->inject_classes_in_tag( $tag, "$lazyload_class " ); |
|
742 | + $tag = $this->inject_classes_in_tag($tag, "$lazyload_class "); |
|
743 | 743 | |
744 | - if ( ! $placeholder || empty( $placeholder ) ) { |
|
744 | + if (!$placeholder || empty($placeholder)) { |
|
745 | 745 | // get image width & heigth for placeholder fun (and to prevent content reflow). |
746 | - $_get_size = $this->get_size_from_tag( $tag ); |
|
746 | + $_get_size = $this->get_size_from_tag($tag); |
|
747 | 747 | $width = $_get_size['width']; |
748 | 748 | $height = $_get_size['height']; |
749 | - if ( false === $width || empty( $width ) ) { |
|
749 | + if (false === $width || empty($width)) { |
|
750 | 750 | $width = 210; // default width for SVG placeholder. |
751 | 751 | } |
752 | - if ( false === $height || empty( $height ) ) { |
|
753 | - $height = $width / 3 * 2; // if no height, base it on width using the 3/2 aspect ratio. |
|
752 | + if (false === $height || empty($height)) { |
|
753 | + $height = $width/3*2; // if no height, base it on width using the 3/2 aspect ratio. |
|
754 | 754 | } |
755 | 755 | |
756 | 756 | // insert the actual lazyload stuff. |
757 | 757 | // see https://css-tricks.com/preventing-content-reflow-from-lazy-loaded-images/ for great read on why we're using empty svg's. |
758 | - $placeholder = apply_filters( 'autoptimize_filter_imgopt_lazyload_placeholder', $this->get_default_lazyload_placeholder( $width, $height ) ); |
|
758 | + $placeholder = apply_filters('autoptimize_filter_imgopt_lazyload_placeholder', $this->get_default_lazyload_placeholder($width, $height)); |
|
759 | 759 | } |
760 | 760 | |
761 | - $tag = preg_replace( '/(\s)src=/', ' src=\'' . $placeholder . '\' data-src=', $tag ); |
|
762 | - $tag = preg_replace( '/(\s)srcset=/', ' data-srcset=', $tag ); |
|
761 | + $tag = preg_replace('/(\s)src=/', ' src=\''.$placeholder.'\' data-src=', $tag); |
|
762 | + $tag = preg_replace('/(\s)srcset=/', ' data-srcset=', $tag); |
|
763 | 763 | |
764 | 764 | // move sizes to data-sizes unless filter says no. |
765 | - if ( apply_filters( 'autoptimize_filter_imgopt_lazyload_move_sizes', true ) ) { |
|
766 | - $tag = str_replace( ' sizes=', ' data-sizes=', $tag ); |
|
765 | + if (apply_filters('autoptimize_filter_imgopt_lazyload_move_sizes', true)) { |
|
766 | + $tag = str_replace(' sizes=', ' data-sizes=', $tag); |
|
767 | 767 | } |
768 | 768 | |
769 | 769 | // add the noscript-tag from earlier. |
770 | - $tag = $noscript_tag . $tag; |
|
771 | - $tag = apply_filters( 'autoptimize_filter_imgopt_lazyloaded_img', $tag ); |
|
770 | + $tag = $noscript_tag.$tag; |
|
771 | + $tag = apply_filters('autoptimize_filter_imgopt_lazyloaded_img', $tag); |
|
772 | 772 | } |
773 | 773 | |
774 | 774 | return $tag; |
775 | 775 | } |
776 | 776 | |
777 | 777 | public function add_lazyload_js_footer() { |
778 | - if ( false === autoptimizeMain::should_buffer() ) { |
|
778 | + if (false === autoptimizeMain::should_buffer()) { |
|
779 | 779 | return; |
780 | 780 | } |
781 | 781 | |
782 | 782 | // The JS will by default be excluded form autoptimization but this can be changed with a filter. |
783 | 783 | $noptimize_flag = ''; |
784 | - if ( apply_filters( 'autoptimize_filter_imgopt_lazyload_js_noptimize', true ) ) { |
|
784 | + if (apply_filters('autoptimize_filter_imgopt_lazyload_js_noptimize', true)) { |
|
785 | 785 | $noptimize_flag = ' data-noptimize="1"'; |
786 | 786 | } |
787 | 787 | |
788 | - $lazysizes_js = plugins_url( 'external/js/lazysizes.min.js?ao_version=' . AUTOPTIMIZE_PLUGIN_VERSION, __FILE__ ); |
|
788 | + $lazysizes_js = plugins_url('external/js/lazysizes.min.js?ao_version='.AUTOPTIMIZE_PLUGIN_VERSION, __FILE__); |
|
789 | 789 | $cdn_url = $this->get_cdn_url(); |
790 | - if ( ! empty( $cdn_url ) ) { |
|
791 | - $lazysizes_js = str_replace( AUTOPTIMIZE_WP_SITE_URL, $cdn_url, $lazysizes_js ); |
|
790 | + if (!empty($cdn_url)) { |
|
791 | + $lazysizes_js = str_replace(AUTOPTIMIZE_WP_SITE_URL, $cdn_url, $lazysizes_js); |
|
792 | 792 | } |
793 | 793 | |
794 | 794 | $type_js = ''; |
795 | - if ( apply_filters( 'autoptimize_filter_cssjs_addtype', false ) ) { |
|
795 | + if (apply_filters('autoptimize_filter_cssjs_addtype', false)) { |
|
796 | 796 | $type_js = ' type="text/javascript"'; |
797 | 797 | } |
798 | 798 | |
799 | 799 | // Adds lazyload CSS & JS to footer, using echo because wp_enqueue_script seems not to support pushing attributes (async). |
800 | - echo apply_filters( 'autoptimize_filter_imgopt_lazyload_cssoutput', '<style>.lazyload,.lazyloading{opacity:0;}.lazyloaded{opacity:1;transition:opacity 300ms;}</style><noscript><style>.lazyload{display:none;}</style></noscript>' ); |
|
801 | - echo apply_filters( 'autoptimize_filter_imgopt_lazyload_jsconfig', '<script' . $type_js . $noptimize_flag . '>window.lazySizesConfig=window.lazySizesConfig||{};window.lazySizesConfig.loadMode=1;</script>' ); |
|
802 | - echo apply_filters( 'autoptimize_filter_imgopt_lazyload_js', '<script async' . $type_js . $noptimize_flag . ' src=\'' . $lazysizes_js . '\'></script>' ); |
|
800 | + echo apply_filters('autoptimize_filter_imgopt_lazyload_cssoutput', '<style>.lazyload,.lazyloading{opacity:0;}.lazyloaded{opacity:1;transition:opacity 300ms;}</style><noscript><style>.lazyload{display:none;}</style></noscript>'); |
|
801 | + echo apply_filters('autoptimize_filter_imgopt_lazyload_jsconfig', '<script'.$type_js.$noptimize_flag.'>window.lazySizesConfig=window.lazySizesConfig||{};window.lazySizesConfig.loadMode=1;</script>'); |
|
802 | + echo apply_filters('autoptimize_filter_imgopt_lazyload_js', '<script async'.$type_js.$noptimize_flag.' src=\''.$lazysizes_js.'\'></script>'); |
|
803 | 803 | |
804 | 804 | // And add webp detection and loading JS. |
805 | - if ( $this->should_webp() ) { |
|
805 | + if ($this->should_webp()) { |
|
806 | 806 | $_webp_detect = "function c_webp(A){var n=new Image;n.onload=function(){var e=0<n.width&&0<n.height;A(e)},n.onerror=function(){A(!1)},n.src='data:image/webp;base64,UklGRhoAAABXRUJQVlA4TA0AAAAvAAAAEAcQERGIiP4HAA=='}function s_webp(e){window.supportsWebP=e}c_webp(s_webp);"; |
807 | 807 | $_webp_load = "document.addEventListener('lazybeforeunveil',function({target:c}){supportsWebP&&['data-src','data-srcset'].forEach(function(a){attr=c.getAttribute(a),null!==attr&&c.setAttribute(a,attr.replace(/\/client\//,'/client/to_webp,'))})});"; |
808 | - echo apply_filters( 'autoptimize_filter_imgopt_webp_js', '<script' . $type_js . $noptimize_flag . '>' . $_webp_detect . $_webp_load . '</script>' ); |
|
808 | + echo apply_filters('autoptimize_filter_imgopt_webp_js', '<script'.$type_js.$noptimize_flag.'>'.$_webp_detect.$_webp_load.'</script>'); |
|
809 | 809 | } |
810 | 810 | } |
811 | 811 | |
@@ -813,10 +813,10 @@ discard block |
||
813 | 813 | // getting CDN url here to avoid having to make bigger changes to autoptimizeBase. |
814 | 814 | static $cdn_url = null; |
815 | 815 | |
816 | - if ( null === $cdn_url ) { |
|
817 | - $cdn_url = autoptimizeOptionWrapper::get_option( 'autoptimize_cdn_url', '' ); |
|
818 | - $cdn_url = autoptimizeUtils::tweak_cdn_url_if_needed( $cdn_url ); |
|
819 | - $cdn_url = apply_filters( 'autoptimize_filter_base_cdnurl', $cdn_url ); |
|
816 | + if (null === $cdn_url) { |
|
817 | + $cdn_url = autoptimizeOptionWrapper::get_option('autoptimize_cdn_url', ''); |
|
818 | + $cdn_url = autoptimizeUtils::tweak_cdn_url_if_needed($cdn_url); |
|
819 | + $cdn_url = apply_filters('autoptimize_filter_base_cdnurl', $cdn_url); |
|
820 | 820 | } |
821 | 821 | |
822 | 822 | return $cdn_url; |
@@ -826,47 +826,47 @@ discard block |
||
826 | 826 | // returns array of strings that if found in an <img tag will stop the img from being lazy-loaded. |
827 | 827 | static $exclude_lazyload_array = null; |
828 | 828 | |
829 | - if ( null === $exclude_lazyload_array ) { |
|
829 | + if (null === $exclude_lazyload_array) { |
|
830 | 830 | $options = $this->options; |
831 | 831 | |
832 | 832 | // set default exclusions. |
833 | - $exclude_lazyload_array = array( 'skip-lazy', 'data-no-lazy', 'notlazy', 'data-src', 'data-srcset', 'data:image/', 'data-lazyload', 'rev-slidebg', 'loading="eager"' ); |
|
833 | + $exclude_lazyload_array = array('skip-lazy', 'data-no-lazy', 'notlazy', 'data-src', 'data-srcset', 'data:image/', 'data-lazyload', 'rev-slidebg', 'loading="eager"'); |
|
834 | 834 | |
835 | 835 | // add from setting. |
836 | - if ( array_key_exists( 'autoptimize_imgopt_text_field_5', $options ) ) { |
|
836 | + if (array_key_exists('autoptimize_imgopt_text_field_5', $options)) { |
|
837 | 837 | $exclude_lazyload_option = $options['autoptimize_imgopt_text_field_5']; |
838 | - if ( ! empty( $exclude_lazyload_option ) ) { |
|
839 | - $exclude_lazyload_array = array_merge( $exclude_lazyload_array, array_filter( array_map( 'trim', explode( ',', $options['autoptimize_imgopt_text_field_5'] ) ) ) ); |
|
838 | + if (!empty($exclude_lazyload_option)) { |
|
839 | + $exclude_lazyload_array = array_merge($exclude_lazyload_array, array_filter(array_map('trim', explode(',', $options['autoptimize_imgopt_text_field_5'])))); |
|
840 | 840 | } |
841 | 841 | } |
842 | 842 | |
843 | 843 | // and filter for developer-initiated changes. |
844 | - $exclude_lazyload_array = apply_filters( 'autoptimize_filter_imgopt_lazyload_exclude_array', $exclude_lazyload_array ); |
|
844 | + $exclude_lazyload_array = apply_filters('autoptimize_filter_imgopt_lazyload_exclude_array', $exclude_lazyload_array); |
|
845 | 845 | } |
846 | 846 | |
847 | 847 | return $exclude_lazyload_array; |
848 | 848 | } |
849 | 849 | |
850 | - public function inject_classes_in_tag( $tag, $target_class ) { |
|
851 | - if ( strpos( $tag, 'class=' ) !== false ) { |
|
852 | - $tag = preg_replace( '/(\sclass\s?=\s?("|\'))/', '$1' . $target_class, $tag ); |
|
850 | + public function inject_classes_in_tag($tag, $target_class) { |
|
851 | + if (strpos($tag, 'class=') !== false) { |
|
852 | + $tag = preg_replace('/(\sclass\s?=\s?("|\'))/', '$1'.$target_class, $tag); |
|
853 | 853 | } else { |
854 | - $tag = preg_replace( '/(<img)\s/', '$1 class="' . trim( $target_class ) . '" ', $tag ); |
|
854 | + $tag = preg_replace('/(<img)\s/', '$1 class="'.trim($target_class).'" ', $tag); |
|
855 | 855 | } |
856 | 856 | |
857 | 857 | return $tag; |
858 | 858 | } |
859 | 859 | |
860 | - public function get_default_lazyload_placeholder( $imgopt_w, $imgopt_h ) { |
|
861 | - return 'data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220%200%20' . $imgopt_w . '%20' . $imgopt_h . '%22%3E%3C/svg%3E'; |
|
860 | + public function get_default_lazyload_placeholder($imgopt_w, $imgopt_h) { |
|
861 | + return 'data:image/svg+xml,%3Csvg%20xmlns=%22http://www.w3.org/2000/svg%22%20viewBox=%220%200%20'.$imgopt_w.'%20'.$imgopt_h.'%22%3E%3C/svg%3E'; |
|
862 | 862 | } |
863 | 863 | |
864 | 864 | public function should_webp() { |
865 | 865 | static $webp_return = null; |
866 | 866 | |
867 | - if ( is_null( $webp_return ) ) { |
|
867 | + if (is_null($webp_return)) { |
|
868 | 868 | // webp only works if imgopt and lazyload are also active. |
869 | - if ( ! empty( $this->options['autoptimize_imgopt_checkbox_field_4'] ) && ! empty( $this->options['autoptimize_imgopt_checkbox_field_3'] ) && $this->imgopt_active() ) { |
|
869 | + if (!empty($this->options['autoptimize_imgopt_checkbox_field_4']) && !empty($this->options['autoptimize_imgopt_checkbox_field_3']) && $this->imgopt_active()) { |
|
870 | 870 | $webp_return = true; |
871 | 871 | } else { |
872 | 872 | $webp_return = false; |
@@ -876,9 +876,9 @@ discard block |
||
876 | 876 | return $webp_return; |
877 | 877 | } |
878 | 878 | |
879 | - public function process_picture_tag( $in, $imgopt = false, $lazy = false ) { |
|
879 | + public function process_picture_tag($in, $imgopt = false, $lazy = false) { |
|
880 | 880 | // check if "<picture" is present and if filter allows us to process <picture>. |
881 | - if ( strpos( $in, '<picture' ) === false || apply_filters( 'autoptimize_filter_imgopt_dopicture', true ) === false ) { |
|
881 | + if (strpos($in, '<picture') === false || apply_filters('autoptimize_filter_imgopt_dopicture', true) === false) { |
|
882 | 882 | return $in; |
883 | 883 | } |
884 | 884 | |
@@ -886,37 +886,37 @@ discard block |
||
886 | 886 | $to_replace_pict = array(); |
887 | 887 | |
888 | 888 | // extract and process each picture-node. |
889 | - preg_match_all( '#<picture.*</picture>#Usmi', $in, $_pictures, PREG_SET_ORDER ); |
|
890 | - foreach ( $_pictures as $_picture ) { |
|
891 | - $_picture = $this->maybe_fix_missing_quotes( $_picture ); |
|
892 | - if ( strpos( $_picture[0], '<source ' ) !== false && preg_match_all( '#<source .*srcset=(?:"|\')(?!data)(.*)(?:"|\').*>#Usmi', $_picture[0], $_sources, PREG_SET_ORDER ) !== false ) { |
|
893 | - foreach ( $_sources as $_source ) { |
|
889 | + preg_match_all('#<picture.*</picture>#Usmi', $in, $_pictures, PREG_SET_ORDER); |
|
890 | + foreach ($_pictures as $_picture) { |
|
891 | + $_picture = $this->maybe_fix_missing_quotes($_picture); |
|
892 | + if (strpos($_picture[0], '<source ') !== false && preg_match_all('#<source .*srcset=(?:"|\')(?!data)(.*)(?:"|\').*>#Usmi', $_picture[0], $_sources, PREG_SET_ORDER) !== false) { |
|
893 | + foreach ($_sources as $_source) { |
|
894 | 894 | $_picture_replacement = $_source[0]; |
895 | 895 | |
896 | 896 | // should we optimize the image? |
897 | - if ( $imgopt && $this->can_optimize_image( $_source[1] ) ) { |
|
898 | - $_picture_replacement = str_replace( $_source[1], $this->build_imgopt_url( $_source[1] ), $_picture_replacement ); |
|
897 | + if ($imgopt && $this->can_optimize_image($_source[1])) { |
|
898 | + $_picture_replacement = str_replace($_source[1], $this->build_imgopt_url($_source[1]), $_picture_replacement); |
|
899 | 899 | } |
900 | 900 | // should we lazy-load? |
901 | - if ( $lazy && $this->should_lazyload() && str_ireplace( $_exclusions, '', $_picture_replacement ) === $_picture_replacement ) { |
|
902 | - $_picture_replacement = str_replace( ' srcset=', ' data-srcset=', $_picture_replacement ); |
|
901 | + if ($lazy && $this->should_lazyload() && str_ireplace($_exclusions, '', $_picture_replacement) === $_picture_replacement) { |
|
902 | + $_picture_replacement = str_replace(' srcset=', ' data-srcset=', $_picture_replacement); |
|
903 | 903 | } |
904 | - $to_replace_pict[ $_source[0] ] = $_picture_replacement; |
|
904 | + $to_replace_pict[$_source[0]] = $_picture_replacement; |
|
905 | 905 | } |
906 | 906 | } |
907 | 907 | } |
908 | 908 | |
909 | 909 | // and return the fully procesed $in. |
910 | - $out = str_replace( array_keys( $to_replace_pict ), array_values( $to_replace_pict ), $in ); |
|
910 | + $out = str_replace(array_keys($to_replace_pict), array_values($to_replace_pict), $in); |
|
911 | 911 | |
912 | 912 | return $out; |
913 | 913 | } |
914 | 914 | |
915 | - public function process_bgimage( $in ) { |
|
916 | - if ( strpos( $in, 'background-image:' ) !== false && apply_filters( 'autoptimize_filter_imgopt_lazyload_backgroundimages', true ) ) { |
|
915 | + public function process_bgimage($in) { |
|
916 | + if (strpos($in, 'background-image:') !== false && apply_filters('autoptimize_filter_imgopt_lazyload_backgroundimages', true)) { |
|
917 | 917 | $out = preg_replace_callback( |
918 | 918 | '/(<(?:article|aside|body|div|footer|header|p|section|table)[^>]*)\sstyle=(?:"|\')[^<>]*?background-image:\s?url\((?:"|\')?([^"\')]*)(?:"|\')?\)[^>]*/', |
919 | - array( $this, 'lazyload_bgimg_callback' ), |
|
919 | + array($this, 'lazyload_bgimg_callback'), |
|
920 | 920 | $in |
921 | 921 | ); |
922 | 922 | return $out; |
@@ -924,27 +924,27 @@ discard block |
||
924 | 924 | return $in; |
925 | 925 | } |
926 | 926 | |
927 | - public function lazyload_bgimg_callback( $matches ) { |
|
928 | - if ( str_ireplace( $this->get_lazyload_exclusions(), '', $matches[0] ) === $matches[0] ) { |
|
927 | + public function lazyload_bgimg_callback($matches) { |
|
928 | + if (str_ireplace($this->get_lazyload_exclusions(), '', $matches[0]) === $matches[0]) { |
|
929 | 929 | // get placeholder & lazyload class strings. |
930 | - $placeholder = apply_filters( 'autoptimize_filter_imgopt_lazyload_placeholder', $this->get_default_lazyload_placeholder( 500, 300 ) ); |
|
931 | - $lazyload_class = apply_filters( 'autoptimize_filter_imgopt_lazyload_class', 'lazyload' ); |
|
930 | + $placeholder = apply_filters('autoptimize_filter_imgopt_lazyload_placeholder', $this->get_default_lazyload_placeholder(500, 300)); |
|
931 | + $lazyload_class = apply_filters('autoptimize_filter_imgopt_lazyload_class', 'lazyload'); |
|
932 | 932 | // replace background-image URL with SVG placeholder. |
933 | - $out = str_replace( $matches[2], $placeholder, $matches[0] ); |
|
933 | + $out = str_replace($matches[2], $placeholder, $matches[0]); |
|
934 | 934 | // add data-bg attribute with real background-image URL for lazyload to pick up. |
935 | - $out = str_replace( $matches[1], $matches[1] . ' data-bg="' . trim( str_replace( "\r\n", '', $matches[2] ) ) . '"', $out ); |
|
935 | + $out = str_replace($matches[1], $matches[1].' data-bg="'.trim(str_replace("\r\n", '', $matches[2])).'"', $out); |
|
936 | 936 | // add lazyload class to tag. |
937 | - $out = $this->inject_classes_in_tag( $out, "$lazyload_class " ); |
|
937 | + $out = $this->inject_classes_in_tag($out, "$lazyload_class "); |
|
938 | 938 | return $out; |
939 | 939 | } |
940 | 940 | return $matches[0]; |
941 | 941 | } |
942 | 942 | |
943 | - public function maybe_fix_missing_quotes( $tag_in ) { |
|
943 | + public function maybe_fix_missing_quotes($tag_in) { |
|
944 | 944 | // W3TC's Minify_HTML class removes quotes around attribute value, this re-adds them for the class and width/height attributes so we can lazyload properly. |
945 | - if ( file_exists( WP_PLUGIN_DIR . '/w3-total-cache/w3-total-cache.php' ) && class_exists( 'Minify_HTML' ) && apply_filters( 'autoptimize_filter_imgopt_fixquotes', true ) ) { |
|
946 | - $tag_out = preg_replace( '/class\s?=([^("|\')]*)(\s|>)/U', 'class=\'$1\'$2', $tag_in ); |
|
947 | - $tag_out = preg_replace( '/\s(width|height)=(?:"|\')?([^\s"\'>]*)(?:"|\')?/', ' $1=\'$2\'', $tag_out ); |
|
945 | + if (file_exists(WP_PLUGIN_DIR.'/w3-total-cache/w3-total-cache.php') && class_exists('Minify_HTML') && apply_filters('autoptimize_filter_imgopt_fixquotes', true)) { |
|
946 | + $tag_out = preg_replace('/class\s?=([^("|\')]*)(\s|>)/U', 'class=\'$1\'$2', $tag_in); |
|
947 | + $tag_out = preg_replace('/\s(width|height)=(?:"|\')?([^\s"\'>]*)(?:"|\')?/', ' $1=\'$2\'', $tag_out); |
|
948 | 948 | return $tag_out; |
949 | 949 | } else { |
950 | 950 | return $tag_in; |
@@ -957,23 +957,23 @@ discard block |
||
957 | 957 | public function imgopt_admin_menu() |
958 | 958 | { |
959 | 959 | // no acces if multisite and not network admin and no site config allowed. |
960 | - if ( autoptimizeConfig::should_show_menu_tabs() ) { |
|
960 | + if (autoptimizeConfig::should_show_menu_tabs()) { |
|
961 | 961 | add_submenu_page( |
962 | 962 | null, |
963 | 963 | 'autoptimize_imgopt', |
964 | 964 | 'autoptimize_imgopt', |
965 | 965 | 'manage_options', |
966 | 966 | 'autoptimize_imgopt', |
967 | - array( $this, 'imgopt_options_page' ) |
|
967 | + array($this, 'imgopt_options_page') |
|
968 | 968 | ); |
969 | 969 | } |
970 | - register_setting( 'autoptimize_imgopt_settings', 'autoptimize_imgopt_settings' ); |
|
970 | + register_setting('autoptimize_imgopt_settings', 'autoptimize_imgopt_settings'); |
|
971 | 971 | } |
972 | 972 | |
973 | - public function add_imgopt_tab( $in ) |
|
973 | + public function add_imgopt_tab($in) |
|
974 | 974 | { |
975 | - if ( autoptimizeConfig::should_show_menu_tabs() ) { |
|
976 | - $in = array_merge( $in, array( 'autoptimize_imgopt' => __( 'Images', 'autoptimize' ) ) ); |
|
975 | + if (autoptimizeConfig::should_show_menu_tabs()) { |
|
976 | + $in = array_merge($in, array('autoptimize_imgopt' => __('Images', 'autoptimize'))); |
|
977 | 977 | } |
978 | 978 | |
979 | 979 | return $in; |
@@ -982,7 +982,7 @@ discard block |
||
982 | 982 | public function imgopt_options_page() |
983 | 983 | { |
984 | 984 | // Check querystring for "refreshCacheChecker" and call cachechecker if so. |
985 | - if ( array_key_exists( 'refreshImgProvStats', $_GET ) && 1 == $_GET['refreshImgProvStats'] ) { |
|
985 | + if (array_key_exists('refreshImgProvStats', $_GET) && 1 == $_GET['refreshImgProvStats']) { |
|
986 | 986 | $this->query_img_provider_stats(); |
987 | 987 | } |
988 | 988 | |
@@ -994,47 +994,47 @@ discard block |
||
994 | 994 | #ao_settings_form .form-table th {font-weight: normal;} |
995 | 995 | #autoptimize_imgopt_descr{font-size: 120%;} |
996 | 996 | </style> |
997 | - <script>document.title = "Autoptimize: <?php _e( 'Images', 'autoptimize' ); ?> " + document.title;</script> |
|
997 | + <script>document.title = "Autoptimize: <?php _e('Images', 'autoptimize'); ?> " + document.title;</script> |
|
998 | 998 | <div class="wrap"> |
999 | - <h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1> |
|
999 | + <h1><?php _e('Autoptimize Settings', 'autoptimize'); ?></h1> |
|
1000 | 1000 | <?php echo autoptimizeConfig::ao_admin_tabs(); ?> |
1001 | - <?php if ( 'down' === $options['availabilities']['extra_imgopt']['status'] ) { ?> |
|
1001 | + <?php if ('down' === $options['availabilities']['extra_imgopt']['status']) { ?> |
|
1002 | 1002 | <div class="notice-warning notice"><p> |
1003 | 1003 | <?php |
1004 | 1004 | // translators: "Autoptimize support forum" will appear in a "a href". |
1005 | - echo sprintf( __( 'The image optimization service is currently down, image optimization will be skipped until further notice. Check the %1$sAutoptimize support forum%2$s for more info.', 'autoptimize' ), '<a href="https://wordpress.org/support/plugin/autoptimize/" target="_blank">', '</a>' ); |
|
1005 | + echo sprintf(__('The image optimization service is currently down, image optimization will be skipped until further notice. Check the %1$sAutoptimize support forum%2$s for more info.', 'autoptimize'), '<a href="https://wordpress.org/support/plugin/autoptimize/" target="_blank">', '</a>'); |
|
1006 | 1006 | ?> |
1007 | 1007 | </p></div> |
1008 | 1008 | <?php } ?> |
1009 | 1009 | |
1010 | - <?php if ( 'launch' === $options['availabilities']['extra_imgopt']['status'] && ! autoptimizeImages::instance()->launch_ok() ) { ?> |
|
1010 | + <?php if ('launch' === $options['availabilities']['extra_imgopt']['status'] && !autoptimizeImages::instance()->launch_ok()) { ?> |
|
1011 | 1011 | <div class="notice-warning notice"><p> |
1012 | - <?php _e( 'The image optimization service is launching, but not yet available for this domain, it should become available in the next couple of days.', 'autoptimize' ); ?> |
|
1012 | + <?php _e('The image optimization service is launching, but not yet available for this domain, it should become available in the next couple of days.', 'autoptimize'); ?> |
|
1013 | 1013 | </p></div> |
1014 | 1014 | <?php } ?> |
1015 | 1015 | |
1016 | - <?php if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'get_active_modules' ) && in_array( 'photon', Jetpack::get_active_modules() ) ) { ?> |
|
1016 | + <?php if (class_exists('Jetpack') && method_exists('Jetpack', 'get_active_modules') && in_array('photon', Jetpack::get_active_modules())) { ?> |
|
1017 | 1017 | <div class="notice-warning notice"><p> |
1018 | 1018 | <?php |
1019 | 1019 | // translators: "disable Jetpack's site accelerator for images" will appear in a "a href" linking to the jetpack settings page. |
1020 | - echo sprintf( __( 'Please %1$sdisable Jetpack\'s site accelerator for images%2$s to be able to use Autoptomize\'s advanced image optimization features below.', 'autoptimize' ), '<a href="admin.php?page=jetpack#/settings">', '</a>' ); |
|
1020 | + echo sprintf(__('Please %1$sdisable Jetpack\'s site accelerator for images%2$s to be able to use Autoptomize\'s advanced image optimization features below.', 'autoptimize'), '<a href="admin.php?page=jetpack#/settings">', '</a>'); |
|
1021 | 1021 | ?> |
1022 | 1022 | </p></div> |
1023 | 1023 | <?php } ?> |
1024 | - <form id='ao_settings_form' action='<?php echo admin_url( 'options.php' ); ?>' method='post'> |
|
1025 | - <?php settings_fields( 'autoptimize_imgopt_settings' ); ?> |
|
1026 | - <h2><?php _e( 'Image optimization', 'autoptimize' ); ?></h2> |
|
1027 | - <span id='autoptimize_imgopt_descr'><?php _e( 'Make your site significantly faster by just ticking a couple of checkboxes to optimize and lazy load your images, WebP support included!', 'autoptimize' ); ?></span> |
|
1024 | + <form id='ao_settings_form' action='<?php echo admin_url('options.php'); ?>' method='post'> |
|
1025 | + <?php settings_fields('autoptimize_imgopt_settings'); ?> |
|
1026 | + <h2><?php _e('Image optimization', 'autoptimize'); ?></h2> |
|
1027 | + <span id='autoptimize_imgopt_descr'><?php _e('Make your site significantly faster by just ticking a couple of checkboxes to optimize and lazy load your images, WebP support included!', 'autoptimize'); ?></span> |
|
1028 | 1028 | <table class="form-table"> |
1029 | 1029 | <tr> |
1030 | - <th scope="row"><?php _e( 'Optimize Images', 'autoptimize' ); ?></th> |
|
1030 | + <th scope="row"><?php _e('Optimize Images', 'autoptimize'); ?></th> |
|
1031 | 1031 | <td> |
1032 | - <label><input id='autoptimize_imgopt_checkbox' type='checkbox' name='autoptimize_imgopt_settings[autoptimize_imgopt_checkbox_field_1]' <?php if ( ! empty( $options['autoptimize_imgopt_checkbox_field_1'] ) && '1' === $options['autoptimize_imgopt_checkbox_field_1'] ) { echo 'checked="checked"'; } ?> value='1'><?php _e( 'Optimize images on the fly and serve them from Shortpixel\'s global CDN.', 'autoptimize' ); ?></label> |
|
1032 | + <label><input id='autoptimize_imgopt_checkbox' type='checkbox' name='autoptimize_imgopt_settings[autoptimize_imgopt_checkbox_field_1]' <?php if (!empty($options['autoptimize_imgopt_checkbox_field_1']) && '1' === $options['autoptimize_imgopt_checkbox_field_1']) { echo 'checked="checked"'; } ?> value='1'><?php _e('Optimize images on the fly and serve them from Shortpixel\'s global CDN.', 'autoptimize'); ?></label> |
|
1033 | 1033 | <?php |
1034 | 1034 | // show shortpixel status. |
1035 | 1035 | $_notice = autoptimizeImages::instance()->get_imgopt_status_notice(); |
1036 | - if ( $_notice ) { |
|
1037 | - switch ( $_notice['status'] ) { |
|
1036 | + if ($_notice) { |
|
1037 | + switch ($_notice['status']) { |
|
1038 | 1038 | case 2: |
1039 | 1039 | $_notice_color = 'green'; |
1040 | 1040 | break; |
@@ -1049,29 +1049,29 @@ discard block |
||
1049 | 1049 | default: |
1050 | 1050 | $_notice_color = 'green'; |
1051 | 1051 | } |
1052 | - echo apply_filters( 'autoptimize_filter_imgopt_settings_status', '<p><strong><span style="color:' . $_notice_color . ';">' . __( 'Shortpixel status: ', 'autoptimize' ) . '</span></strong>' . $_notice['notice'] . '</p>' ); |
|
1052 | + echo apply_filters('autoptimize_filter_imgopt_settings_status', '<p><strong><span style="color:'.$_notice_color.';">'.__('Shortpixel status: ', 'autoptimize').'</span></strong>'.$_notice['notice'].'</p>'); |
|
1053 | 1053 | } else { |
1054 | 1054 | // translators: link points to shortpixel. |
1055 | - $upsell_msg_1 = '<p>' . sprintf( __( 'Get more Google love and improve your website\'s loading speed by having your publicly available images optimized on the fly (also in the "next-gen" WebP image format) by %1$sShortPixel%2$s and then cached and served fast from Shortpixel\'s global CDN.', 'autoptimize' ), '<a href="https://shortpixel.com/aospai' . $sp_url_suffix . '" target="_blank">', '</a>' ); |
|
1056 | - if ( 'launch' === $options['availabilities']['extra_imgopt']['status'] ) { |
|
1057 | - $upsell_msg_2 = __( 'For a limited time only, this service is offered free for all Autoptimize users, <b>don\'t miss the chance to test it</b> and see how much it could improve your site\'s speed.', 'autoptimize' ); |
|
1055 | + $upsell_msg_1 = '<p>'.sprintf(__('Get more Google love and improve your website\'s loading speed by having your publicly available images optimized on the fly (also in the "next-gen" WebP image format) by %1$sShortPixel%2$s and then cached and served fast from Shortpixel\'s global CDN.', 'autoptimize'), '<a href="https://shortpixel.com/aospai'.$sp_url_suffix.'" target="_blank">', '</a>'); |
|
1056 | + if ('launch' === $options['availabilities']['extra_imgopt']['status']) { |
|
1057 | + $upsell_msg_2 = __('For a limited time only, this service is offered free for all Autoptimize users, <b>don\'t miss the chance to test it</b> and see how much it could improve your site\'s speed.', 'autoptimize'); |
|
1058 | 1058 | } else { |
1059 | 1059 | // translators: link points to shortpixel. |
1060 | - $upsell_msg_2 = sprintf( __( '%1$sSign-up now%2$s to receive a 1 000 bonus + 50% more image optimization credits regardless of the traffic used. More image optimizations can be purchased starting with $4.99.', 'autoptimize' ), '<a href="https://shortpixel.com/aospai' . $sp_url_suffix . '" target="_blank">', '</a>' ); |
|
1060 | + $upsell_msg_2 = sprintf(__('%1$sSign-up now%2$s to receive a 1 000 bonus + 50% more image optimization credits regardless of the traffic used. More image optimizations can be purchased starting with $4.99.', 'autoptimize'), '<a href="https://shortpixel.com/aospai'.$sp_url_suffix.'" target="_blank">', '</a>'); |
|
1061 | 1061 | } |
1062 | - echo apply_filters( 'autoptimize_imgopt_imgopt_settings_copy', $upsell_msg_1 . ' ' . $upsell_msg_2 . '</p>' ); |
|
1062 | + echo apply_filters('autoptimize_imgopt_imgopt_settings_copy', $upsell_msg_1.' '.$upsell_msg_2.'</p>'); |
|
1063 | 1063 | } |
1064 | 1064 | // translators: link points to shortpixel FAQ. |
1065 | - $faqcopy = sprintf( __( '<strong>Questions</strong>? Have a look at the %1$sShortPixel FAQ%2$s!', 'autoptimize' ), '<strong><a href="https://shortpixel.helpscoutdocs.com/category/60-shortpixel-ai-cdn" target="_blank">', '</strong></a>' ); |
|
1066 | - $faqcopy = $faqcopy . ' ' . __( 'Only works for sites/ images that are publicly available.', 'autoptimize' ); |
|
1065 | + $faqcopy = sprintf(__('<strong>Questions</strong>? Have a look at the %1$sShortPixel FAQ%2$s!', 'autoptimize'), '<strong><a href="https://shortpixel.helpscoutdocs.com/category/60-shortpixel-ai-cdn" target="_blank">', '</strong></a>'); |
|
1066 | + $faqcopy = $faqcopy.' '.__('Only works for sites/ images that are publicly available.', 'autoptimize'); |
|
1067 | 1067 | // translators: links points to shortpixel TOS & Privacy Policy. |
1068 | - $toscopy = sprintf( __( 'Usage of this feature is subject to Shortpixel\'s %1$sTerms of Use%2$s and %3$sPrivacy policy%4$s.', 'autoptimize' ), '<a href="https://shortpixel.com/tos' . $sp_url_suffix . '" target="_blank">', '</a>', '<a href="https://shortpixel.com/pp' . $sp_url_suffix . '" target="_blank">', '</a>' ); |
|
1069 | - echo apply_filters( 'autoptimize_imgopt_imgopt_settings_tos', '<p>' . $faqcopy . ' ' . $toscopy . '</p>' ); |
|
1068 | + $toscopy = sprintf(__('Usage of this feature is subject to Shortpixel\'s %1$sTerms of Use%2$s and %3$sPrivacy policy%4$s.', 'autoptimize'), '<a href="https://shortpixel.com/tos'.$sp_url_suffix.'" target="_blank">', '</a>', '<a href="https://shortpixel.com/pp'.$sp_url_suffix.'" target="_blank">', '</a>'); |
|
1069 | + echo apply_filters('autoptimize_imgopt_imgopt_settings_tos', '<p>'.$faqcopy.' '.$toscopy.'</p>'); |
|
1070 | 1070 | ?> |
1071 | 1071 | </td> |
1072 | 1072 | </tr> |
1073 | - <tr id='autoptimize_imgopt_quality' <?php if ( ! array_key_exists( 'autoptimize_imgopt_checkbox_field_1', $options ) || ( isset( $options['autoptimize_imgopt_checkbox_field_1'] ) && '1' !== $options['autoptimize_imgopt_checkbox_field_1'] ) ) { echo 'class="hidden"'; } ?>> |
|
1074 | - <th scope="row"><?php _e( 'Image Optimization quality', 'autoptimize' ); ?></th> |
|
1073 | + <tr id='autoptimize_imgopt_quality' <?php if (!array_key_exists('autoptimize_imgopt_checkbox_field_1', $options) || (isset($options['autoptimize_imgopt_checkbox_field_1']) && '1' !== $options['autoptimize_imgopt_checkbox_field_1'])) { echo 'class="hidden"'; } ?>> |
|
1074 | + <th scope="row"><?php _e('Image Optimization quality', 'autoptimize'); ?></th> |
|
1075 | 1075 | <td> |
1076 | 1076 | <label> |
1077 | 1077 | <select name='autoptimize_imgopt_settings[autoptimize_imgopt_select_field_2]'> |
@@ -1079,12 +1079,12 @@ discard block |
||
1079 | 1079 | $_imgopt_array = autoptimizeImages::instance()->get_img_quality_array(); |
1080 | 1080 | $_imgopt_val = autoptimizeImages::instance()->get_img_quality_setting(); |
1081 | 1081 | |
1082 | - foreach ( $_imgopt_array as $key => $value ) { |
|
1083 | - echo '<option value="' . $key . '"'; |
|
1084 | - if ( $_imgopt_val == $key ) { |
|
1082 | + foreach ($_imgopt_array as $key => $value) { |
|
1083 | + echo '<option value="'.$key.'"'; |
|
1084 | + if ($_imgopt_val == $key) { |
|
1085 | 1085 | echo ' selected'; |
1086 | 1086 | } |
1087 | - echo '>' . ucfirst( $value ) . '</option>'; |
|
1087 | + echo '>'.ucfirst($value).'</option>'; |
|
1088 | 1088 | } |
1089 | 1089 | echo "\n"; |
1090 | 1090 | ?> |
@@ -1093,31 +1093,31 @@ discard block |
||
1093 | 1093 | <p> |
1094 | 1094 | <?php |
1095 | 1095 | // translators: link points to shortpixel image test page. |
1096 | - echo apply_filters( 'autoptimize_imgopt_imgopt_quality_copy', sprintf( __( 'You can %1$stest compression levels here%2$s.', 'autoptimize' ), '<a href="https://shortpixel.com/oic' . $sp_url_suffix . '" target="_blank">', '</a>' ) ); |
|
1096 | + echo apply_filters('autoptimize_imgopt_imgopt_quality_copy', sprintf(__('You can %1$stest compression levels here%2$s.', 'autoptimize'), '<a href="https://shortpixel.com/oic'.$sp_url_suffix.'" target="_blank">', '</a>')); |
|
1097 | 1097 | ?> |
1098 | 1098 | </p> |
1099 | 1099 | </td> |
1100 | 1100 | </tr> |
1101 | - <tr id='autoptimize_imgopt_webp' <?php if ( ! array_key_exists( 'autoptimize_imgopt_checkbox_field_1', $options ) || ( isset( $options['autoptimize_imgopt_checkbox_field_1'] ) && '1' !== $options['autoptimize_imgopt_checkbox_field_1'] ) ) { echo 'class="hidden"'; } ?>> |
|
1102 | - <th scope="row"><?php _e( 'Load WebP in supported browsers?', 'autoptimize' ); ?></th> |
|
1101 | + <tr id='autoptimize_imgopt_webp' <?php if (!array_key_exists('autoptimize_imgopt_checkbox_field_1', $options) || (isset($options['autoptimize_imgopt_checkbox_field_1']) && '1' !== $options['autoptimize_imgopt_checkbox_field_1'])) { echo 'class="hidden"'; } ?>> |
|
1102 | + <th scope="row"><?php _e('Load WebP in supported browsers?', 'autoptimize'); ?></th> |
|
1103 | 1103 | <td> |
1104 | - <label><input type='checkbox' id='autoptimize_imgopt_webp_checkbox' name='autoptimize_imgopt_settings[autoptimize_imgopt_checkbox_field_4]' <?php if ( ! empty( $options['autoptimize_imgopt_checkbox_field_4'] ) && '1' === $options['autoptimize_imgopt_checkbox_field_3'] ) { echo 'checked="checked"'; } ?> value='1'><?php _e( 'Automatically serve "next-gen" WebP image format to any browser that supports it (requires lazy load to be active).', 'autoptimize' ); ?></label> |
|
1104 | + <label><input type='checkbox' id='autoptimize_imgopt_webp_checkbox' name='autoptimize_imgopt_settings[autoptimize_imgopt_checkbox_field_4]' <?php if (!empty($options['autoptimize_imgopt_checkbox_field_4']) && '1' === $options['autoptimize_imgopt_checkbox_field_3']) { echo 'checked="checked"'; } ?> value='1'><?php _e('Automatically serve "next-gen" WebP image format to any browser that supports it (requires lazy load to be active).', 'autoptimize'); ?></label> |
|
1105 | 1105 | </td> |
1106 | 1106 | </tr> |
1107 | 1107 | <tr> |
1108 | - <th scope="row"><?php _e( 'Lazy-load images?', 'autoptimize' ); ?></th> |
|
1108 | + <th scope="row"><?php _e('Lazy-load images?', 'autoptimize'); ?></th> |
|
1109 | 1109 | <td> |
1110 | - <label><input type='checkbox' id='autoptimize_imgopt_lazyload_checkbox' name='autoptimize_imgopt_settings[autoptimize_imgopt_checkbox_field_3]' <?php if ( ! empty( $options['autoptimize_imgopt_checkbox_field_3'] ) && '1' === $options['autoptimize_imgopt_checkbox_field_3'] ) { echo 'checked="checked"'; } ?> value='1'><?php _e( 'Image lazy-loading will delay the loading of non-visible images to allow the browser to optimally load all resources for the "above the fold"-page first.', 'autoptimize' ); ?></label> |
|
1110 | + <label><input type='checkbox' id='autoptimize_imgopt_lazyload_checkbox' name='autoptimize_imgopt_settings[autoptimize_imgopt_checkbox_field_3]' <?php if (!empty($options['autoptimize_imgopt_checkbox_field_3']) && '1' === $options['autoptimize_imgopt_checkbox_field_3']) { echo 'checked="checked"'; } ?> value='1'><?php _e('Image lazy-loading will delay the loading of non-visible images to allow the browser to optimally load all resources for the "above the fold"-page first.', 'autoptimize'); ?></label> |
|
1111 | 1111 | </td> |
1112 | 1112 | </tr> |
1113 | - <tr id='autoptimize_imgopt_lazyload_exclusions' <?php if ( ! array_key_exists( 'autoptimize_imgopt_checkbox_field_3', $options ) || ( isset( $options['autoptimize_imgopt_checkbox_field_3'] ) && '1' !== $options['autoptimize_imgopt_checkbox_field_3'] ) ) { echo 'class="hidden"'; } ?>> |
|
1114 | - <th scope="row"><?php _e( 'Lazy-load exclusions', 'autoptimize' ); ?></th> |
|
1113 | + <tr id='autoptimize_imgopt_lazyload_exclusions' <?php if (!array_key_exists('autoptimize_imgopt_checkbox_field_3', $options) || (isset($options['autoptimize_imgopt_checkbox_field_3']) && '1' !== $options['autoptimize_imgopt_checkbox_field_3'])) { echo 'class="hidden"'; } ?>> |
|
1114 | + <th scope="row"><?php _e('Lazy-load exclusions', 'autoptimize'); ?></th> |
|
1115 | 1115 | <td> |
1116 | - <label><input type='text' style='width:80%' id='autoptimize_imgopt_lazyload_exclusions' name='autoptimize_imgopt_settings[autoptimize_imgopt_text_field_5]' value='<?php if ( ! empty( $options['autoptimize_imgopt_text_field_5'] ) ) { echo esc_attr( $options['autoptimize_imgopt_text_field_5'] ); } ?>'><br /><?php _e( 'Comma-separated list of to be excluded image classes or filenames.', 'autoptimize' ); ?></label> |
|
1116 | + <label><input type='text' style='width:80%' id='autoptimize_imgopt_lazyload_exclusions' name='autoptimize_imgopt_settings[autoptimize_imgopt_text_field_5]' value='<?php if (!empty($options['autoptimize_imgopt_text_field_5'])) { echo esc_attr($options['autoptimize_imgopt_text_field_5']); } ?>'><br /><?php _e('Comma-separated list of to be excluded image classes or filenames.', 'autoptimize'); ?></label> |
|
1117 | 1117 | </td> |
1118 | 1118 | </tr> |
1119 | 1119 | </table> |
1120 | - <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e( 'Save Changes', 'autoptimize' ); ?>" /></p> |
|
1120 | + <p class="submit"><input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes', 'autoptimize'); ?>" /></p> |
|
1121 | 1121 | </form> |
1122 | 1122 | <script> |
1123 | 1123 | jQuery(document).ready(function() { |
@@ -1153,50 +1153,50 @@ discard block |
||
1153 | 1153 | * Ïmg opt status as used on dashboard. |
1154 | 1154 | */ |
1155 | 1155 | public function get_imgopt_status_notice() { |
1156 | - if ( $this->imgopt_active() ) { |
|
1156 | + if ($this->imgopt_active()) { |
|
1157 | 1157 | $_imgopt_notice = ''; |
1158 | - $_stat = autoptimizeOptionWrapper::get_option( 'autoptimize_imgopt_provider_stat', '' ); |
|
1158 | + $_stat = autoptimizeOptionWrapper::get_option('autoptimize_imgopt_provider_stat', ''); |
|
1159 | 1159 | $_site_host = AUTOPTIMIZE_SITE_DOMAIN; |
1160 | - $_imgopt_upsell = 'https://shortpixel.com/aospai/af/GWRGFLW109483/' . $_site_host; |
|
1160 | + $_imgopt_upsell = 'https://shortpixel.com/aospai/af/GWRGFLW109483/'.$_site_host; |
|
1161 | 1161 | $_imgopt_assoc = 'https://shortpixel.helpscoutdocs.com/article/94-how-to-associate-a-domain-to-my-account'; |
1162 | 1162 | $_imgopt_unreach = 'https://shortpixel.helpscoutdocs.com/article/148-why-are-my-images-redirected-from-cdn-shortpixel-ai'; |
1163 | 1163 | |
1164 | - if ( is_array( $_stat ) ) { |
|
1165 | - if ( 1 == $_stat['Status'] ) { |
|
1164 | + if (is_array($_stat)) { |
|
1165 | + if (1 == $_stat['Status']) { |
|
1166 | 1166 | // translators: "add more credits" will appear in a "a href". |
1167 | - $_imgopt_notice = sprintf( __( 'Your ShortPixel image optimization and CDN quota is almost used, make sure you %1$sadd more credits%2$s to avoid slowing down your website.', 'autoptimize' ), '<a href="' . $_imgopt_upsell . '" target="_blank">', '</a>' ); |
|
1167 | + $_imgopt_notice = sprintf(__('Your ShortPixel image optimization and CDN quota is almost used, make sure you %1$sadd more credits%2$s to avoid slowing down your website.', 'autoptimize'), '<a href="'.$_imgopt_upsell.'" target="_blank">', '</a>'); |
|
1168 | 1168 | } elseif ( -1 == $_stat['Status'] || -2 == $_stat['Status'] ) { |
1169 | 1169 | // translators: "add more credits" will appear in a "a href". |
1170 | - $_imgopt_notice = sprintf( __( 'Your ShortPixel image optimization and CDN quota was used, %1$sadd more credits%2$s to keep fast serving optimized images on your site', 'autoptimize' ), '<a href="' . $_imgopt_upsell . '" target="_blank">', '</a>' ); |
|
1170 | + $_imgopt_notice = sprintf(__('Your ShortPixel image optimization and CDN quota was used, %1$sadd more credits%2$s to keep fast serving optimized images on your site', 'autoptimize'), '<a href="'.$_imgopt_upsell.'" target="_blank">', '</a>'); |
|
1171 | 1171 | // translators: "associate your domain" will appear in a "a href". |
1172 | - $_imgopt_notice = $_imgopt_notice . ' ' . sprintf( __( 'If you already have enough credits then you may need to %1$sassociate your domain%2$s to your Shortpixel account.', 'autoptimize' ), '<a rel="noopener noreferrer" href="' . $_imgopt_assoc . '" target="_blank">', '</a>' ); |
|
1172 | + $_imgopt_notice = $_imgopt_notice.' '.sprintf(__('If you already have enough credits then you may need to %1$sassociate your domain%2$s to your Shortpixel account.', 'autoptimize'), '<a rel="noopener noreferrer" href="'.$_imgopt_assoc.'" target="_blank">', '</a>'); |
|
1173 | 1173 | } elseif ( -3 == $_stat['Status'] ) { |
1174 | 1174 | // translators: "check the documentation here" will appear in a "a href". |
1175 | - $_imgopt_notice = sprintf( __( 'It seems ShortPixel image optimization is not able to fetch images from your site, %1$scheck the documentation here%2$s for more information', 'autoptimize' ), '<a href="' . $_imgopt_unreach . '" target="_blank">', '</a>' ); |
|
1175 | + $_imgopt_notice = sprintf(__('It seems ShortPixel image optimization is not able to fetch images from your site, %1$scheck the documentation here%2$s for more information', 'autoptimize'), '<a href="'.$_imgopt_unreach.'" target="_blank">', '</a>'); |
|
1176 | 1176 | } else { |
1177 | 1177 | $_imgopt_upsell = 'https://shortpixel.com/g/af/GWRGFLW109483'; |
1178 | 1178 | // translators: "log in to check your account" will appear in a "a href". |
1179 | - $_imgopt_notice = sprintf( __( 'Your ShortPixel image optimization and CDN quota are in good shape, %1$slog in to check your account%2$s.', 'autoptimize' ), '<a href="' . $_imgopt_upsell . '" target="_blank">', '</a>' ); |
|
1179 | + $_imgopt_notice = sprintf(__('Your ShortPixel image optimization and CDN quota are in good shape, %1$slog in to check your account%2$s.', 'autoptimize'), '<a href="'.$_imgopt_upsell.'" target="_blank">', '</a>'); |
|
1180 | 1180 | } |
1181 | 1181 | |
1182 | 1182 | // add info on freshness + refresh link if status is not 2 (good shape). |
1183 | - if ( 2 != $_stat['Status'] ) { |
|
1184 | - $_imgopt_stats_refresh_url = add_query_arg( array( |
|
1183 | + if (2 != $_stat['Status']) { |
|
1184 | + $_imgopt_stats_refresh_url = add_query_arg(array( |
|
1185 | 1185 | 'page' => 'autoptimize_imgopt', |
1186 | 1186 | 'refreshImgProvStats' => '1', |
1187 | - ), admin_url( 'options-general.php' ) ); |
|
1188 | - if ( $_stat && array_key_exists( 'timestamp', $_stat ) && ! empty( $_stat['timestamp'] ) ) { |
|
1189 | - $_imgopt_stats_last_run = __( 'based on status at ', 'autoptimize' ) . date_i18n( autoptimizeOptionWrapper::get_option( 'time_format' ), $_stat['timestamp'] ); |
|
1187 | + ), admin_url('options-general.php')); |
|
1188 | + if ($_stat && array_key_exists('timestamp', $_stat) && !empty($_stat['timestamp'])) { |
|
1189 | + $_imgopt_stats_last_run = __('based on status at ', 'autoptimize').date_i18n(autoptimizeOptionWrapper::get_option('time_format'), $_stat['timestamp']); |
|
1190 | 1190 | } else { |
1191 | - $_imgopt_stats_last_run = __( 'based on previously fetched data', 'autoptimize' ); |
|
1191 | + $_imgopt_stats_last_run = __('based on previously fetched data', 'autoptimize'); |
|
1192 | 1192 | } |
1193 | - $_imgopt_notice .= ' (' . $_imgopt_stats_last_run . ', '; |
|
1193 | + $_imgopt_notice .= ' ('.$_imgopt_stats_last_run.', '; |
|
1194 | 1194 | // translators: "here to refresh" links to the Autoptimize Extra page and forces a refresh of the img opt stats. |
1195 | - $_imgopt_notice .= sprintf( __( 'click %1$shere to refresh%2$s', 'autoptimize' ), '<a href="' . $_imgopt_stats_refresh_url . '">', '</a>).' ); |
|
1195 | + $_imgopt_notice .= sprintf(__('click %1$shere to refresh%2$s', 'autoptimize'), '<a href="'.$_imgopt_stats_refresh_url.'">', '</a>).'); |
|
1196 | 1196 | } |
1197 | 1197 | |
1198 | 1198 | // and make the full notice filterable. |
1199 | - $_imgopt_notice = apply_filters( 'autoptimize_filter_imgopt_notice', $_imgopt_notice ); |
|
1199 | + $_imgopt_notice = apply_filters('autoptimize_filter_imgopt_notice', $_imgopt_notice); |
|
1200 | 1200 | |
1201 | 1201 | return array( |
1202 | 1202 | 'status' => $_stat['Status'], |
@@ -1217,14 +1217,14 @@ discard block |
||
1217 | 1217 | * Get img provider stats (used to display notice). |
1218 | 1218 | */ |
1219 | 1219 | public function query_img_provider_stats() { |
1220 | - if ( ! empty( $this->options['autoptimize_imgopt_checkbox_field_1'] ) ) { |
|
1220 | + if (!empty($this->options['autoptimize_imgopt_checkbox_field_1'])) { |
|
1221 | 1221 | $url = ''; |
1222 | - $endpoint = $this->get_imgopt_host() . 'read-domain/'; |
|
1222 | + $endpoint = $this->get_imgopt_host().'read-domain/'; |
|
1223 | 1223 | $domain = AUTOPTIMIZE_SITE_DOMAIN; |
1224 | 1224 | |
1225 | 1225 | // make sure parse_url result makes sense, keeping $url empty if not. |
1226 | - if ( $domain && ! empty( $domain ) ) { |
|
1227 | - $url = $endpoint . $domain; |
|
1226 | + if ($domain && !empty($domain)) { |
|
1227 | + $url = $endpoint.$domain; |
|
1228 | 1228 | } |
1229 | 1229 | |
1230 | 1230 | $url = apply_filters( |
@@ -1234,12 +1234,12 @@ discard block |
||
1234 | 1234 | |
1235 | 1235 | // only do the remote call if $url is not empty to make sure no parse_url |
1236 | 1236 | // weirdness results in useless calls. |
1237 | - if ( ! empty( $url ) ) { |
|
1238 | - $response = wp_remote_get( $url ); |
|
1239 | - if ( ! is_wp_error( $response ) ) { |
|
1240 | - if ( '200' == wp_remote_retrieve_response_code( $response ) ) { |
|
1241 | - $stats = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
1242 | - autoptimizeOptionWrapper::update_option( 'autoptimize_imgopt_provider_stat', $stats ); |
|
1237 | + if (!empty($url)) { |
|
1238 | + $response = wp_remote_get($url); |
|
1239 | + if (!is_wp_error($response)) { |
|
1240 | + if ('200' == wp_remote_retrieve_response_code($response)) { |
|
1241 | + $stats = json_decode(wp_remote_retrieve_body($response), true); |
|
1242 | + autoptimizeOptionWrapper::update_option('autoptimize_imgopt_provider_stat', $stats); |
|
1243 | 1243 | } |
1244 | 1244 | } |
1245 | 1245 | } |
@@ -1262,15 +1262,15 @@ discard block |
||
1262 | 1262 | { |
1263 | 1263 | static $launch_status = null; |
1264 | 1264 | |
1265 | - if ( null === $launch_status ) { |
|
1265 | + if (null === $launch_status) { |
|
1266 | 1266 | $avail_imgopt = $this->options['availabilities']['extra_imgopt']; |
1267 | - $magic_number = intval( substr( md5( parse_url( AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST ) ), 0, 3 ), 16 ); |
|
1268 | - $has_launched = autoptimizeOptionWrapper::get_option( 'autoptimize_imgopt_launched', '' ); |
|
1267 | + $magic_number = intval(substr(md5(parse_url(AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST)), 0, 3), 16); |
|
1268 | + $has_launched = autoptimizeOptionWrapper::get_option('autoptimize_imgopt_launched', ''); |
|
1269 | 1269 | $launch_status = false; |
1270 | - if ( $has_launched || ( is_array( $avail_imgopt ) && array_key_exists( 'launch-threshold', $avail_imgopt ) && $magic_number < $avail_imgopt['launch-threshold'] ) ) { |
|
1270 | + if ($has_launched || (is_array($avail_imgopt) && array_key_exists('launch-threshold', $avail_imgopt) && $magic_number < $avail_imgopt['launch-threshold'])) { |
|
1271 | 1271 | $launch_status = true; |
1272 | - if ( ! $has_launched ) { |
|
1273 | - autoptimizeOptionWrapper::update_option( 'autoptimize_imgopt_launched', 'on' ); |
|
1272 | + if (!$has_launched) { |
|
1273 | + autoptimizeOptionWrapper::update_option('autoptimize_imgopt_launched', 'on'); |
|
1274 | 1274 | } |
1275 | 1275 | } |
1276 | 1276 | } |
@@ -1287,16 +1287,16 @@ discard block |
||
1287 | 1287 | public function get_imgopt_provider_userstatus() { |
1288 | 1288 | static $_provider_userstatus = null; |
1289 | 1289 | |
1290 | - if ( is_null( $_provider_userstatus ) ) { |
|
1291 | - $_stat = autoptimizeOptionWrapper::get_option( 'autoptimize_imgopt_provider_stat', '' ); |
|
1292 | - if ( is_array( $_stat ) ) { |
|
1293 | - if ( array_key_exists( 'Status', $_stat ) ) { |
|
1290 | + if (is_null($_provider_userstatus)) { |
|
1291 | + $_stat = autoptimizeOptionWrapper::get_option('autoptimize_imgopt_provider_stat', ''); |
|
1292 | + if (is_array($_stat)) { |
|
1293 | + if (array_key_exists('Status', $_stat)) { |
|
1294 | 1294 | $_provider_userstatus['Status'] = $_stat['Status']; |
1295 | 1295 | } else { |
1296 | 1296 | // if no stats then we assume all is well. |
1297 | 1297 | $_provider_userstatus['Status'] = 2; |
1298 | 1298 | } |
1299 | - if ( array_key_exists( 'timestamp', $_stat ) ) { |
|
1299 | + if (array_key_exists('timestamp', $_stat)) { |
|
1300 | 1300 | $_provider_userstatus['timestamp'] = $_stat['timestamp']; |
1301 | 1301 | } else { |
1302 | 1302 | // if no timestamp then we return "". |
@@ -12,28 +12,28 @@ |
||
12 | 12 | .ao_settings_div {background: white;border: 1px solid #ccc;padding: 1px 15px;margin: 15px 10px 10px 0;} |
13 | 13 | .ao_settings_div .form-table th {font-weight: normal;} |
14 | 14 | </style> |
15 | - <script>document.title = "Autoptimize: <?php _e( 'Critical CSS', 'autoptimize' ); ?> " + document.title;</script> |
|
15 | + <script>document.title = "Autoptimize: <?php _e('Critical CSS', 'autoptimize'); ?> " + document.title;</script> |
|
16 | 16 | <ul id="explain-panel"> |
17 | 17 | <div class="ao_settings_div"> |
18 | 18 | <?php |
19 | 19 | $ccss_explanation = ''; |
20 | 20 | |
21 | 21 | // get the HTML with the explanation of what critical CSS is. |
22 | - if ( apply_filters( 'autoptimize_settingsscreen_remotehttp', true ) ) { |
|
23 | - $ccss_explanation = get_transient( 'ao_ccss_explain' ); |
|
24 | - if ( empty( $ccss_explanation ) ) { |
|
25 | - $ccss_expl_resp = wp_remote_get( 'https://misc.optimizingmatters.com/autoptimize_ccss_explain.html?ao_ver=' . AUTOPTIMIZE_PLUGIN_VERSION ); |
|
26 | - if ( ! is_wp_error( $ccss_expl_resp ) ) { |
|
27 | - if ( '200' == wp_remote_retrieve_response_code( $ccss_expl_resp ) ) { |
|
28 | - $ccss_explanation = wp_kses_post( wp_remote_retrieve_body( $ccss_expl_resp ) ); |
|
29 | - set_transient( 'ao_ccss_explain', $ccss_explanation, WEEK_IN_SECONDS ); |
|
22 | + if (apply_filters('autoptimize_settingsscreen_remotehttp', true)) { |
|
23 | + $ccss_explanation = get_transient('ao_ccss_explain'); |
|
24 | + if (empty($ccss_explanation)) { |
|
25 | + $ccss_expl_resp = wp_remote_get('https://misc.optimizingmatters.com/autoptimize_ccss_explain.html?ao_ver='.AUTOPTIMIZE_PLUGIN_VERSION); |
|
26 | + if (!is_wp_error($ccss_expl_resp)) { |
|
27 | + if ('200' == wp_remote_retrieve_response_code($ccss_expl_resp)) { |
|
28 | + $ccss_explanation = wp_kses_post(wp_remote_retrieve_body($ccss_expl_resp)); |
|
29 | + set_transient('ao_ccss_explain', $ccss_explanation, WEEK_IN_SECONDS); |
|
30 | 30 | } |
31 | 31 | } |
32 | 32 | } |
33 | 33 | } |
34 | 34 | |
35 | 35 | // placeholder text in case HTML is empty. |
36 | - if ( empty( $ccss_explanation ) ) { |
|
36 | + if (empty($ccss_explanation)) { |
|
37 | 37 | $ccss_explanation = '<h2>Fix render-blocking CSS!</h2><p>Significantly improve your first-paint times by making CSS non-render-blocking.</p><p>The next step is to sign up at <a href="https://criticalcss.com/?aff=1" target="_blank">https://criticalcss.com</a> (this is a premium service, priced 2 GBP/month for membership and 5 GBP/month per domain) <strong>and get the API key, which you can copy from <a href="https://criticalcss.com/account/api-keys?aff=1" target="_blank">the API-keys page</a></strong> and paste below.</p><p>If you have any questions or need support, head on over to <a href="https://wordpress.org/support/plugin/autoptimize" target="_blank">our support forum</a> and we\'ll help you get up and running in no time!</p>'; |
38 | 38 | } |
39 | 39 |