PluginProbe
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score / 1.2.8
Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score v1.2.8
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 / class-powered-cache-admin.php

class-powered-cache-admin.php in Powered Cache – Caching and Optimization for WordPress – Easily Improve PageSpeed & Web Vitals Score 1.2.8, at includes/admin/class-powered-cache-admin.php

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