[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Dark Mode Class 4 * 5 * @package WordPress 6 * @subpackage Twenty_Twenty_One 7 * @since Twenty Twenty-One 1.0 8 */ 9 10 /** 11 * This class is in charge of Dark Mode. 12 */ 13 class Twenty_Twenty_One_Dark_Mode { 14 15 /** 16 * Instantiates the object. 17 * 18 * @since Twenty Twenty-One 1.0 19 */ 20 public function __construct() { 21 22 // Enqueue assets for the block-editor. 23 add_action( 'enqueue_block_assets', array( $this, 'editor_custom_color_variables' ) ); 24 25 // Add styles for dark-mode. 26 add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 27 28 // Add scripts for customizer controls. 29 add_action( 'customize_controls_enqueue_scripts', array( $this, 'customize_controls_enqueue_scripts' ) ); 30 31 // Add customizer controls. 32 add_action( 'customize_register', array( $this, 'customizer_controls' ) ); 33 34 // Add HTML classes. 35 add_filter( 'twentytwentyone_html_classes', array( $this, 'html_classes' ) ); 36 37 // Add classes to <body> in the dashboard. 38 add_filter( 'admin_body_class', array( $this, 'admin_body_classes' ) ); 39 40 // Add the switch on the frontend & customizer. 41 add_action( 'wp_footer', array( $this, 'the_switch' ) ); 42 43 // Add the privacy policy content. 44 add_action( 'admin_init', array( $this, 'add_privacy_policy_content' ) ); 45 } 46 47 /** 48 * Enqueues editor custom color variables & scripts. 49 * 50 * @since Twenty Twenty-One 1.0 51 * 52 * @return void 53 */ 54 public function editor_custom_color_variables() { 55 if ( ! $this->switch_should_render() ) { 56 return; 57 } 58 $background_color = get_theme_mod( 'background_color', 'D1E4DD' ); 59 $should_respect_color_scheme = get_theme_mod( 'respect_user_color_preference', false ); 60 if ( $should_respect_color_scheme && Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( $background_color ) > 127 ) { 61 // Add Dark Mode variable overrides. 62 wp_add_inline_style( 63 'twenty-twenty-one-custom-color-overrides', 64 '.is-dark-theme.is-dark-theme .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); --button--color-text: var(--global--color-background); --button--color-text-hover: var(--global--color-secondary); --button--color-text-active: var(--global--color-secondary); --button--color-background: var(--global--color-secondary); --button--color-background-active: var(--global--color-background); --global--color-border: #9ea1a7; --table--stripes-border-color: rgba(240, 240, 240, 0.15); --table--stripes-background-color: rgba(240, 240, 240, 0.15); }' 65 ); 66 } 67 wp_enqueue_script( 68 'twentytwentyone-dark-mode-support-toggle', 69 get_template_directory_uri() . '/assets/js/dark-mode-toggler.js', 70 array(), 71 '1.0.0', 72 array( 'in_footer' => true ) 73 ); 74 75 wp_enqueue_script( 76 'twentytwentyone-editor-dark-mode-support', 77 get_template_directory_uri() . '/assets/js/editor-dark-mode-support.js', 78 array( 'twentytwentyone-dark-mode-support-toggle' ), 79 '1.0.0', 80 array( 'in_footer' => true ) 81 ); 82 } 83 84 /** 85 * Enqueues scripts and styles. 86 * 87 * @since Twenty Twenty-One 1.0 88 * 89 * @return void 90 */ 91 public function enqueue_scripts() { 92 if ( ! $this->switch_should_render() ) { 93 return; 94 } 95 $url = get_template_directory_uri() . '/assets/css/style-dark-mode.css'; 96 if ( is_rtl() ) { 97 $url = get_template_directory_uri() . '/assets/css/style-dark-mode-rtl.css'; 98 } 99 wp_enqueue_style( 'tt1-dark-mode', $url, array( 'twenty-twenty-one-style' ), wp_get_theme()->get( 'Version' ) ); // @phpstan-ignore-line. Version is always a string. 100 } 101 102 /** 103 * Enqueues scripts for the customizer. 104 * 105 * @since Twenty Twenty-One 1.0 106 * 107 * @return void 108 */ 109 public function customize_controls_enqueue_scripts() { 110 if ( ! $this->switch_should_render() ) { 111 return; 112 } 113 wp_enqueue_script( 114 'twentytwentyone-customize-controls', 115 get_template_directory_uri() . '/assets/js/customize.js', 116 array( 'customize-base', 'customize-controls', 'underscore', 'jquery', 'twentytwentyone-customize-helpers' ), 117 '1.0.0', 118 array( 'in_footer' => true ) 119 ); 120 } 121 122 /** 123 * Registers customizer options. 124 * 125 * @since Twenty Twenty-One 1.0 126 * 127 * @param WP_Customize_Manager $wp_customize Theme Customizer object. 128 * @return void 129 */ 130 public function customizer_controls( $wp_customize ) { 131 132 $colors_section = $wp_customize->get_section( 'colors' ); 133 if ( is_object( $colors_section ) ) { 134 $colors_section->title = __( 'Colors & Dark Mode', 'twentytwentyone' ); 135 } 136 137 // Custom notice control. 138 require_once get_theme_file_path( 'classes/class-twenty-twenty-one-customize-notice-control.php' ); // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude.FileIncludeFound 139 140 $wp_customize->add_setting( 141 'respect_user_color_preference_notice', 142 array( 143 'capability' => 'edit_theme_options', 144 'default' => '', 145 'sanitize_callback' => '__return_empty_string', 146 ) 147 ); 148 149 $wp_customize->add_control( 150 new Twenty_Twenty_One_Customize_Notice_Control( 151 $wp_customize, 152 'respect_user_color_preference_notice', 153 array( 154 'section' => 'colors', 155 'priority' => 100, 156 'active_callback' => static function () { 157 return 127 >= Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( get_theme_mod( 'background_color', 'D1E4DD' ) ); 158 }, 159 ) 160 ) 161 ); 162 163 $wp_customize->add_setting( 164 'respect_user_color_preference', 165 array( 166 'capability' => 'edit_theme_options', 167 'default' => false, 168 'sanitize_callback' => static function ( $value ) { 169 return (bool) $value; 170 }, 171 ) 172 ); 173 174 $description = '<p>'; 175 $description .= sprintf( 176 /* translators: %s: Twenty Twenty-One support article URL. */ 177 __( '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. <a href="%s">Learn more about Dark Mode.</a>', 'twentytwentyone' ), 178 esc_url( __( 'https://wordpress.org/documentation/article/twenty-twenty-one/#dark-mode-support', 'twentytwentyone' ) ) 179 ); 180 $description .= '</p>'; 181 $description .= '<p>' . __( 'Dark Mode can also be turned on and off with a button that you can find in the bottom corner of the page.', 'twentytwentyone' ) . '</p>'; 182 183 $wp_customize->add_control( 184 'respect_user_color_preference', 185 array( 186 'type' => 'checkbox', 187 'section' => 'colors', 188 'label' => esc_html__( 'Dark Mode support', 'twentytwentyone' ), 189 'priority' => 110, 190 'description' => $description, 191 'active_callback' => static function ( $value ) { 192 return 127 < Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( get_theme_mod( 'background_color', 'D1E4DD' ) ); 193 }, 194 ) 195 ); 196 197 // Add partial for background_color. 198 $wp_customize->selective_refresh->add_partial( 199 'background_color', 200 array( 201 'selector' => '#dark-mode-toggler', 202 'container_inclusive' => true, 203 'render_callback' => function () { 204 $attrs = ( $this->switch_should_render() ) ? array() : array( 'style' => 'display:none;' ); 205 $this->the_html( $attrs ); 206 }, 207 ) 208 ); 209 } 210 211 /** 212 * Calculates classes for the main <html> element. 213 * 214 * @since Twenty Twenty-One 1.0 215 * 216 * @param string $classes The classes for <html> element. 217 * @return string 218 */ 219 public function html_classes( $classes ) { 220 if ( ! $this->switch_should_render() ) { 221 return $classes; 222 } 223 224 $background_color = get_theme_mod( 'background_color', 'D1E4DD' ); 225 $should_respect_color_scheme = get_theme_mod( 'respect_user_color_preference', false ); 226 if ( $should_respect_color_scheme && 127 <= Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( $background_color ) ) { 227 return ( $classes ) ? ' respect-color-scheme-preference' : 'respect-color-scheme-preference'; 228 } 229 230 return $classes; 231 } 232 233 /** 234 * Adds a class to the <body> element in the editor to accommodate dark-mode. 235 * 236 * @since Twenty Twenty-One 1.0 237 * 238 * @global WP_Screen $current_screen WordPress current screen object. 239 * 240 * @param string $classes The admin body-classes. 241 * @return string 242 */ 243 public function admin_body_classes( $classes ) { 244 if ( ! $this->switch_should_render() ) { 245 return $classes; 246 } 247 248 global $current_screen; 249 if ( empty( $current_screen ) ) { 250 set_current_screen(); 251 } 252 253 if ( $current_screen->is_block_editor() ) { 254 $should_respect_color_scheme = get_theme_mod( 'respect_user_color_preference', false ); 255 $background_color = get_theme_mod( 'background_color', 'D1E4DD' ); 256 257 if ( $should_respect_color_scheme && Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( $background_color ) > 127 ) { 258 $classes .= ' twentytwentyone-supports-dark-theme'; 259 } 260 } 261 262 return $classes; 263 } 264 265 /** 266 * Determines if we want to print the dark-mode switch or not. 267 * 268 * @since Twenty Twenty-One 1.0 269 * 270 * @global bool $is_IE 271 * 272 * @return bool 273 */ 274 public function switch_should_render() { 275 global $is_IE; 276 return ( 277 get_theme_mod( 'respect_user_color_preference', false ) && 278 ! $is_IE && 279 127 <= Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( get_theme_mod( 'background_color', 'D1E4DD' ) ) 280 ); 281 } 282 283 /** 284 * Adds night/day switch. 285 * 286 * @since Twenty Twenty-One 1.0 287 * 288 * @return void 289 */ 290 public function the_switch() { 291 if ( ! $this->switch_should_render() ) { 292 return; 293 } 294 $this->the_html(); 295 $this->the_script(); 296 } 297 298 /** 299 * Prints the dark-mode switch HTML. 300 * 301 * Inspired from https://codepen.io/aaroniker/pen/KGpXZo (MIT-licensed) 302 * 303 * @since Twenty Twenty-One 1.0 304 * 305 * @param array $attrs The attributes to add to our <button> element. 306 * @return void 307 */ 308 public function the_html( $attrs = array() ) { 309 $attrs = wp_parse_args( 310 $attrs, 311 array( 312 'id' => 'dark-mode-toggler', 313 'class' => 'fixed-bottom', 314 'aria-pressed' => 'false', 315 'onClick' => 'toggleDarkMode()', 316 ) 317 ); 318 echo '<button'; 319 foreach ( $attrs as $key => $val ) { 320 echo ' ' . esc_attr( $key ) . '="' . esc_attr( $val ) . '"'; 321 } 322 echo '>'; 323 printf( 324 /* translators: %s: On/Off */ 325 esc_html__( 'Dark Mode: %s', 'twentytwentyone' ), 326 '<span aria-hidden="true"></span>' 327 ); 328 echo '</button>'; 329 ?> 330 <style> 331 #dark-mode-toggler > span { 332 margin-<?php echo is_rtl() ? 'right' : 'left'; ?>: 5px; 333 } 334 #dark-mode-toggler > span::before { 335 content: '<?php esc_attr_e( 'Off', 'twentytwentyone' ); ?>'; 336 } 337 #dark-mode-toggler[aria-pressed="true"] > span::before { 338 content: '<?php esc_attr_e( 'On', 'twentytwentyone' ); ?>'; 339 } 340 <?php if ( is_admin() || wp_is_json_request() ) : ?> 341 .components-editor-notices__pinned ~ .edit-post-visual-editor #dark-mode-toggler { 342 z-index: 20; 343 } 344 .is-dark-theme.is-dark-theme #dark-mode-toggler:not(:hover):not(:focus) { 345 color: var(--global--color-primary); 346 } 347 @media only screen and (max-width: 782px) { 348 #dark-mode-toggler { 349 margin-top: 32px; 350 } 351 } 352 <?php endif; ?> 353 </style> 354 355 <?php 356 } 357 358 /** 359 * Prints the dark-mode switch script. 360 * 361 * @since Twenty Twenty-One 1.0 362 * 363 * @return void 364 */ 365 public function the_script() { 366 echo '<script>'; 367 include get_template_directory() . '/assets/js/dark-mode-toggler.js'; // phpcs:ignore WPThemeReview.CoreFunctionality.FileInclude 368 echo '</script>'; 369 } 370 371 /** 372 * Adds information to the privacy policy. 373 * 374 * @since Twenty Twenty-One 1.0 375 * 376 * @return void 377 */ 378 public function add_privacy_policy_content() { 379 if ( ! function_exists( 'wp_add_privacy_policy_content' ) ) { 380 return; 381 } 382 $content = '<p class="privacy-policy-tutorial">' . __( 'Twenty Twenty-One uses LocalStorage when Dark Mode support is enabled.', 'twentytwentyone' ) . '</p>' 383 . '<strong class="privacy-policy-tutorial">' . __( 'Suggested text:', 'twentytwentyone' ) . '</strong> ' 384 . __( 'This website uses LocalStorage to save the setting when Dark Mode support is turned on or off.<br> LocalStorage is necessary for the setting to work and is only used when a user clicks on the Dark Mode button.<br> No data is saved in the database or transferred.', 'twentytwentyone' ); 385 wp_add_privacy_policy_content( __( 'Twenty Twenty-One', 'twentytwentyone' ), wp_kses_post( wpautop( $content, false ) ) ); 386 } 387 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Sat Dec 21 08:20:01 2024 | Cross-referenced by PHPXref |