| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
class Powered_Cache_Admin_Helper { |
| 8 |
/** |
| 9 |
* Register admin sections |
| 10 |
* |
| 11 |
* @since 1.0 |
| 12 |
* |
| 13 |
* @return mixed|void |
| 14 |
*/ |
| 15 |
public static function admin_sections(){ |
| 16 |
$sections = array( |
| 17 |
'basic-options' => __( 'Basic Options', 'powered-cache' ), |
| 18 |
'advanced-options' => __( 'Advanced Options', 'powered-cache' ), |
| 19 |
'cdn' => __( 'CDN', 'powered-cache' ), |
| 20 |
'extensions' => __( 'Extensions', 'powered-cache' ), |
| 21 |
'misc' => __( 'Misc', 'powered-cache' ), |
| 22 |
'premium' => __( 'Go Premium', 'powered-cache' ), |
| 23 |
'support' => __( 'Support', 'powered-cache' ), |
| 24 |
); |
| 25 |
|
| 26 |
if ( is_multisite() && ! current_user_can( 'manage_network' ) ) { |
| 27 |
unset( $sections['premium'] ); |
| 28 |
unset( $sections['support'] ); |
| 29 |
} |
| 30 |
|
| 31 |
return apply_filters( 'powered_cache_admin_settings_sections', $sections ); |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* Set flash message for admin page |
| 37 |
* |
| 38 |
* @since 1.0 |
| 39 |
* |
| 40 |
* @param $msg string |
| 41 |
* @param $class string |
| 42 |
*/ |
| 43 |
public static function set_flash_message( $msg, $class = 'updated' ) { |
| 44 |
if ( ! empty( $msg ) ) { |
| 45 |
$message_data = array( 'message' => $msg, 'class' => $class ); |
| 46 |
set_site_transient( 'powered_cache_flash_msg', $message_data, 30 ); |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Echo flash message and destroy |
| 52 |
* |
| 53 |
* @since 1.0 |
| 54 |
*/ |
| 55 |
public static function get_flash_message() { |
| 56 |
$flash_message = get_site_transient( 'powered_cache_flash_msg' ); |
| 57 |
|
| 58 |
if ( $flash_message && is_array( $flash_message ) ) { |
| 59 |
$html = '<div id="setting-error-settings_updated" class="' . esc_attr( $flash_message['class'] ) . ' notice is-dismissible"> |
| 60 |
<p><strong>' . esc_attr( $flash_message['message'] ) . '</strong></p> |
| 61 |
<button type="button" class="notice-dismiss"><span class="screen-reader-text">' . __( 'Dismiss this notice', 'powered-cache' ) . '</span></button> |
| 62 |
</div>'; |
| 63 |
echo $html; |
| 64 |
//destroy transient |
| 65 |
delete_site_transient( 'powered_cache_flash_msg' ); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Object cache methods keys will use as option |
| 71 |
* |
| 72 |
* @since 1.0 |
| 73 |
* @since 1.2 apcu added |
| 74 |
* |
| 75 |
* @return array $object_caches |
| 76 |
*/ |
| 77 |
public static function object_cache_dropins() { |
| 78 |
|
| 79 |
$object_caches = array( |
| 80 |
'memcache' => POWERED_CACHE_DROPIN_DIR . 'memcache-object-cache.php', |
| 81 |
'memcached' => POWERED_CACHE_DROPIN_DIR . 'memcached-object-cache.php', |
| 82 |
'redis' => POWERED_CACHE_DROPIN_DIR . 'redis-object-cache.php', |
| 83 |
'apcu' => POWERED_CACHE_DROPIN_DIR . 'apcu-object-cache.php', |
| 84 |
); |
| 85 |
|
| 86 |
return apply_filters( 'powered_cache_object_cache_dropins', $object_caches ); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
/** |
| 91 |
* Get available object cache backends |
| 92 |
* |
| 93 |
* @since 1.0 |
| 94 |
* @since 1.2 unset apcu |
| 95 |
* @return array |
| 96 |
*/ |
| 97 |
public static function available_object_caches() { |
| 98 |
$object_cache_methods = self::object_cache_dropins(); |
| 99 |
|
| 100 |
if ( ! class_exists( 'Memcache' ) ) { |
| 101 |
unset( $object_cache_methods['memcache'] ); |
| 102 |
} |
| 103 |
|
| 104 |
if ( ! class_exists( 'Memcached' ) ) { |
| 105 |
unset( $object_cache_methods['memcached'] ); |
| 106 |
} |
| 107 |
|
| 108 |
if ( ! class_exists( 'Redis' ) ) { |
| 109 |
unset( $object_cache_methods['redis'] ); |
| 110 |
} |
| 111 |
|
| 112 |
if ( ! function_exists( 'apcu_add' ) ) { |
| 113 |
unset( $object_cache_methods['apcu'] ); |
| 114 |
} |
| 115 |
|
| 116 |
return array_keys( $object_cache_methods ); |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
/** |
| 123 |
* Generates button for given plugin |
| 124 |
* |
| 125 |
* @since 1.0 |
| 126 |
* |
| 127 |
* @param string $plugin_id unique plugin identity |
| 128 |
* @return string html output |
| 129 |
*/ |
| 130 |
public static function plugin_button( $plugin_id ) { |
| 131 |
|
| 132 |
if ( true === Powered_Cache_Extensions::factory()->is_active( $plugin_id ) ) { |
| 133 |
$action = 'deactivate'; |
| 134 |
$button = __( 'Dectivate', 'powered-cache' ); |
| 135 |
} else { |
| 136 |
$action = 'activate'; |
| 137 |
$button = __( 'Activate', 'powered-cache' ); |
| 138 |
} |
| 139 |
|
| 140 |
$url = add_query_arg( array( |
| 141 |
'page' => esc_attr( 'powered-cache' ), |
| 142 |
'section' => 'extensions', |
| 143 |
'extension' => $plugin_id, |
| 144 |
'status' => $action, |
| 145 |
'wp_http_referer' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), |
| 146 |
'action' => 'powered_cache_update_settings', |
| 147 |
'powered_cache_settings_nonce' => wp_create_nonce( 'powered_cache_update_settings' ), |
| 148 |
), admin_url( 'admin.php' ) ); |
| 149 |
|
| 150 |
$html = '<a href="' . esc_url( $url ) . '" class="button button-primary" >' . $button . '</a>'; |
| 151 |
|
| 152 |
return $html; |
| 153 |
} |
| 154 |
|
| 155 |
public static function upgrade_button(){ |
| 156 |
$url = 'https://poweredcache.com/'; |
| 157 |
$html = '<a href="' . esc_url( $url ) . '" class="upgrade-now" >' . __('Upgrade Now','powered-cache') . '</a>'; |
| 158 |
|
| 159 |
return $html; |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Generates cache flush button |
| 164 |
* |
| 165 |
* @since 1.0 |
| 166 |
* @since 1.1 $url changed to admin-post.php |
| 167 |
* @return string |
| 168 |
*/ |
| 169 |
public static function flush_cache_button() { |
| 170 |
$url = wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_all_cache' ), 'powered_cache_purge_all_cache' ); |
| 171 |
$html = '<a href="' . esc_url( $url ) . '" class="button" >' . esc_html__( 'Clear Cache', 'powered-cache' ) . '</a>'; |
| 172 |
|
| 173 |
return $html; |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* Generates settings export button |
| 178 |
* |
| 179 |
* @since 1.0 |
| 180 |
* |
| 181 |
* @return string |
| 182 |
*/ |
| 183 |
public static function export_settings_button(){ |
| 184 |
$url = add_query_arg( array( |
| 185 |
'page' => esc_attr( 'powered-cache' ), |
| 186 |
'section' => 'misc', |
| 187 |
'action' => 'export_settings', |
| 188 |
'wp_http_referer' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), |
| 189 |
'powered_cache_settings_nonce' => wp_create_nonce( 'powered_cache_update_settings' ), |
| 190 |
), admin_url( 'admin.php' ) ); |
| 191 |
|
| 192 |
$html = '<a href="' . esc_url( $url ) . '" class="button" >' . esc_html__( 'Download Settings', 'powered-cache' ) . '</a>'; |
| 193 |
|
| 194 |
return $html; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
/** |
| 199 |
* Generates settings reset button |
| 200 |
* |
| 201 |
* @since 1.0 |
| 202 |
* |
| 203 |
* @return string |
| 204 |
*/ |
| 205 |
public static function reset_settings_button(){ |
| 206 |
$url = add_query_arg( array( |
| 207 |
'page' => esc_attr( 'powered-cache' ), |
| 208 |
'section' => 'misc', |
| 209 |
'action' => 'reset_settings', |
| 210 |
'wp_http_referer' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), |
| 211 |
'powered_cache_settings_nonce' => wp_create_nonce( 'powered_cache_update_settings' ), |
| 212 |
), admin_url( 'admin.php' ) ); |
| 213 |
|
| 214 |
$html = '<a href="' . esc_url( $url ) . '" class="button" >' . esc_html__( 'Reset Settings', 'powered-cache' ) . '</a>'; |
| 215 |
|
| 216 |
return $html; |
| 217 |
} |
| 218 |
|
| 219 |
/** |
| 220 |
* Link for the diagnostic |
| 221 |
* |
| 222 |
* @since 1.2 |
| 223 |
* @return string |
| 224 |
*/ |
| 225 |
public static function diagnostic_button() { |
| 226 |
$url = add_query_arg( array( |
| 227 |
'page' => esc_attr( 'powered-cache' ), |
| 228 |
'section' => 'misc', |
| 229 |
'action' => 'run-diagnostic', |
| 230 |
'powered_cache_settings_nonce' => wp_create_nonce( 'powered_cache_update_settings' ), |
| 231 |
), admin_url( 'admin.php' ) ); |
| 232 |
|
| 233 |
$html = '<a href="' . esc_url( $url ) . '" class="button" >' . esc_html__( 'Run Diagnostic', 'powered-cache' ) . '</a>'; |
| 234 |
|
| 235 |
return $html; |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
/** |
| 240 |
* Check capability and nonce during admin action |
| 241 |
* |
| 242 |
* @since 1.0 |
| 243 |
* @param $cap string capability |
| 244 |
*/ |
| 245 |
public static function check_cap_and_nonce( $cap ) { |
| 246 |
if ( isset( $_REQUEST['action'] ) |
| 247 |
&& ( ! current_user_can( $cap ) || empty( $_REQUEST['powered_cache_settings_nonce'] ) |
| 248 |
|| ! wp_verify_nonce( $_REQUEST['powered_cache_settings_nonce'], 'powered_cache_update_settings' ) ) |
| 249 |
) { |
| 250 |
wp_die( esc_html__( 'Cheatin, uh?', 'powered-cache' ) ); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Get available zones |
| 256 |
* |
| 257 |
* @since 1.0 |
| 258 |
* |
| 259 |
* @return mixed|void |
| 260 |
*/ |
| 261 |
public static function cdn_zones() { |
| 262 |
$zones = array( |
| 263 |
'all' => __( 'All files', 'powered-cache' ), |
| 264 |
'image' => __( 'Images', 'powered-cache' ), |
| 265 |
'js' => __( 'JavaScript', 'powered-cache' ), |
| 266 |
'css' => __( 'CSS', 'powered-cache' ), |
| 267 |
); |
| 268 |
|
| 269 |
return apply_filters( 'powered_cache_admin_cdn_zones', $zones ); |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
/** |
| 274 |
* Template loader of settings page |
| 275 |
* actions fires before and after it can be handy when custom message needed |
| 276 |
* |
| 277 |
* @since 1.0 |
| 278 |
* |
| 279 |
* @param string $section tab of settings |
| 280 |
*/ |
| 281 |
public static function load_settings_template( $section ) { |
| 282 |
$page = POWERED_CACHE_ADMIN_DIR . 'settings/' . $section . '.php'; |
| 283 |
if ( file_exists( $page ) ) { |
| 284 |
do_action( 'powered_cache_before_load_settings_template_' . $section ); |
| 285 |
include $page; |
| 286 |
do_action( 'powered_cache_after_load_settings_template_' . $section ); |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* convert minutes to possible time format |
| 292 |
* |
| 293 |
* @param int $timeout_in_minutes |
| 294 |
* |
| 295 |
* @since 1.1 |
| 296 |
* @return array |
| 297 |
*/ |
| 298 |
public static function get_timeout_interval( $timeout_in_minutes ) { |
| 299 |
$cache_timeout = $timeout_in_minutes; |
| 300 |
$selected_interval = 'MINUTE'; |
| 301 |
|
| 302 |
if ( $cache_timeout > 0 ) { |
| 303 |
if ( 0 === (int) ( $cache_timeout % 1440 ) ) { |
| 304 |
$cache_timeout = $cache_timeout / 1440; |
| 305 |
$selected_interval = 'DAY'; |
| 306 |
} elseif ( 0 === (int) ( $cache_timeout % 60 ) ) { |
| 307 |
$cache_timeout = $cache_timeout / 60; |
| 308 |
$selected_interval = 'HOUR'; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
return array( |
| 313 |
$cache_timeout, |
| 314 |
$selected_interval, |
| 315 |
); |
| 316 |
} |
| 317 |
|
| 318 |
|
| 319 |
/** |
| 320 |
* run diagnostic checks |
| 321 |
* |
| 322 |
* @since 1.2 |
| 323 |
* @return array |
| 324 |
*/ |
| 325 |
public static function diagnostic_info() { |
| 326 |
global $is_apache; |
| 327 |
|
| 328 |
// hold the checks! HODORRR!!! |
| 329 |
$checks = array(); |
| 330 |
|
| 331 |
// check config file |
| 332 |
$config_file = Powered_Cache_Config::factory()->find_wp_config_file(); |
| 333 |
$config_file_status = is_writeable( $config_file ); |
| 334 |
|
| 335 |
if ( $config_file_status ) { |
| 336 |
$config_file_desc = __( 'wp-config.php is writable.', 'powered-cache' ); |
| 337 |
} else { |
| 338 |
$config_file_desc = sprintf( __( 'wp-config.php is not writable. Please make sure the file writable or you can manually define %s constant.', 'powered-cache' ), '<code>WP_CACHE</code>' ); |
| 339 |
} |
| 340 |
|
| 341 |
$checks['config'] = array( |
| 342 |
'status' => $config_file_status, |
| 343 |
'description' => $config_file_desc, |
| 344 |
); |
| 345 |
|
| 346 |
|
| 347 |
// check cache directory |
| 348 |
$cache_dir = powered_cache_get_cache_dir(); |
| 349 |
$cache_dir_status = false; |
| 350 |
if ( ! file_exists( $cache_dir ) ) { |
| 351 |
$cache_dir_desc = sprintf( __( 'Cache directory %s is not exist!', 'powered-cache' ), '<code>' . $cache_dir . '</code>' ); |
| 352 |
} elseif ( ! is_writeable( $cache_dir ) ) { |
| 353 |
$cache_dir_desc = sprintf( __( 'Cache directory %s is not writeable!', 'powered-cache' ), '<code>' . $cache_dir . '</code>' ); |
| 354 |
} else { |
| 355 |
$cache_dir_status = true; |
| 356 |
$cache_dir_desc = sprintf( __( 'Cache directory %s exist and writable!', 'powered-cache' ), '<code>' . $cache_dir . '</code>' ); |
| 357 |
} |
| 358 |
|
| 359 |
$checks['cache-dir'] = array( |
| 360 |
'status' => $cache_dir_status, |
| 361 |
'description' => $cache_dir_desc, |
| 362 |
); |
| 363 |
|
| 364 |
|
| 365 |
// check .htaccess file |
| 366 |
if ( $is_apache && powered_cache_get_option( 'configure_htaccess' ) ) { |
| 367 |
$htaccess_file = get_home_path() . '.htaccess'; |
| 368 |
$htaccess_file_status = false; |
| 369 |
if ( ! file_exists( $htaccess_file ) ) { |
| 370 |
$htaccess_file_desc = sprintf( __( '.htaccess file %s is not exist!', 'powered-cache' ), '<code>' . $htaccess_file . '</code>' ); |
| 371 |
} elseif ( ! is_writeable( $htaccess_file ) ) { |
| 372 |
$htaccess_file_desc = sprintf( __( '.htaccess file %s is not writeable!', 'powered-cache' ), '<code>' . $htaccess_file . '</code>' ); |
| 373 |
} else { |
| 374 |
$htaccess_file_status = true; |
| 375 |
$htaccess_file_desc = sprintf( __( '.htaccess file %s exist and writable!', 'powered-cache' ), '<code>' . $htaccess_file . '</code>' ); |
| 376 |
} |
| 377 |
|
| 378 |
$checks['htaccess'] = array( |
| 379 |
'status' => $htaccess_file_status, |
| 380 |
'description' => $htaccess_file_desc, |
| 381 |
); |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
// check page cache |
| 386 |
if ( powered_cache_get_option( 'enable_page_caching' ) ) { |
| 387 |
$advanced_cache_file = untrailingslashit( WP_CONTENT_DIR ) . '/advanced-cache.php'; |
| 388 |
$advanced_cache_file_status = false; |
| 389 |
if ( ! file_exists( $advanced_cache_file ) ) { |
| 390 |
$advanced_cache_file_desc = sprintf( __( 'Required file for the page caching %s is not exist!', 'powered-cache' ), '<code>' . $advanced_cache_file . '</code>' ); |
| 391 |
} elseif ( ! is_writeable( $advanced_cache_file ) ) { |
| 392 |
$advanced_cache_file_desc = sprintf( __( 'Required file for the page caching %s is not writeable!', 'powered-cache' ), '<code>' . $advanced_cache_file . '</code>' ); |
| 393 |
} else { |
| 394 |
$advanced_cache_file_status = true; |
| 395 |
$advanced_cache_file_desc = sprintf( __( 'Required file for the page caching %s exist and writable!', 'powered-cache' ), '<code>' . $advanced_cache_file . '</code>' ); |
| 396 |
} |
| 397 |
|
| 398 |
$checks['advanced-cache'] = array( |
| 399 |
'status' => $advanced_cache_file_status, |
| 400 |
'description' => $advanced_cache_file_desc, |
| 401 |
); |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
// check object cache |
| 406 |
if ( 'off' !== powered_cache_get_option( 'object_cache' ) ) { |
| 407 |
$object_cache_file = untrailingslashit( WP_CONTENT_DIR ) . '/object-cache.php'; |
| 408 |
$object_cache_file_status = false; |
| 409 |
if ( ! file_exists( $object_cache_file ) ) { |
| 410 |
$object_cache_file_desc = sprintf( __( 'Required file for the object caching %s is not exist!', 'powered-cache' ), '<code>' . $object_cache_file . '</code>' ); |
| 411 |
} elseif ( ! is_writeable( $object_cache_file ) ) { |
| 412 |
$object_cache_file_desc = sprintf( __( 'Required file for the object caching %s is not writeable!', 'powered-cache' ), '<code>' . $object_cache_file . '</code>' ); |
| 413 |
} else { |
| 414 |
$object_cache_file_status = true; |
| 415 |
$object_cache_file_desc = sprintf( __( 'Required file for the object caching %s exist and writable!', 'powered-cache' ), '<code>' . $object_cache_file . '</code>' ); |
| 416 |
} |
| 417 |
|
| 418 |
$checks['object-cache'] = array( |
| 419 |
'status' => $object_cache_file_status, |
| 420 |
'description' => $object_cache_file_desc, |
| 421 |
); |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
return $checks; |
| 426 |
} |
| 427 |
|
| 428 |
|
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|