-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-config.php
345 lines (320 loc) · 7.53 KB
/
class-config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
<?php
/**
* @author naxvog <[email protected]>
* @version 1.0.0
*/
namespace WPR_Redis;
defined( '\\ABSPATH' ) || exit;
class Config {
/**
* @var string Configuration path
*/
const PATH = '/wpr-redis-config/config.php';
/**
* @var string Action nonce
*/
const NONCE = 'wpr-redis-actions';
/**
* @var string Config page slug
*/
const SETTINGS_SLUG = 'wpr-redis';
/**
* @var string|null Option page hook suffix
*/
private $options_page = null;
/**
* Constructor
*
* @since 1.0.0
*/
public function __construct() {
add_action( 'admin_menu', [ $this, 'add_menu' ] );
}
/**
* Returns the settings page url
*
* @since 1.0.0
* @return string
*/
public static function url() {
$url = add_query_arg(
[
'page' => self::SETTINGS_SLUG,
],
admin_url( 'options-general.php' )
);
return $url;
}
/**
* Adds a plugin action link to the settings page
*
* @since 1.0.0
* @param string[]
* @return string[]
*/
public function add_settings_link( $links ) {
$url = self::url();
$link = "<a href=\"{$url}\">" . __( 'Settings', 'wpr-redis' ) . '</a>';
array_unshift( $links, $link );
return $links;
}
/**
* Adds the options page to the menu and registers all necessary hooks
*
* @since 1.0.0
* @return void
*/
public function add_menu() {
$this->options_page = add_options_page(
__( 'WP Rocket Redis Settings', 'wpr-redis' ),
__( 'WP Rocket Redis' ),
apply_filters( 'wpr_redis_config_capability', 'manage_options' ),
self::SETTINGS_SLUG,
[ $this, 'page_template' ]
);
if ( $this->options_page ) {
add_filter(
'plugin_action_links_' . WPR_REDIS_BASENAME,
[ $this, 'add_settings_link' ]
);
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_resources' ] );
add_action( 'admin_init', [ $this, 'register_settings' ], 10 );
add_action( 'admin_init', [ $this, 'settings_fields' ], 11 );
add_action( 'current_screen', [ $this, 'process_actions' ] );
}
}
/**
* Enqueues all necessary resources
*
* @since 1.0.0
* @return void
*/
public function enqueue_resources() {
$handle = 'wpr-redis-admin-style';
wp_register_style(
$handle,
WPR_REDIS_URL . 'assets/css/admin.css',
[],
WPR_Redis::meta( 'Version' )
);
wp_enqueue_style( $handle );
}
/**
* Adds all setting fields
*
* @since 1.0.0
* @return void
*/
public function settings_fields() {
$section = 'wpr_redis';
add_settings_section(
$section,
'Redis Connection Parameters',
[ $this, 'settings_section' ],
$this->options_page
);
foreach ( self::settings() as $name => $args ) {
add_settings_field(
$name,
"<label for=\"{$name}\">{$args['label']}</label>",
[ $this, 'settings_field' ],
$this->options_page,
$section,
array_merge( [ 'name' => $name ], $args )
);
}
}
/**
* Description for the connection parameters section
*
* @since 1.0.0
* @return void
*/
public function settings_section() {
_e( 'Setup your Redis connection', 'wpr-redis' );
}
/**
* Renders a settings field
*
* @since 1.0.0
* @param array $setting
* @return void
*/
public function settings_field( $setting ) {
$constant = strtoupper( "WPR_{$setting['name']}" );
$extra = '';
$type = 'integer' === $setting['setting'][0]
? 'number'
: 'text';
if ( 'number' === $type ) {
$extra .= ' min="0" step="1"';
}
if ( defined( $constant ) ) {
$value = constant( $constant );
$extra .= ' disabled';
} else {
$value = get_option( $setting['name'] );
}
?>
<input type="<?php echo esc_attr( $type ); ?>"
id="<?php echo esc_attr( $setting['name'] ); ?>"
name="<?php echo esc_attr( $setting['name'] ); ?>"
value="<?php echo esc_attr( $value ); ?>"
<?php echo trim( $extra ); ?>/>
<?php
}
/**
* Renders the page template
*
* @since 1.0.0
* @param bool $echo
* @return mixed
*/
public function page_template( $echo = true ) {
$path = WPR_REDIS_VIEW_PATH . '/admin-settings.php';
if ( false === $echo ) {
ob_start();
require $path;
return ob_get_clean();
}
return require $path;
}
/**
* Processes settings actions
*
* @since 1.0.0
* @param WP_Screen $screen
* @return void
*/
public function process_actions( $screen ) {
if ( false
|| $this->options_page !== $screen->id
|| ! isset( $_REQUEST['action'] )
|| ! check_admin_referer( self::NONCE )
|| ! current_user_can( apply_filters( 'wpr_redis_config_capability', 'manage_options' ) )
) {
return;
}
if ( 'integrate' === $_REQUEST['action'] ) {
rocket_generate_advanced_cache_file();
rocket_clean_cache_dir();
} elseif ( 'remove_integration' === $_REQUEST['action'] ) {
remove_filter(
'rocket_advanced_cache_file',
[ WPR_Redis::class, 'maybe_alter_adv_cache' ]
);
rocket_generate_advanced_cache_file();
}
wp_safe_redirect( remove_query_arg( 'action' ) );
exit;
}
/**
* Returns all defined settings
*
* @since 1.0.0
* @return array
*/
public static function settings() {
$settings = [
'wpr_redis_scheme' => [
'label' => __( 'Scheme', 'wpr-redis' ),
'setting' => [ 'string', 'sanitize_text_field', null ],
],
'wpr_redis_host' => [
'label' => __( 'Host', 'wpr-redis' ),
'setting' => [ 'string', 'sanitize_text_field', 'localhost' ],
],
'wpr_redis_port' => [
'label' => __( 'Port', 'wpr-redis' ),
'setting' => [ 'integer', 'sanitize_text_field', 6379 ],
],
'wpr_redis_db' => [
'label' => __( 'Database', 'wpr-redis' ),
'setting' => [ 'integer', 'sanitize_text_field', 0 ],
],
'wpr_redis_pwd' => [
'label' => __( 'Password', 'wpr-redis' ),
'setting' => [ 'string', 'sanitize_text_field', null ],
],
];
return $settings;
}
/**
* Registers all settings fields
*
* @since 1.0.0
* @return void
*/
public function register_settings() {
$group = 'wpr_redis';
foreach ( self::settings() as $name => $args ) {
list( $type, $sanitize_cb, $default ) = $args['setting'];
register_setting(
$group,
$name,
[
'type' => $type,
'sanitize_callback' => $sanitize_cb,
'default' => $default,
]
);
add_action( "update_option_$name", [ $this, 'trigger_save' ] );
}
}
/**
* Triggers a config save on shutdown if a setting was modified
*
* @since 1.0.0
* @return void
*/
public function trigger_save() {
static $once;
if ( ! isset( $once ) ) {
$once = true;
add_action( 'shutdown', [ Config::class, 'save' ] );
}
}
/**
* Returns the full config file path
*
* @since 1.0.0
* @return string
*/
public static function path() {
return WP_CONTENT_DIR . self::PATH;
}
/**
* Retrieves the config path
*
* @since 1.0.0
* @param string|null $path (Optional) Configuration path.
* @return mixed|null
*/
public static function get( $path = null ) {
$path = $path ?? self::path();
if ( is_readable( $path ) ) {
return require $path;
}
return null;
}
/**
* Saves the configuration to its own file using the config template
*
* @since 1.0.0
*/
public static function save() {
$keys = array_keys( self::settings() );
$repl = [];
foreach ( $keys as $key ) {
$repl[ "###$key###" ] = get_option( $key );
}
$filesystem = rocket_direct_filesystem();
$tpl_path = WPR_REDIS_INCLUDE_PATH . '/config-template.php';
$template = $filesystem->get_contents( $tpl_path );
$contents = strtr( $template, $repl );
$path = self::path();
wp_mkdir_p( dirname( $path ) );
$filesystem->put_contents( $path, $contents );
rocket_generate_advanced_cache_file();
}
} // Config