PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 3.6
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v3.6
trunk 1.0 1.0.1 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 1.2.8 2.0 2.0.1 2.0.2 2.0.3 2.0.4 2.1 2.1.1 2.1.2 2.2 2.2.1 All 69 releases
powered-cache / includes / admin / notices.php

notices.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 3.6, at includes/admin/notices.php

448 lines 15.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Admin Notices
4 *
5 * @package PoweredCache
6 */
7
8 namespace PoweredCache\Admin\Notices;
9
10 use function PoweredCache\Utils\can_configure_htaccess;
11 use function PoweredCache\Utils\can_configure_object_cache;
12 use function PoweredCache\Utils\can_control_all_settings;
13 use function PoweredCache\Utils\get_object_cache_dropins;
14 use function PoweredCache\Utils\is_premium;
15 use const PoweredCache\Constants\PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT;
16
17 /**
18 * Default setup routine
19 *
20 * @return void
21 */
22 function setup() {
23 if ( POWERED_CACHE_IS_NETWORK ) {
24 add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_plugin_compatibility_notices' );
25 add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_advanced_cache_notices' );
26 add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_object_cache_notices' );
27 add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_htaccess_notice' );
28 add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_purge_cache_plugin_notice' );
29 add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_dev_mode_notice' );
30 } else {
31 add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_plugin_compatibility_notices' );
32 add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_advanced_cache_notices' );
33 add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_object_cache_notices' );
34 add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_htaccess_notice' );
35 add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_purge_cache_plugin_notice' );
36 add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_dev_mode_notice' );
37 }
38
39 add_action( 'activated_plugin', __NAMESPACE__ . '\\observe_plugin_changes', 10, 2 );
40 add_action( 'deactivated_plugin', __NAMESPACE__ . '\\observe_plugin_changes', 10, 2 );
41 add_action( 'admin_post_powered_cache_dismiss_notice', __NAMESPACE__ . '\\dismiss_notice' );
42 }
43
44 /**
45 * Display incompatible plugins
46 *
47 * @since 1.0
48 */
49 function maybe_display_plugin_compatibility_notices() {
50 $settings = \PoweredCache\Utils\get_settings();
51
52 $plugins = array(
53 'hummingbird-performance' => 'hummingbird-performance/wp-hummingbird.php',
54 'wp-rocket' => 'wp-rocket/wp-rocket.php',
55 'w3-total-cache' => 'w3-total-cache/w3-total-cache.php',
56 'wp-super-cache' => 'wp-super-cache/wp-cache.php',
57 'hyper-cache' => 'hyper-cache/plugin.php',
58 'hyper-cache-extended' => 'hyper-cache-extended/plugin.php',
59 'wp-fast-cache' => 'wp-fast-cache/wp-fast-cache.php',
60 'flexicache' => 'flexicache/wp-plugin.php',
61 'wp-fastest-cache' => 'wp-fastest-cache/wpFastestCache.php',
62 'wp-http-compression' => 'wp-http-compression/wp-http-compression.php',
63 'wordpress-gzip-compression' => 'wordpress-gzip-compression/ezgz.php',
64 'gzip-ninja-speed-compression' => 'gzip-ninja-speed-compression/gzip-ninja-speed.php',
65 'speed-booster-pack' => 'speed-booster-pack/speed-booster-pack.php',
66 'wp-performance-score-booster' => 'wp-performance-score-booster/wp-performance-score-booster.php',
67 'check-and-enable-gzip-compression' => 'check-and-enable-gzip-compression/richards-toolbox.php',
68 'swift-performance-lite' => 'swift-performance-lite/performance.php',
69 'swift-performance' => 'swift-performance/performance.php',
70 'litespeed-cache' => 'litespeed-cache/litespeed-cache.php',
71 'wp-optimize' => 'wp-optimize/wp-optimize.php',
72 );
73
74 if ( $settings['prefetch_links'] && is_premium() ) {
75 $plugins['quicklink'] = 'quicklink/quicklink.php';
76 $plugins['flying-pages'] = 'flying-pages/flying-pages.php';
77 $plugins['instant-page'] = 'instant-page/instantpage.php';
78 }
79
80 $callback = POWERED_CACHE_IS_NETWORK ? 'is_plugin_active_for_network' : 'is_plugin_active';
81
82 $plugins = array_filter( $plugins, $callback );
83
84 if ( 0 >= count( $plugins ) ) {
85 return;
86 }
87 ?>
88
89 <?php if ( current_user_can( 'activate_plugins' ) ) : ?>
90 <div class="error">
91 <p><?php esc_html_e( 'The following plugins are not compatible with Powered Cache and may cause unintended results:', 'powered-cache' ); ?></p>
92 <ul class="incompatible-plugin-list">
93 <?php
94 foreach ( $plugins as $plugin ) {
95 $plugin_data = get_plugin_data( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin );
96 echo '<li>' . esc_attr( $plugin_data['Name'] ) . '</span> <a href="' . esc_url_raw( wp_nonce_url( admin_url( 'admin-post.php?action=deactivate_plugin&plugin=' . rawurlencode( $plugin ) ), 'deactivate_plugin' ) ) . '" class="button-secondary">' . esc_html__( 'Deactivate', 'powered-cache' ) . '</a></li>'; // phpcs:ignore WordPressVIPMinimum.Security.ProperEscapingFunction.notAttrEscAttr
97 }
98 ?>
99 </ul>
100 </div>
101 <?php endif; ?>
102
103 <?php
104 }
105
106
107 /**
108 * Show notices about page cache
109 *
110 * @since 1.0
111 */
112 function maybe_display_advanced_cache_notices() {
113 /**
114 * Determine whether show or not show advanced cache related notices
115 * eg: Varnish users don't need to turning on page cache.
116 *
117 * @hook powered_cache_disable_advanced_cache_notices
118 *
119 * @param {boolean} $status false
120 *
121 * @return {boolean} New value
122 * @since 1.2
123 */
124 if ( apply_filters( 'powered_cache_disable_advanced_cache_notices', false ) ) {
125 return;
126 }
127
128 $settings = \PoweredCache\Utils\get_settings();
129
130 if ( ! $settings['enable_page_cache'] ) {
131
132 $settings_page = POWERED_CACHE_IS_NETWORK ? network_admin_url( 'admin.php?page=powered-cache#basic-options' ) : admin_url( 'admin.php?page=powered-cache#basic-options' );
133
134 /* translators: %s: Powered Cache settings page URL */
135 $message = sprintf( __( '<strong>Powered Cache:</strong> Page caching needs to be activated in order to speed up your website. Please activate it on <a href="%s">settings page</a>', 'powered-cache' ), esc_url( $settings_page ) );
136 ?>
137 <div class="notice notice-warning">
138 <p>
139 <?php echo wp_kses_post( $message ); ?>
140 </p>
141 </div>
142 <?php
143 return;
144 }
145
146 if ( file_exists( untrailingslashit( WP_CONTENT_DIR ) . '/advanced-cache.php' ) ) {
147 $modify_time = filemtime( untrailingslashit( WP_CONTENT_DIR ) . '/advanced-cache.php' );
148 if ( time() - absint( $modify_time ) < 10 ) {
149 /**
150 * Modify drop-ins and can take ~5 seconds to place the configuration and it might cause misinformation to the user
151 * So, suppress the error message here when doing something related to settings.
152 *
153 * This wouldn't be an issue if there is no immediate redirection right after saving the settings. However, in that case
154 * the admin bar items won't be properly utilized unless refreshing the page
155 */
156 return;
157 }
158 }
159
160 $err = array();
161
162 if ( ! defined( 'WP_CACHE' ) || true !== WP_CACHE ) {
163 /* translators: %s: WP_CACHE definition*/
164 $err['wp_cache'] = sprintf( __( '<code>%s</code> is not found in wp-config.php.', 'powered-cache' ), 'define("WP_CACHE", true);' );
165 }
166
167 if ( defined( 'WP_CACHE' ) && WP_CACHE && ( ! defined( 'POWERED_CACHE_PAGE_CACHING' ) || true !== POWERED_CACHE_PAGE_CACHING ) ) {
168 /* translators: %s: advanced-cache.php drop-in path */
169 $err['powered_cache_page_cache'] = sprintf( __( '<code>%s</code> file was edited or deleted. You can recreate the correct configuration files by saving Powered Cache settings.', 'powered-cache' ), basename( WP_CONTENT_DIR ) . '/advanced-cache.php' );
170 }
171
172 if ( defined( 'POWERED_CACHE_PAGE_CACHING_HAS_PROBLEM' ) && POWERED_CACHE_PAGE_CACHING_HAS_PROBLEM ) {
173 /* translators: %s: page-cache.php drop-in path */
174 $err['powered_cache_page_cache_has_problem'] = sprintf( __( 'Powered Cache could not access dropin. Please check <code>%s</code> exist and accessible on your server.', 'powered-cache' ), POWERED_CACHE_DROPIN_DIR . 'page-cache.php' );
175 }
176
177 // everything ok
178 if ( empty( $err ) ) {
179 return;
180 }
181
182 // dont show when settings just saved
183 if ( did_action( 'powered_cache_settings_saved' ) ) {
184 return;
185 }
186
187 $capability = POWERED_CACHE_IS_NETWORK ? 'manage_network' : 'manage_options';
188
189 if ( ! current_user_can( $capability ) ) {
190 return;
191 }
192 ?>
193 <div class="error">
194 <p>
195 <strong><?php esc_html_e( 'Page Cache is not working, because:', 'powered-cache' ); ?></strong>
196 </p>
197 <?php foreach ( $err as $error_msg ) : ?>
198 <p><?php echo wp_kses_post( $error_msg ); ?></p>
199 <?php endforeach; ?>
200 </div>
201 <?php
202
203 }
204
205
206 /**
207 * Display object cache broken msg
208 *
209 * @since 1.0
210 */
211 function maybe_display_object_cache_notices() {
212 $settings = \PoweredCache\Utils\get_settings();
213
214 $object_cache_backends = get_object_cache_dropins();
215 $object_cache_driver = $settings['object_cache'];
216 $object_cache_dropin = untrailingslashit( WP_CONTENT_DIR ) . '/object-cache.php';
217
218 if ( ! can_configure_object_cache() ) {
219 return;
220 }
221
222 // dont show when settings just saved
223 if ( did_action( 'powered_cache_settings_saved' ) ) {
224 return;
225 }
226
227 if ( file_exists( $object_cache_dropin ) ) {
228 $modify_time = filemtime( $object_cache_dropin );
229 if ( time() - absint( $modify_time ) < 10 ) { // just created
230 return;
231 }
232 }
233
234 // first check object cache file exist
235 if ( isset( $object_cache_backends[ $object_cache_driver ] ) && ! file_exists( $object_cache_dropin ) ) {
236 /* translators: %s: object cache dropin path */
237 $message = sprintf( __( 'The object cache file seems missing. Please check <code>%s</code> exist, writable and accessible on your server.', 'powered-cache' ), $object_cache_dropin );
238 ?>
239 <div class="error">
240 <p><strong><?php esc_html_e( 'Powered Cache:', 'powered-cache' ); ?></strong>
241 <?php echo wp_kses_post( $message ); ?>
242 </p>
243 </div>
244 <?php
245 return;
246 }
247
248 if ( defined( 'POWERED_OBJECT_CACHE_HAS_PROBLEM' ) && POWERED_OBJECT_CACHE_HAS_PROBLEM ) {
249 $broken_file = '';
250
251 if ( isset( $object_cache_backends[ $object_cache_driver ] ) ) {
252 $broken_file = $object_cache_backends[ $object_cache_driver ];
253 }
254 /* translators: %s: object cache dropin path */
255 $message = sprintf( __( 'The object cache file couldn\'t be loaded. Please check <code>%s</code> exist and accessible on your server.', 'powered-cache' ), $broken_file );
256 ?>
257 <div class="error">
258 <p><strong><?php esc_html_e( 'Powered Cache:', 'powered-cache' ); ?></strong>
259 <?php echo wp_kses_post( $message ); ?>
260 </p>
261 </div>
262 <?php
263 }
264
265 }
266
267 /**
268 * Notices for the .htaccess
269 *
270 * @since 1.2
271 */
272 function maybe_display_htaccess_notice() {
273 global $is_apache;
274
275 if ( ! $is_apache ) {
276 return;
277 }
278
279 $settings = \PoweredCache\Utils\get_settings();
280
281 if ( ! $settings['auto_configure_htaccess'] ) {
282 return;
283 }
284
285 if ( ! can_configure_htaccess() ) {
286 return;
287 }
288
289 // dont show when settings just saved
290 if ( did_action( 'powered_cache_settings_saved' ) ) {
291 return;
292 }
293
294 $htaccess_file = get_home_path() . '.htaccess';
295
296 if ( file_exists( $htaccess_file ) ) {
297 $modify_time = filemtime( $htaccess_file );
298 if ( time() - absint( $modify_time ) < 10 ) { // just modified
299 return;
300 }
301 }
302
303 $message = '';
304
305 if ( ! file_exists( $htaccess_file ) ) {
306 $message = __( 'The <code>.htaccess</code> couldn\'t be found on your server. Please create a new <code>.htaccess</code> file. (<a href="https://wordpress.org/support/article/htaccess/" target="_blank" rel="noopener">?</a>)', 'powered-cache' );
307 } elseif ( ! is_writeable( $htaccess_file ) ) {
308 $message = __( 'Oh no! It looks <code>.htaccess</code> file is not writable. Please make sure it is writable by the application server. Your website will be much faster when .htaccess is configured for Powered Cache.', 'powered-cache' );
309 }
310
311 if ( empty( $message ) ) {
312 return;
313 }
314
315 ?>
316
317 <div class="error">
318 <p><strong><?php esc_html_e( 'Powered Cache:', 'powered-cache' ); ?></strong>
319 <?php echo wp_kses_post( $message ); ?>
320 </p>
321 </div>
322
323 <?php
324 }
325
326 /**
327 * Observe new plugin activation/deactivation
328 *
329 * @param string $plugin file
330 * @param boolean $network_wide Whether plugin de/activated network wide or not
331 *
332 * @return void
333 * @since 3.2
334 */
335 function observe_plugin_changes( $plugin, $network_wide ) {
336 if ( false !== stripos( $plugin, 'powered-cache' ) ) {
337 return;
338 }
339
340 if ( $network_wide ) {
341 set_site_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT, '1' );
342
343 return;
344 }
345
346 set_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT, '1' );
347 }
348
349 /**
350 * Display cache purging notice upon a new plugin activated/deactivated
351 *
352 * @return void
353 * @since 3.2
354 */
355 function maybe_display_purge_cache_plugin_notice() {
356 $has_notice = false;
357
358 if ( POWERED_CACHE_IS_NETWORK && current_user_can( 'manage_network' ) ) {
359 $has_notice = get_site_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT );
360 $purge_url = wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_page_cache_network' ), 'powered_cache_purge_page_cache_network' );
361 } elseif ( current_user_can( 'activate_plugins' ) ) {
362 $has_notice = get_transient( PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT );
363 $purge_url = wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_all_cache' ), 'powered_cache_purge_all_cache' );
364 }
365
366 if ( $has_notice ) {
367 $message = __( '<strong>Powered Cache:</strong> One or more plugins have been activated or deactivated; consider clearing the cache if these changes impact your site\'s front end.', 'powered-cache' );
368 ?>
369 <div class="notice notice-warning is-dismissible">
370 <p>
371 <?php echo wp_kses_post( $message ); ?>
372 </p>
373 <p>
374 <a href="<?php echo esc_url_raw( $purge_url ); ?>" class="button-primary">
375 <?php esc_html_e( 'Purge Cache', 'powered-cache' ); ?>
376 </a>
377 <a href="<?php echo esc_url_raw( wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_dismiss_notice&notice=' . PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT ), 'powered_cache_dismiss_notice' ) ); ?>" class="button-secondary">
378 <?php esc_html_e( 'Dismiss this notice', 'powered-cache' ); ?>
379 </a>
380 </p>
381 <a href="<?php echo esc_url_raw( wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_dismiss_notice&notice=' . PURGE_CACHE_PLUGIN_NOTICE_TRANSIENT ), 'powered_cache_dismiss_notice' ) ); ?>" type="button" class="notice-dismiss" style="text-decoration:none;">
382 <span class="screen-reader-text"><?php esc_html_e( 'Dismiss this notice', 'powered-cache' ); ?></span>
383 </a>
384 </div>
385 <?php
386 }
387 }
388
389 /**
390 * Dismis given notice
391 *
392 * @return void
393 * @since 3.2
394 */
395 function dismiss_notice() {
396 if ( ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'powered_cache_dismiss_notice' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotValidated
397 wp_nonce_ays( '' );
398 }
399
400 if ( current_user_can( 'manage_options' ) && ! empty( $_GET['notice'] ) ) {
401 $notice = sanitize_text_field( wp_unslash( $_GET['notice'] ) );
402
403 if ( POWERED_CACHE_IS_NETWORK ) {
404 delete_site_transient( $notice );
405 } else {
406 delete_transient( $notice );
407 }
408 }
409
410 wp_safe_redirect( esc_url_raw( wp_get_referer() ) );
411 exit;
412 }
413
414 /**
415 * Display a notice when development mode is active
416 *
417 * @return void
418 * @since 3.6
419 * @related \PoweredCache\DevMode
420 */
421 function maybe_display_dev_mode_notice() {
422 if ( ! \PoweredCache\Utils\is_dev_mode_active() ) {
423 return;
424 }
425
426 if ( ! current_user_can( 'manage_options' ) ) {
427 return;
428 }
429
430 // Only display on Powered Cache settings page
431 $current_screen = get_current_screen();
432
433 if (
434 ! $current_screen
435 || ( ! isset( $current_screen->id ) || false === strpos( $current_screen->id, 'powered-cache' ) )
436 ) {
437 return;
438 }
439
440 ?>
441 <div class="notice notice-warning is-dismissible">
442 <p><strong><?php esc_html_e( 'Development Mode is active.', 'powered-cache' ); ?></strong>
443 <?php esc_html_e( 'Caching and optimizations are currently disabled. Don’t forget to disable it when done.', 'powered-cache' ); ?>
444 </p>
445 </div>
446 <?php
447 }
448