| @@ -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,45 +125,56 @@ | ||
| 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 | - } else { | |
| 125 | - $mkdirresp = true; | |
| 126 | - } | |
| 129 | + $mkdirresult = $this->criticalcss->create_ao_ccss_dir(); | |
| 127 | 130 | |
| 128 | - // Make sure our index.html is there. | |
| 129 | - if ( ! is_file( AO_CCSS_DIR . 'index.html' ) ) { | |
| 130 | - $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>' ); | |
| 131 | - } else { | |
| 132 | - $fileresp = true; | |
| 133 | - } | |
| 134 | - | |
| 135 | 131 | // Warn if we could not create those files. |
| 136 | - if ( ( ! $mkdirresp ) || ( ! $fileresp ) ) { | |
| 132 | + if ( ( true !== $mkdirresult ) ) { | |
| 137 | 133 | ?> |
| 138 | 134 | <div class="notice-error notice"><p> |
| 139 | 135 | <?php |
| 140 | - _e( 'Could not create the required directory. Make sure the webserver can write to the wp-content directory.', 'autoptimize' ); | |
| 136 | + _e( 'Could not create the required directory. Make sure the webserver can write to the wp-content/uploads directory.', 'autoptimize' ); | |
| 141 | 137 | ?> |
| 142 | 138 | </p></div> |
| 143 | 139 | <?php |
| 144 | 140 | } |
| 145 | 141 | |
| 146 | - // Check for Autoptimize. | |
| 147 | - if ( ! empty( $ao_ccss_key ) && ! $ao_css_defer ) { | |
| 142 | + // Check if CSS optimization is on. | |
| 143 | + if ( 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css' ) || 'on' !== autoptimizeOptionWrapper::get_option( 'autoptimize_css_defer' ) ) { | |
| 148 | 144 | ?> |
| 149 | - <div class="notice-error notice"><p> | |
| 145 | + <div class="notice-info notice"><p> | |
| 150 | 146 | <?php |
| 151 | - _e( "Oops! Please <strong>activate the \"Inline and Defer CSS\" option</strong> on Autoptimize's main settings page to use this power-up.", 'autoptimize' ); | |
| 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' ); | |
| 152 | 148 | ?> |
| 153 | 149 | </p></div> |
| 154 | 150 | <?php |
| 155 | - return; | |
| 156 | 151 | } |
| 157 | 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' ); | |
| 158 | + ?> | |
| 159 | + <div class="notice-info notice"><p> | |
| 160 | + <?php | |
| 161 | + _e( "The \"Eliminate render-blocking CSS\" option was activated to allow critical CSS to be used.", 'autoptimize' ); | |
| 162 | + ?> | |
| 163 | + </p></div> | |
| 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 | |
| 174 | + } | |
| 175 | + } | |
| 176 | + | |
| 158 | 177 | // check if WordPress cron is disabled and warn if so. |
| 159 | 178 | if ( ! empty( $ao_ccss_key ) && defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON && PAnD::is_admin_notice_active( 'i-know-about-disable-cron-forever' ) ) { |
| 160 | 179 | ?> |
| 161 | 180 | <div data-dismissible="i-know-about-disable-cron-forever" class="notice-warning notice is-dismissible"><p> |
| @@ -165,8 +184,19 @@ | ||
| 165 | 184 | </p></div> |
| 166 | 185 | <?php |
| 167 | 186 | } |
| 168 | 187 | |
| 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' ) ) { | |
| 190 | + ?> | |
| 191 | + <div data-dismissible="i-know-about-defer-inline-forever" class="notice-warning notice is-dismissible"><p> | |
| 192 | + <?php | |
| 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' ); | |
| 194 | + ?> | |
| 195 | + </p></div> | |
| 196 | + <?php | |
| 197 | + } | |
| 198 | + | |
| 169 | 199 | // warn if it looks as though the queue processing job looks isn't running |
| 170 | 200 | // but store result in transient as to not to have to go through 2 arrays each and every time. |
| 171 | 201 | $_warn_cron = get_transient( 'ao_ccss_cronwarning' ); |
| 172 | 202 | if ( ! empty( $ao_ccss_key ) && false === $_warn_cron ) { |
| @@ -221,8 +251,10 @@ | ||
| 221 | 251 | ?> |
| 222 | 252 | <div class="notice-success notice"><p> |
| 223 | 253 | <?php |
| 224 | 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' ); | |
| 225 | 257 | ?> |
| 226 | 258 | </p></div> |
| 227 | 259 | <?php |
| 228 | 260 | } |
| @@ -237,8 +269,21 @@ | ||
| 237 | 269 | </p></div> |
| 238 | 270 | <?php |
| 239 | 271 | } |
| 240 | 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 | + | |
| 241 | 286 | // Settings Form. |
| 242 | 287 | ?> |
| 243 | 288 | <form id="settings" method="post" action="options.php"> |
| 244 | 289 | <?php |
| @@ -244,9 +289,9 @@ | ||
| 244 | 289 | <?php |
| 245 | 290 | settings_fields( 'ao_ccss_options_group' ); |
| 246 | 291 | |
| 247 | 292 | // Get API key status. |
| 248 | - $key = autoptimizeCriticalCSSCore::ao_ccss_key_status( true ); | |
| 293 | + $key = $this->criticalcss->key_status( true ); | |
| 249 | 294 | |
| 250 | 295 | if ( $this->is_multisite_network_admin() ) { |
| 251 | 296 | ?> |
| 252 | 297 | <ul id="key-panel"> |
| @@ -267,27 +312,33 @@ | ||
| 267 | 312 | ao_ccss_render_queue(); |
| 268 | 313 | // Render advanced panel. |
| 269 | 314 | ao_ccss_render_adv(); |
| 270 | 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 | + | |
| 271 | 323 | // But if key is other than valid, add hidden fields to persist settings when submitting form |
| 272 | 324 | // Show explanation of why and how to get a API key. |
| 273 | 325 | ao_ccss_render_explain(); |
| 274 | 326 | |
| 275 | 327 | // Get viewport size. |
| 276 | - $viewport = autoptimizeCriticalCSSCore::ao_ccss_viewport(); | |
| 328 | + $viewport = $this->criticalcss->viewport(); | |
| 277 | 329 | |
| 278 | 330 | // Add hidden fields. |
| 279 | - echo "<input class='hidden' name='autoptimize_ccss_rules' value='" . $ao_ccss_rules_raw . "'>"; | |
| 280 | - echo "<input class='hidden' name='autoptimize_ccss_queue' value='" . $ao_ccss_queue_raw . "'>"; | |
| 281 | - echo '<input class="hidden" name="autoptimize_ccss_viewport[w]" value="' . $viewport['w'] . '">'; | |
| 282 | - echo '<input class="hidden" name="autoptimize_ccss_viewport[h]" value="' . $viewport['h'] . '">'; | |
| 283 | - echo '<input class="hidden" name="autoptimize_ccss_finclude" value="' . $ao_ccss_finclude . '">'; | |
| 284 | - echo '<input class="hidden" name="autoptimize_ccss_rlimit" value="' . $ao_ccss_rlimit . '">'; | |
| 285 | - echo '<input class="hidden" name="autoptimize_ccss_debug" value="' . $ao_ccss_debug . '">'; | |
| 286 | - 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 ) . '">'; | |
| 287 | 338 | echo '<input class="hidden" name="autoptimize_css_defer_inline" value="' . esc_attr( $ao_css_defer_inline ) . '">'; |
| 288 | - echo '<input class="hidden" name="autoptimize_ccss_loggedin" value="' . $ao_ccss_loggedin . '">'; | |
| 289 | - 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 ) . '">'; | |
| 290 | 341 | } |
| 291 | 342 | // Render key panel unconditionally. |
| 292 | 343 | ao_ccss_render_key( $ao_ccss_key, $key['status'], $key['stmsg'], $key['msg'], $key['color'] ); |
| 293 | 344 | ?> |
| @@ -338,13 +389,13 @@ | ||
| 338 | 389 | echo '</script>'; |
| 339 | 390 | } |
| 340 | 391 | } |
| 341 | 392 | |
| 342 | - public static function ao_ccss_has_autorules() { | |
| 393 | + public function ao_ccss_has_autorules() { | |
| 343 | 394 | static $_has_auto_rules = null; |
| 344 | 395 | |
| 345 | 396 | if ( null === $_has_auto_rules ) { |
| 346 | - global $ao_ccss_rules; | |
| 397 | + $ao_ccss_rules = $this->criticalcss->get_option( 'rules' ); | |
| 347 | 398 | $_has_auto_rules = false; |
| 348 | 399 | if ( ! empty( $ao_ccss_rules ) ) { |
| 349 | 400 | foreach ( array( 'types', 'paths' ) as $_typat ) { |
| 350 | 401 | foreach ( $ao_ccss_rules[ $_typat ] as $rule ) { |