| 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 |
|
| 15 |
/** |
| 16 |
* Default setup routine |
| 17 |
* |
| 18 |
* @return void |
| 19 |
*/ |
| 20 |
function setup() { |
| 21 |
if ( POWERED_CACHE_IS_NETWORK ) { |
| 22 |
add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_plugin_compatability_notices' ); |
| 23 |
add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_advanced_cache_notices' ); |
| 24 |
add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_object_cache_notices' ); |
| 25 |
add_action( 'network_admin_notices', __NAMESPACE__ . '\\maybe_display_htaccess_notice' ); |
| 26 |
} else { |
| 27 |
add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_plugin_compatability_notices' ); |
| 28 |
add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_advanced_cache_notices' ); |
| 29 |
add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_object_cache_notices' ); |
| 30 |
add_action( 'admin_notices', __NAMESPACE__ . '\\maybe_display_htaccess_notice' ); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
/** |
| 35 |
* Display incompatible plugins |
| 36 |
* |
| 37 |
* @since 1.0 |
| 38 |
*/ |
| 39 |
function maybe_display_plugin_compatability_notices() { |
| 40 |
$plugins = array( |
| 41 |
'hummingbird-performance' => 'hummingbird-performance/wp-hummingbird.php', |
| 42 |
'wp-rocket' => 'wp-rocket/wp-rocket.php', |
| 43 |
'w3-total-cache' => 'w3-total-cache/w3-total-cache.php', |
| 44 |
'wp-super-cache' => 'wp-super-cache/wp-cache.php', |
| 45 |
'hyper-cache' => 'hyper-cache/plugin.php', |
| 46 |
'hyper-cache-extended' => 'hyper-cache-extended/plugin.php', |
| 47 |
'wp-fast-cache' => 'wp-fast-cache/wp-fast-cache.php', |
| 48 |
'flexicache' => 'flexicache/wp-plugin.php', |
| 49 |
'wp-fastest-cache' => 'wp-fastest-cache/wpFastestCache.php', |
| 50 |
'wp-http-compression' => 'wp-http-compression/wp-http-compression.php', |
| 51 |
'wordpress-gzip-compression' => 'wordpress-gzip-compression/ezgz.php', |
| 52 |
'gzip-ninja-speed-compression' => 'gzip-ninja-speed-compression/gzip-ninja-speed.php', |
| 53 |
'speed-booster-pack' => 'speed-booster-pack/speed-booster-pack.php', |
| 54 |
'wp-performance-score-booster' => 'wp-performance-score-booster/wp-performance-score-booster.php', |
| 55 |
'check-and-enable-gzip-compression' => 'check-and-enable-gzip-compression/richards-toolbox.php', |
| 56 |
); |
| 57 |
|
| 58 |
$callback = POWERED_CACHE_IS_NETWORK ? 'is_plugin_active_for_network' : 'is_plugin_active'; |
| 59 |
|
| 60 |
$plugins = array_filter( $plugins, $callback ); |
| 61 |
|
| 62 |
if ( 0 >= count( $plugins ) ) { |
| 63 |
return; |
| 64 |
} |
| 65 |
?> |
| 66 |
|
| 67 |
<?php if ( current_user_can( 'activate_plugins' ) ) : ?> |
| 68 |
<div class="error"> |
| 69 |
<p><?php esc_html_e( 'The following plugins are not compatible with Powered Cache and may cause unintended results:', 'powered-cache' ); ?></p> |
| 70 |
<ul class="incompatible-plugin-list"> |
| 71 |
<?php |
| 72 |
foreach ( $plugins as $plugin ) { |
| 73 |
$plugin_data = get_plugin_data( WP_PLUGIN_DIR . DIRECTORY_SEPARATOR . $plugin ); |
| 74 |
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>'; |
| 75 |
} |
| 76 |
?> |
| 77 |
</ul> |
| 78 |
</div> |
| 79 |
<?php endif; ?> |
| 80 |
|
| 81 |
<?php |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
/** |
| 86 |
* Show notices about page cache |
| 87 |
* |
| 88 |
* @since 1.0 |
| 89 |
*/ |
| 90 |
function maybe_display_advanced_cache_notices() { |
| 91 |
/** |
| 92 |
* Determine whether show or not show advanced cache related notices |
| 93 |
* eg: Varnish users don't need to turning on page cache. |
| 94 |
* |
| 95 |
* @hook powered_cache_disable_advanced_cache_notices |
| 96 |
* |
| 97 |
* @param {boolean} $status false |
| 98 |
* |
| 99 |
* @return {boolean} New value |
| 100 |
* @since 1.2 |
| 101 |
*/ |
| 102 |
if ( apply_filters( 'powered_cache_disable_advanced_cache_notices', false ) ) { |
| 103 |
return; |
| 104 |
} |
| 105 |
|
| 106 |
$settings = \PoweredCache\Utils\get_settings(); |
| 107 |
|
| 108 |
if ( ! $settings['enable_page_cache'] ) { |
| 109 |
|
| 110 |
$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' ); |
| 111 |
|
| 112 |
/* translators: %s: Powered Cache settings page URL */ |
| 113 |
$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 ) ); |
| 114 |
?> |
| 115 |
<div class="notice notice-warning"> |
| 116 |
<p> |
| 117 |
<?php echo wp_kses_post( $message ); ?> |
| 118 |
</p> |
| 119 |
</div> |
| 120 |
<?php |
| 121 |
return; |
| 122 |
} |
| 123 |
|
| 124 |
if ( file_exists( untrailingslashit( WP_CONTENT_DIR ) . '/advanced-cache.php' ) ) { |
| 125 |
$modify_time = filemtime( untrailingslashit( WP_CONTENT_DIR ) . '/advanced-cache.php' ); |
| 126 |
if ( time() - absint( $modify_time ) < 10 ) { |
| 127 |
/** |
| 128 |
* Modify drop-ins and can take ~5 seconds to place the configuration and it might cause misinformation to the user |
| 129 |
* So, suppress the error message here when doing something related to settings. |
| 130 |
* |
| 131 |
* This wouldn't be an issue if there is no immediate redirection right after saving the settings. However, in that case |
| 132 |
* the admin bar items won't be properly utilized unless refreshing the page |
| 133 |
*/ |
| 134 |
return; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
$err = array(); |
| 139 |
|
| 140 |
if ( ! defined( 'WP_CACHE' ) || true !== WP_CACHE ) { |
| 141 |
/* translators: %s: WP_CACHE definition*/ |
| 142 |
$err['wp_cache'] = sprintf( __( '<code>%s</code> is not found in wp-config.php.', 'powered-cache' ), 'define("WP_CACHE", true);' ); |
| 143 |
} |
| 144 |
|
| 145 |
if ( defined( 'WP_CACHE' ) && WP_CACHE && ( ! defined( 'POWERED_CACHE_PAGE_CACHING' ) || true !== POWERED_CACHE_PAGE_CACHING ) ) { |
| 146 |
/* translators: %s: advanced-cache.php drop-in path */ |
| 147 |
$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' ); |
| 148 |
} |
| 149 |
|
| 150 |
if ( defined( 'POWERED_CACHE_PAGE_CACHING_HAS_PROBLEM' ) && POWERED_CACHE_PAGE_CACHING_HAS_PROBLEM ) { |
| 151 |
/* translators: %s: page-cache.php drop-in path */ |
| 152 |
$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' ); |
| 153 |
} |
| 154 |
|
| 155 |
// everything ok |
| 156 |
if ( empty( $err ) ) { |
| 157 |
return; |
| 158 |
} |
| 159 |
|
| 160 |
// dont show when settings just saved |
| 161 |
if ( did_action( 'powered_cache_settings_saved' ) ) { |
| 162 |
return; |
| 163 |
} |
| 164 |
|
| 165 |
$capability = POWERED_CACHE_IS_NETWORK ? 'manage_network' : 'manage_options'; |
| 166 |
|
| 167 |
if ( ! current_user_can( $capability ) ) { |
| 168 |
return; |
| 169 |
} |
| 170 |
?> |
| 171 |
<div class="error"> |
| 172 |
<p> |
| 173 |
<strong><?php esc_html_e( 'Page Cache is not working, because:', 'powered-cache' ); ?></strong> |
| 174 |
</p> |
| 175 |
<?php foreach ( $err as $error_msg ) : ?> |
| 176 |
<p><?php echo wp_kses_post( $error_msg ); ?></p> |
| 177 |
<?php endforeach; ?> |
| 178 |
</div> |
| 179 |
<?php |
| 180 |
|
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
/** |
| 185 |
* Display object cache broken msg |
| 186 |
* |
| 187 |
* @since 1.0 |
| 188 |
*/ |
| 189 |
function maybe_display_object_cache_notices() { |
| 190 |
$settings = \PoweredCache\Utils\get_settings(); |
| 191 |
|
| 192 |
$object_cache_backends = get_object_cache_dropins(); |
| 193 |
$object_cache_driver = $settings['object_cache']; |
| 194 |
$object_cache_dropin = untrailingslashit( WP_CONTENT_DIR ) . '/object-cache.php'; |
| 195 |
|
| 196 |
if ( ! can_configure_object_cache() ) { |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
// dont show when settings just saved |
| 201 |
if ( did_action( 'powered_cache_settings_saved' ) ) { |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
if ( file_exists( $object_cache_dropin ) ) { |
| 206 |
$modify_time = filemtime( $object_cache_dropin ); |
| 207 |
if ( time() - absint( $modify_time ) < 10 ) { // just created |
| 208 |
return; |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
// first check object cache file exist |
| 213 |
if ( isset( $object_cache_backends[ $object_cache_driver ] ) && ! file_exists( $object_cache_dropin ) ) { |
| 214 |
/* translators: %s: object cache dropin path */ |
| 215 |
$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 ); |
| 216 |
?> |
| 217 |
<div class="error"> |
| 218 |
<p><strong><?php esc_html_e( 'Powered Cache:', 'powered-cache' ); ?></strong> |
| 219 |
<?php echo wp_kses_post( $message ); ?> |
| 220 |
</p> |
| 221 |
</div> |
| 222 |
<?php |
| 223 |
return; |
| 224 |
} |
| 225 |
|
| 226 |
if ( defined( 'POWERED_OBJECT_CACHE_HAS_PROBLEM' ) && POWERED_OBJECT_CACHE_HAS_PROBLEM ) { |
| 227 |
$broken_file = ''; |
| 228 |
|
| 229 |
if ( isset( $object_cache_backends[ $object_cache_driver ] ) ) { |
| 230 |
$broken_file = $object_cache_backends[ $object_cache_driver ]; |
| 231 |
} |
| 232 |
/* translators: %s: object cache dropin path */ |
| 233 |
$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 ); |
| 234 |
?> |
| 235 |
<div class="error"> |
| 236 |
<p><strong><?php esc_html_e( 'Powered Cache:', 'powered-cache' ); ?></strong> |
| 237 |
<?php echo wp_kses_post( $message ); ?> |
| 238 |
</p> |
| 239 |
</div> |
| 240 |
<?php |
| 241 |
} |
| 242 |
|
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Notices for the .htaccess |
| 247 |
* |
| 248 |
* @since 1.2 |
| 249 |
*/ |
| 250 |
function maybe_display_htaccess_notice() { |
| 251 |
global $is_apache; |
| 252 |
|
| 253 |
if ( ! $is_apache ) { |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
$settings = \PoweredCache\Utils\get_settings(); |
| 258 |
|
| 259 |
if ( ! $settings['auto_configure_htaccess'] ) { |
| 260 |
return; |
| 261 |
} |
| 262 |
|
| 263 |
if ( ! can_configure_htaccess() ) { |
| 264 |
return; |
| 265 |
} |
| 266 |
|
| 267 |
// dont show when settings just saved |
| 268 |
if ( did_action( 'powered_cache_settings_saved' ) ) { |
| 269 |
return; |
| 270 |
} |
| 271 |
|
| 272 |
$htaccess_file = get_home_path() . '.htaccess'; |
| 273 |
|
| 274 |
if ( file_exists( $htaccess_file ) ) { |
| 275 |
$modify_time = filemtime( $htaccess_file ); |
| 276 |
if ( time() - absint( $modify_time ) < 10 ) { // just modified |
| 277 |
return; |
| 278 |
} |
| 279 |
} |
| 280 |
|
| 281 |
$message = ''; |
| 282 |
|
| 283 |
if ( ! file_exists( $htaccess_file ) ) { |
| 284 |
$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' ); |
| 285 |
} elseif ( ! is_writeable( $htaccess_file ) ) { |
| 286 |
$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' ); |
| 287 |
} |
| 288 |
|
| 289 |
if ( empty( $message ) ) { |
| 290 |
return; |
| 291 |
} |
| 292 |
|
| 293 |
?> |
| 294 |
|
| 295 |
<div class="error"> |
| 296 |
<p><strong><?php esc_html_e( 'Powered Cache:', 'powered-cache' ); ?></strong> |
| 297 |
<?php echo wp_kses_post( $message ); ?> |
| 298 |
</p> |
| 299 |
</div> |
| 300 |
|
| 301 |
<?php |
| 302 |
} |
| 303 |
|