| 1 |
<?php |
| 2 |
|
| 3 |
class Darkify_Admin |
| 4 |
{ |
| 5 |
|
| 6 |
public $utils; |
| 7 |
public $settings; |
| 8 |
public $external_support; |
| 9 |
|
| 10 |
public $unique_id; |
| 11 |
|
| 12 |
private $plugin_name; |
| 13 |
private $version; |
| 14 |
|
| 15 |
public function __construct($plugin_name, $version) |
| 16 |
{ |
| 17 |
|
| 18 |
$this->utils = new DarkifyUtils($this); |
| 19 |
$this->external_support = new DarkifyExternalSupport($this); |
| 20 |
if (function_exists('wp_rand')) { |
| 21 |
$this->unique_id = wp_rand(); |
| 22 |
} |
| 23 |
$options = get_option('darkify'); |
| 24 |
$enable_admin_panel_dark_mode = isset($options['enable_admin_panel_dark_mode']) ? $options['enable_admin_panel_dark_mode'] : false; |
| 25 |
|
| 26 |
$this->plugin_name = $plugin_name; |
| 27 |
$this->version = $version; |
| 28 |
|
| 29 |
add_filter('plugin_action_links', array($this, 'add_plugin_action_links'), 10, 2); |
| 30 |
|
| 31 |
add_action('admin_enqueue_scripts', array($this, 'darkify_admin_enqueue')); |
| 32 |
|
| 33 |
if ($enable_admin_panel_dark_mode) { |
| 34 |
add_action('admin_bar_menu', array($this, 'darkify_admin_bar_switch'), 9999); |
| 35 |
add_action('admin_print_scripts', array($this, 'darkify_admin_header_script'), 1); |
| 36 |
add_action('admin_footer', array($this, 'darkify_admin_footer_script')); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
public function enqueue_styles() |
| 41 |
{ |
| 42 |
wp_enqueue_style($this->plugin_name, DRK_LITE_DIR_URL . 'admin/css/darkify-admin.css', array(), $this->version, 'all'); |
| 43 |
} |
| 44 |
|
| 45 |
public function enqueue_scripts($hook) |
| 46 |
{ |
| 47 |
if($hook !== 'toplevel_page_darkify'){ |
| 48 |
return; |
| 49 |
} |
| 50 |
wp_enqueue_script($this->plugin_name, DRK_LITE_DIR_URL . 'admin/js/darkify-admin.js', array(), $this->version, 'all'); |
| 51 |
} |
| 52 |
|
| 53 |
public function add_plugin_action_links($links, $file) |
| 54 |
{ |
| 55 |
if (DRK_LITE_BASENAME === $file) { |
| 56 |
$new_links = array( |
| 57 |
sprintf('<a href="%s">%s</a>', admin_url('admin.php?page=darkify#tab=control'), __('Settings', 'darkify')), |
| 58 |
); |
| 59 |
return array_merge($new_links, $links); |
| 60 |
} |
| 61 |
return $links; |
| 62 |
} |
| 63 |
|
| 64 |
function darkify_admin_enqueue($page) |
| 65 |
{ |
| 66 |
$options = get_option('darkify'); |
| 67 |
$enable_default_dark_mode = $options['enable_default_dark_mode']; |
| 68 |
$enable_time = $options['enable_time']; |
| 69 |
$enable_time_based_dark = isset($enable_time['enable_time_based_dark']) ? $enable_time['enable_time_based_dark'] : ""; |
| 70 |
$time_based_dark_start = isset($enable_time['time_based_dark_start']['from']) ? $enable_time['time_based_dark_start']['from'] : ""; |
| 71 |
$time_based_dark_stop = isset($enable_time['time_based_dark_start']['to']) ? $enable_time['time_based_dark_start']['to'] : ""; |
| 72 |
$grayscale = $options['brightness']; |
| 73 |
$enable_low_image_brightness = isset($grayscale['enable_low_image_brightness']) ? $grayscale['enable_low_image_brightness'] : ""; |
| 74 |
$low_image_brightness_label = isset($grayscale['low_image_brightness_label']) ? $grayscale['low_image_brightness_label'] : ""; |
| 75 |
$grayscale = isset($options['grayscale']); |
| 76 |
$enable_image_grayscale = isset($grayscale['enable_image_grayscale']) ? $grayscale['enable_image_grayscale'] : ""; |
| 77 |
$image_grayscale_label = isset($grayscale['image_grayscale_label']) ? $grayscale['image_grayscale_label'] : ""; |
| 78 |
$disallowed_low_grayscale_images = isset($grayscale['disallowed_low_grayscale_images']) ? $grayscale['disallowed_low_grayscale_images'] : ""; |
| 79 |
$darken_background = isset($options['darken_background']); |
| 80 |
$enable_low_image_darken = isset($darken_background['enable_low_image_darken']) ? $darken_background['enable_low_image_darken'] : ""; |
| 81 |
$low_image_darken_label = isset($darken_background['low_image_darken_label']) ? $darken_background['low_image_darken_label'] : ""; |
| 82 |
$video_brightness = isset($options['video_brightness']); |
| 83 |
$enable_low_video_brightness = isset($video_brightness['enable_low_video_brightness']) ? $video_brightness['enable_low_video_brightness'] : ""; |
| 84 |
$low_video_brightness_label = isset($video_brightness['low_video_brightness_label']) ? $video_brightness['low_video_brightness_label'] : ""; |
| 85 |
$video_grayscale = isset($options['video_grayscale']); |
| 86 |
$enable_video_grayscale = isset($video_grayscale['enable_video_grayscale']) ? $video_grayscale['enable_video_grayscale'] : ""; |
| 87 |
|
| 88 |
if ($options["enable_admin_panel_dark_mode"]) { |
| 89 |
if ($this->darkify_is_dark_mode_allowed()) { |
| 90 |
if (!wp_style_is('darkify-admin-switch', 'enqueued')) { |
| 91 |
wp_enqueue_style('darkify-admin-switch', DRK_LITE_DIR_URL . 'assets/css/client_main.css', array(), $this->version, 'all'); |
| 92 |
} |
| 93 |
wp_enqueue_script('darkify-admin-client-main', DRK_LITE_DIR_URL . 'assets/js/client_main.js', array(), $this->version, true); |
| 94 |
|
| 95 |
$darkify_data = array( |
| 96 |
'darkify_switch_unique_id' => $this->unique_id, |
| 97 |
'darkify_is_this_admin_panel' => is_admin() ? "1" : "0", |
| 98 |
'darkify_enable_default_dark_mode' => $enable_default_dark_mode, |
| 99 |
'darkify_enable_os_aware' => $options["enable_os_aware"], |
| 100 |
'darkify_enable_keyboard_shortcut' => $options["enable_keyboard_shortcut"], |
| 101 |
'darkify_enable_time_based_dark' => $enable_time_based_dark, |
| 102 |
'darkify_time_based_dark_start' => $time_based_dark_start ? $time_based_dark_start : "19:00", |
| 103 |
'darkify_time_based_dark_stop' => $time_based_dark_stop ? $time_based_dark_stop : "07:00", |
| 104 |
'darkify_alternative_dark_mode_switch' => $options["alternative_dark_mode_switcher"], |
| 105 |
'darkify_enable_low_image_brightness' => $enable_low_image_brightness, |
| 106 |
'darkify_image_brightness_to' => $low_image_brightness_label, |
| 107 |
'darkify_enable_image_grayscale' => $enable_image_grayscale, |
| 108 |
'darkify_image_grayscale_to' => $image_grayscale_label, |
| 109 |
'darkify_disallowed_grayscale_images' => $disallowed_low_grayscale_images, |
| 110 |
'darkify_enable_bg_image_darken' => $enable_low_image_darken, |
| 111 |
'darkify_bg_image_darken_to' => $low_image_darken_label, |
| 112 |
'darkify_enable_invert_inline_svg' => $options["enable_invert_inline_svg"], |
| 113 |
'darkify_enable_low_video_brightness' => $enable_low_video_brightness, |
| 114 |
'darkify_video_brightness_to' => $low_video_brightness_label, |
| 115 |
'darkify_enable_video_grayscale' => $enable_video_grayscale, |
| 116 |
// Add more variables as needed... |
| 117 |
); |
| 118 |
wp_localize_script('darkify-admin-client-main', 'darkify_data', $darkify_data); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
function darkify_admin_bar_switch($wp_admin_bar) |
| 124 |
{ |
| 125 |
if ($this->darkify_is_dark_mode_allowed()) { |
| 126 |
$args = array( |
| 127 |
'parent' => 'top-secondary', |
| 128 |
'id' => 'darkify_admin_bar_switch_container', |
| 129 |
'meta' => array( |
| 130 |
'class' => 'darkify_admin_bar_switch_container', |
| 131 |
'onclick' => 'darkify_switch_trigger()', |
| 132 |
) |
| 133 |
); |
| 134 |
$wp_admin_bar->add_node($args); |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
function darkify_is_dark_mode_allowed() |
| 139 |
{ |
| 140 |
|
| 141 |
$options = get_option('darkify'); |
| 142 |
$disallowed_admin_pages = isset($options["disallowed_admin_pages"]) ? $options["disallowed_admin_pages"] : ""; |
| 143 |
/* Disable on Disallowed Admin Plugins */ |
| 144 |
if ($this->utils->isRestrictedByDisallowedAdminPages($disallowed_admin_pages)) { |
| 145 |
return False; |
| 146 |
} |
| 147 |
return True; |
| 148 |
} |
| 149 |
|
| 150 |
function darkify_admin_header_script() |
| 151 |
{ |
| 152 |
if ($this->darkify_is_dark_mode_allowed()) { |
| 153 |
include_once DRK_LITE_PATH . "public/templates/header_script.php"; |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
function darkify_admin_footer_script() |
| 158 |
{ |
| 159 |
if ($this->darkify_is_dark_mode_allowed()) { |
| 160 |
include_once DRK_LITE_PATH . "public/templates/footer_script.php"; |
| 161 |
} |
| 162 |
} |
| 163 |
} |
| 164 |
|