class-twenty-twenty-one-dark-mode.php 12 KB

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