| @@ -14,10 +14,10 @@ | ||
| 14 | 14 | * @var bool |
| 15 | 15 | */ |
| 16 | 16 | private $settings_screen_do_remote_http = true; |
| 17 | 17 | |
| 18 | - public function __construct() | |
| 19 | - { | |
| 18 | + public function __construct() { | |
| 19 | + $this->criticalcss = autoptimize()->criticalcss(); | |
| 20 | 20 | $this->settings_screen_do_remote_http = apply_filters( 'autoptimize_settingsscreen_remotehttp', $this->settings_screen_do_remote_http ); |
| 21 | 21 | $this->run(); |
| 22 | 22 | } |
| 23 | 23 | |
| @@ -57,9 +57,9 @@ | ||
| 57 | 57 | register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_additional' ); |
| 58 | 58 | register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_queue' ); |
| 59 | 59 | register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_viewport' ); |
| 60 | 60 | register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_finclude' ); |
| 61 | - register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_rlimit' ); | |
| 61 | + register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_rtimelimit' ); | |
| 62 | 62 | register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_noptimize' ); |
| 63 | 63 | register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_debug' ); |
| 64 | 64 | register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_key' ); |
| 65 | 65 | register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_keyst' ); |
| @@ -99,20 +99,28 @@ | ||
| 99 | 99 | require_once( 'critcss-inc/admin_settings_key.php' ); |
| 100 | 100 | require_once( 'critcss-inc/admin_settings_adv.php' ); |
| 101 | 101 | require_once( 'critcss-inc/admin_settings_explain.php' ); |
| 102 | 102 | |
| 103 | - // fetch all options at once and populate them individually explicitely as globals. | |
| 104 | - $all_options = autoptimizeCriticalCSSBase::fetch_options(); | |
| 105 | - foreach ( $all_options as $_option => $_value ) { | |
| 106 | - global ${$_option}; | |
| 107 | - ${$_option} = $_value; | |
| 108 | - } | |
| 103 | + $ao_ccss_key = $this->criticalcss->get_option( 'key' ); | |
| 104 | + $ao_ccss_keyst = $this->criticalcss->get_option( 'keyst' ); | |
| 105 | + $ao_css_defer = $this->criticalcss->get_option( 'css_defer' ); | |
| 106 | + $ao_ccss_deferjquery = $this->criticalcss->get_option( 'deferjquery' ); | |
| 107 | + $ao_ccss_queue = $this->criticalcss->get_option( 'queue' ); | |
| 108 | + $ao_ccss_rules = $this->criticalcss->get_option( 'rules' ); | |
| 109 | + $ao_ccss_servicestatus = $this->criticalcss->get_option( 'servicestatus' ); | |
| 110 | + $ao_ccss_finclude = $this->criticalcss->get_option( 'finclude' ); | |
| 111 | + $ao_ccss_rtimelimit = $this->criticalcss->get_option( 'rtimelimit' ); | |
| 112 | + $ao_ccss_debug = $this->criticalcss->get_option( 'debug' ); | |
| 113 | + $ao_ccss_noptimize = $this->criticalcss->get_option( 'noptimize' ); | |
| 114 | + $ao_css_defer_inline = $this->criticalcss->get_option( 'css_defer_inline' ); | |
| 115 | + $ao_ccss_loggedin = $this->criticalcss->get_option( 'loggedin' ); | |
| 116 | + $ao_ccss_forcepath = $this->criticalcss->get_option( 'forcepath' ); | |
| 109 | 117 | ?> |
| 110 | 118 | <script>document.title = "Autoptimize: <?php _e( 'Critical CSS', 'autoptimize' ); ?> " + document.title;</script> |
| 111 | 119 | <div class="wrap"> |
| 112 | 120 | <div id="autoptimize_main"> |
| 113 | 121 | <div id="ao_title_and_button"> |
| 114 | - <h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1> | |
| 122 | + <h1><?php apply_filters( 'autoptimize_filter_settings_is_pro', false ) ? _e( 'Autoptimize Pro Settings', 'autoptimize' ) : _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1> | |
| 115 | 123 | </div> |
| 116 | 124 | |
| 117 | 125 | <?php |
| 118 | 126 | // Print AO settings tabs. |
| @@ -117,41 +125,73 @@ | ||
| 117 | 125 | <?php |
| 118 | 126 | // Print AO settings tabs. |
| 119 | 127 | echo autoptimizeConfig::ao_admin_tabs(); |
| 120 | 128 | |
| 121 | - // Make sure dir to write ao_ccss exists and is writable. | |
| 122 | - if ( ! is_dir( AO_CCSS_DIR ) ) { | |
| 123 | - $mkdirresp = @mkdir( AO_CCSS_DIR, 0775, true ); // @codingStandardsIgnoreLine | |
| 124 | - $fileresp = file_put_contents( AO_CCSS_DIR . 'index.html', '<html><head><meta name="robots" content="noindex, nofollow"></head><body>Generated by <a href="http://wordpress.org/extend/plugins/autoptimize/" rel="nofollow">Autoptimize</a></body></html>' ); | |
| 125 | - if ( ( ! $mkdirresp ) || ( ! $fileresp ) ) { | |
| 129 | + $mkdirresult = $this->criticalcss->create_ao_ccss_dir(); | |
| 130 | + | |
| 131 | + // Warn if we could not create those files. | |
| 132 | + if ( ( true !== $mkdirresult ) ) { | |
| 133 | + ?> | |
| 134 | + <div class="notice-error notice"><p> | |
| 135 | + <?php | |
| 136 | + _e( 'Could not create the required directory. Make sure the webserver can write to the wp-content/uploads directory.', 'autoptimize' ); | |
| 137 | + ?> | |
| 138 | + </p></div> | |
| 139 | + <?php | |
| 140 | + } | |
| 141 | + | |
| 142 | + // Check if CSS optimization is on. | |
| 143 | + if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css' ) || 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer' ) ) { | |
| 144 | + ?> | |
| 145 | + <div class="notice-info notice"><p> | |
| 146 | + <?php | |
| 147 | + _e( 'To be able to use Critical CSS you will have to enable CSS optimization and make sure "eliminate render-blocking CSS" is active on the main Autoptimize settings page.', 'autoptimize' ); | |
| 148 | + ?> | |
| 149 | + </p></div> | |
| 150 | + <?php | |
| 151 | + } | |
| 152 | + | |
| 153 | + // Check for "inline & defer CSS" being active in Autoptimize. | |
| 154 | + if ( ! empty( $ao_ccss_key ) && ! $ao_css_defer ) { | |
| 155 | + if ( empty( $ao_ccss_keyst ) ) { | |
| 156 | + // no keystate so likely in activation-process of CCSS, let's enable "inline & defer CSS" immediately to make things easier! | |
| 157 | + autoptimizeOptionWrapper::update_option( 'autoptimize_css_defer', 'on' ); | |
| 126 | 158 | ?> |
| 127 | - <div class="notice-error notice"><p> | |
| 159 | + <div class="notice-info notice"><p> | |
| 128 | 160 | <?php |
| 129 | - _e( 'Could not create the required directory. Make sure the webserver can write to the wp-content directory.', 'autoptimize' ); | |
| 161 | + _e( "The \"Eliminate render-blocking CSS\" option was activated to allow critical CSS to be used.", 'autoptimize' ); | |
| 130 | 162 | ?> |
| 131 | 163 | </p></div> |
| 132 | 164 | <?php |
| 165 | + } else { | |
| 166 | + // we have keystate, so "inline & defer CSS" was probably disabled for troubleshooting, warn but let users continue. | |
| 167 | + ?> | |
| 168 | + <div class="notice-warning notice"><p> | |
| 169 | + <?php | |
| 170 | + _e( "Please <strong>activate the \"Eliminate render-blocking CSS\" option</strong> on Autoptimize's main settings page to ensure critical CSS is used on the front-end.", 'autoptimize' ); | |
| 171 | + ?> | |
| 172 | + </p></div> | |
| 173 | + <?php | |
| 133 | 174 | } |
| 134 | 175 | } |
| 135 | 176 | |
| 136 | - // Check for Autoptimize. | |
| 137 | - if ( ! empty( $ao_ccss_key ) && ! $ao_css_defer ) { | |
| 177 | + // check if WordPress cron is disabled and warn if so. | |
| 178 | + if ( ! empty( $ao_ccss_key ) && defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON && PAnD::is_admin_notice_active( 'i-know-about-disable-cron-forever' ) ) { | |
| 138 | 179 | ?> |
| 139 | - <div class="notice-error notice"><p> | |
| 180 | + <div data-dismissible="i-know-about-disable-cron-forever" class="notice-warning notice is-dismissible"><p> | |
| 140 | 181 | <?php |
| 141 | - _e( "Oops! Please <strong>activate the \"Inline and Defer CSS\" option</strong> on Autoptimize's main settings page to use this power-up.", 'autoptimize' ); | |
| 182 | + _e( 'WordPress cron (for task scheduling) seems to be disabled. Have a look at <a href="https://wordpress.org/plugins/autoptimize-criticalcss/faq/" target="_blank">the FAQ</a> or the info in the Job Queue instructions if all jobs remain in "N" status and no rules are created.', 'autoptimize' ); | |
| 142 | 183 | ?> |
| 143 | 184 | </p></div> |
| 144 | 185 | <?php |
| 145 | - return; | |
| 146 | 186 | } |
| 147 | 187 | |
| 148 | - // check if WordPress cron is disabled and warn if so. | |
| 149 | - if ( ! empty( $ao_ccss_key ) && defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON && PAnD::is_admin_notice_active( 'i-know-about-disable-cron-forever' ) ) { | |
| 188 | + // check if defer jQuery is active and warn if so. | |
| 189 | + if ( 1 == $ao_ccss_deferjquery && PAnD::is_admin_notice_active( 'i-know-about-defer-inline-forever' ) ) { | |
| 150 | 190 | ?> |
| 151 | - <div data-dismissible="i-know-about-disable-cron-forever" class="notice-warning notice is-dismissible"><p> | |
| 191 | + <div data-dismissible="i-know-about-defer-inline-forever" class="notice-warning notice is-dismissible"><p> | |
| 152 | 192 | <?php |
| 153 | - _e( 'WordPress cron (for task scheduling) seems to be disabled. Have a look at <a href="https://wordpress.org/plugins/autoptimize-criticalcss/faq/" target="_blank">the FAQ</a> or the info in the Job Queue instructions if all jobs remain in "N" status and no rules are created.', 'autoptimize' ); | |
| 193 | + _e( 'You have "defer jQuery and other non-aggregated JS-files" active (under Advanced Settings), but that functionality is deprecated and will be removed in the next major version of Autoptimize. Consider using the new "Do not aggregate but defer" and "Also defer inline JS" options on the main settings page instead.', 'autoptimize' ); | |
| 154 | 194 | ?> |
| 155 | 195 | </p></div> |
| 156 | 196 | <?php |
| 157 | 197 | } |
| @@ -211,8 +251,10 @@ | ||
| 211 | 251 | ?> |
| 212 | 252 | <div class="notice-success notice"><p> |
| 213 | 253 | <?php |
| 214 | 254 | _e( 'Great, Autoptimize will now automatically start creating new critical CSS rules, you should see those appearing below in the next couple of hours.', 'autoptimize' ); |
| 255 | + echo ' '; | |
| 256 | + _e( 'In the meantime you might want to <strong>edit default rule CSS now</strong>, to avoid all CSS being inlined when no (applicable) rules are found.', 'autoptimize' ); | |
| 215 | 257 | ?> |
| 216 | 258 | </p></div> |
| 217 | 259 | <?php |
| 218 | 260 | } |
| @@ -227,8 +269,21 @@ | ||
| 227 | 269 | </p></div> |
| 228 | 270 | <?php |
| 229 | 271 | } |
| 230 | 272 | |
| 273 | + // warn if too many rules (based on length of ao_ccss_rules option) as that might cause issues at e.g. wpengine | |
| 274 | + // see https://wpengine.com/support/database-optimization-best-practices/#Autoloaded_Data | |
| 275 | + $_raw_rules_length = strlen( get_option( 'autoptimize_ccss_rules', '') ); | |
| 276 | + if ( $_raw_rules_length > apply_filters( 'autoptimize_ccss_rules_length_warning', 500000 ) ) { | |
| 277 | + ?> | |
| 278 | + <div class="notice-warning notice"><p> | |
| 279 | + <?php | |
| 280 | + _e( 'It looks like the amount of Critical CSS rules is very high, it is recommended to reconfigure Autoptimize (e.g. by manually creating broader rules) to ensure less rules are created.', 'autoptimize' ); | |
| 281 | + ?> | |
| 282 | + </p></div> | |
| 283 | + <?php | |
| 284 | + } | |
| 285 | + | |
| 231 | 286 | // Settings Form. |
| 232 | 287 | ?> |
| 233 | 288 | <form id="settings" method="post" action="options.php"> |
| 234 | 289 | <?php |
| @@ -234,9 +289,9 @@ | ||
| 234 | 289 | <?php |
| 235 | 290 | settings_fields( 'ao_ccss_options_group' ); |
| 236 | 291 | |
| 237 | 292 | // Get API key status. |
| 238 | - $key = autoptimizeCriticalCSSCore::ao_ccss_key_status( true ); | |
| 293 | + $key = $this->criticalcss->key_status( true ); | |
| 239 | 294 | |
| 240 | 295 | if ( $this->is_multisite_network_admin() ) { |
| 241 | 296 | ?> |
| 242 | 297 | <ul id="key-panel"> |
| @@ -257,27 +312,33 @@ | ||
| 257 | 312 | ao_ccss_render_queue(); |
| 258 | 313 | // Render advanced panel. |
| 259 | 314 | ao_ccss_render_adv(); |
| 260 | 315 | } else { |
| 316 | + if ( apply_filters( 'autoptimize_filter_ccss_rules_without_api', true ) ) { | |
| 317 | + // Render rules section for manual rules. | |
| 318 | + ao_ccss_render_rules(); | |
| 319 | + } else { | |
| 320 | + echo "<input class='hidden' name='autoptimize_ccss_queue' value='" . json_encode( $ao_ccss_rules, JSON_FORCE_OBJECT ) . "'>"; | |
| 321 | + } | |
| 322 | + | |
| 261 | 323 | // But if key is other than valid, add hidden fields to persist settings when submitting form |
| 262 | 324 | // Show explanation of why and how to get a API key. |
| 263 | 325 | ao_ccss_render_explain(); |
| 264 | 326 | |
| 265 | 327 | // Get viewport size. |
| 266 | - $viewport = autoptimizeCriticalCSSCore::ao_ccss_viewport(); | |
| 328 | + $viewport = $this->criticalcss->viewport(); | |
| 267 | 329 | |
| 268 | 330 | // Add hidden fields. |
| 269 | - echo "<input class='hidden' name='autoptimize_ccss_rules' value='" . $ao_ccss_rules_raw . "'>"; | |
| 270 | - echo "<input class='hidden' name='autoptimize_ccss_queue' value='" . $ao_ccss_queue_raw . "'>"; | |
| 271 | - echo '<input class="hidden" name="autoptimize_ccss_viewport[w]" value="' . $viewport['w'] . '">'; | |
| 272 | - echo '<input class="hidden" name="autoptimize_ccss_viewport[h]" value="' . $viewport['h'] . '">'; | |
| 273 | - echo '<input class="hidden" name="autoptimize_ccss_finclude" value="' . $ao_ccss_finclude . '">'; | |
| 274 | - echo '<input class="hidden" name="autoptimize_ccss_rlimit" value="' . $ao_ccss_rlimit . '">'; | |
| 275 | - echo '<input class="hidden" name="autoptimize_ccss_debug" value="' . $ao_ccss_debug . '">'; | |
| 276 | - echo '<input class="hidden" name="autoptimize_ccss_noptimize" value="' . $ao_ccss_noptimize . '">'; | |
| 331 | + echo "<input class='hidden' name='autoptimize_ccss_queue' value='" . json_encode( $ao_ccss_queue, JSON_FORCE_OBJECT ) . "'>"; | |
| 332 | + echo '<input class="hidden" name="autoptimize_ccss_viewport[w]" value="' . esc_attr( $viewport['w'] ) . '">'; | |
| 333 | + echo '<input class="hidden" name="autoptimize_ccss_viewport[h]" value="' . esc_attr( $viewport['h'] ) . '">'; | |
| 334 | + echo '<input class="hidden" name="autoptimize_ccss_finclude" value="' . esc_attr( $ao_ccss_finclude ) . '">'; | |
| 335 | + echo '<input class="hidden" name="autoptimize_ccss_rtimelimit" value="' . esc_attr( $ao_ccss_rtimelimit ) . '">'; | |
| 336 | + echo '<input class="hidden" name="autoptimize_ccss_debug" value="' . esc_attr( $ao_ccss_debug ) . '">'; | |
| 337 | + echo '<input class="hidden" name="autoptimize_ccss_noptimize" value="' . esc_attr( $ao_ccss_noptimize ) . '">'; | |
| 277 | 338 | echo '<input class="hidden" name="autoptimize_css_defer_inline" value="' . esc_attr( $ao_css_defer_inline ) . '">'; |
| 278 | - echo '<input class="hidden" name="autoptimize_ccss_loggedin" value="' . $ao_ccss_loggedin . '">'; | |
| 279 | - echo '<input class="hidden" name="autoptimize_ccss_forcepath" value="' . $ao_ccss_forcepath . '">'; | |
| 339 | + echo '<input class="hidden" name="autoptimize_ccss_loggedin" value="' . esc_attr( $ao_ccss_loggedin ). '">'; | |
| 340 | + echo '<input class="hidden" name="autoptimize_ccss_forcepath" value="' . esc_attr( $ao_ccss_forcepath ) . '">'; | |
| 280 | 341 | } |
| 281 | 342 | // Render key panel unconditionally. |
| 282 | 343 | ao_ccss_render_key( $ao_ccss_key, $key['status'], $key['stmsg'], $key['msg'], $key['color'] ); |
| 283 | 344 | ?> |
| @@ -328,13 +389,13 @@ | ||
| 328 | 389 | echo '</script>'; |
| 329 | 390 | } |
| 330 | 391 | } |
| 331 | 392 | |
| 332 | - public static function ao_ccss_has_autorules() { | |
| 393 | + public function ao_ccss_has_autorules() { | |
| 333 | 394 | static $_has_auto_rules = null; |
| 334 | 395 | |
| 335 | 396 | if ( null === $_has_auto_rules ) { |
| 336 | - global $ao_ccss_rules; | |
| 397 | + $ao_ccss_rules = $this->criticalcss->get_option( 'rules' ); | |
| 337 | 398 | $_has_auto_rules = false; |
| 338 | 399 | if ( ! empty( $ao_ccss_rules ) ) { |
| 339 | 400 | foreach ( array( 'types', 'paths' ) as $_typat ) { |
| 340 | 401 | foreach ( $ao_ccss_rules[ $_typat ] as $rule ) { |