| 1 |
<?php |
| 2 |
// Exit if accessed directly |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
|
| 8 |
class Powered_Cache_Admin { |
| 9 |
|
| 10 |
/** |
| 11 |
* Change by using powered_cache_cap filter |
| 12 |
* |
| 13 |
* @var string |
| 14 |
*/ |
| 15 |
public $capability = 'manage_options'; |
| 16 |
|
| 17 |
/** |
| 18 |
* @var string $slug used for settings page, menu etc |
| 19 |
* @since 1.0 |
| 20 |
*/ |
| 21 |
public $slug = 'powered-cache'; |
| 22 |
|
| 23 |
function __construct() { } |
| 24 |
|
| 25 |
/** |
| 26 |
* Get things started |
| 27 |
* |
| 28 |
* @since 1.0 |
| 29 |
*/ |
| 30 |
function setup() { |
| 31 |
$this->capability = apply_filters( 'powered_cache_cap', $this->capability ); |
| 32 |
|
| 33 |
add_action( 'admin_menu', array( $this, 'admin_menu' ) ); |
| 34 |
add_action( 'admin_bar_menu', array( $this, 'admin_bar_menu' ), 999 ); |
| 35 |
add_filter( 'plugin_action_links_' . plugin_basename( POWERED_CACHE_PLUGIN_FILE ), array( $this, 'action_links' ) ); |
| 36 |
add_action( 'load-toplevel_page_powered-cache', array( $this, 'update_options' ) ); |
| 37 |
add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) ); |
| 38 |
add_action( 'admin_post_deactivate_plugin', array( $this, 'deactivate_plugin' ) ); |
| 39 |
add_action( 'admin_post_powered_cache_download_rewrite_settings', array( $this, 'download_rewrite_config' ) ); |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* Fires when clicking buttons from admin ui. `update_options` doesn't mean setup db things all the time. |
| 46 |
* |
| 47 |
* @since 1.0 |
| 48 |
*/ |
| 49 |
public function update_options() { |
| 50 |
|
| 51 |
Powered_Cache_Admin_Helper::check_cap_and_nonce( $this->capability ); |
| 52 |
|
| 53 |
if ( ! empty( $_REQUEST['action'] ) ) { |
| 54 |
|
| 55 |
$action = apply_filters( 'powered_cache_update_options', $_REQUEST['action'] ); |
| 56 |
|
| 57 |
switch ( $action ) { |
| 58 |
case 'powered_cache_update_settings': |
| 59 |
Powered_Cache_Admin_Actions::update_settings(); |
| 60 |
break; |
| 61 |
case 'reset_settings': |
| 62 |
Powered_Cache_Admin_Actions::reset_settings(); |
| 63 |
break; |
| 64 |
case 'export_settings': |
| 65 |
Powered_Cache_Admin_Actions::export_settings(); |
| 66 |
break; |
| 67 |
} |
| 68 |
|
| 69 |
do_action( 'powered_cache_update_options', $action ); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
/** |
| 77 |
* Register assets for settings page |
| 78 |
* |
| 79 |
* @since 1.0 |
| 80 |
* @param $hook |
| 81 |
*/ |
| 82 |
public function load_scripts( $hook ) { |
| 83 |
global $powered_cache_settings_page; |
| 84 |
|
| 85 |
wp_enqueue_style( 'powered-cache-admin', plugins_url( '/assets/css/admin.css', POWERED_CACHE_PLUGIN_FILE ), array(), POWERED_CACHE_PLUGIN_VERSION ); |
| 86 |
|
| 87 |
if ( $hook != $powered_cache_settings_page ) { |
| 88 |
return; |
| 89 |
} |
| 90 |
|
| 91 |
wp_enqueue_script( 'powered-cache-admin', plugins_url( '/assets/js/admin.js', POWERED_CACHE_PLUGIN_FILE ), array( 'jquery' ), POWERED_CACHE_PLUGIN_VERSION ); |
| 92 |
wp_localize_script( 'powered-cache-admin', 'powered_cache_vars', array( |
| 93 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 94 |
'nonce' => wp_create_nonce( 'powered-cache-ajax-nonce' ), |
| 95 |
) ); |
| 96 |
|
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
/** |
| 101 |
* Set up settings page |
| 102 |
* |
| 103 |
* @since 1.0 |
| 104 |
*/ |
| 105 |
public function settings_page() { |
| 106 |
// load settings template |
| 107 |
require_once POWERED_CACHE_ADMIN_DIR . 'settings.php'; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Adds admin menu item |
| 112 |
* |
| 113 |
* @since 1.0 |
| 114 |
*/ |
| 115 |
public function admin_menu() { |
| 116 |
global $powered_cache_settings_page; |
| 117 |
$powered_cache_settings_page = add_menu_page( __( 'Powered Cache Settings', 'powered-cache' ), __( 'Powered Cache', 'powered-cache' ), $this->capability, $this->slug, array( $this, 'settings_page' ), |
| 118 |
'data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiA/PjwhRE9DVFlQRSBzdmcgIFBVQkxJQyAnLS8vVzNDLy9EVEQgU1ZHIDEuMS8vRU4nICAnaHR0cDovL3d3dy53My5vcmcvR3JhcGhpY3MvU1ZHLzEuMS9EVEQvc3ZnMTEuZHRkJz48c3ZnIGhlaWdodD0iMzJweCIgaWQ9IkxheWVyXzEiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDMyIDMyOyIgdmVyc2lvbj0iMS4xIiB2aWV3Qm94PSIwIDAgMzIgMzIiIHdpZHRoPSIzMnB4IiB4bWw6c3BhY2U9InByZXNlcnZlIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHhtbG5zOnhsaW5rPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5L3hsaW5rIj48ZyB0cmFuc2Zvcm09InRyYW5zbGF0ZSgxNDQgMzM2KSI+PHBhdGggZD0iTS0xMTcuMTc2LTMzNC4wNjNoLTkuMzUzTC0xMzguMTExLTMyMGg5LjMwNGwtMTEuNzQ1LDE0LjA2M2wyNS4xMDUtMTguMzE2aC05LjgwOUwtMTE3LjE3Ni0zMzQuMDYzeiIvPjwvZz48L3N2Zz4=' ); |
| 119 |
|
| 120 |
/** |
| 121 |
* Different name submenu item, url point same address with parent. |
| 122 |
*/ |
| 123 |
add_submenu_page( $this->slug, __( 'Powered Cache Settings', 'powered-cache' ), __( 'Settings', 'powered-cache' ), $this->capability, $this->slug ); |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* Adds admin bar menu |
| 128 |
* @since 1.0 |
| 129 |
*/ |
| 130 |
public function admin_bar_menu( $wp_admin_bar ) { |
| 131 |
if ( current_user_can( $this->capability ) ) { |
| 132 |
$wp_admin_bar->add_menu( array( |
| 133 |
'id' => $this->slug, |
| 134 |
'title' => __('Powered Cache','powered-cache'), |
| 135 |
'href' => admin_url( 'admin.php?page=powered-cache' ), |
| 136 |
)); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Adds settings link to plugin actions |
| 142 |
* |
| 143 |
* @since 1.0 |
| 144 |
* @param array $actions |
| 145 |
* @return array |
| 146 |
*/ |
| 147 |
function action_links( $actions ) { |
| 148 |
|
| 149 |
$actions['powered_settings'] = sprintf( __( '<a href="%s">Settings</a>', 'powered-cache' ), esc_url( admin_url( 'admin.php?page=powered-cache' ) ) ); |
| 150 |
if ( ! powered_cache_is_premium() ) { |
| 151 |
$actions['get_premium'] = sprintf( __( '<a href="%s" style="color: red;">Get Premium</a>', 'powered-cache' ), esc_url( 'https://poweredcache.com' ) ); |
| 152 |
} |
| 153 |
|
| 154 |
return array_reverse( $actions ); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Deactivate incompatible plugins |
| 159 |
* |
| 160 |
* @since 1.0 |
| 161 |
*/ |
| 162 |
function deactivate_plugin() |
| 163 |
{ |
| 164 |
if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'deactivate_plugin' ) ) { |
| 165 |
wp_nonce_ays( '' ); |
| 166 |
} |
| 167 |
|
| 168 |
deactivate_plugins( $_GET['plugin'] ); |
| 169 |
|
| 170 |
wp_safe_redirect( wp_get_referer() ); |
| 171 |
die(); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
/** |
| 176 |
* Downloads proper configuration file |
| 177 |
* |
| 178 |
* @since 1.1 |
| 179 |
*/ |
| 180 |
public function download_rewrite_config() { |
| 181 |
if ( ! wp_verify_nonce( $_GET['_wpnonce'], 'powered_cache_download_rewrite' ) ) { |
| 182 |
wp_nonce_ays( '' ); |
| 183 |
} |
| 184 |
|
| 185 |
$server = $_GET['server']; |
| 186 |
Powered_Cache_Config::factory()->download_rewrite_rules( $server ); |
| 187 |
|
| 188 |
wp_safe_redirect( wp_get_referer() ); |
| 189 |
die(); |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
/** |
| 194 |
* Return an instance of the current class |
| 195 |
* |
| 196 |
* @since 1.0 |
| 197 |
* @return Powered_Cache_Admin |
| 198 |
*/ |
| 199 |
public static function factory() { |
| 200 |
static $instance = false; |
| 201 |
|
| 202 |
if ( ! $instance ) { |
| 203 |
$instance = new self(); |
| 204 |
$instance->setup(); |
| 205 |
} |
| 206 |
|
| 207 |
return $instance; |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|