in the dashboard.
add_filter( 'admin_body_class', array( $this, 'admin_body_classes' ) );
// Add the switch on the frontend & customizer.
add_action( 'wp_footer', array( $this, 'the_switch' ) );
// Add the switch in the editor.
add_action( 'wp_ajax_tt1_dark_mode_editor_switch', array( $this, 'editor_ajax_callback' ) );
// Add the privacy policy content.
add_action( 'admin_init', array( $this, 'add_privacy_policy_content' ) );
}
/**
* Editor custom color variables & scripts.
*
* @access public
*
* @since 1.0.0
*
* @return void
*/
public function editor_custom_color_variables() {
if ( ! $this->switch_should_render() ) {
return;
}
$background_color = get_theme_mod( 'background_color', 'D1E4DD' );
$should_respect_color_scheme = get_theme_mod( 'respect_user_color_preference', false );
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',
'.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); }'
);
}
wp_enqueue_script(
'twentytwentyone-dark-mode-support-toggle',
get_template_directory_uri() . '/assets/js/dark-mode-toggler.js',
array(),
'1.0.0',
true
);
wp_enqueue_script(
'twentytwentyone-editor-dark-mode-support',
get_template_directory_uri() . '/assets/js/editor-dark-mode-support.js',
array( 'twentytwentyone-dark-mode-support-toggle' ),
'1.0.0',
true
);
}
/**
* Enqueue scripts and styles.
*
* @access public
*
* @since 1.0.0
*
* @return void
*/
public function enqueue_scripts() {
if ( ! $this->switch_should_render() ) {
return;
}
$url = get_template_directory_uri() . '/assets/css/style-dark-mode.css';
if ( is_rtl() ) {
$url = get_template_directory_uri() . '/assets/css/style-dark-mode-rtl.css';
}
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.
}
/**
* Enqueue scripts for the customizer.
*
* @access public
*
* @since 1.0.0
*
* @return void
*/
public function customize_controls_enqueue_scripts() {
if ( ! $this->switch_should_render() ) {
return;
}
wp_enqueue_script(
'twentytwentyone-customize-controls',
get_template_directory_uri() . '/assets/js/customize.js',
array( 'customize-base', 'customize-controls', 'underscore', 'jquery', 'twentytwentyone-customize-helpers' ),
'1.0.0',
true
);
}
/**
* Register customizer options.
*
* @access public
*
* @since 1.0.0
*
* @param WP_Customize_Manager $wp_customize Theme Customizer object.
*
* @return void
*/
public function customizer_controls( $wp_customize ) {
$colors_section = $wp_customize->get_section( 'colors' );
if ( is_object( $colors_section ) ) {
$colors_section->title = __( 'Colors & Dark Mode', 'twentytwentyone' );
$colors_section->description = __( 'To access the Dark Mode settings, select a light background color.', 'twentytwentyone' ) . '
' . __( 'Learn more about Dark Mode.', 'twentytwentyone' ) . '';
}
$wp_customize->add_setting(
'respect_user_color_preference',
array(
'capability' => 'edit_theme_options',
'default' => false,
'sanitize_callback' => function( $value ) {
return (bool) $value;
},
)
);
$wp_customize->add_control(
'respect_user_color_preference',
array(
'type' => 'checkbox',
'section' => 'colors',
'label' => esc_html__( 'Dark Mode Support', 'twentytwentyone' ),
'description' => __( 'Respect visitor\'s device dark mode settings.
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.
Dark Mode can also be turned on and off with a button that you can find in the bottom right corner of the page.', 'twentytwentyone' ),
'active_callback' => function( $value ) {
return 127 < Twenty_Twenty_One_Custom_Colors::get_relative_luminance_from_hex( get_theme_mod( 'background_color', 'D1E4DD' ) );
},
)
);
// Add partial for background_color.
$wp_customize->selective_refresh->add_partial(
'background_color',
array(
'selector' => '#dark-mode-toggler',
'container_inclusive' => true,
'render_callback' => function() {
$attrs = ( $this->switch_should_render() ) ? array() : array( 'style' => 'display:none;' );
$this->the_html( $attrs );
},
)
);
}
/**
* Calculate classes for the main element.
*
* @access public
*
* @since 1.0.0
*
* @param string $classes The classes for element.
*
* @return string
*/
public function html_classes( $classes ) {
if ( ! $this->switch_should_render() ) {
return $classes;
}
$background_color = get_theme_mod( 'background_color', 'D1E4DD' );
$should_respect_color_scheme = get_theme_mod( 'respect_user_color_preference', false );
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;
}
/**
* Adds a class to the