| @@ -1,207 +1,80 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -use ThemeAtelier\Darkify\Includes\DarkifyUtils; | |
| 3 | +use ThemeAtelier\Darkify\Helpers\DarkifyUtils; | |
| 4 | +use ThemeAtelier\Darkify\Helpers\Helpers; | |
| 4 | 5 | |
| 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; | |
| 6 | +$options = get_option('darkify_pro'); | |
| 12 | 7 | $hide_on_desktop = isset($options["hide_on_desktop"]) ? $options["hide_on_desktop"] : ''; |
| 13 | 8 | $hide_on_mobile = isset($options["hide_on_mobile"]) ? $options["hide_on_mobile"] : ''; |
| 14 | 9 | $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 | 10 | $hide_dark_mode_on_mobile = isset($hide_on_mobile["hide_dark_mode_on_mobile"]) ? $hide_on_mobile["hide_dark_mode_on_mobile"] : 0; |
| 17 | 11 | $type_of_hide_by = isset($hide_on_mobile["type_of_hide_by"]) ? $hide_on_mobile["type_of_hide_by"] : 0; |
| 12 | +$normal_custom_css = isset($hide_on_mobile["normal_custom_css"]) ? $hide_on_mobile["normal_custom_css"] : ""; | |
| 13 | +$disallowed_elements_force_to_correct = isset($hide_on_mobile["disallowed_elements_force_to_correct"]) ? $hide_on_mobile["disallowed_elements_force_to_correct"] : ""; | |
| 14 | +$custom_css = isset($hide_on_mobile["custom_css"]) ? $hide_on_mobile["custom_css"] : ""; | |
| 18 | 15 | |
| 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 | 16 | if (!is_admin()) { |
| 24 | - if ($enable_dark_mode_switch) { | |
| 17 | + if ($options["enable_dark_mode_switch"]) { | |
| 25 | 18 | 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'); | |
| 19 | + include_once Helpers::darkify_locate_template('views/switch.php'); | |
| 27 | 20 | } |
| 28 | 21 | } |
| 29 | 22 | } |
| 23 | + | |
| 30 | 24 | ?> |
| 31 | 25 | |
| 32 | 26 | <style type="text/css" class="darkify_inline_css"> |
| 33 | - <?php echo wp_strip_all_tags(DarkifyUtils::parseAndProcessNormalCustomCSS($normal_custom_css)); ?> | |
| 27 | + <?php echo esc_attr(DarkifyUtils::parseAndProcessNormalCustomCSS($normal_custom_css)); ?> | |
| 34 | 28 | </style> |
| 35 | 29 | <style type="text/css" class="darkify_inline_css"> |
| 36 | - <?php echo wp_strip_all_tags(DarkifyUtils::parseAndProcessCustomCSS($custom_css, false)); ?> | |
| 30 | + <?php echo esc_attr(DarkifyUtils::parseAndProcessCustomCSS($custom_css, $disallowed_elements_force_to_correct)); ?> | |
| 37 | 31 | </style> |
| 38 | 32 | |
| 33 | + | |
| 34 | + | |
| 39 | 35 | <?php if (!is_admin()) { ?> |
| 40 | 36 | <script type="text/javascript" class="darkify_inline_js"> |
| 41 | 37 | document.addEventListener("DOMContentLoaded", function(event) { |
| 38 | + darkify_init_draggable_floating_switch(); | |
| 42 | 39 | darkify_init_alternative_dark_mode_switch(); |
| 43 | 40 | }); |
| 44 | 41 | </script> |
| 45 | -<?php } | |
| 42 | +<?php } ?> | |
| 46 | 43 | |
| 47 | -if (is_admin() && $block_editor_dark_mode) { | |
| 48 | -?><style type="text/css" class="darkify_inline_css"> | |
| 49 | - <?php echo esc_html(DarkifyUtils::parseAndProcessCustomCSS($admin_custom_css, 'yes')); ?> | |
| 50 | - </style> | |
| 51 | - <?php | |
| 52 | - /* Check if block editor is on, then add the dark mode button there */ | |
| 53 | - if ($enable_admin_panel_dark_mode) { | |
| 54 | - if (class_exists('WP_Block_Type_Registry')) { | |
| 55 | - | |
| 56 | - if (get_current_screen() && 'post' === get_current_screen()->base && ('post' === get_current_screen()->post_type || 'page' === get_current_screen()->post_type)) { ?> | |
| 44 | +<?php if (is_admin()) { ?> | |
| 45 | + <?php /* Check if block editor is on, then add the dark mode button there */ ?> | |
| 46 | + <?php if ($enable_admin_panel_dark_mode) { ?> | |
| 47 | + <?php if (class_exists('WP_Block_Type_Registry')) { ?> | |
| 48 | + <?php if (get_current_screen() && 'post' === get_current_screen()->base && ('post' === get_current_screen()->post_type || 'page' === get_current_screen()->post_type)) { ?> | |
| 57 | 49 | <script type="text/javascript" class="darkify_inline_js"> |
| 58 | - (function checkForWP() { | |
| 59 | - if ( | |
| 60 | - typeof wp === 'undefined' || | |
| 61 | - typeof wp.domReady === 'undefined' || | |
| 62 | - typeof wp.plugins === 'undefined' || | |
| 63 | - typeof wp.element === 'undefined' | |
| 64 | - ) { | |
| 65 | - return setTimeout(checkForWP, 200); | |
| 66 | - } | |
| 67 | - | |
| 68 | - wp.domReady(function() { | |
| 69 | - | |
| 70 | - function injectDarkify() { | |
| 71 | - // Target the LEFT side toolbar | |
| 72 | - const target = | |
| 73 | - document.querySelector('.editor-header__toolbar') || | |
| 74 | - document.querySelector('.edit-post-header-toolbar') || | |
| 75 | - document.querySelector('.editor-document-tools'); | |
| 76 | - | |
| 77 | - if (!target) return false; | |
| 78 | - if (target.querySelector('.darkify-dropdown')) return true; | |
| 79 | - | |
| 80 | - const themes = [{ | |
| 81 | - value: 'set1', | |
| 82 | - label: 'Carbon Mist', | |
| 83 | - cls: 'carbon_mist' | |
| 84 | - }, | |
| 85 | - { | |
| 86 | - value: 'set3', | |
| 87 | - label: 'Midnight Reverie', | |
| 88 | - cls: 'midnight_reverie' | |
| 89 | - }, | |
| 90 | - { | |
| 91 | - value: 'set9', | |
| 92 | - label: 'Verdant Depths', | |
| 93 | - cls: 'verdant_depths' | |
| 94 | - }, | |
| 95 | - { | |
| 96 | - value: 'set6', | |
| 97 | - label: 'Celestial Tide', | |
| 98 | - cls: 'celestial_tide' | |
| 99 | - }, | |
| 100 | - { | |
| 101 | - value: 'set10', | |
| 102 | - label: 'Emberwood', | |
| 103 | - cls: 'emberwood' | |
| 104 | - }, | |
| 105 | - { | |
| 106 | - value: 'set2', | |
| 107 | - label: 'Glacier Drift (Pro)', | |
| 108 | - cls: 'glacier_drift' | |
| 109 | - }, | |
| 110 | - { | |
| 111 | - value: 'set4', | |
| 112 | - label: 'Indigo Veil (Pro)', | |
| 113 | - cls: 'indigo_veil' | |
| 114 | - }, | |
| 115 | - { | |
| 116 | - value: 'set5', | |
| 117 | - label: 'Duskmire (Pro)', | |
| 118 | - cls: 'duskmire' | |
| 119 | - }, | |
| 120 | - { | |
| 121 | - value: 'set7', | |
| 122 | - label: 'Tidal Frost (Pro)', | |
| 123 | - cls: 'tidal_frost' | |
| 124 | - }, | |
| 125 | - { | |
| 126 | - value: 'set8', | |
| 127 | - label: 'Blue Bastion (Pro)', | |
| 128 | - cls: 'blue_bastion' | |
| 129 | - }, | |
| 130 | - { | |
| 131 | - value: 'set11', | |
| 132 | - label: 'Crimson Veil (Pro)', | |
| 133 | - cls: 'crimson_veil' | |
| 134 | - }, | |
| 135 | - ]; | |
| 136 | - | |
| 137 | - // Dark mode toggle button | |
| 138 | - const button = document.createElement('button'); | |
| 139 | - button.className = 'darkify_block_editor_switch darkify_ignore'; | |
| 140 | - button.innerHTML = '<div class="icon"></div>'; | |
| 141 | - button.onclick = function() { | |
| 142 | - darkify_switch_trigger(); | |
| 143 | - }; | |
| 144 | - | |
| 145 | - // Dropdown wrapper | |
| 146 | - const wrapper = document.createElement('div'); | |
| 147 | - wrapper.className = 'darkify-dropdown'; | |
| 148 | - wrapper.style.cssText = 'position:relative;display:inline-flex;align-items:center;'; | |
| 149 | - | |
| 150 | - const selected = document.createElement('div'); | |
| 151 | - selected.className = 'darkify-selected'; | |
| 152 | - | |
| 153 | - const list = document.createElement('div'); | |
| 154 | - list.className = 'darkify-options'; | |
| 155 | - | |
| 156 | - themes.forEach(t => { | |
| 157 | - const item = document.createElement('div'); | |
| 158 | - item.className = 'darkify-option'; | |
| 159 | - item.innerHTML = `<span class="theme_color ${t.cls}"></span> ${t.label}`; | |
| 160 | - item.onclick = () => { | |
| 161 | - selected.innerHTML = item.innerHTML; | |
| 162 | - localStorage.darkify_selected_theme = t.value; | |
| 163 | - darkify_theme_select(t.value); | |
| 164 | - list.classList.remove('show'); | |
| 165 | - }; | |
| 166 | - list.appendChild(item); | |
| 167 | - }); | |
| 168 | - | |
| 169 | - // Load saved theme | |
| 170 | - const savedVal = localStorage.darkify_selected_theme || 'set1'; | |
| 171 | - const savedTheme = themes.find(t => t.value === savedVal) || themes[0]; | |
| 172 | - selected.innerHTML = `<span class="theme_color ${savedTheme.cls}"></span> ${savedTheme.label}`; | |
| 173 | - | |
| 174 | - selected.onclick = (e) => { | |
| 175 | - e.stopPropagation(); | |
| 176 | - list.classList.toggle('show'); | |
| 177 | - }; | |
| 178 | - | |
| 179 | - document.addEventListener('click', function(e) { | |
| 180 | - if (!wrapper.contains(e.target)) list.classList.remove('show'); | |
| 181 | - }); | |
| 182 | - | |
| 183 | - wrapper.appendChild(selected); | |
| 184 | - wrapper.appendChild(list); | |
| 185 | - | |
| 186 | - // Append to END of left toolbar | |
| 187 | - target.appendChild(button); | |
| 188 | - target.appendChild(wrapper); | |
| 189 | - | |
| 190 | - return true; | |
| 191 | - } | |
| 192 | - | |
| 193 | - if (!injectDarkify()) { | |
| 194 | - const interval = setInterval(function() { | |
| 195 | - if (injectDarkify()) clearInterval(interval); | |
| 196 | - }, 300); | |
| 197 | - setTimeout(function() { | |
| 198 | - clearInterval(interval); | |
| 199 | - }, 10000); | |
| 200 | - } | |
| 50 | + wp.domReady(function() { | |
| 51 | + const observer = new MutationObserver(function(mutations) { | |
| 52 | + mutations.forEach(function(mutation) { | |
| 53 | + if (mutation.addedNodes && mutation.addedNodes.length) { | |
| 54 | + for (let i = 0; i < mutation.addedNodes.length; i++) { | |
| 55 | + const node = mutation.addedNodes[i]; | |
| 56 | + if (node.classList && node.classList.contains('edit-post-header-toolbar')) { | |
| 57 | + const button = document.createElement('button'); | |
| 58 | + button.className = 'darkify_block_editor_switch darkify_ignore'; | |
| 59 | + button.innerHTML = '<div class="icon"></div>'; | |
| 60 | + button.onclick = function() { | |
| 61 | + darkify_switch_trigger(); | |
| 62 | + }; | |
| 63 | + node.appendChild(button); | |
| 64 | + observer.disconnect(); | |
| 65 | + return; | |
| 66 | + } | |
| 67 | + } | |
| 68 | + } | |
| 69 | + }); | |
| 201 | 70 | }); |
| 202 | - })(); | |
| 71 | + observer.observe(document.body, { | |
| 72 | + childList: true, | |
| 73 | + subtree: true | |
| 74 | + }); | |
| 75 | + }); | |
| 203 | 76 | </script> |
| 204 | -<?php } | |
| 205 | - } | |
| 206 | - } | |
| 207 | -} | |
| 77 | + <?php } ?> | |
| 78 | + <?php } ?> | |
| 79 | + <?php } ?> | |
| 80 | +<?php } ?> | |