PluginProbe
Autoptimize / 3.1.4
Autoptimize v3.1.4
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
← All changes | classes/autoptimizeCriticalCSSCore.php +238 -228 2.7.63.1.4 View file →
@@ -8,90 +8,98 @@
8 8 exit;
9 9 }
10 10
11 11 class autoptimizeCriticalCSSCore {
12 - public function __construct()
13 - {
14 - // fetch all options at once and populate them individually explicitely as globals.
15 - $all_options = autoptimizeCriticalCSSBase::fetch_options();
16 - foreach ( $all_options as $_option => $_value ) {
17 - global ${$_option};
18 - ${$_option} = $_value;
19 - }
12 + /**
13 + * Critical CSS page types.
14 + *
15 + * @var array
16 + */
17 + protected $_types = null;
20 18
19 + public function __construct() {
20 + $this->criticalcss = autoptimize()->criticalcss();
21 21 $this->run();
22 22 }
23 23
24 24 public function run() {
25 - global $ao_css_defer;
26 - global $ao_ccss_deferjquery;
27 - global $ao_ccss_key;
28 - global $ao_ccss_unloadccss;
25 + $css_defer = $this->criticalcss->get_option( 'css_defer' );
26 + $deferjquery = $this->criticalcss->get_option( 'deferjquery' );
27 + $unloadccss = $this->criticalcss->get_option( 'unloadccss' );
29 28
30 - // add all filters to do CCSS if key present.
31 - if ( $ao_css_defer && isset( $ao_ccss_key ) && ! empty( $ao_ccss_key ) ) {
32 - // Set AO behavior: disable minification to avoid double minifying and caching.
33 - add_filter( 'autoptimize_filter_css_critcss_minify', '__return_false' );
34 - add_filter( 'autoptimize_filter_css_defer_inline', array( $this, 'ao_ccss_frontend' ), 10, 1 );
29 + if ( ! $css_defer ) {
30 + return;
31 + }
35 32
36 - // Add the action to enqueue jobs for CriticalCSS cron.
37 - add_action( 'autoptimize_action_css_hash', array( 'autoptimizeCriticalCSSEnqueue', 'ao_ccss_enqueue' ), 10, 1 );
33 + // add all filters to do CCSS
34 + // Set AO behavior: disable minification to avoid double minifying and caching.
35 + add_filter( 'autoptimize_filter_css_critcss_minify', '__return_false' );
36 + add_filter( 'autoptimize_filter_css_defer_inline', array( $this, 'ao_ccss_frontend' ), 10, 1 );
38 37
39 - // conditionally add the filter to defer jquery and others.
40 - if ( $ao_ccss_deferjquery ) {
41 - add_filter( 'autoptimize_html_after_minify', array( $this, 'ao_ccss_defer_jquery' ), 11, 1 );
42 - }
38 + // Add the action to enqueue jobs for CriticalCSS cron.
39 + if ( $this->criticalcss->is_api_active() ) {
40 + add_action( 'autoptimize_action_css_hash', array( $this->criticalcss, 'enqueue' ), 10, 1 );
41 + }
43 42
44 - // conditionally add filter to unload the CCSS.
45 - if ( $ao_ccss_unloadccss ) {
46 - add_filter( 'autoptimize_html_after_minify', array( $this, 'ao_ccss_unloadccss' ), 12, 1 );
47 - }
43 + // conditionally add the filter to defer jquery and others but only if not done so in autoptimizeScripts.
44 + $_native_defer = false;
45 + if ( 'on' === autoptimizeOptionWrapper::get_option( 'autoptimize_js_defer_not_aggregate' ) && 'on' === autoptimizeOptionWrapper::get_option( 'autoptimize_js_defer_inline' ) ) {
46 + $_native_defer = true;
47 + }
48 + if ( $deferjquery && ! $_native_defer ) {
49 + add_filter( 'autoptimize_html_after_minify', array( $this, 'ao_ccss_defer_jquery' ), 11, 1 );
50 + }
48 51
49 - // Order paths by length, as longest ones have greater priority in the rules.
50 - if ( ! empty( $ao_ccss_rules['paths'] ) ) {
51 - $keys = array_map( 'strlen', array_keys( $ao_ccss_rules['paths'] ) );
52 - array_multisort( $keys, SORT_DESC, $ao_ccss_rules['paths'] );
53 - }
52 + // conditionally add filter to unload the CCSS.
53 + if ( $unloadccss ) {
54 + add_filter( 'autoptimize_html_after_minify', array( $this, 'ao_ccss_unloadccss' ), 12, 1 );
55 + }
54 56
55 - // Add an array with default WordPress's conditional tags
56 - // NOTE: these tags are sorted.
57 - global $ao_ccss_types;
58 - $ao_ccss_types = $this->get_ao_ccss_core_types();
57 + // Order paths by length, as longest ones have greater priority in the rules.
58 + $rules = $this->criticalcss->get_option( 'rules' );
59 + if ( ! empty( $rules['paths'] ) ) {
60 + $keys = array_map( 'strlen', array_keys( $rules['paths'] ) );
61 + array_multisort( $keys, SORT_DESC, $rules['paths'] );
62 + // TODO: Not sure what we're doing here. Sorted the $keys,
63 + // but they don't seem to be used anywhere.
64 + }
59 65
60 - // Extend conditional tags on plugin initalization.
61 - add_action( apply_filters( 'autoptimize_filter_ccss_extend_types_hook', 'init' ), array( $this, 'ao_ccss_extend_types' ) );
66 + // Add an array with default WordPress's conditional tags
67 + // NOTE: these tags are sorted.
68 + $this->_types = $this->get_ao_ccss_core_types();
62 69
63 - // When autoptimize cache is cleared, also clear transient cache for page templates.
64 - add_action( 'autoptimize_action_cachepurged', array( 'autoptimizeCriticalCSSCore', 'ao_ccss_clear_page_tpl_cache' ), 10, 0 );
65 - }
70 + // Extend conditional tags on plugin initalization.
71 + add_action( apply_filters( 'autoptimize_filter_ccss_extend_types_hook', 'init' ), array( $this, 'ao_ccss_extend_types' ) );
72 +
73 + // When autoptimize cache is cleared, also clear transient cache for page templates.
74 + add_action( 'autoptimize_action_cachepurged', array( $this, 'ao_ccss_clear_page_tpl_cache' ), 10, 0 );
66 75 }
67 76
68 77 public function ao_ccss_frontend( $inlined ) {
69 78 // Apply CriticalCSS to frontend pages
70 79 // Attach types and settings arrays.
71 - global $ao_ccss_types;
72 - global $ao_ccss_rules;
73 - global $ao_ccss_additional;
74 - global $ao_ccss_loggedin;
75 - global $ao_ccss_debug;
76 - global $ao_ccss_keyst;
77 - $no_ccss = '';
80 + $rules = $this->criticalcss->get_option( 'rules' );
81 + $additional = $this->criticalcss->get_option( 'additional' );
82 + $loggedin = $this->criticalcss->get_option( 'loggedin' );
83 + $debug = $this->criticalcss->get_option( 'debug' );
84 + $no_ccss = '';
85 + $additional = autoptimizeStyles::sanitize_css( $additional );
78 86
79 87 // Only if keystatus is OK and option to add CCSS for logged on users is on or user is not logged in.
80 - if ( ( $ao_ccss_keyst && 2 == $ao_ccss_keyst ) && ( $ao_ccss_loggedin || ! is_user_logged_in() ) ) {
88 + if ( $loggedin || ! is_user_logged_in() ) {
81 89 // Check for a valid CriticalCSS based on path to return its contents.
82 90 $req_path = strtok( $_SERVER['REQUEST_URI'], '?' );
83 - if ( ! empty( $ao_ccss_rules['paths'] ) ) {
84 - foreach ( $ao_ccss_rules['paths'] as $path => $rule ) {
91 + if ( ! empty( $rules['paths'] ) ) {
92 + foreach ( $rules['paths'] as $path => $rule ) {
85 93 // explicit match OR partial match if MANUAL rule.
86 - if ( $req_path == $path || urldecode( $req_path ) == $path || ( false == $rule['hash'] && false != $rule['file'] && strpos( $req_path, str_replace( site_url(), '', $path ) ) !== false ) ) {
94 + if ( ( $this->criticalcss->is_api_active() || $this->criticalcss->is_rule_manual( $rule ) ) && ( $req_path == $path || urldecode( $req_path ) == $path || ( apply_filters( 'autoptimize_filter_ccss_core_path_partial_match', true ) && false == $rule['hash'] && false != $rule['file'] && strpos( $req_path, str_replace( site_url(), '', $path ) ) !== false ) ) ) {
87 95 if ( file_exists( AO_CCSS_DIR . $rule['file'] ) ) {
88 96 $_ccss_contents = file_get_contents( AO_CCSS_DIR . $rule['file'] );
89 97 if ( 'none' != $_ccss_contents ) {
90 - if ( $ao_ccss_debug ) {
98 + if ( $debug ) {
91 99 $_ccss_contents = '/* PATH: ' . $path . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents;
92 100 }
93 - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional );
101 + return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $additional );
94 102 } else {
95 103 $no_ccss = 'none';
96 104 }
97 105 }
@@ -99,57 +107,57 @@
99 107 }
100 108 }
101 109
102 110 // Check for a valid CriticalCSS based on conditional tags to return its contents.
103 - if ( ! empty( $ao_ccss_rules['types'] ) && 'none' !== $no_ccss ) {
111 + if ( ! empty( $rules['types'] ) && 'none' !== $no_ccss ) {
104 112 // 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.
105 - $ao_ccss_rules['types'] = array_replace( array_intersect_key( array_flip( $ao_ccss_types ), $ao_ccss_rules['types'] ), $ao_ccss_rules['types'] );
106 - $is_front_page = is_front_page();
113 + $rules['types'] = array_replace( array_intersect_key( array_flip( $this->_types ), $rules['types'] ), $rules['types'] );
114 + $is_front_page = is_front_page();
107 115
108 - foreach ( $ao_ccss_rules['types'] as $type => $rule ) {
109 - if ( in_array( $type, $ao_ccss_types ) && file_exists( AO_CCSS_DIR . $rule['file'] ) ) {
116 + foreach ( $rules['types'] as $type => $rule ) {
117 + if ( ( $this->criticalcss->is_api_active() || $this->criticalcss->is_rule_manual( $rule ) ) && in_array( $type, $this->_types ) && file_exists( AO_CCSS_DIR . $rule['file'] ) ) {
110 118 $_ccss_contents = file_get_contents( AO_CCSS_DIR . $rule['file'] );
111 119 if ( $is_front_page && 'is_front_page' == $type ) {
112 120 if ( 'none' != $_ccss_contents ) {
113 - if ( $ao_ccss_debug ) {
121 + if ( $debug ) {
114 122 $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents;
115 123 }
116 - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional );
124 + return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $additional );
117 125 } else {
118 126 $no_ccss = 'none';
119 127 }
120 - } elseif ( strpos( $type, 'custom_post_' ) === 0 && ! $is_front_page ) {
128 + } elseif ( ( $this->criticalcss->is_api_active() || $this->criticalcss->is_rule_manual( $rule ) ) && strpos( $type, 'custom_post_' ) === 0 && ! $is_front_page ) {
121 129 if ( get_post_type( get_the_ID() ) === substr( $type, 12 ) ) {
122 130 if ( 'none' != $_ccss_contents ) {
123 - if ( $ao_ccss_debug ) {
131 + if ( $debug ) {
124 132 $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents;
125 133 }
126 - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional );
134 + return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $additional );
127 135 } else {
128 136 $no_ccss = 'none';
129 137 }
130 138 }
131 - } elseif ( 0 === strpos( $type, 'template_' ) && ! $is_front_page ) {
139 + } elseif ( ( $this->criticalcss->is_api_active() || $this->criticalcss->is_rule_manual( $rule ) ) && 0 === strpos( $type, 'template_' ) && ! $is_front_page ) {
132 140 if ( is_page_template( substr( $type, 9 ) ) ) {
133 141 if ( 'none' != $_ccss_contents ) {
134 - if ( $ao_ccss_debug ) {
142 + if ( $debug ) {
135 143 $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents;
136 144 }
137 - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional );
145 + return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $additional );
138 146 } else {
139 147 $no_ccss = 'none';
140 148 }
141 149 }
142 - } elseif ( ! $is_front_page ) {
150 + } elseif ( ( $this->criticalcss->is_api_active() || $this->criticalcss->is_rule_manual( $rule ) ) && ! $is_front_page ) {
143 151 // all "normal" conditional tags, core + woo + buddypress + edd + bbpress
144 152 // but we have to remove the prefix for the non-core ones for them to function.
145 153 $type = str_replace( array( 'woo_', 'bp_', 'bbp_', 'edd_' ), '', $type );
146 154 if ( function_exists( $type ) && call_user_func( $type ) ) {
147 155 if ( 'none' != $_ccss_contents ) {
148 - if ( $ao_ccss_debug ) {
156 + if ( $debug ) {
149 157 $_ccss_contents = '/* TYPES: ' . $type . ' hash: ' . $rule['hash'] . ' file: ' . $rule['file'] . ' */ ' . $_ccss_contents;
150 158 }
151 - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $ao_ccss_additional );
159 + return apply_filters( 'autoptimize_filter_ccss_core_ccss', $_ccss_contents . $additional );
152 160 } else {
153 161 $no_ccss = 'none';
154 162 }
155 163 }
@@ -161,9 +169,9 @@
161 169
162 170 // Finally, inline the default CriticalCSS if any or else the entire CSS for the page
163 171 // This also applies to logged in users if the option to add CCSS for logged in users has been disabled.
164 172 if ( ! empty( $inlined ) && 'none' !== $no_ccss ) {
165 - return apply_filters( 'autoptimize_filter_ccss_core_ccss', $inlined . $ao_ccss_additional );
173 + return apply_filters( 'autoptimize_filter_ccss_core_ccss', $inlined . $additional );
166 174 } else {
167 175 add_filter( 'autoptimize_filter_css_inline', '__return_true' );
168 176 return;
169 177 }
@@ -169,11 +177,12 @@
169 177 }
170 178 }
171 179
172 180 public function ao_ccss_defer_jquery( $in ) {
173 - global $ao_ccss_loggedin;
181 + $loggedin = $this->criticalcss->get_option( 'loggedin' );
182 +
174 183 // defer all linked and inline JS.
175 - if ( ( ! is_user_logged_in() || $ao_ccss_loggedin ) && preg_match_all( '#<script.*>(.*)</script>#Usmi', $in, $matches, PREG_SET_ORDER ) ) {
184 + if ( ( ! is_user_logged_in() || $loggedin ) && preg_match_all( '#<script.*>(.*)</script>#Usmi', $in, $matches, PREG_SET_ORDER ) ) {
176 185 foreach ( $matches as $match ) {
177 186 if ( str_replace( apply_filters( 'autoptimize_filter_ccss_core_defer_exclude', array( 'data-noptimize="1"', 'data-cfasync="false"', 'data-pagespeed-no-defer' ) ), '', $match[0] ) !== $match[0] ) {
178 187 // do not touch JS with noptimize/ cfasync/ pagespeed-no-defer flags.
179 188 continue;
@@ -192,22 +201,34 @@
192 201 }
193 202
194 203 public function ao_ccss_unloadccss( $html_in ) {
195 204 // set media attrib of inline CCSS to none at onLoad to avoid it impacting full CSS (rarely needed).
196 - $_unloadccss_js = apply_filters( 'autoptimize_filter_ccss_core_unloadccss_js', '<script>window.addEventListener("load", function(event) {document.getElementById("aoatfcss").media="none";})</script>' );
205 + $_unloadccss_js = apply_filters( 'autoptimize_filter_ccss_core_unloadccss_js', '<script>window.addEventListener("load", function(event) {var el = document.getElementById("aoatfcss"); if(el) el.media = "none";})</script>' );
197 206
207 + if ( false !== strpos( $html_in, $_unloadccss_js . '</body>' ) ) {
208 + return $html_in;
209 + }
210 +
198 211 return str_replace( '</body>', $_unloadccss_js . '</body>', $html_in );
199 212 }
200 213
214 + /**
215 + * Get the types array.
216 + *
217 + * @return array|null
218 + */
219 + public function get_types() {
220 + return $this->_types;
221 + }
222 +
201 223 public function ao_ccss_extend_types() {
202 224 // Extend contidional tags
203 225 // Attach the conditional tags array.
204 - global $ao_ccss_types;
205 226
206 227 // in some cases $ao_ccss_types is empty and/or not an array, this should work around that problem.
207 - if ( empty( $ao_ccss_types ) || ! is_array( $ao_ccss_types ) ) {
208 - $ao_ccss_types = get_ao_ccss_core_types();
209 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Empty types array in extend, refetching array with core conditionals.', 3 );
228 + if ( empty( $this->_types ) || ! is_array( $this->_types ) ) {
229 + $this->_types = $this->get_ao_ccss_core_types();
230 + $this->ao_ccss_log( 'Empty types array in extend, refetching array with core conditionals.', 3 );
210 231 }
211 232
212 233 // Custom Post Types.
213 234 $cpts = get_post_types(
@@ -218,9 +239,9 @@
218 239 'names',
219 240 'and'
220 241 );
221 242 foreach ( $cpts as $cpt ) {
222 - array_unshift( $ao_ccss_types, 'custom_post_' . $cpt );
243 + array_unshift( $this->_types, 'custom_post_' . $cpt );
223 244 }
224 245
225 246 // Templates.
226 247 // Transient cache to avoid frequent disk reads.
@@ -229,14 +250,14 @@
229 250 $templates = wp_get_theme()->get_page_templates();
230 251 set_transient( 'autoptimize_ccss_page_templates', $templates, HOUR_IN_SECONDS );
231 252 }
232 253 foreach ( $templates as $tplfile => $tplname ) {
233 - array_unshift( $ao_ccss_types, 'template_' . $tplfile );
254 + array_unshift( $this->_types, 'template_' . $tplfile );
234 255 }
235 256
236 257 // bbPress tags.
237 258 if ( function_exists( 'is_bbpress' ) ) {
238 - $ao_ccss_types = array_merge(
259 + $this->_types = array_merge(
239 260 array(
240 261 'bbp_is_bbpress',
241 262 'bbp_is_favorites',
242 263 'bbp_is_forum_archive',
@@ -260,15 +281,16 @@
260 281 'bbp_is_topic_tag_edit',
261 282 'bbp_is_topics_created',
262 283 'bbp_is_user_home',
263 284 'bbp_is_user_home_edit',
264 - ), $ao_ccss_types
285 + ),
286 + $this->_types
265 287 );
266 288 }
267 289
268 290 // BuddyPress tags.
269 291 if ( function_exists( 'is_buddypress' ) ) {
270 - $ao_ccss_types = array_merge(
292 + $this->_types = array_merge(
271 293 array(
272 294 'bp_is_activation_page',
273 295 'bp_is_activity',
274 296 'bp_is_blogs',
@@ -302,27 +324,29 @@
302 324 'bp_is_settings_component',
303 325 'bp_is_user',
304 326 'bp_is_user_profile',
305 327 'bp_is_wire',
306 - ), $ao_ccss_types
328 + ),
329 + $this->_types
307 330 );
308 331 }
309 332
310 333 // Easy Digital Downloads (EDD) tags.
311 334 if ( function_exists( 'edd_is_checkout' ) ) {
312 - $ao_ccss_types = array_merge(
335 + $this->_types = array_merge(
313 336 array(
314 337 'edd_is_checkout',
315 338 'edd_is_failed_transaction_page',
316 339 'edd_is_purchase_history_page',
317 340 'edd_is_success_page',
318 - ), $ao_ccss_types
341 + ),
342 + $this->_types
319 343 );
320 344 }
321 345
322 346 // WooCommerce tags.
323 347 if ( class_exists( 'WooCommerce' ) ) {
324 - $ao_ccss_types = array_merge(
348 + $this->_types = array_merge(
325 349 array(
326 350 'woo_is_account_page',
327 351 'woo_is_cart',
328 352 'woo_is_checkout',
@@ -331,44 +355,36 @@
331 355 'woo_is_product_tag',
332 356 'woo_is_shop',
333 357 'woo_is_wc_endpoint_url',
334 358 'woo_is_woocommerce',
335 - ), $ao_ccss_types
359 + ),
360 + $this->_types
336 361 );
337 362 }
338 363 }
339 364
340 365 public function get_ao_ccss_core_types() {
341 - global $ao_ccss_types;
342 - if ( empty( $ao_ccss_types ) || ! is_array( $ao_ccss_types ) ) {
343 - return array(
344 - 'is_404',
345 - 'is_archive',
346 - 'is_author',
347 - 'is_category',
348 - 'is_front_page',
349 - 'is_home',
350 - 'is_page',
351 - 'is_post',
352 - 'is_search',
353 - 'is_attachment',
354 - 'is_single',
355 - 'is_sticky',
356 - 'is_paged',
357 - );
358 - } else {
359 - return $ao_ccss_types;
360 - }
366 + return array(
367 + 'is_404',
368 + 'is_front_page',
369 + 'is_home',
370 + 'is_page',
371 + 'is_single',
372 + 'is_category',
373 + 'is_author',
374 + 'is_archive',
375 + 'is_search',
376 + 'is_attachment',
377 + 'is_sticky',
378 + 'is_paged',
379 + );
361 380 }
362 381
363 - public static function ao_ccss_key_status( $render ) {
382 + public function ao_ccss_key_status( $render ) {
364 383 // Provide key status
365 384 // Get key and key status.
366 - global $ao_ccss_key;
367 - global $ao_ccss_keyst;
368 - $self = new self();
369 - $key = $ao_ccss_key;
370 - $key_status = $ao_ccss_keyst;
385 + $key = $this->criticalcss->get_option( 'key' );
386 + $key_status = $this->criticalcss->get_option( 'keyst' );
371 387
372 388 // Prepare returned variables.
373 389 $key_return = array();
374 390 $status = false;
@@ -389,9 +405,9 @@
389 405 $message = __( 'Your API key is invalid. Please enter a valid <a href="https://criticalcss.com/?aff=1" target="_blank">criticalcss.com</a> key.', 'autoptimize' );
390 406 } elseif ( $key && ! $key_status ) {
391 407 // Key exists but it has no valid status yet
392 408 // Perform key validation.
393 - $key_check = $self->ao_ccss_key_validation( $key );
409 + $key_check = $this->ao_ccss_key_validation( $key );
394 410
395 411 // Key is valid, set valid status.
396 412 if ( $key_check ) {
397 413 $status = 'valid';
@@ -431,9 +447,9 @@
431 447 return $key_return;
432 448 }
433 449
434 450 public function ao_ccss_key_validation( $key ) {
435 - global $ao_ccss_noptimize;
451 + $noptimize = $this->criticalcss->get_option( 'noptimize' );
436 452
437 453 // POST a dummy job to criticalcss.com to check for key validation
438 454 // Prepare home URL for the request.
439 455 $src_url = get_home_url();
@@ -438,127 +454,123 @@
438 454 // Prepare home URL for the request.
439 455 $src_url = get_home_url();
440 456
441 457 // Avoid AO optimizations if required by config or avoid lazyload if lazyload is active in AO.
442 - if ( ! empty( $ao_ccss_noptimize ) ) {
443 - $src_url .= '?ao_noptirocket=1';
458 + if ( ! empty( $noptimize ) ) {
459 + $src_url .= '/?ao_noptirocket=1';
444 460 } elseif ( class_exists( 'autoptimizeImages', false ) && autoptimizeImages::should_lazyload_wrapper() ) {
445 - $src_url .= '?ao_nolazy=1';
461 + $src_url .= '/?ao_nolazy=1';
446 462 }
447 463
448 464 $src_url = apply_filters( 'autoptimize_filter_ccss_cron_srcurl', $src_url );
449 465
450 - // Prepare the request.
451 - $url = esc_url_raw( AO_CCSS_API . 'generate' );
452 - $args = array(
453 - 'headers' => array(
454 - 'User-Agent' => 'Autoptimize v' . AO_CCSS_VER,
455 - 'Content-type' => 'application/json; charset=utf-8',
456 - 'Authorization' => 'JWT ' . $key,
457 - 'Connection' => 'close',
458 - ),
459 - // Body must be JSON.
460 - 'body' => json_encode(
461 - apply_filters( 'autoptimize_ccss_cron_api_generate_body',
466 + if ( true !== autoptimizeUtils::is_local_server( parse_url( $src_url, PHP_URL_HOST ) ) ) {
467 + // Prepare the request.
468 + $url = esc_url_raw( AO_CCSS_API . 'generate' );
469 + $args = array(
470 + 'headers' => apply_filters(
471 + 'autoptimize_ccss_cron_api_generate_headers',
462 472 array(
463 - 'url' => $src_url,
464 - 'aff' => 1,
465 - 'aocssv' => AO_CCSS_VER,
473 + 'User-Agent' => 'Autoptimize v' . AO_CCSS_VER,
474 + 'Content-type' => 'application/json; charset=utf-8',
475 + 'Authorization' => 'JWT ' . $key,
476 + 'Connection' => 'close',
466 477 )
467 - )
468 - ),
469 - );
478 + ),
479 + // Body must be JSON.
480 + 'body' => json_encode(
481 + apply_filters(
482 + 'autoptimize_ccss_cron_api_generate_body',
483 + array(
484 + 'url' => $src_url,
485 + 'aff' => 1,
486 + 'aocssv' => AO_CCSS_VER,
487 + )
488 + ),
489 + JSON_UNESCAPED_SLASHES
490 + ),
491 + );
470 492
471 - // Dispatch the request and store its response code.
472 - $req = wp_safe_remote_post( $url, $args );
473 - $code = wp_remote_retrieve_response_code( $req );
474 - $body = json_decode( wp_remote_retrieve_body( $req ), true );
493 + // Dispatch the request and store its response code.
494 + $req = wp_safe_remote_post( $url, $args );
495 + $code = wp_remote_retrieve_response_code( $req );
496 + $body = json_decode( wp_remote_retrieve_body( $req ), true );
475 497
476 - if ( 200 == $code ) {
477 - // Response is OK.
478 - // Set key status as valid and log key check.
479 - update_option( 'autoptimize_ccss_keyst', 2 );
480 - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is valid, updating key status', 3 );
498 + if ( 200 == $code ) {
499 + // Response is OK.
500 + // Set key status as valid and log key check.
501 + update_option( 'autoptimize_ccss_keyst', 2 );
502 + $this->ao_ccss_log( 'criticalcss.com: API key is valid, updating key status', 3 );
481 503
482 - // extract job-id from $body and put it in the queue as a P job
483 - // but only if no jobs and no rules!
484 - global $ao_ccss_queue;
485 - global $ao_ccss_rules;
504 + // extract job-id from $body and put it in the queue as a P job
505 + // but only if no jobs and no rules!
506 + $queue = $this->criticalcss->get_option( 'queue' );
507 + $rules = $this->criticalcss->get_option( 'rules' );
486 508
487 - if ( 0 == count( $ao_ccss_queue ) && 0 == count( $ao_ccss_rules['types'] ) && 0 == count( $ao_ccss_rules['paths'] ) ) {
488 - if ( 'JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] ) {
489 - $jprops['ljid'] = 'firstrun';
490 - $jprops['rtarget'] = 'types|is_front_page';
491 - $jprops['ptype'] = 'is_front_page';
492 - $jprops['hashes'][] = 'dummyhash';
493 - $jprops['hash'] = 'dummyhash';
494 - $jprops['file'] = null;
495 - $jprops['jid'] = $body['job']['id'];
496 - $jprops['jqstat'] = $body['job']['status'];
497 - $jprops['jrstat'] = null;
498 - $jprops['jvstat'] = null;
499 - $jprops['jctime'] = microtime( true );
500 - $jprops['jftime'] = null;
501 - $ao_ccss_queue['/'] = $jprops;
502 - $ao_ccss_queue_raw = json_encode( $ao_ccss_queue );
503 - update_option( 'autoptimize_ccss_queue', $ao_ccss_queue_raw, false );
504 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Created P job for is_front_page based on API key check response.', 3 );
509 + if ( 0 == count( $queue ) && 0 == count( $rules['types'] ) && 0 == count( $rules['paths'] ) ) {
510 + if ( 'JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] ) {
511 + $jprops['ljid'] = 'firstrun';
512 + $jprops['rtarget'] = 'types|is_front_page';
513 + $jprops['ptype'] = 'is_front_page';
514 + $jprops['hashes'][] = 'dummyhash';
515 + $jprops['hash'] = 'dummyhash';
516 + $jprops['file'] = null;
517 + $jprops['jid'] = $body['job']['id'];
518 + $jprops['jqstat'] = $body['job']['status'];
519 + $jprops['jrstat'] = null;
520 + $jprops['jvstat'] = null;
521 + $jprops['jctime'] = microtime( true );
522 + $jprops['jftime'] = null;
523 + $queue['/'] = $jprops;
524 + $queue_raw = json_encode( $queue );
525 + update_option( 'autoptimize_ccss_queue', $queue_raw, false );
526 + $this->ao_ccss_log( 'Created P job for is_front_page based on API key check response.', 3 );
527 + }
505 528 }
529 + return true;
530 + } elseif ( 401 == $code ) {
531 + // Response is unauthorized
532 + // Set key status as invalid and log key check.
533 + update_option( 'autoptimize_ccss_keyst', 1 );
534 + $this->ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 );
535 + return false;
536 + } else {
537 + // Response unkown
538 + // Log key check attempt.
539 + $this->ao_ccss_log( 'criticalcss.com: could not check API key status, this is a service error, body follows if any...', 2 );
540 + if ( ! empty( $body ) ) {
541 + $this->ao_ccss_log( print_r( $body, true ), 2 );
542 + }
543 + if ( is_wp_error( $req ) ) {
544 + $this->ao_ccss_log( $req->get_error_message(), 2 );
545 + }
546 + return false;
506 547 }
507 - return true;
508 - } elseif ( 401 == $code ) {
509 - // Response is unauthorized
510 - // Set key status as invalid and log key check.
511 - update_option( 'autoptimize_ccss_keyst', 1 );
512 - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: API key is invalid, updating key status', 3 );
513 - return false;
514 548 } else {
515 - // Response unkown
516 - // Log key check attempt.
517 - autoptimizeCriticalCSSCore::ao_ccss_log( 'criticalcss.com: could not check API key status, this is a service error, body follows if any...', 2 );
518 - if ( ! empty( $body ) ) {
519 - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 );
520 - }
521 - if ( is_wp_error( $req ) ) {
522 - autoptimizeCriticalCSSCore::ao_ccss_log( $req->get_error_message(), 2 );
523 - }
549 + // localhost/ private network server, no API check possible.
524 550 return false;
525 551 }
526 552 }
527 553
528 - public static function ao_ccss_viewport() {
554 + public function ao_ccss_viewport() {
529 555 // Get viewport size
530 556 // Attach viewport option.
531 - global $ao_ccss_viewport;
557 + $viewport = $this->criticalcss->get_option( 'viewport' );
532 558
533 - // Prepare viewport array.
534 - $viewport = array();
535 -
536 - // Viewport Width.
537 - if ( ! empty( $ao_ccss_viewport['w'] ) ) {
538 - $viewport['w'] = $ao_ccss_viewport['w'];
539 - } else {
540 - $viewport['w'] = '';
541 - }
542 -
543 - // Viewport Height.
544 - if ( ! empty( $ao_ccss_viewport['h'] ) ) {
545 - $viewport['h'] = $ao_ccss_viewport['h'];
546 - } else {
547 - $viewport['h'] = '';
548 - }
549 -
550 - return $viewport;
559 + return array(
560 + 'w' => ! empty( $viewport['w'] ) ? $viewport['w'] : '',
561 + 'h' => ! empty( $viewport['h'] ) ? $viewport['h'] : '',
562 + );
551 563 }
552 564
553 - public static function ao_ccss_check_contents( $ccss ) {
565 + public function ao_ccss_check_contents( $ccss ) {
554 566 // Perform basic exploit avoidance and CSS validation.
555 567 if ( ! empty( $ccss ) ) {
556 568 // Try to avoid code injection.
557 - $blocklist = array( '#!/', 'function(', '<script', '<?php' );
569 + $blocklist = array( '#!/', 'function(', '<script', '<?php', '</style', ' onload=', ' onerror=', ' onmouse', ' onscroll=', ' onclick=' );
558 570 foreach ( $blocklist as $blocklisted ) {
559 - if ( strpos( $ccss, $blocklisted ) !== false ) {
560 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received contained blocklisted content.', 2 );
571 + if ( stripos( $ccss, $blocklisted ) !== false ) {
572 + $this->ao_ccss_log( 'Critical CSS received contained blocklisted content.', 2 );
561 573 return false;
562 574 }
563 575 }
564 576
@@ -565,9 +577,9 @@
565 577 // Check for most basics CSS structures.
566 578 $needlist = array( '{', '}', ':' );
567 579 foreach ( $needlist as $needed ) {
568 580 if ( false === strpos( $ccss, $needed ) && 'none' !== $ccss ) {
569 - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received did not seem to contain real CSS.', 2 );
581 + $this->ao_ccss_log( 'Critical CSS received did not seem to contain real CSS.', 2 );
570 582 return false;
571 583 }
572 584 }
573 585 }
@@ -575,12 +587,12 @@
575 587 // Return true if file critical CSS is sane.
576 588 return true;
577 589 }
578 590
579 - public static function ao_ccss_log( $msg, $lvl ) {
591 + public function ao_ccss_log( $msg, $lvl ) {
580 592 // Commom logging facility
581 593 // Attach debug option.
582 - global $ao_ccss_debug;
594 + $debug = $this->criticalcss->get_option( 'debug' );
583 595
584 596 // Prepare log levels, where accepted $lvl are:
585 597 // 1: II (for info)
586 598 // 2: EE (for error)
@@ -586,23 +598,22 @@
586 598 // 2: EE (for error)
587 599 // 3: DD (for debug)
588 600 // Default: UU (for unkown).
589 601 $level = false;
590 - switch ( $lvl ) {
591 - case 1:
592 - $level = 'II';
593 - break;
594 - case 2:
595 - $level = 'EE';
596 - break;
597 - case 3:
598 - // Output debug messages only if debug mode is enabled.
599 - if ( $ao_ccss_debug ) {
602 + if ( $debug ) {
603 + switch ( $lvl ) {
604 + case 1:
605 + $level = 'II';
606 + break;
607 + case 2:
608 + $level = 'EE';
609 + break;
610 + case 3:
600 611 $level = 'DD';
601 - }
602 - break;
603 - default:
604 - $level = 'UU';
612 + break;
613 + default:
614 + $level = 'UU';
615 + }
605 616 }
606 617
607 618 // Prepare and write a log message if there's a valid level.
608 619 if ( $level ) {
@@ -607,9 +618,9 @@
607 618 // Prepare and write a log message if there's a valid level.
608 619 if ( $level ) {
609 620
610 621 // Prepare message.
611 - $message = date( 'c' ) . ' - [' . $level . '] ' . htmlentities( $msg ) . '<br>';
622 + $message = date( 'c' ) . ' - [' . $level . '] ' . htmlentities( $msg ) . '<br>'; // @codingStandardsIgnoreLine
612 623
613 624 // Write message to log file.
614 625 error_log( $message, 3, AO_CCSS_LOG );
615 626 }
@@ -614,10 +625,9 @@
614 625 error_log( $message, 3, AO_CCSS_LOG );
615 626 }
616 627 }
617 628
618 - public static function ao_ccss_clear_page_tpl_cache() {
629 + public function ao_ccss_clear_page_tpl_cache() {
619 630 // Clears transient cache for page templates.
620 631 delete_transient( 'autoptimize_ccss_page_templates' );
621 632 }
622 -
623 633 }