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