-
Notifications
You must be signed in to change notification settings - Fork 18
/
class.minqueue-admin.php
366 lines (257 loc) · 11.6 KB
/
class.minqueue-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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
<?php
class MinQueue_Admin {
// Plugin unique prefix. Used for options, filenames etc.
private $prefix = 'minqueue';
// Plugin options
private $options;
// Admin Notices.
private $admin_notices;
function __construct() {
$this->prefix = apply_filters( 'minqueue_prefix', $this->prefix );
$this->options = minqueue_get_options();
$this->admin_notices = new MinQueue_Admin_Notices( $this->prefix );
add_action( 'admin_menu', array( $this, 'admin_menu' ) );
add_action( 'admin_init', array( $this, 'admin_init' ) );
add_action( 'admin_notices', array( $this, 'display_admin_notices' ), 1 );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
}
/**
* Add the adminmenu item
*
* @return null
*/
function admin_menu() {
add_options_page( 'MinQueue Plugin Page', 'MinQueue', 'manage_options', 'minqueue', array( $this, 'options_page' ) );
}
/**
* Register plugin settings
*
* @return null
*/
function admin_init(){
// Maybe clear cache
if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], 'minqueue_clear_cache' ) )
$this->clear_cache();
register_setting( 'minqueue_options', 'minqueue_options', array( $this, 'validate_options' ) );
add_settings_section( 'plugin_main', 'General Options', '__return_true', 'general_minify_options' );
add_settings_section( 'plugin_main', 'Script Minification', '__return_true', 'script_minify_options' );
add_settings_section( 'plugin_main', 'Style Minification', '__return_true', 'style_minify_options' );
add_settings_field( 'minqueue_helper', 'Enable helper', array( $this, 'field_helper' ), 'general_minify_options', 'plugin_main' );
add_settings_field( 'minqueue_clear_cache', 'Delete all cached files', array( $this, 'field_clear_cache' ), 'general_minify_options', 'plugin_main' );
add_settings_field( 'minqueue_styles_method', 'Script minification method', array( $this, 'field_method_scripts' ), 'script_minify_options', 'plugin_main' );
add_settings_field( 'minqueue_scripts', 'Script minification queue(s)', array( $this, 'field_scripts' ), 'script_minify_options', 'plugin_main' );
add_settings_field( 'minqueue_styles_method', 'Style minification method', array( $this, 'field_method_styles' ), 'style_minify_options', 'plugin_main' );
add_settings_field( 'minqueue_styles', 'Style minification queue(s)', array( $this, 'field_styles' ), 'style_minify_options', 'plugin_main' );
}
/**
* Output the main options page content.
*
* @return null
*/
function options_page() { ?>
<div class="wrap">
<h2>MinQueue Plugin Settings</h2>
<form action="options.php" method="post">
<?php
settings_fields('minqueue_options');
do_settings_sections('general_minify_options');
do_settings_sections('script_minify_options');
do_settings_sections('style_minify_options');
?>
<p class="submit">
<input type="submit" name="submit" id="submit" class="button button-primary" value="Save Changes">
</p>
</form>
</div>
<?php
}
/**
* Output clear cache button
*
* @return null
*/
function field_clear_cache() { ?>
<a href="<?php echo wp_nonce_url( 'options-general.php?page=minqueue', 'minqueue_clear_cache' ); ?>" class="button" style="margin-right: 10px;">Clear Cache</a>
<?php if ( $cached_files_count = $this->get_cached_files_count() ) : ?>
<?php echo intval( $cached_files_count ); ?> files cached.
<?php endif; ?>
<?php }
/**
* Output helper setting field.
*
* @return null
*/
function field_helper() { ?>
<input type="checkbox" id="minqueue_options_helper" name="minqueue_options[helper]" <?php checked( true, ( ! ( ! isset( $this->options['helper'] ) || isset( $this->options['helper'] ) && $this->options['helper'] === false ) ) ); ?>/>
<label for="minqueue_options_helper">Enable the helper in the front end of the site (only visible for site admin users).</label>
<?php }
/**
* Output settings section for scripts enable/disable toggle.
*
* @return null
*/
function field_method_scripts() {
if ( empty( $this->options['scripts_method'] ) )
$this->options['scripts_method'] = 'disabled';
?>
<input type="radio" id="minqueue_options_scripts_method_manual" name="minqueue_options[scripts_method]" value="manual" <?php checked( 'manual', $this->options['scripts_method'] ); ?>/> <label for="minqueue_options_scripts_method_manual">Enable script minification</label><br/>
<input type="radio" id="minqueue_options_scripts_method_disabled" name="minqueue_options[scripts_method]" value="disabled" <?php checked( 'disabled', $this->options['scripts_method'] ); ?>/> <label for="minqueue_options_scripts_method_disabled">Disable script minification</label>
<?php }
/**
* Output settings section for scripts queue textarea/s
*
* @return null
*/
function field_scripts() {
$values = ( ! empty( $this->options['scripts_manual'] ) ) ? $this->options['scripts_manual'] : array();
?>
<div id="field_manual_scripts">
<label for="minqueue_field_manual_scripts_0">
<p><span class="description">For each queue, provide a list of script handles that will be concatenated into one file and minified. (Comma separated or on a new line)</span></p>
<p><span class="description">Multiple queues will be processed separately, creating a minified file for each. Note that scripts can be placed in the header or footer. If header & footer scripts are added to the same queue, they will still be proccessed separately.</span></p>
</label>
<textarea id="minqueue_field_manual_scripts_hidden" name="minqueue_options[scripts_manual][]" class="large-text code input-template" style="display:none;"></textarea>
<?php for ( $i = 0; $i < ( ( count( $values ) > 0 ) ? count( $values ) : 1 ); $i++ ) : ?>
<?php if ( $i > 0 && empty( $values[$i]) ) continue; ?>
<textarea id="minqueue_field_manual_scripts_<?php echo $i; ?>" name="minqueue_options[scripts_manual][]" class="large-text code"><?php echo ( ! empty( $values[$i] ) ) ? esc_attr( implode( ', ', $values[$i] ) ) : null; ?></textarea>
<?php endfor; ?>
</div>
<div id="field_disabled_scripts">
<span class="description">Script minification is disabled</span>
</div>
<?php
}
/**
* Output settings section for styles enable/disable toggle.
*
* @return null
*/
function field_method_styles() {
if ( empty( $this->options['styles_method'] ) )
$this->options['styles_method'] = 'disabled';
?>
<input type="radio" id="minqueue_options_styles_method_manual" name="minqueue_options[styles_method]" value="manual" <?php checked( 'manual', $this->options['styles_method'] ); ?>/> <label for="minqueue_options_styles_method_manual">Enable style minification</label><br/>
<input type="radio" id="minqueue_options_styles_method_disabled" name="minqueue_options[styles_method]" value="disabled" <?php checked( 'disabled', $this->options['styles_method'] ); ?>/> <label for="minqueue_options_styles_method_disabled">Disable style minification</label>
<?php }
/**
* Output settings section for styles queue textarea/s
*
* @return null
*/
function field_styles() {
$values = ( ! empty( $this->options['styles_manual'] ) ) ? $this->options['styles_manual'] : array();
?>
<div id="field_manual_styles">
<label for="minqueue_field_manual_styles_0">
<p><span class="description">List of style handles to minify and concatenate into one file. Comma separated or on a new line</span></p>
<p><span class="description">Multiple queues will be processed separately, creating a minified file for each. Note that enqueued CSS files targeting different media will always be minified separately even if they are part of the same queue.</span></p>
</label>
<textarea id="minqueue_field_manual_styles_template" name="minqueue_options[styles_manual][]" class="large-text code input-template" style="display:none;"></textarea>
<?php for ( $i = 0; $i < ( ( count( $values ) > 0 ) ? count( $values ) : 1 ); $i++ ) : ?>
<?php if ( $i > 0 && empty( $values[$i]) ) continue; ?>
<textarea id="minqueue_field_manual_styles_<?php echo $i; ?>" name="minqueue_options[styles_manual][]" class="large-text code"><?php echo ( ! empty( $values[$i] ) ) ? esc_attr( implode( ', ', $values[$i] ) ) : null; ?></textarea>
<?php endfor; ?>
</div>
<div id="field_disabled_styles">
<span class="description">Style minification is disabled</span>
</div>
<?php
}
/**
* Settings validation.
*
* @return null
*/
function validate_options( $input ) {
if ( ! empty( $input['scripts_manual'] ) ) {
foreach ( $input['scripts_manual'] as $key => $queue )
$input['scripts_manual'][$key] = $this->validate_handle_list( $queue );
// Remove empty & reset array keys.
$input['scripts_manual'] = array_merge( array_filter( $input['scripts_manual'] ) );
}
if ( ! empty( $input['styles_manual'] ) ) {
foreach ( $input['styles_manual'] as $key => $queue )
$input['styles_manual'][$key] = $this->validate_handle_list( $queue );
// Remove empty & reset array keys.
$input['styles_manual'] = array_merge( array_filter( $input['styles_manual'] ) );
}
$input['helper'] = ( empty( $input['helper'] ) ) ? false : true;
// If method is manual, and no manual handles are set, disable minification.
if ( isset( $input['styles_method'] ) && 'manual' == $input['styles_method'] && empty( $input['styles_manual'] ) )
unset( $input['styles_method'] );
if ( isset( $input['scripts_method'] ) && 'manual' == $input['scripts_method'] && empty( $input['scripts_manual'] ) )
unset( $input['scripts_method'] );
// Delete empty fields
foreach( $input as $key => $field )
if ( empty( $field ) )
unset( $input[$key] );
return $input;
}
/**
* Validate the list of handles from the scripts & style queue textareas.
*
* Deal with new lines, spaces, double commas & convert to array.
*
* Return an array ready for saving.
*
* @param string $list string of comma separated handles
* @return array array of handles
*/
function validate_handle_list( $list ) {
$list = str_replace( array( "\n", "\r" ), ',', $list );
$list = explode(',', $list );
foreach ( $list as &$item )
$item = trim( $item );
return array_filter( $list );
}
/**
* Enqueue all scripts required by the admin page
*
* @return null
*/
function enqueue_scripts( $hook ) {
if ( 'settings_page_minqueue' !== $hook )
return;
wp_enqueue_script( 'minqueue-admin', trailingslashit( plugins_url( basename( __DIR__ ) ) ) . 'admin.js' );
}
/**
* Display Admin notices.
*
* Hook in on display admin notices, otherwise this is called when saving, before the redirect, causing the notice to be displayed and extra time.
*
* @return null
*/
function display_admin_notices() {
$current_screen = get_current_screen();
if ( isset( $this->options['helper'] ) && $this->options['helper'] === true )
$this->admin_notices->add_notice( 'MinQueue helper is currently active', true );
if ( 'settings_page_minqueue' == $current_screen->id ) {
$this->admin_notices->delete_notice( 'minqueue_min_activation_notice' );
}
}
/**
* Delete all cached files
*
* @param boolean $redirect whether
* @return [type] [description]
*/
function clear_cache( $redirect = true ) {
// Delete the cache if requested.
$minify = new MinQueue_Scripts();
$minify->delete_cache();
// Redirect.
if ( $redirect ) {
wp_redirect( remove_query_arg( '_wpnonce' ) );
exit;
}
}
/**
* Get number of cached files.
*
* @return int number of cached files.
*/
function get_cached_files_count() {
$minify = new MinQueue_Scripts();
return $minify->get_cached_files_count();
}
}