PluginProbe
Autoptimize / 3.0.3
Autoptimize v3.0.3
2.2.2 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.5.0 2.5.1 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 All 107 releases
← All changes | classes/autoptimizeCriticalCSSSettings.php +103 -40 2.7.23.0.3 View file →
@@ -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' );
@@ -66,8 +66,9 @@
66 66 register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_loggedin' );
67 67 register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_forcepath' );
68 68 register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_deferjquery' );
69 69 register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_domain' );
70 + register_setting( 'ao_ccss_options_group', 'autoptimize_ccss_unloadccss' );
70 71
71 72 // And add submenu-page.
72 73 add_submenu_page( null, 'Critical CSS', 'Critical CSS', 'manage_options', 'ao_critcss', array( $this, 'ao_criticalcsssettings_page' ) );
73 74 }
@@ -98,19 +99,28 @@
98 99 require_once( 'critcss-inc/admin_settings_key.php' );
99 100 require_once( 'critcss-inc/admin_settings_adv.php' );
100 101 require_once( 'critcss-inc/admin_settings_explain.php' );
101 102
102 - // fetch all options at once and populate them individually explicitely as globals.
103 - $all_options = autoptimizeCriticalCSSBase::fetch_options();
104 - foreach ( $all_options as $_option => $_value ) {
105 - global ${$_option};
106 - ${$_option} = $_value;
107 - }
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' );
108 117 ?>
118 + <script>document.title = "Autoptimize: <?php _e( 'Critical CSS', 'autoptimize' ); ?> " + document.title;</script>
109 119 <div class="wrap">
110 120 <div id="autoptimize_main">
111 121 <div id="ao_title_and_button">
112 - <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>
113 123 </div>
114 124
115 125 <?php
116 126 // Print AO settings tabs.
@@ -115,41 +125,73 @@
115 125 <?php
116 126 // Print AO settings tabs.
117 127 echo autoptimizeConfig::ao_admin_tabs();
118 128
119 - // Make sure dir to write ao_ccss exists and is writable.
120 - if ( ! is_dir( AO_CCSS_DIR ) ) {
121 - $mkdirresp = @mkdir( AO_CCSS_DIR, 0775, true ); // @codingStandardsIgnoreLine
122 - $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>' );
123 - 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' );
124 158 ?>
125 - <div class="notice-error notice"><p>
159 + <div class="notice-info notice"><p>
126 160 <?php
127 - _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' );
128 162 ?>
129 163 </p></div>
130 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
131 174 }
132 175 }
133 176
134 - // Check for Autoptimize.
135 - 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' ) ) {
136 179 ?>
137 - <div class="notice-error notice"><p>
180 + <div data-dismissible="i-know-about-disable-cron-forever" class="notice-warning notice is-dismissible"><p>
138 181 <?php
139 - _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' );
140 183 ?>
141 184 </p></div>
142 185 <?php
143 - return;
144 186 }
145 187
146 - // check if WordPress cron is disabled and warn if so.
147 - 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' ) ) {
148 190 ?>
149 - <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>
150 192 <?php
151 - _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' );
152 194 ?>
153 195 </p></div>
154 196 <?php
155 197 }
@@ -209,8 +251,10 @@
209 251 ?>
210 252 <div class="notice-success notice"><p>
211 253 <?php
212 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' );
213 257 ?>
214 258 </p></div>
215 259 <?php
216 260 }
@@ -225,8 +269,21 @@
225 269 </p></div>
226 270 <?php
227 271 }
228 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 +
229 286 // Settings Form.
230 287 ?>
231 288 <form id="settings" method="post" action="options.php">
232 289 <?php
@@ -232,9 +289,9 @@
232 289 <?php
233 290 settings_fields( 'ao_ccss_options_group' );
234 291
235 292 // Get API key status.
236 - $key = autoptimizeCriticalCSSCore::ao_ccss_key_status( true );
293 + $key = $this->criticalcss->key_status( true );
237 294
238 295 if ( $this->is_multisite_network_admin() ) {
239 296 ?>
240 297 <ul id="key-panel">
@@ -255,27 +312,33 @@
255 312 ao_ccss_render_queue();
256 313 // Render advanced panel.
257 314 ao_ccss_render_adv();
258 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 +
259 323 // But if key is other than valid, add hidden fields to persist settings when submitting form
260 324 // Show explanation of why and how to get a API key.
261 325 ao_ccss_render_explain();
262 326
263 327 // Get viewport size.
264 - $viewport = autoptimizeCriticalCSSCore::ao_ccss_viewport();
328 + $viewport = $this->criticalcss->viewport();
265 329
266 330 // Add hidden fields.
267 - echo "<input class='hidden' name='autoptimize_ccss_rules' value='" . $ao_ccss_rules_raw . "'>";
268 - echo "<input class='hidden' name='autoptimize_ccss_queue' value='" . $ao_ccss_queue_raw . "'>";
269 - echo '<input class="hidden" name="autoptimize_ccss_viewport[w]" value="' . $viewport['w'] . '">';
270 - echo '<input class="hidden" name="autoptimize_ccss_viewport[h]" value="' . $viewport['h'] . '">';
271 - echo '<input class="hidden" name="autoptimize_ccss_finclude" value="' . $ao_ccss_finclude . '">';
272 - echo '<input class="hidden" name="autoptimize_ccss_rlimit" value="' . $ao_ccss_rlimit . '">';
273 - echo '<input class="hidden" name="autoptimize_ccss_debug" value="' . $ao_ccss_debug . '">';
274 - 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 ) . '">';
275 338 echo '<input class="hidden" name="autoptimize_css_defer_inline" value="' . esc_attr( $ao_css_defer_inline ) . '">';
276 - echo '<input class="hidden" name="autoptimize_ccss_loggedin" value="' . $ao_ccss_loggedin . '">';
277 - 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 ) . '">';
278 341 }
279 342 // Render key panel unconditionally.
280 343 ao_ccss_render_key( $ao_ccss_key, $key['status'], $key['stmsg'], $key['msg'], $key['color'] );
281 344 ?>
@@ -326,13 +389,13 @@
326 389 echo '</script>';
327 390 }
328 391 }
329 392
330 - public static function ao_ccss_has_autorules() {
393 + public function ao_ccss_has_autorules() {
331 394 static $_has_auto_rules = null;
332 395
333 396 if ( null === $_has_auto_rules ) {
334 - global $ao_ccss_rules;
397 + $ao_ccss_rules = $this->criticalcss->get_option( 'rules' );
335 398 $_has_auto_rules = false;
336 399 if ( ! empty( $ao_ccss_rules ) ) {
337 400 foreach ( array( 'types', 'paths' ) as $_typat ) {
338 401 foreach ( $ao_ccss_rules[ $_typat ] as $rule ) {