| @@ -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,22 +196,34 @@ | ||
| 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 | |
| 202 | + if ( false !== strpos( $html_in, $_unloadccss_js . '</body>' ) ) { | |
| 203 | + return $html_in; | |
| 204 | + } | |
| 205 | + | |
| 198 | 206 | return str_replace( '</body>', $_unloadccss_js . '</body>', $html_in ); |
| 199 | 207 | } |
| 200 | 208 | |
| 209 | + /** | |
| 210 | + * Get the types array. | |
| 211 | + * | |
| 212 | + * @return array|null | |
| 213 | + */ | |
| 214 | + public function get_types() { | |
| 215 | + return $this->_types; | |
| 216 | + } | |
| 217 | + | |
| 201 | 218 | public function ao_ccss_extend_types() { |
| 202 | 219 | // Extend contidional tags |
| 203 | 220 | // Attach the conditional tags array. |
| 204 | - global $ao_ccss_types; | |
| 205 | 221 | |
| 206 | 222 | // 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 ); | |
| 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 ); | |
| 210 | 226 | } |
| 211 | 227 | |
| 212 | 228 | // Custom Post Types. |
| 213 | 229 | $cpts = get_post_types( |
| @@ -218,9 +234,9 @@ | ||
| 218 | 234 | 'names', |
| 219 | 235 | 'and' |
| 220 | 236 | ); |
| 221 | 237 | foreach ( $cpts as $cpt ) { |
| 222 | - array_unshift( $ao_ccss_types, 'custom_post_' . $cpt ); | |
| 238 | + array_unshift( $this->_types, 'custom_post_' . $cpt ); | |
| 223 | 239 | } |
| 224 | 240 | |
| 225 | 241 | // Templates. |
| 226 | 242 | // Transient cache to avoid frequent disk reads. |
| @@ -229,14 +245,14 @@ | ||
| 229 | 245 | $templates = wp_get_theme()->get_page_templates(); |
| 230 | 246 | set_transient( 'autoptimize_ccss_page_templates', $templates, HOUR_IN_SECONDS ); |
| 231 | 247 | } |
| 232 | 248 | foreach ( $templates as $tplfile => $tplname ) { |
| 233 | - array_unshift( $ao_ccss_types, 'template_' . $tplfile ); | |
| 249 | + array_unshift( $this->_types, 'template_' . $tplfile ); | |
| 234 | 250 | } |
| 235 | 251 | |
| 236 | 252 | // bbPress tags. |
| 237 | 253 | if ( function_exists( 'is_bbpress' ) ) { |
| 238 | - $ao_ccss_types = array_merge( | |
| 254 | + $this->_types = array_merge( | |
| 239 | 255 | array( |
| 240 | 256 | 'bbp_is_bbpress', |
| 241 | 257 | 'bbp_is_favorites', |
| 242 | 258 | 'bbp_is_forum_archive', |
| @@ -260,15 +276,15 @@ | ||
| 260 | 276 | 'bbp_is_topic_tag_edit', |
| 261 | 277 | 'bbp_is_topics_created', |
| 262 | 278 | 'bbp_is_user_home', |
| 263 | 279 | 'bbp_is_user_home_edit', |
| 264 | - ), $ao_ccss_types | |
| 280 | + ), $this->_types | |
| 265 | 281 | ); |
| 266 | 282 | } |
| 267 | 283 | |
| 268 | 284 | // BuddyPress tags. |
| 269 | 285 | if ( function_exists( 'is_buddypress' ) ) { |
| 270 | - $ao_ccss_types = array_merge( | |
| 286 | + $this->_types = array_merge( | |
| 271 | 287 | array( |
| 272 | 288 | 'bp_is_activation_page', |
| 273 | 289 | 'bp_is_activity', |
| 274 | 290 | 'bp_is_blogs', |
| @@ -302,27 +318,27 @@ | ||
| 302 | 318 | 'bp_is_settings_component', |
| 303 | 319 | 'bp_is_user', |
| 304 | 320 | 'bp_is_user_profile', |
| 305 | 321 | 'bp_is_wire', |
| 306 | - ), $ao_ccss_types | |
| 322 | + ), $this->_types | |
| 307 | 323 | ); |
| 308 | 324 | } |
| 309 | 325 | |
| 310 | 326 | // Easy Digital Downloads (EDD) tags. |
| 311 | 327 | if ( function_exists( 'edd_is_checkout' ) ) { |
| 312 | - $ao_ccss_types = array_merge( | |
| 328 | + $this->_types = array_merge( | |
| 313 | 329 | array( |
| 314 | 330 | 'edd_is_checkout', |
| 315 | 331 | 'edd_is_failed_transaction_page', |
| 316 | 332 | 'edd_is_purchase_history_page', |
| 317 | 333 | 'edd_is_success_page', |
| 318 | - ), $ao_ccss_types | |
| 334 | + ), $this->_types | |
| 319 | 335 | ); |
| 320 | 336 | } |
| 321 | 337 | |
| 322 | 338 | // WooCommerce tags. |
| 323 | 339 | if ( class_exists( 'WooCommerce' ) ) { |
| 324 | - $ao_ccss_types = array_merge( | |
| 340 | + $this->_types = array_merge( | |
| 325 | 341 | array( |
| 326 | 342 | 'woo_is_account_page', |
| 327 | 343 | 'woo_is_cart', |
| 328 | 344 | 'woo_is_checkout', |
| @@ -331,44 +347,35 @@ | ||
| 331 | 347 | 'woo_is_product_tag', |
| 332 | 348 | 'woo_is_shop', |
| 333 | 349 | 'woo_is_wc_endpoint_url', |
| 334 | 350 | 'woo_is_woocommerce', |
| 335 | - ), $ao_ccss_types | |
| 351 | + ), $this->_types | |
| 336 | 352 | ); |
| 337 | 353 | } |
| 338 | 354 | } |
| 339 | 355 | |
| 340 | 356 | 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 | - } | |
| 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 | + ); | |
| 361 | 371 | } |
| 362 | 372 | |
| 363 | - public static function ao_ccss_key_status( $render ) { | |
| 373 | + public function ao_ccss_key_status( $render ) { | |
| 364 | 374 | // Provide key status |
| 365 | 375 | // 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; | |
| 376 | + $key = $this->criticalcss->get_option( 'key' ); | |
| 377 | + $key_status = $this->criticalcss->get_option( 'keyst' ); | |
| 371 | 378 | |
| 372 | 379 | // Prepare returned variables. |
| 373 | 380 | $key_return = array(); |
| 374 | 381 | $status = false; |
| @@ -389,9 +396,9 @@ | ||
| 389 | 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' ); |
| 390 | 397 | } elseif ( $key && ! $key_status ) { |
| 391 | 398 | // Key exists but it has no valid status yet |
| 392 | 399 | // Perform key validation. |
| 393 | - $key_check = $self->ao_ccss_key_validation( $key ); | |
| 400 | + $key_check = $this->ao_ccss_key_validation( $key ); | |
| 394 | 401 | |
| 395 | 402 | // Key is valid, set valid status. |
| 396 | 403 | if ( $key_check ) { |
| 397 | 404 | $status = 'valid'; |
| @@ -431,9 +438,9 @@ | ||
| 431 | 438 | return $key_return; |
| 432 | 439 | } |
| 433 | 440 | |
| 434 | 441 | public function ao_ccss_key_validation( $key ) { |
| 435 | - global $ao_ccss_noptimize; | |
| 442 | + $noptimize = $this->criticalcss->get_option( 'noptimize' ); | |
| 436 | 443 | |
| 437 | 444 | // POST a dummy job to criticalcss.com to check for key validation |
| 438 | 445 | // Prepare home URL for the request. |
| 439 | 446 | $src_url = get_home_url(); |
| @@ -438,9 +445,9 @@ | ||
| 438 | 445 | // Prepare home URL for the request. |
| 439 | 446 | $src_url = get_home_url(); |
| 440 | 447 | |
| 441 | 448 | // Avoid AO optimizations if required by config or avoid lazyload if lazyload is active in AO. |
| 442 | - if ( ! empty( $ao_ccss_noptimize ) ) { | |
| 449 | + if ( ! empty( $noptimize ) ) { | |
| 443 | 450 | $src_url .= '?ao_noptirocket=1'; |
| 444 | 451 | } elseif ( class_exists( 'autoptimizeImages', false ) && autoptimizeImages::should_lazyload_wrapper() ) { |
| 445 | 452 | $src_url .= '?ao_nolazy=1'; |
| 446 | 453 | } |
| @@ -449,14 +456,14 @@ | ||
| 449 | 456 | |
| 450 | 457 | // Prepare the request. |
| 451 | 458 | $url = esc_url_raw( AO_CCSS_API . 'generate' ); |
| 452 | 459 | $args = array( |
| 453 | - 'headers' => array( | |
| 460 | + 'headers' => apply_filters( 'autoptimize_ccss_cron_api_generate_headers', array( | |
| 454 | 461 | 'User-Agent' => 'Autoptimize v' . AO_CCSS_VER, |
| 455 | 462 | 'Content-type' => 'application/json; charset=utf-8', |
| 456 | 463 | 'Authorization' => 'JWT ' . $key, |
| 457 | 464 | 'Connection' => 'close', |
| 458 | - ), | |
| 465 | + ) ), | |
| 459 | 466 | // Body must be JSON. |
| 460 | 467 | 'body' => json_encode( |
| 461 | 468 | apply_filters( 'autoptimize_ccss_cron_api_generate_body', |
| 462 | 469 | array( |
| @@ -476,16 +483,16 @@ | ||
| 476 | 483 | if ( 200 == $code ) { |
| 477 | 484 | // Response is OK. |
| 478 | 485 | // Set key status as valid and log key check. |
| 479 | 486 | update_option( 'autoptimize_ccss_keyst', 2 ); |
| 480 | - 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 ); | |
| 481 | 488 | |
| 482 | 489 | // extract job-id from $body and put it in the queue as a P job |
| 483 | 490 | // but only if no jobs and no rules! |
| 484 | - global $ao_ccss_queue; | |
| 485 | - global $ao_ccss_rules; | |
| 491 | + $queue = $this->criticalcss->get_option( 'queue' ); | |
| 492 | + $rules = $this->criticalcss->get_option( 'rules' ); | |
| 486 | 493 | |
| 487 | - 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'] ) ) { | |
| 488 | 495 | if ( 'JOB_QUEUED' == $body['job']['status'] || 'JOB_ONGOING' == $body['job']['status'] ) { |
| 489 | 496 | $jprops['ljid'] = 'firstrun'; |
| 490 | 497 | $jprops['rtarget'] = 'types|is_front_page'; |
| 491 | 498 | $jprops['ptype'] = 'is_front_page'; |
| @@ -497,12 +504,12 @@ | ||
| 497 | 504 | $jprops['jrstat'] = null; |
| 498 | 505 | $jprops['jvstat'] = null; |
| 499 | 506 | $jprops['jctime'] = microtime( true ); |
| 500 | 507 | $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 ); | |
| 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 ); | |
| 505 | 512 | } |
| 506 | 513 | } |
| 507 | 514 | return true; |
| 508 | 515 | } elseif ( 401 == $code ) { |
| @@ -508,50 +515,36 @@ | ||
| 508 | 515 | } elseif ( 401 == $code ) { |
| 509 | 516 | // Response is unauthorized |
| 510 | 517 | // Set key status as invalid and log key check. |
| 511 | 518 | update_option( 'autoptimize_ccss_keyst', 1 ); |
| 512 | - 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 ); | |
| 513 | 520 | return false; |
| 514 | 521 | } else { |
| 515 | 522 | // Response unkown |
| 516 | 523 | // 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 ); | |
| 524 | + $this->ao_ccss_log( 'criticalcss.com: could not check API key status, this is a service error, body follows if any...', 2 ); | |
| 518 | 525 | if ( ! empty( $body ) ) { |
| 519 | - autoptimizeCriticalCSSCore::ao_ccss_log( print_r( $body, true ), 2 ); | |
| 526 | + $this->ao_ccss_log( print_r( $body, true ), 2 ); | |
| 520 | 527 | } |
| 521 | 528 | if ( is_wp_error( $req ) ) { |
| 522 | - autoptimizeCriticalCSSCore::ao_ccss_log( $req->get_error_message(), 2 ); | |
| 529 | + $this->ao_ccss_log( $req->get_error_message(), 2 ); | |
| 523 | 530 | } |
| 524 | 531 | return false; |
| 525 | 532 | } |
| 526 | 533 | } |
| 527 | 534 | |
| 528 | - public static function ao_ccss_viewport() { | |
| 535 | + public function ao_ccss_viewport() { | |
| 529 | 536 | // Get viewport size |
| 530 | 537 | // Attach viewport option. |
| 531 | - global $ao_ccss_viewport; | |
| 538 | + $viewport = $this->criticalcss->get_option( 'viewport' ); | |
| 532 | 539 | |
| 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; | |
| 540 | + return array( | |
| 541 | + 'w' => ! empty( $viewport['w'] ) ? $viewport['w'] : '', | |
| 542 | + 'h' => ! empty( $viewport['h'] ) ? $viewport['h'] : '', | |
| 543 | + ); | |
| 551 | 544 | } |
| 552 | 545 | |
| 553 | - public static function ao_ccss_check_contents( $ccss ) { | |
| 546 | + public function ao_ccss_check_contents( $ccss ) { | |
| 554 | 547 | // Perform basic exploit avoidance and CSS validation. |
| 555 | 548 | if ( ! empty( $ccss ) ) { |
| 556 | 549 | // Try to avoid code injection. |
| 557 | 550 | $blocklist = array( '#!/', 'function(', '<script', '<?php' ); |
| @@ -556,9 +549,9 @@ | ||
| 556 | 549 | // Try to avoid code injection. |
| 557 | 550 | $blocklist = array( '#!/', 'function(', '<script', '<?php' ); |
| 558 | 551 | foreach ( $blocklist as $blocklisted ) { |
| 559 | 552 | if ( strpos( $ccss, $blocklisted ) !== false ) { |
| 560 | - autoptimizeCriticalCSSCore::ao_ccss_log( 'Critical CSS received contained blocklisted content.', 2 ); | |
| 553 | + $this->ao_ccss_log( 'Critical CSS received contained blocklisted content.', 2 ); | |
| 561 | 554 | return false; |
| 562 | 555 | } |
| 563 | 556 | } |
| 564 | 557 | |
| @@ -565,9 +558,9 @@ | ||
| 565 | 558 | // Check for most basics CSS structures. |
| 566 | 559 | $needlist = array( '{', '}', ':' ); |
| 567 | 560 | foreach ( $needlist as $needed ) { |
| 568 | 561 | if ( false === strpos( $ccss, $needed ) && 'none' !== $ccss ) { |
| 569 | - 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 ); | |
| 570 | 563 | return false; |
| 571 | 564 | } |
| 572 | 565 | } |
| 573 | 566 | } |
| @@ -575,12 +568,12 @@ | ||
| 575 | 568 | // Return true if file critical CSS is sane. |
| 576 | 569 | return true; |
| 577 | 570 | } |
| 578 | 571 | |
| 579 | - public static function ao_ccss_log( $msg, $lvl ) { | |
| 572 | + public function ao_ccss_log( $msg, $lvl ) { | |
| 580 | 573 | // Commom logging facility |
| 581 | 574 | // Attach debug option. |
| 582 | - global $ao_ccss_debug; | |
| 575 | + $debug = $this->criticalcss->get_option( 'debug' ); | |
| 583 | 576 | |
| 584 | 577 | // Prepare log levels, where accepted $lvl are: |
| 585 | 578 | // 1: II (for info) |
| 586 | 579 | // 2: EE (for error) |
| @@ -595,9 +588,9 @@ | ||
| 595 | 588 | $level = 'EE'; |
| 596 | 589 | break; |
| 597 | 590 | case 3: |
| 598 | 591 | // Output debug messages only if debug mode is enabled. |
| 599 | - if ( $ao_ccss_debug ) { | |
| 592 | + if ( $debug ) { | |
| 600 | 593 | $level = 'DD'; |
| 601 | 594 | } |
| 602 | 595 | break; |
| 603 | 596 | default: |
| @@ -614,10 +607,9 @@ | ||
| 614 | 607 | error_log( $message, 3, AO_CCSS_LOG ); |
| 615 | 608 | } |
| 616 | 609 | } |
| 617 | 610 | |
| 618 | - public static function ao_ccss_clear_page_tpl_cache() { | |
| 611 | + public function ao_ccss_clear_page_tpl_cache() { | |
| 619 | 612 | // Clears transient cache for page templates. |
| 620 | 613 | delete_transient( 'autoptimize_ccss_page_templates' ); |
| 621 | 614 | } |
| 622 | - | |
| 623 | 615 | } |