-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass-default-admin-color-scheme.php
470 lines (390 loc) · 12.2 KB
/
class-default-admin-color-scheme.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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?php
/**
* Contains the main plugin class.
*
* @package PluginPizza\DefaultAdminColorScheme
*/
/**
* Plugin class.
*/
class Default_Admin_Color_Scheme {
/**
* Plugin version, used for cache-busting of style and script file references.
*
* @since 1.0.0
*
* @var string
*/
const VERSION = '1.0.3';
/**
* Instance of this class.
*
* @since 1.0.0
*
* @var object
*/
protected static $instance = null;
/**
* Initialize the plugin by setting localization and loading public scripts
* and styles.
*
* @since 1.0.0
*/
private function __construct() {
// Load plugin text domain.
add_action( 'init', array( $this, 'load_plugin_textdomain' ) );
// Activate plugin when new blog is added.
add_action( 'wpmu_new_blog', array( $this, 'activate_new_site' ) );
// Register settings.
add_action( 'admin_init', array( $this, 'register_settings' ) );
// Save default color scheme in a hacky way.
add_filter( 'pre_update_option_plugin_default_admin_color_scheme', array( $this, 'save_color_scheme' ), 0 );
// Save color scheme via ajax.
add_action( 'wp_ajax_save-default-color-scheme', array( $this, 'ajax_save_color_scheme' ), 1 );
// Load JavaScript.
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
// Maybe remove color picker from user profile.
add_action( 'admin_head-profile.php', array( $this, 'maybe_remove_profile_color_picker' ) );
// Filter 'get_user_option_admin_color' to preselect the default scheme on the "General Settings" page.
add_action( 'admin_head-options-general.php', array( $this, 'filter_get_user_option_admin_color_on_options_general' ) );
// Unfortunately, a default value is set on user creation, let's work around this.
add_filter( 'update_user_metadata', array( $this, 'maybe_set_override' ), 10, 3 );
// Add an action link pointing to the general options page.
$plugin_basename = plugin_basename( plugin_dir_path( __FILE__ ) . 'default-admin-color-scheme.php' );
add_filter( 'plugin_action_links_' . $plugin_basename, array( $this, 'add_action_links' ) );
// All of the above, solely for this little pièce de résistance.
add_filter( 'get_user_option_admin_color', array( $this, 'filter_get_user_option_admin_color' ) );
}
/**
* Return an instance of this class.
*
* @since 1.0.0
*
* @return object A single instance of this class.
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null === self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Fired when the plugin is activated.
*
* @since 1.0.0
*
* @param bool $network_wide True if WPMU superadmin uses
* "Network Activate" action, false if
* WPMU is disabled or plugin is
* activated on an individual blog.
*/
public static function activate( $network_wide ) {
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
if ( $network_wide ) {
// Get all blog ids.
$blog_ids = self::get_blog_ids();
foreach ( $blog_ids as $blog_id ) {
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.switch_to_blog_switch_to_blog
switch_to_blog( $blog_id );
self::single_activate();
}
restore_current_blog();
} else {
self::single_activate();
}
} else {
self::single_activate();
}
}
/**
* Fired when the plugin is deactivated.
*
* @since 1.0.0
*
* @param boolean $network_wide True if WPMU superadmin uses
* "Network Deactivate" action, false if
* WPMU is disabled or plugin is
* deactivated on an individual blog.
*/
public static function deactivate( $network_wide ) {
if ( function_exists( 'is_multisite' ) && is_multisite() ) {
if ( $network_wide ) {
// Get all blog ids.
$blog_ids = self::get_blog_ids();
foreach ( $blog_ids as $blog_id ) {
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.switch_to_blog_switch_to_blog
switch_to_blog( $blog_id );
self::single_deactivate();
}
restore_current_blog();
} else {
self::single_deactivate();
}
} else {
self::single_deactivate();
}
}
/**
* Fired when a new site is activated with a WPMU environment.
*
* @since 1.0.0
*
* @param int $blog_id ID of the new blog.
*/
public function activate_new_site( $blog_id ) {
if ( 1 !== did_action( 'wpmu_new_blog' ) ) {
return;
}
// phpcs:ignore WordPressVIPMinimum.Functions.RestrictedFunctions.switch_to_blog_switch_to_blog
switch_to_blog( $blog_id );
self::single_activate();
restore_current_blog();
}
/**
* Get all blog ids of blogs in the current network that are:
* - not archived
* - not spam
* - not deleted
*
* @since 1.0.0
*
* @return array|false The blog ids, false if no matches.
*/
private static function get_blog_ids() {
global $wpdb;
// get an array of blog ids.
$sql = "SELECT blog_id FROM $wpdb->blogs WHERE archived = '0' AND spam = '0' AND deleted = '0'";
// phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:disable WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:disable WordPress.DB.PreparedSQL.NotPrepared
return $wpdb->get_col( $sql );
// phpcs:enable WordPress.DB.DirectDatabaseQuery.DirectQuery
// phpcs:enable WordPress.DB.DirectDatabaseQuery.NoCaching
// phpcs:enable WordPress.DB.PreparedSQL.NotPrepared
}
/**
* Fired for each blog when the plugin is activated.
*
* @since 1.0.0
*/
private static function single_activate() {
add_option(
'plugin_default_admin_color_scheme',
array(
'users_can_change_color_scheme' => 1,
'color_scheme' => 'fresh',
)
);
}
/**
* Load the plugin text domain for translation.
*
* @since 1.0.0
*/
public function load_plugin_textdomain() {
load_plugin_textdomain( 'default-admin-color-scheme', false, dirname( plugin_basename( __FILE__ ) ) . '/languages' );
}
/**
* Add a settings section to the 'General Settings' page.
*
* @since 1.0.0
*/
public function register_settings() {
$option_name = 'plugin_default_admin_color_scheme';
register_setting(
'general',
$option_name,
array( $this, 'settings_validate' )
);
add_settings_section(
$option_name,
__( 'Admin Color Scheme', 'default-admin-color-scheme' ),
'__return_false',
'general'
);
add_settings_field(
'user-can-change-color',
__( 'User Color Scheme', 'default-admin-color-scheme' ),
array( $this, 'settings_checkbox' ),
'general',
$option_name
);
add_settings_field(
'color-picker',
__( 'Default Color Scheme', 'default-admin-color-scheme' ),
'admin_color_scheme_picker', // Uses the default color scheme picker.
'general',
$option_name
);
}
/**
* Validate settings.
*
* @since 1.0.0
*
* @param array $input Plugin setting to validate.
*/
public function settings_validate( $input ) {
$input['users_can_change_color_scheme'] = absint( $input['users_can_change_color_scheme'] );
return $input;
}
/**
* Display a checkbox for the 'users_can_change_color_scheme' option.
*
* @since 1.0.0
*/
public function settings_checkbox() {
// phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
$option = get_option( 'plugin_default_admin_color_scheme' );
include plugin_dir_path( __FILE__ ) . 'templates/settings-checkbox.php';
}
/**
* Save color scheme.
*
* To select the default scheme the color scheme picker of the user profile page is used.
* Its value is not picked up by our settings_validate() method as the field values are not namespaced.
* To work around that limitation, this function is hooked into saving the 'plugin_default_admin_color_scheme' option.
*
* Suggestions for improvement welcome!
*
* @since 1.0.0
*
* @param string $value Color scheme setting, eg. 'fresh'.
*/
public function save_color_scheme( $value ) {
// Do not filter $value when doing ajax.
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
return $value;
}
if (
! empty( $_POST['color-nonce'] ) // WPCS: input var okay.
&&
wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['color-nonce'] ) ), 'save-color-scheme' ) // WPCS: input var okay.
) {
$value['color_scheme'] = isset( $_POST['admin_color'] ) ? sanitize_key( $_POST['admin_color'] ) : 'fresh'; // WPCS: input var okay.
}
return $value;
}
/**
* Auto-save the selected color scheme.
*
* @since 1.0.0
*/
public function ajax_save_color_scheme() {
global $_wp_admin_css_colors;
check_ajax_referer( 'save-color-scheme', 'nonce' );
if ( empty( $_POST['color_scheme'] ) ) { // WPCS: input var okay.
wp_send_json_error();
}
$color_scheme = sanitize_key( $_POST['color_scheme'] ); // WPCS: input var okay.
if ( ! isset( $_wp_admin_css_colors[ $color_scheme ] ) ) {
wp_send_json_error();
}
$option = get_option( 'plugin_default_admin_color_scheme' );
$option['color_scheme'] = $color_scheme;
update_option( 'plugin_default_admin_color_scheme', $option );
wp_send_json_success();
}
/**
* Register and enqueue JavaScript files.
*
* @since 1.0.0
*/
public function enqueue_scripts() {
$screen = get_current_screen();
if ( 'options-general' !== $screen->id ) {
return;
}
wp_enqueue_script(
'default-admin-color-scheme',
plugins_url( 'js/admin.js', __FILE__ ),
array( 'jquery' ),
self::VERSION,
true
);
}
/**
* Remove color picker from profile edit screen if users are not allowed to choose their own admin color scheme,
*
* @since 1.0.0
*/
public function maybe_remove_profile_color_picker() {
$option = get_option( 'plugin_default_admin_color_scheme' );
if ( 1 !== $option['users_can_change_color_scheme'] ) {
remove_action( 'admin_color_scheme_picker', 'admin_color_scheme_picker' );
}
}
/**
* Filter 'get_user_option_admin_color' right before showing the color picker in preferences.
*
* @since 1.0.0
*/
public function filter_get_user_option_admin_color_on_options_general() {
add_filter( 'get_user_option_admin_color', array( $this, 'get_default_admin_color_scheme' ) );
}
/**
* Get the default admin color scheme.
*
* Helper for the 'filter_get_user_option_admin_color_on_options_general' function.
*
* @since 1.0.0
*
* @return string
*/
public function get_default_admin_color_scheme() {
$option = get_option( 'plugin_default_admin_color_scheme' );
$color_scheme = isset( $option['color_scheme'] ) ? $option['color_scheme'] : 'fresh';
return $color_scheme;
}
/**
* Set flag if the user selects a color scheme.
*
* @since 1.0.0
*
* @param null|bool $check Whether to allow updating metadata for the given type.
* @param int $object_id User ID.
* @param string $meta_key Meta key.
*/
public function maybe_set_override( $check, $object_id, $meta_key ) {
if ( 'admin_color' === $meta_key ) {
update_user_option( $object_id, 'plugin_default_admin_color_scheme_override', 1 );
}
return $check;
}
/**
* Add settings action link to the plugins page.
*
* @since 1.0.0
*
* @param array $actions An array of plugin action links.
*/
public function add_action_links( $actions ) {
return array_merge(
array(
'settings' => '<a href="' . admin_url( 'options-general.php?#users_can_change_color_scheme' ) . '">' . __( 'Settings', 'default-admin-color-scheme' ) . '</a>',
),
$actions
);
}
/**
* Filter 'get_user_option_admin_color' to return the desired color scheme.
*
* @since 1.0.0
*
* @param string $color_scheme User color scheme option.
* @return string
*/
public function filter_get_user_option_admin_color( $color_scheme ) {
$option = get_option( 'plugin_default_admin_color_scheme' );
$default_color_scheme = isset( $option['color_scheme'] ) ? $option['color_scheme'] : $color_scheme;
if ( isset( $option['users_can_change_color_scheme'] ) && 1 === (int) $option['users_can_change_color_scheme'] ) {
if ( 1 === (int) get_user_option( 'plugin_default_admin_color_scheme_override' ) ) {
return $color_scheme;
} else {
return $default_color_scheme;
}
}
return $default_color_scheme;
}
}