php 允许您在特定时间之间自动启用暗模式。需要Dark Mode插件版本2.0或更高版本。 https://github.com/danieltj27

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php 允许您在特定时间之间自动启用暗模式。需要Dark Mode插件版本2.0或更高版本。 https://github.com/danieltj27相关的知识,希望对你有一定的参考价值。

<?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 );
	
	}

}

以上是关于php 允许您在特定时间之间自动启用暗模式。需要Dark Mode插件版本2.0或更高版本。 https://github.com/danieltj27的主要内容,如果未能解决你的问题,请参考以下文章

如何在模拟器上启用暗模式?

Vaadin Flow 应用程序自动在明暗模式之间切换

iPad 检测用户是不是启用了暗模式,但不支持暗模式

无法修复 React 暗模式闪烁

启用暗模式时更改 iframe 源,禁用暗模式时将其更改回来

Tailwind css 暗模式未启用