-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.php
335 lines (284 loc) · 12 KB
/
admin.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
<?php
/**
* Administrative interface.
*/
class Assets_Pack_Admin {
/**
* @var Assets_Pack_Admin
*/
protected static $instance;
/**
* @var array
*/
protected $settings;
/**
* @var string
*/
protected $settings_page;
/**
* @return Assets_Pack_Admin
*/
public static function get_instance() {
if ( static::$instance === null ) {
static::$instance = new static();
}
return static::$instance;
}
/**
* Setup interface actions.
*/
public function setup() {
add_action( 'admin_menu', [$this, 'admin_menu'] );
add_action( 'admin_init', [$this, 'init_settings'] );
}
/**
* Options menu item.
*/
public function admin_menu() {
$this->settings_page = add_options_page(
__( 'Assets Pack', 'assets-pack' ),
__( 'Assets', 'assets-pack' ),
apply_filters( 'assets_settings_capability', 'manage_options' ),
'assets-settings',
[$this, 'settings_page']
);
}
/**
* Settings initialization.
*/
public function init_settings() {
$upload_dir = wp_upload_dir();
register_setting( 'assets_pack', 'assets_pack', [
'sanitize_callback' => [$this, 'validate_settings'],
'default' => [
'assets_dir' => $upload_dir['basedir'] . '/assets',
'assets_url' => $upload_dir['baseurl'] . '/assets',
'skip_js' => [],
'enable_js' => false,
'debug_js' => false,
'skip_css' => [],
'enable_css' => false,
'css_inline_url' => false,
'debug_css' => false,
],
] );
// Main options.
add_settings_section( 'main', '', '__return_false', $this->settings_page );
add_settings_field( 'assets_dir', __( 'Bundles directory', 'assets-pack' ), [$this, 'field_assets_store'], $this->settings_page, 'main' );
add_settings_field( 'assets_url', __( 'Bundles URL', 'assets-pack' ), [$this, 'field_assets_url'], $this->settings_page, 'main' );
// Javascript options.
add_settings_section( 'js', __( 'JavaScript', 'assets-pack' ), [$this, 'hr'], $this->settings_page );
add_settings_field( 'enable_js', '', [$this, 'field_enable_js'], $this->settings_page, 'js' );
add_settings_field( 'skip_js', __( 'Skip these scripts', 'assets-pack' ), [$this, 'field_skip_js'], $this->settings_page, 'js' );
add_settings_field( 'debug_js', __( 'Debug', 'assets-pack' ), [$this, 'field_debug_js'], $this->settings_page, 'js' );
// CSS options.
add_settings_section( 'css', __( 'CSS', 'assets-pack' ), [$this, 'hr'], $this->settings_page );
add_settings_field( 'enable_css', '', [$this, 'field_enable_css'], $this->settings_page, 'css' );
add_settings_field( 'skip_css', __( 'Skip these styles', 'assets-pack' ), [$this, 'field_skip_css'], $this->settings_page, 'css' );
add_settings_field( 'css_inline_url', __( 'Convert inline urls', 'assets-pack' ), [$this, 'field_css_inline_url'], $this->settings_page, 'css' );
add_settings_field( 'debug_css', __( 'Debug', 'assets-pack' ), [$this, 'field_debug_css'], $this->settings_page, 'css' );
}
/**
* Just a horizontal line.
* For sections separation.
*/
public function hr() {
echo '<hr>';
}
/**
* Settings page callback.
*/
public function settings_page() { ?>
<div class="wrap">
<h1><?php esc_html_e( 'Assets pack', 'assets-pack' ) ?></h1>
<form method="POST" action="options.php">
<?php settings_fields( 'assets_pack' ) ?>
<?php do_settings_sections( $this->settings_page ) ?>
<?php submit_button( __( 'Update settings', 'assets-pack' ) ) ?>
</form>
</div>
<?php }
public function field_skip_js() { ?>
<input type="text" name="assets_pack[skip_js]" value="<?= esc_attr( implode( ',', $this->get_setting( 'skip_js', [] ) ) ) ?>" size="64"/>
<p class="description">
<?php esc_html_e( 'Do not include these scripts into a bundle file. You should enter handle names separated by commas. You can get names from a .js.debug file.', 'assets-pack' ) ?>
</p>
<?php }
public function field_enable_js() { ?>
<label>
<input type="checkbox" name="assets_pack[enable_js]" value="true" <?php checked( $this->get_setting( 'enable_js' ) ) ?>/>
<?php esc_html_e( 'Combine javascripts into a bundle', 'assets-pack' ) ?>
</label>
<?php }
public function field_skip_css() { ?>
<input type="text" name="assets_pack[skip_css]" value="<?= esc_attr( implode( ',', $this->get_setting( 'skip_css', [] ) ) ) ?>" size="64"/>
<p class="description">
<?php esc_html_e( 'Do not include these styles to assets file. You should enter handle names separated by commas. You can get names from a .css.debug file.', 'assets-pack' ) ?>
</p>
<?php }
public function field_enable_css() { ?>
<label>
<input type="checkbox" name="assets_pack[enable_css]" value="true" <?php checked( $this->get_setting( 'enable_css' ) ) ?>/>
<?php esc_html_e( 'Combine styles into a bundle', 'assets-pack' ) ?>
</label>
<?php }
public function field_debug_js() { ?>
<label>
<input type="checkbox" name="assets_pack[debug_js]" value="true" <?php checked( $this->get_setting( 'debug_js') ) ?>/>
<?php esc_html_e( 'Create a debug file with script names which contains in a bundle', 'assets-pack' ) ?>
</label>
<?php }
public function field_debug_css() { ?>
<label>
<input type="checkbox" name="assets_pack[debug_css]" value="true" <?php checked( $this->get_setting( 'debug_css' ) ) ?>/>
<?php esc_html_e( 'Create a debug file with style names which contains in a bundle', 'assets-pack' ) ?>
</label>
<?php }
public function field_assets_store() { ?>
<input type="text" name="assets_pack[assets_dir]" value="<?= $this->get_setting( 'assets_dir' ) ?>" size="64"/>
<p>
<button name="clear" class="button"><?php esc_html_e( 'Clear cache', 'assets-pack' ) ?></button>
</p>
<?php }
public function field_assets_url() { ?>
<input type="text" name="assets_pack[assets_url]" value="<?= $this->get_setting( 'assets_url' ) ?>" size="64"/>
<?php }
public function field_css_inline_url() { ?>
<label>
<input type="checkbox" name="assets_pack[css_inline_url]" value="true" <?php checked( $this->get_setting( 'css_inline_url' ) ) ?>/>
<?php esc_html_e( 'Convert all url() values to local if it possible', 'assets-pack' ) ?>
<p class="description">
<?php esc_html_e( 'Always clear cache if this setting is changed.', 'assets-pack' ) ?>
</p>
</label>
<?php }
/**
* Validate and sanitize settings.
*
* @param array $settings
* @return array Sanitized settings.
*/
public function validate_settings( $settings ) {
if ( isset( $_POST['clear'] ) ) {
$this->clear_assets();
add_settings_error( 'assets_dir', 'success', __( 'Cached bundles has been removed.', 'assets-pack' ), 'updated' );
return $this->get_setting();
}
$settings['enable_js'] = !empty( $settings['enable_js'] );
$settings['debug_js'] = !empty( $settings['debug_js'] );
$settings['enable_css'] = !empty( $settings['enable_css'] );
$settings['debug_css'] = !empty( $settings['debug_css'] );
$settings['css_inline_url'] = !empty( $settings['css_inline_url'] );
if ( !$settings['enable_js'] && $settings['debug_js'] ) {
add_settings_error( 'debug_js', 'debug_js', __( 'Js debug is useless when js aggregation is disabled.', 'assets-pack' ) );
}
if ( !$settings['enable_css'] && $settings['debug_css'] ) {
add_settings_error( 'debug_css', 'debug_css', __( 'Css debug is useless when css aggregation is disabled.', 'assets-pack' ) );
}
$dir = $this->validate_assets_dir( $settings['assets_dir'] );
if ( $dir === false ) {
$settings['assets_dir'] = $this->get_setting( 'assets_dir' );
add_settings_error( 'assets_dir', 'error', __( 'Could not create directory or directory is not writeable.', 'assets-pack' ) );
} else {
$settings['assets_dir'] = $dir;
}
// TODO: needs to be proper url validating.
$settings['assets_url'] = rtrim( $settings['assets_url'], '/' ) . '/';
if ( is_string( $settings['skip_js'] ) ) {
$settings['skip_js'] = array_filter(
array_map( 'trim', explode( ',', $settings['skip_js'] ) )
);
}
if ( is_string( $settings['skip_css'] ) ) {
$settings['skip_css'] = array_filter(
array_map( 'trim', explode( ',', $settings['skip_css'] ) )
);
}
return $settings;
}
/**
* Validate assets directory.
*
* @param string $dir Directory.
* @return string|false Full directory path or false if cannot create
* directory or directory is not writable.
*/
public function validate_assets_dir( $dir = null ) {
if ( $dir === null ) {
$dir = $this->get_setting( 'assets_dir' );
}
// TODO: normalize path.
if ( !is_dir( $dir ) && !mkdir( $dir ) ) {
return false;
}
if ( !is_writable( $dir ) ) {
return false;
}
return rtrim( $dir, DIRECTORY_SEPARATOR ) . DIRECTORY_SEPARATOR;
}
/**
* Get setting field value.
*
* @param string $field Optional. Field name.
* @param mixed $default
* @return mixed
*/
public function get_setting( $field = null, $default = false ) {
if ( empty( $this->settings ) ) {
$this->settings = get_option( 'assets_pack' );
}
if ( $field === null ) {
return $this->settings;
}
// FIXME: php7.x: $this->settings[$field] ?: $default;
return isset( $this->settings[$field] ) ? $this->settings[$field] : $default;
}
/**
* Set setting field value.
*
* @param string $field
* @param mixed $value
* @return true|string Success or validation error message.
*/
public function set_setting( $field, $value ) {
global $wp_settings_errors;
$settings = $this->get_setting();
if ( !isset( $settings[$field] ) ) {
return 'Setting field not registered.';
}
$settings[$field] = $value;
$settings = $this->validate_settings( $settings );
if ( !empty( $wp_settings_errors ) ) {
foreach ( $wp_settings_errors as $error ) {
if ( $error['setting'] === $field ) {
return $error['message'];
}
}
}
// Update option in db.
update_option( 'assets_pack', $settings );
// Update object settings.
$this->settings[$field] = $settings[$field];
return true;
}
/**
* Clears asset files.
*
* @param string $dir Assets dir.
* @return bool
*/
public function clear_assets( $dir = null ) {
if ( $dir === null ) {
$dir = $this->get_setting( 'assets_dir' );
}
$success = true;
$pattern = $dir . '*.{js,css,js.debug,css.debug}';
$files = glob( $pattern, GLOB_BRACE | GLOB_NOSORT );
foreach ( $files as $file ) {
if ( !unlink( $file ) ) {
$success = false;
}
}
return $success;
}
}