| 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 |
* |
| 74 |
* @return array $object_caches |
| 75 |
*/ |
| 76 |
public static function object_cache_dropins() { |
| 77 |
|
| 78 |
$object_caches = array( |
| 79 |
'memcache' => POWERED_CACHE_DROPIN_DIR . 'memcache-object-cache.php', |
| 80 |
'memcached' => POWERED_CACHE_DROPIN_DIR . 'memcached-object-cache.php', |
| 81 |
'redis' => POWERED_CACHE_DROPIN_DIR . 'redis-object-cache.php', |
| 82 |
); |
| 83 |
|
| 84 |
return apply_filters( 'powered_cache_object_cache_dropins', $object_caches ); |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
/** |
| 89 |
* Get available object cache backends |
| 90 |
* |
| 91 |
* @since 1.0 |
| 92 |
* @return array |
| 93 |
*/ |
| 94 |
public static function available_object_caches() { |
| 95 |
$object_cache_methods = self::object_cache_dropins(); |
| 96 |
|
| 97 |
if ( ! class_exists( 'Memcache' ) ) { |
| 98 |
unset( $object_cache_methods['memcache'] ); |
| 99 |
} |
| 100 |
|
| 101 |
if ( ! class_exists( 'Memcached' ) ) { |
| 102 |
unset( $object_cache_methods['memcached'] ); |
| 103 |
} |
| 104 |
|
| 105 |
if ( ! class_exists( 'Redis' ) ) { |
| 106 |
unset( $object_cache_methods['redis'] ); |
| 107 |
} |
| 108 |
|
| 109 |
return array_keys( $object_cache_methods ); |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
/** |
| 116 |
* Generates button for given plugin |
| 117 |
* |
| 118 |
* @since 1.0 |
| 119 |
* |
| 120 |
* @param string $plugin_id unique plugin identity |
| 121 |
* @return string html output |
| 122 |
*/ |
| 123 |
public static function plugin_button( $plugin_id ) { |
| 124 |
|
| 125 |
if ( true === Powered_Cache_Extensions::factory()->is_active( $plugin_id ) ) { |
| 126 |
$action = 'deactivate'; |
| 127 |
$button = __( 'Dectivate', 'powered-cache' ); |
| 128 |
} else { |
| 129 |
$action = 'activate'; |
| 130 |
$button = __( 'Activate', 'powered-cache' ); |
| 131 |
} |
| 132 |
|
| 133 |
$url = add_query_arg( array( |
| 134 |
'page' => esc_attr( 'powered-cache' ), |
| 135 |
'section' => 'extensions', |
| 136 |
'extension' => $plugin_id, |
| 137 |
'status' => $action, |
| 138 |
'wp_http_referer' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), |
| 139 |
'action' => 'powered_cache_update_settings', |
| 140 |
'powered_cache_settings_nonce' => wp_create_nonce( 'powered_cache_update_settings' ), |
| 141 |
), admin_url( 'admin.php' ) ); |
| 142 |
|
| 143 |
$html = '<a href="' . esc_url( $url ) . '" class="button button-primary" >' . $button . '</a>'; |
| 144 |
|
| 145 |
return $html; |
| 146 |
} |
| 147 |
|
| 148 |
public static function upgrade_button(){ |
| 149 |
$url = 'https://poweredcache.com/'; |
| 150 |
$html = '<a href="' . esc_url( $url ) . '" class="upgrade-now" >' . __('Upgrade Now','powered-cache') . '</a>'; |
| 151 |
|
| 152 |
return $html; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Generates cache flush button |
| 157 |
* |
| 158 |
* @since 1.0 |
| 159 |
* @since 1.1 $url changed to admin-post.php |
| 160 |
* @return string |
| 161 |
*/ |
| 162 |
public static function flush_cache_button() { |
| 163 |
$url = wp_nonce_url( admin_url( 'admin-post.php?action=powered_cache_purge_all_cache' ), 'powered_cache_purge_all_cache' ); |
| 164 |
$html = '<a href="' . esc_url( $url ) . '" class="button" >' . esc_html__( 'Clear Cache', 'powered-cache' ) . '</a>'; |
| 165 |
|
| 166 |
return $html; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Generates settings export button |
| 171 |
* |
| 172 |
* @since 1.0 |
| 173 |
* |
| 174 |
* @return string |
| 175 |
*/ |
| 176 |
public static function export_settings_button(){ |
| 177 |
$url = add_query_arg( array( |
| 178 |
'page' => esc_attr( 'powered-cache' ), |
| 179 |
'section' => 'misc', |
| 180 |
'action' => 'export_settings', |
| 181 |
'wp_http_referer' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), |
| 182 |
'powered_cache_settings_nonce' => wp_create_nonce( 'powered_cache_update_settings' ), |
| 183 |
), admin_url( 'admin.php' ) ); |
| 184 |
|
| 185 |
$html = '<a href="' . esc_url( $url ) . '" class="button" >' . esc_html__( 'Download Settings', 'powered-cache' ) . '</a>'; |
| 186 |
|
| 187 |
return $html; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
/** |
| 192 |
* Generates settings reset button |
| 193 |
* |
| 194 |
* @since 1.0 |
| 195 |
* |
| 196 |
* @return string |
| 197 |
*/ |
| 198 |
public static function reset_settings_button(){ |
| 199 |
$url = add_query_arg( array( |
| 200 |
'page' => esc_attr( 'powered-cache' ), |
| 201 |
'section' => 'misc', |
| 202 |
'action' => 'reset_settings', |
| 203 |
'wp_http_referer' => urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), |
| 204 |
'powered_cache_settings_nonce' => wp_create_nonce( 'powered_cache_update_settings' ), |
| 205 |
), admin_url( 'admin.php' ) ); |
| 206 |
|
| 207 |
$html = '<a href="' . esc_url( $url ) . '" class="button" >' . esc_html__( 'Reset Settings', 'powered-cache' ) . '</a>'; |
| 208 |
|
| 209 |
return $html; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
/** |
| 214 |
* Check capability and nonce during admin action |
| 215 |
* |
| 216 |
* @since 1.0 |
| 217 |
* @param $cap string capability |
| 218 |
*/ |
| 219 |
public static function check_cap_and_nonce( $cap ) { |
| 220 |
if ( isset( $_REQUEST['action'] ) |
| 221 |
&& ( ! current_user_can( $cap ) || empty( $_REQUEST['powered_cache_settings_nonce'] ) |
| 222 |
|| ! wp_verify_nonce( $_REQUEST['powered_cache_settings_nonce'], 'powered_cache_update_settings' ) ) |
| 223 |
) { |
| 224 |
wp_die( esc_html__( 'Cheatin, uh?', 'powered-cache' ) ); |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Get available zones |
| 230 |
* |
| 231 |
* @since 1.0 |
| 232 |
* |
| 233 |
* @return mixed|void |
| 234 |
*/ |
| 235 |
public static function cdn_zones() { |
| 236 |
$zones = array( |
| 237 |
'all' => __( 'All files', 'powered-cache' ), |
| 238 |
'image' => __( 'Images', 'powered-cache' ), |
| 239 |
'js' => __( 'JavaScript', 'powered-cache' ), |
| 240 |
'css' => __( 'CSS', 'powered-cache' ), |
| 241 |
); |
| 242 |
|
| 243 |
return apply_filters( 'powered_cache_admin_cdn_zones', $zones ); |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
/** |
| 248 |
* Template loader of settings page |
| 249 |
* actions fires before and after it can be handy when custom message needed |
| 250 |
* |
| 251 |
* @since 1.0 |
| 252 |
* |
| 253 |
* @param string $section tab of settings |
| 254 |
*/ |
| 255 |
public static function load_settings_template( $section ) { |
| 256 |
$page = POWERED_CACHE_ADMIN_DIR . 'settings/' . $section . '.php'; |
| 257 |
if ( file_exists( $page ) ) { |
| 258 |
do_action( 'powered_cache_before_load_settings_template_' . $section ); |
| 259 |
include $page; |
| 260 |
do_action( 'powered_cache_after_load_settings_template_' . $section ); |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
/** |
| 265 |
* convert minutes to possible time format |
| 266 |
* |
| 267 |
* @param int $timeout_in_minutes |
| 268 |
* |
| 269 |
* @since 1.1 |
| 270 |
* @return array |
| 271 |
*/ |
| 272 |
public static function get_timeout_interval( $timeout_in_minutes ) { |
| 273 |
$cache_timeout = $timeout_in_minutes; |
| 274 |
$selected_interval = 'MINUTE'; |
| 275 |
|
| 276 |
if ( $cache_timeout > 0 ) { |
| 277 |
if ( 0 === (int) ( $cache_timeout % 1440 ) ) { |
| 278 |
$cache_timeout = $cache_timeout / 1440; |
| 279 |
$selected_interval = 'DAY'; |
| 280 |
} elseif ( 0 === (int) ( $cache_timeout % 60 ) ) { |
| 281 |
$cache_timeout = $cache_timeout / 60; |
| 282 |
$selected_interval = 'HOUR'; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
return array( |
| 287 |
$cache_timeout, |
| 288 |
$selected_interval, |
| 289 |
); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|