| 1 |
<?php |
| 2 |
|
| 3 |
use ThemeAtelier\Darkify\Includes\DarkifyUtils; |
| 4 |
|
| 5 |
// don't call the file directly. |
| 6 |
if (!defined('ABSPATH')) { |
| 7 |
exit; |
| 8 |
} |
| 9 |
|
| 10 |
$options = get_option('darkify'); |
| 11 |
$enable_dark_mode_switch = isset($options["enable_dark_mode_switch"]) ? $options["enable_dark_mode_switch"] : true; |
| 12 |
$hide_on_desktop = isset($options["hide_on_desktop"]) ? $options["hide_on_desktop"] : ''; |
| 13 |
$hide_on_mobile = isset($options["hide_on_mobile"]) ? $options["hide_on_mobile"] : ''; |
| 14 |
$enable_admin_panel_dark_mode = isset($options["enable_admin_panel_dark_mode"]) ? $options["enable_admin_panel_dark_mode"] : ''; |
| 15 |
$block_editor_dark_mode = isset($options['block_editor_dark_mode']) ? $options['block_editor_dark_mode'] : false; |
| 16 |
$hide_dark_mode_on_mobile = isset($hide_on_mobile["hide_dark_mode_on_mobile"]) ? $hide_on_mobile["hide_dark_mode_on_mobile"] : 0; |
| 17 |
$type_of_hide_by = isset($hide_on_mobile["type_of_hide_by"]) ? $hide_on_mobile["type_of_hide_by"] : 0; |
| 18 |
|
| 19 |
$normal_custom_css = isset($options["normal_custom_css"]) ? $options["normal_custom_css"] : ""; |
| 20 |
$custom_css = isset($options["custom_css"]) ? $options["custom_css"] : ""; |
| 21 |
$admin_custom_css = isset($options['admin_custom_css']) ? $options['admin_custom_css'] : ""; |
| 22 |
|
| 23 |
if (!is_admin()) { |
| 24 |
if ($enable_dark_mode_switch) { |
| 25 |
if (!$this->utils->is_hidden_by_user_agent($hide_on_desktop, $hide_dark_mode_on_mobile, $type_of_hide_by)) { |
| 26 |
include_once DarkifyUtils::darkify_locate_template('views/switch.php'); |
| 27 |
} |
| 28 |
} |
| 29 |
} |
| 30 |
?> |
| 31 |
|
| 32 |
<style type="text/css" class="darkify_inline_css"> |
| 33 |
<?php |
| 34 |
// CSS body, not HTML: no core output-escaping function applies. The rules |
| 35 |
// are assembled by parseAndProcessNormalCustomCSS() and wp_strip_all_tags() |
| 36 |
// removes any tag-based `</style>` breakout, so the string is safe to print. |
| 37 |
echo wp_strip_all_tags(DarkifyUtils::parseAndProcessNormalCustomCSS($normal_custom_css)); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 38 |
?> |
| 39 |
</style> |
| 40 |
<style type="text/css" class="darkify_inline_css"> |
| 41 |
<?php |
| 42 |
echo wp_strip_all_tags(DarkifyUtils::parseAndProcessCustomCSS($custom_css, false)); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 43 |
?> |
| 44 |
</style> |
| 45 |
|
| 46 |
<?php if (!is_admin()) { ?> |
| 47 |
<script type="text/javascript" class="darkify_inline_js"> |
| 48 |
document.addEventListener("DOMContentLoaded", function(event) { |
| 49 |
darkify_init_alternative_dark_mode_switch(); |
| 50 |
darkify_init_attention_effect(); |
| 51 |
if (typeof darkify_init_draggable_floating_switch === "function") { |
| 52 |
darkify_init_draggable_floating_switch(); |
| 53 |
} |
| 54 |
}); |
| 55 |
</script> |
| 56 |
<?php } |
| 57 |
|
| 58 |
if (is_admin() && $block_editor_dark_mode) { |
| 59 |
?><style type="text/css" class="darkify_inline_css"> |
| 60 |
<?php echo esc_html(DarkifyUtils::parseAndProcessCustomCSS($admin_custom_css, 'yes')); ?> |
| 61 |
</style> |
| 62 |
<?php |
| 63 |
/* Block editor dark switch — injected whenever Block Editor Dark Mode is on, |
| 64 |
independently of Admin Panel Dark Mode. */ |
| 65 |
if (class_exists('WP_Block_Type_Registry')) { |
| 66 |
|
| 67 |
if (get_current_screen() && 'post' === get_current_screen()->base && ('post' === get_current_screen()->post_type || 'page' === get_current_screen()->post_type)) { ?> |
| 68 |
<script type="text/javascript" class="darkify_inline_js"> |
| 69 |
(function checkForWP() { |
| 70 |
if ( |
| 71 |
typeof wp === 'undefined' || |
| 72 |
typeof wp.domReady === 'undefined' || |
| 73 |
typeof wp.plugins === 'undefined' || |
| 74 |
typeof wp.element === 'undefined' |
| 75 |
) { |
| 76 |
return setTimeout(checkForWP, 200); |
| 77 |
} |
| 78 |
|
| 79 |
wp.domReady(function() { |
| 80 |
|
| 81 |
function injectDarkify() { |
| 82 |
// Target the LEFT side toolbar |
| 83 |
const target = |
| 84 |
document.querySelector('.editor-header__toolbar') || |
| 85 |
document.querySelector('.edit-post-header-toolbar') || |
| 86 |
document.querySelector('.editor-document-tools'); |
| 87 |
|
| 88 |
if (!target) return false; |
| 89 |
if (target.querySelector('.darkify-dropdown')) return true; |
| 90 |
|
| 91 |
const themes = [{ |
| 92 |
// Hands the colours back to Dark Reader's own |
| 93 |
// per-colour derivation instead of pinning a |
| 94 |
// background and a text colour. First in the |
| 95 |
// list and the default, because a derived |
| 96 |
// palette keeps distinctions a fixed one |
| 97 |
// flattens. |
| 98 |
value: 'auto', |
| 99 |
label: 'Auto', |
| 100 |
cls: 'auto_palette' |
| 101 |
}, |
| 102 |
{ |
| 103 |
value: 'set1', |
| 104 |
label: 'Carbon Mist', |
| 105 |
cls: 'carbon_mist' |
| 106 |
}, |
| 107 |
{ |
| 108 |
value: 'set3', |
| 109 |
label: 'Midnight Reverie', |
| 110 |
cls: 'midnight_reverie' |
| 111 |
}, |
| 112 |
{ |
| 113 |
value: 'set9', |
| 114 |
label: 'Verdant Depths', |
| 115 |
cls: 'verdant_depths' |
| 116 |
}, |
| 117 |
{ |
| 118 |
value: 'set6', |
| 119 |
label: 'Celestial Tide', |
| 120 |
cls: 'celestial_tide' |
| 121 |
}, |
| 122 |
{ |
| 123 |
value: 'set10', |
| 124 |
label: 'Emberwood', |
| 125 |
cls: 'emberwood' |
| 126 |
}, |
| 127 |
{ |
| 128 |
value: 'set2', |
| 129 |
label: 'Glacier Drift', |
| 130 |
cls: 'glacier_drift', |
| 131 |
pro: true |
| 132 |
}, |
| 133 |
{ |
| 134 |
value: 'set4', |
| 135 |
label: 'Indigo Veil', |
| 136 |
cls: 'indigo_veil', |
| 137 |
pro: true |
| 138 |
}, |
| 139 |
{ |
| 140 |
value: 'set5', |
| 141 |
label: 'Duskmire', |
| 142 |
cls: 'duskmire', |
| 143 |
pro: true |
| 144 |
}, |
| 145 |
{ |
| 146 |
value: 'set7', |
| 147 |
label: 'Tidal Frost', |
| 148 |
cls: 'tidal_frost', |
| 149 |
pro: true |
| 150 |
}, |
| 151 |
{ |
| 152 |
value: 'set8', |
| 153 |
label: 'Blue Bastion', |
| 154 |
cls: 'blue_bastion', |
| 155 |
pro: true |
| 156 |
}, |
| 157 |
{ |
| 158 |
value: 'set11', |
| 159 |
label: 'Crimson Veil', |
| 160 |
cls: 'crimson_veil', |
| 161 |
pro: true |
| 162 |
}, |
| 163 |
]; |
| 164 |
|
| 165 |
// Dark mode toggle button |
| 166 |
const button = document.createElement('button'); |
| 167 |
button.className = 'darkify_block_editor_switch darkify_ignore'; |
| 168 |
button.innerHTML = '<div class="icon"></div>'; |
| 169 |
button.onclick = function() { |
| 170 |
darkify_switch_trigger(); |
| 171 |
}; |
| 172 |
|
| 173 |
// Dropdown wrapper |
| 174 |
const wrapper = document.createElement('div'); |
| 175 |
wrapper.className = 'darkify-dropdown'; |
| 176 |
wrapper.style.cssText = 'position:relative;display:inline-flex;align-items:center;'; |
| 177 |
|
| 178 |
const selected = document.createElement('div'); |
| 179 |
selected.className = 'darkify-selected'; |
| 180 |
|
| 181 |
const list = document.createElement('div'); |
| 182 |
list.className = 'darkify-options'; |
| 183 |
|
| 184 |
themes.forEach(t => { |
| 185 |
const item = document.createElement('div'); |
| 186 |
if (t.pro) { |
| 187 |
item.className = 'darkify-option darkify-pro-option'; |
| 188 |
item.innerHTML = `<span class="theme_color ${t.cls}"></span> ${t.label}<span class="darkify-option-pro-badge">PRO</span>`; |
| 189 |
item.title = 'Upgrade to Pro to unlock this theme'; |
| 190 |
} else { |
| 191 |
item.className = 'darkify-option'; |
| 192 |
item.innerHTML = `<span class="theme_color ${t.cls}"></span> ${t.label}`; |
| 193 |
item.onclick = () => { |
| 194 |
selected.innerHTML = `<span class="theme_color ${t.cls}"></span> ${t.label}`; |
| 195 |
localStorage.darkify_selected_theme = t.value; |
| 196 |
darkify_theme_select(t.value); |
| 197 |
list.classList.remove('show'); |
| 198 |
}; |
| 199 |
} |
| 200 |
list.appendChild(item); |
| 201 |
}); |
| 202 |
|
| 203 |
// Load saved theme — fall back to first free theme if saved value is pro-only |
| 204 |
// Per-user choice first, then the site default from |
| 205 |
// settings, then Auto — the same precedence the engine |
| 206 |
// applies, so the label always names what is painted. |
| 207 |
const savedVal = localStorage.darkify_selected_theme |
| 208 |
|| (typeof darkify_editor_palette_default !== 'undefined' ? darkify_editor_palette_default : '') |
| 209 |
|| 'auto'; |
| 210 |
const savedTheme = themes.find(t => t.value === savedVal && !t.pro) || themes[0]; |
| 211 |
selected.innerHTML = `<span class="theme_color ${savedTheme.cls}"></span> ${savedTheme.label}`; |
| 212 |
|
| 213 |
selected.onclick = (e) => { |
| 214 |
e.stopPropagation(); |
| 215 |
list.classList.toggle('show'); |
| 216 |
}; |
| 217 |
|
| 218 |
document.addEventListener('click', function(e) { |
| 219 |
if (!wrapper.contains(e.target)) list.classList.remove('show'); |
| 220 |
}); |
| 221 |
|
| 222 |
wrapper.appendChild(selected); |
| 223 |
wrapper.appendChild(list); |
| 224 |
|
| 225 |
// Append to END of left toolbar |
| 226 |
target.appendChild(button); |
| 227 |
target.appendChild(wrapper); |
| 228 |
|
| 229 |
return true; |
| 230 |
} |
| 231 |
|
| 232 |
if (!injectDarkify()) { |
| 233 |
const interval = setInterval(function() { |
| 234 |
if (injectDarkify()) clearInterval(interval); |
| 235 |
}, 300); |
| 236 |
setTimeout(function() { |
| 237 |
clearInterval(interval); |
| 238 |
}, 10000); |
| 239 |
} |
| 240 |
}); |
| 241 |
})(); |
| 242 |
</script> |
| 243 |
<?php } |
| 244 |
} |
| 245 |
} |
| 246 |
|