This repository has been archived by the owner on Dec 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
functions.php
270 lines (247 loc) · 7.59 KB
/
functions.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
<?php
/**
* Dark-mode related functions & hooks.
*
* @package wordpress/twentytwentyone-dark-mode
*/
/**
* Editor custom color variables.
*
* @since 1.0.0
*
* @return void
*/
function tt1_dark_mode_editor_custom_color_variables() {
$background_color = get_theme_mod( 'background_color', 'D1E4DD' );
$should_respect_color_scheme = get_theme_mod( 'respect_user_color_preference', true ); // @phpstan-ignore-line. Passing true instead of default value of false to get_theme_mod.
if ( $should_respect_color_scheme && Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( $background_color ) > 127 ) {
// Add dark mode variable overrides.
wp_add_inline_style(
'twenty-twenty-one-custom-color-overrides',
'@media (prefers-color-scheme: dark) { html.respect-color-scheme-preference .editor-styles-wrapper { --global--color-background: var(--global--color-dark-gray); --global--color-primary: var(--global--color-light-gray); --global--color-secondary: var(--global--color-light-gray); } }'
);
}
wp_enqueue_script(
'twentytwentyone-dark-mode-support-toggle',
plugins_url( 'assets/js/toggler.js', __FILE__ ),
array(),
'1.0.0',
true
);
wp_enqueue_script(
'twentytwentyone-editor-dark-mode-support',
plugins_url( 'assets/js/editor-dark-mode-support.js', __FILE__ ),
array( 'twentytwentyone-dark-mode-support-toggle' ),
'1.0.0',
true
);
}
add_action( 'enqueue_block_editor_assets', 'tt1_dark_mode_editor_custom_color_variables' );
/**
* Enqueue scripts and styles.
*
* @since 1.0.0
*
* @return void
*/
function tt1_dark_mode_scripts() {
wp_enqueue_style(
'tt1-dark-mode',
plugins_url( 'assets/css/style.css', __FILE__ ),
array( 'twenty-twenty-one-style' ),
'1.0.0'
);
}
add_action( 'wp_enqueue_scripts', 'tt1_dark_mode_scripts' );
/**
* Enqueue scripts for the customizer.
*
* @since 1.0.0
*
* @return void
*/
function tt1_dark_mode_customize_controls_enqueue_scripts() {
wp_enqueue_script(
'twentytwentyone-customize-controls',
plugins_url( 'assets/js/customize.js', __FILE__ ),
array( 'customize-base', 'customize-controls', 'underscore', 'jquery', 'twentytwentyone-customize-helpers' ),
'1.0.0',
true
);
wp_localize_script(
'twentytwentyone-customize-controls',
'backgroundColorNotice',
array(
'message' => esc_html__( 'You currently have dark mode enabled on your device. Changing the color picker will allow you to preview light mode.', 'twentytwentyone-dark-mode' ),
)
);
}
add_action( 'customize_controls_enqueue_scripts', 'tt1_dark_mode_customize_controls_enqueue_scripts' );
/**
* Register customizer options.
*
* @since 1.0.0
*
* @param WP_Customize_Manager $wp_customize Theme Customizer object.
*
* @return void
*/
function tt1_dark_mode_register_customizer_controls( $wp_customize ) {
$wp_customize->add_setting(
'respect_user_color_preference',
array(
'capability' => 'edit_theme_options',
'default' => true,
'sanitize_callback' => function( $value ) {
return (bool) $value;
},
)
);
$wp_customize->add_control(
'respect_user_color_preference',
array(
'type' => 'checkbox',
'section' => 'colors',
'label' => esc_html__( 'Respect visitor\'s device dark mode settings', 'twentytwentyone-dark-mode' ),
'description' => __( 'Dark mode is a device setting. If a visitor to your site requests it, your site will be shown with a dark background and light text.', 'twentytwentyone-dark-mode' ),
'active_callback' => function( $value ) {
return 127 < Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( get_theme_mod( 'background_color', 'D1E4DD' ) );
},
)
);
}
add_action( 'customize_register', 'tt1_dark_mode_register_customizer_controls' );
/**
* Calculate classes for the main <html> element.
*
* @since 1.0.0
*
* @param string $classes The classes for <html> element.
*
* @return string
*/
function tt1_dark_mode_the_html_classes( $classes ) {
$background_color = get_theme_mod( 'background_color', 'D1E4DD' );
$should_respect_color_scheme = get_theme_mod( 'respect_user_color_preference', true );
if ( $should_respect_color_scheme && 127 <= Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( $background_color ) ) {
return ( $classes ) ? ' respect-color-scheme-preference' : 'respect-color-scheme-preference';
}
return $classes;
}
add_filter( 'twentytwentyone_html_classes', 'tt1_dark_mode_the_html_classes' );
/**
* Adds a class to the <body> element in the editor to accommodate dark-mode.
*
* @since 1.0.0
*
* @param string $classes The admin body-classes.
*
* @return string
*/
function tt1_dark_mode_admin_body_classes( $classes ) {
global $current_screen;
if ( empty( $current_screen ) ) {
set_current_screen();
}
if ( $current_screen->is_block_editor() ) {
$should_respect_color_scheme = get_theme_mod( 'respect_user_color_preference', true ); // @phpstan-ignore-line. Passing true instead of default value of false to get_theme_mod.
$background_color = get_theme_mod( 'background_color', 'D1E4DD' );
if ( $should_respect_color_scheme && Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( $background_color ) > 127 ) {
$classes .= ' twentytwentyone-supports-dark-theme';
}
}
return $classes;
}
add_filter( 'admin_body_class', 'tt1_dark_mode_admin_body_classes' );
/**
* Determine if we want to print the dark-mode switch or not.
*
* @since 1.0.0
*
* @return void
*/
function tt1_dark_mode_switch_should_render() {
return (
get_theme_mod( 'respect_user_color_preference', true ) &&
127 <= Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( get_theme_mod( 'background_color', 'D1E4DD' ) )
);
}
/**
* Add night/day switch.
*
* @since 1.0.0
*
* @return void
*/
function tt1_the_dark_mode_switch() {
if ( ! tt1_dark_mode_switch_should_render() ) {
return;
}
tt1_dark_mode_switch_the_html();
tt1_dark_mode_switch_the_script();
}
add_action( 'get_template_part_template-parts/header/site-nav', 'tt1_the_dark_mode_switch' );
/**
* Print the dark-mode switch HTML.
*
* Inspired from https://codepen.io/aaroniker/pen/KGpXZo (MIT-licensed)
*
* @since 1.0.0
*
* @param string $classes The classes to add.
*
* @return void
*/
function tt1_dark_mode_switch_the_html( $classes = 'relative' ) {
?>
<button id="dark-mode-toggler" class="<?php echo esc_attr( $classes ); ?>" aria-pressed="false" onClick="toggleDarkMode()">
<?php
printf(
esc_html__( 'Dark theme: %s', 'twentytwentyone-dark-mode' ),
'<span aria-hidden="true"></span>'
);
?>
</button>
<style>
#dark-mode-toggler > span::before { content: '<?php esc_attr_e( 'off', 'twentytwentyone-dark-mode' ); ?>'; }
#dark-mode-toggler[aria-pressed="true"] > span::before { content: '<?php esc_attr_e( 'on', 'twentytwentyone-dark-mode' ); ?>'; }
</style>
<?php
}
/**
* Print the dark-mode switch script.
*
* @since 1.0.0
*
* @return void
*/
function tt1_dark_mode_switch_the_script() {
echo '<script>';
include 'assets/js/toggler.js'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude
echo '</script>';
}
/**
* Print the dark-mode switch styles.
*
* @since 1.0.0
*
* @return void
*/
function tt1_dark_mode_switch_the_styles() {
echo '<style>';
include 'assets/css/style.css'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude
echo '</style>';
}
/**
* Call the tt1_the_dark_mode_switch and exit.
*
* @since 1.0.0
*
* @return void
*/
function tt1_dark_mode_editor_ajax_callback() {
tt1_dark_mode_switch_the_html();
tt1_dark_mode_switch_the_styles();
wp_die();
}
add_action( 'wp_ajax_tt1_dark_mode_editor_switch', 'tt1_dark_mode_editor_ajax_callback' );