-
Notifications
You must be signed in to change notification settings - Fork 27
/
dark-mode.php
154 lines (118 loc) · 3.43 KB
/
dark-mode.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
<?php
/**
* Plugin Name: Dark Mode
* Plugin URI: https://wordpress.org/plugins/dark-mode/
* Description: Let's your users make the WordPress admin dashboard darker.
* Author: Daniel James
* Author URI: https://www.danieltj.co.uk/
* Text Domain: dark-mode
* Version: 1.0
*/
// No thank you
if ( ! defined( 'ABSPATH' ) ) die();
// Create a new instance
new Dark_Mode;
/**
* Define the Dark Mode class.
*
* @package Dark_Mode
* @since 1.0
*/
class Dark_Mode {
/**
* Function which hooks into WordPress Core.
*
* @since 1.0
*/
public function __construct() {
// Hook into WordPress
add_action('plugins_loaded', array( __CLASS__, 'load_text_domain' ), 10);
add_action('admin_enqueue_scripts', array( __CLASS__, 'load_dark_mode_css' ), 10);
add_action('personal_options', array( __CLASS__, 'add_profile_fields' ), 10);
add_action('personal_options_update', array( __CLASS__, 'save_profile_fields' ), 10);
add_action('edit_user_profile_update', array( __CLASS__, 'save_profile_fields' ), 10);
}
/**
* Load the plugin text domain.
*
* @since 1.0
*/
public static function load_text_domain() {
// Load the plugin text domain for language localisation
load_plugin_textdomain( 'dark-mode', false, untrailingslashit(dirname(__FILE__)) . '/languages' );
}
/**
* Checks if the current user has Dark Mode on.
*
* @param $user_id string ID of a given user.
*
* @since 1.0
* @return boolean
*/
public static function is_using_dark_mode( $user_id = NULL ) {
// Check if we have a user id given to us
if ( empty( $user_id ) ) {
$user_id = get_current_user_id();
}
// Check if the user is using Dark Mode based on the meta value
if ( 'on' == get_user_meta( $user_id, 'dark_mode', true ) ) {
// Using Dark Mode
return true;
} else {
// Not using Dark Mode
return false;
}
}
/**
* Add the stylesheet to the dashboard.
*
* @since 1.0
* @return void
*/
public static function load_dark_mode_css() {
// Is the current user using Dark Mode?
if ( false !== self::is_using_dark_mode() ) {
// Run the hook before the dark mode css is added
do_action('doing_dark_mode');
// Register the Dark Mode stylesheet
wp_register_style('dark_mode_css', plugins_url('dark-mode', 'dark-mode') . '/darkmode.css', array(), '1.0');
// Enqueue the stylesheet in the dashboard
wp_enqueue_style('dark_mode_css');
}
}
/**
* Create the HTML markup for the profile setting.
*
* @param $profileuser
*
* @since 1.0
* @return boolean
*/
public static function add_profile_fields( $profileuser ) {
?>
<tr class="dark-mode user-dark-mode-option">
<th scope="row"><?php _e('Dark Mode', 'dark-mode'); ?></th>
<td>
<label for="dark_mode">
<input type="checkbox" id="dark_mode" name="dark_mode" class="dark_mode"<?php if ( 'on' == get_user_meta( $profileuser->data->ID, 'dark_mode', true ) ) : ?> checked="checked"<?php endif; ?> />
<?php _e('Enable the Dark Mode on the admin dashboard', 'dark-mode'); ?>
</label>
</td>
</tr>
<?php
}
/**
* Save the value of the profile field.
*
* @param $user_id
*
* @since 1.0
* @return mixed
*/
public static function save_profile_fields( $user_id ) {
// Set the value of the users choice
$dark_mode_choice = isset ( $_POST['dark_mode'] ) ? 'on' : 'off';
// Update the users meta data with the new value
update_user_meta( $user_id, 'dark_mode', $dark_mode_choice );
}
}