Skip to content

Instantly share code, notes, and snippets.

@deckerweb
Created April 19, 2018 20:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deckerweb/1b0ec9c7e12b113b693a639b5d95a8ab to your computer and use it in GitHub Desktop.
Save deckerweb/1b0ec9c7e12b113b693a639b5d95a8ab to your computer and use it in GitHub Desktop.
Lets you enable Dark Mode automatically between certain times. Requires Dark Mode plugin version 2.0 or later.
<?php
/**
* Plugin Name: Automatic Dark Mode
* Plugin URI: https://github.com/danieltj27/Dark-Mode/
* Description: Lets your users make the WordPress admin dashboard darker.
* Author: Daniel James
* Author URI: https://www.danieltj.co.uk/
* Text Domain: auto-dark-mode
* Version: 1.0
*/
if ( ! defined( 'ABSPATH' ) ) {
die();
}
$auto_dark_mode = new Auto_Dark_Mode;
class Auto_Dark_Mode {
/**
* Define the plugin version.
*
* @since 1.0
*
* @var string
*/
public static $version = '1.0';
/**
* Make WordPress Dark sometimes.
*
* @since 1.0
*
* @return void
*/
public function __construct() {
add_action( 'plugins_loaded', array( __CLASS__, 'load_text_domain' ), 10, 0 );
add_action( 'dark_mode_profile_settings', array( __CLASS__, 'add_profile_fields' ), 10, 1 );
add_action( 'after_dark_mode_saved', array( __CLASS__, 'save_profile_fields' ), 10, 2 );
add_filter( 'is_using_dark_mode', array( __CLASS__, 'is_dark_mode_auto' ), 10, 2 );
}
/**
* Load the plugin text domain.
*
* @since 1.0
*
* @return void
*/
public static function load_text_domain() {
load_plugin_textdomain( 'auto-dark-mode', false, untrailingslashit( dirname( __FILE__ ) ) . '/languages' );
}
/**
* Check if Dark Mode should be enabled
* based on the automatic settings.
*
* @since 1.0
*
* @param boolean $return The return value passed through.
* @param int $user_id The current user's id.
*
* @return boolean
*/
public static function is_dark_mode_auto( $return, $user_id ) {
// Should we check for auto mode
if ( true === self::check_dark_mode_auto( $user_id ) ) {
// Get the time frames for auto mode
$auto_start = date_i18n( 'Y-m-d H:i:s', strtotime( get_user_meta( $user_id, 'dark_mode_start', true ) ) );
$auto_end = date_i18n( 'Y-m-d H:i:s', strtotime( get_user_meta( $user_id, 'dark_mode_end', true ) ) );
// Check the start time is greater than the end time
if ( $auto_start > $auto_end ) {
$auto_end = date_i18n( 'Y-m-d H:i:s', strtotime( '+1 day', strtotime( get_user_meta( $user_id, 'dark_mode_end', true ) ) ) );
}
// Get the current time
$current_time = date_i18n( 'Y-m-d H:i:s' );
// Check the current time is between the start and end time
if ( $current_time >= $auto_start && $current_time <= $auto_end ) {
return true;
} else {
return false;
}
}
return true;
}
/**
* Checks if the user is using automatic Dark Mode.
*
* This checks if Dark Mode is set to come on automatically
* for a given user. This is set to private as it's an extension
* of `is_using_dark_mode()` and only checks the auto value is set.
*
* @since 1.0
*
* @param int $user_id The user id to check.
*
* @return boolean
*/
public static function check_dark_mode_auto( $user_id ) {
if ( 0 == $user_id ) {
// Default to the current user
$user_id = get_current_user_id();
}
// Has automatic Dark Mode been turned on
if ( 'on' == get_user_meta( $user_id, 'dark_mode_auto', true ) ) {
return true;
}
return false;
}
/**
* Print the auto setting HTML.
*
* @since 1.0
*
* @param object $user WP_User object data.
*
* @return mixed
*/
public static function add_profile_fields( $user ) {
?>
<p>
<label for="dark_mode_auto">
<input type="checkbox" id="dark_mode_auto" name="dark_mode_auto" class="dark_mode_auto"<?php if ( 'on' == get_user_meta( $user->data->ID, 'dark_mode_auto', true ) ) : ?> checked="checked"<?php endif; ?> />
<?php _e('Automatically enable Dark Mode over night between these times:', 'auto-dark-mode'); ?>
</label>
</p>
<p>
<label>
<?php _ex('From', 'Time frame starting at', 'auto-dark-mode'); ?> <input type="time" name="dark_mode_start" id="dark_mode_start" placeholder="00:00"<?php if ( false !== get_user_meta( $user->data->ID, 'dark_mode_start' ) ) : ?> value="<?php echo esc_attr( get_user_meta( $user->data->ID, 'dark_mode_start', true ) ); ?>"<?php endif; ?> />
</label>
<label>
<?php _ex('To', 'Time frame ending at', 'auto-dark-mode'); ?> <input type="time" name="dark_mode_end" id="dark_mode_end" placeholder="00:00"<?php if ( false !== get_user_meta( $user->data->ID, 'dark_mode_end' ) ) : ?> value="<?php echo esc_attr( get_user_meta( $user->data->ID, 'dark_mode_end', true ) ); ?>"<?php endif; ?> />
</label>
</p>
<?php
}
/**
* Save the value of the profile field.
*
* @since 1.0
*
* @param string $option The Dark Mode preference.
* @param int $user_id The current user's id.
*
* @return void
*/
public static function save_profile_fields( $option, $user_id ) {
// Set the values
$dark_mode_auto = isset ( $_POST['dark_mode_auto'] ) ? 'on' : 'off';
$dark_mode_start = isset ( $_POST['dark_mode_start'] ) ? sanitize_text_field( $_POST['dark_mode_start'] ) : '';
$dark_mode_end = isset ( $_POST['dark_mode_end'] ) ? sanitize_text_field( $_POST['dark_mode_end'] ) : '';
// Update the user's meta
update_user_meta( $user_id, 'dark_mode_auto', $dark_mode_auto );
update_user_meta( $user_id, 'dark_mode_start', $dark_mode_start );
update_user_meta( $user_id, 'dark_mode_end', $dark_mode_end );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment