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