| @@ -1,15 +1,12 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace ThemeAtelier\Darkify\Frontend; |
| 4 | 4 | |
| 5 | -use ThemeAtelier\Darkify\Includes\DarkifyExternalSupport; | |
| 6 | -use ThemeAtelier\Darkify\Includes\DarkifyUtils; | |
| 5 | +use ThemeAtelier\Darkify\Helpers\DarkifyExternalSupport; | |
| 6 | +use ThemeAtelier\Darkify\Helpers\DarkifyUtils; | |
| 7 | +use ThemeAtelier\Darkify\Helpers\Helpers; | |
| 7 | 8 | |
| 8 | -if (!defined('ABSPATH')) { | |
| 9 | - die; | |
| 10 | -} // Cannot access directly. | |
| 11 | - | |
| 12 | 9 | /** |
| 13 | 10 | * The public-facing functionality of the plugin. |
| 14 | 11 | * |
| 15 | 12 | * @link https://themeatelier.net |
| @@ -83,9 +80,9 @@ | ||
| 83 | 80 | if ($enable_switcher_in_menu) { |
| 84 | 81 | add_filter('wp_nav_menu_items', array($this, 'darkify_switch_in_menu'), 10, 2); |
| 85 | 82 | } |
| 86 | 83 | |
| 87 | - add_action('wp_enqueue_scripts', array($this, 'darkify_client_enqueue'), 100); | |
| 84 | + add_action('wp_enqueue_scripts', array($this, 'darkify_client_enqueue')); | |
| 88 | 85 | add_action('login_enqueue_scripts', array($this, 'darkify_client_enqueue')); |
| 89 | 86 | add_action('register_enqueue_scripts', array($this, 'darkify_client_enqueue')); |
| 90 | 87 | add_action('wp_head', array($this, 'darkify_client_header_script'), 1); |
| 91 | 88 | add_action('login_head', array($this, 'darkify_client_header_script'), 1); |
| @@ -92,235 +89,84 @@ | ||
| 92 | 89 | add_action('register_head', array($this, 'darkify_client_header_script'), 1); |
| 93 | 90 | add_action('wp_footer', array($this, 'darkify_client_footer_script')); |
| 94 | 91 | add_action('login_footer', array($this, 'darkify_client_footer_script')); |
| 95 | 92 | add_action('register_footer', array($this, 'darkify_client_footer_script')); |
| 96 | - | |
| 97 | - add_action('init', array($this, 'darkify_register_switcher_styles'), 5); | |
| 98 | - add_filter('autoptimize_filter_js_exclude', array($this, 'darkify_exclude_js_from_cache_plugins')); | |
| 99 | 93 | } |
| 100 | 94 | |
| 101 | - public function darkify_exclude_js_from_cache_plugins($excludes) | |
| 102 | - { | |
| 103 | - $excludes .= ',client_main.js,client_main.min.js'; | |
| 104 | - return $excludes; | |
| 105 | - } | |
| 106 | 95 | |
| 107 | - public function darkify_register_switcher_styles() | |
| 96 | + public function darkify_client_enqueue() | |
| 108 | 97 | { |
| 109 | - $min = (apply_filters('darkify_dev_mode', false) || WP_DEBUG) ? '' : '.min'; | |
| 110 | - wp_register_style('darkify-client-main', DARKIFY_DIR_URL . 'src/assets/css/client_main' . $min . '.css', array(), DARKIFY_VERSION); | |
| 111 | - wp_register_style('theme-classic', DARKIFY_DIR_URL . 'src/assets/css/switcher/classic' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); | |
| 112 | - wp_register_style('theme-expand', DARKIFY_DIR_URL . 'src/assets/css/switcher/expand' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); | |
| 113 | - wp_register_style('theme-inner-moon', DARKIFY_DIR_URL . 'src/assets/css/switcher/inner-moon' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); | |
| 114 | - wp_register_style('theme-within', DARKIFY_DIR_URL . 'src/assets/css/switcher/within' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); | |
| 115 | - wp_register_style('theme-orbit', DARKIFY_DIR_URL . 'src/assets/css/switcher/orbit' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); | |
| 116 | - } | |
| 117 | - | |
| 118 | - function darkify_client_enqueue() | |
| 119 | - { | |
| 120 | 98 | if ($this->darkify_is_dark_mode_allowed()) { |
| 121 | 99 | $options = get_option('darkify'); |
| 122 | - $min = (apply_filters('darkify_dev_mode', false) || WP_DEBUG) ? '' : '.min'; | |
| 100 | + $enable_dark_switcher = $options['enable_dark_switcher']; | |
| 101 | + $enable_time = $options['enable_time']; | |
| 123 | 102 | |
| 124 | - wp_enqueue_style('darkify-client-main', DARKIFY_DIR_URL . 'src/assets/css/client_main' . $min . '.css', array(), DARKIFY_VERSION); | |
| 125 | - wp_enqueue_script('darkify-client-main', DARKIFY_DIR_URL . 'src/assets/js/client_main' . $min . '.js', array('jquery'), DARKIFY_VERSION); | |
| 103 | + $enable_default_dark_mode = $options['enable_default_dark_mode']; | |
| 104 | + $enable_time = $options['enable_time']; | |
| 105 | + $enable_time_based_dark = isset($enable_time['enable_time_based_dark']) ? $enable_time['enable_time_based_dark'] : ""; | |
| 106 | + $time_based_dark_start = isset($enable_time['time_based_dark_start']['from']) ? $enable_time['time_based_dark_start']['from'] : ""; | |
| 107 | + $time_based_dark_stop = isset($enable_time['time_based_dark_start']['to']) ? $enable_time['time_based_dark_start']['to'] : ""; | |
| 108 | + $grayscale = $options['brightness']; | |
| 109 | + $enable_low_image_brightness = isset($grayscale['enable_low_image_brightness']) ? $grayscale['enable_low_image_brightness'] : ""; | |
| 110 | + $low_image_brightness_label = isset($grayscale['low_image_brightness_label']) ? $grayscale['low_image_brightness_label'] : ""; | |
| 111 | + $grayscale = isset($options['grayscale']); | |
| 112 | + $enable_image_grayscale = isset($grayscale['enable_image_grayscale']) ? $grayscale['enable_image_grayscale'] : ""; | |
| 113 | + $image_grayscale_label = isset($grayscale['image_grayscale_label']) ? $grayscale['image_grayscale_label'] : ""; | |
| 114 | + $disallowed_low_grayscale_images = isset($grayscale['disallowed_low_grayscale_images']) ? $grayscale['disallowed_low_grayscale_images'] : ""; | |
| 115 | + $darken_background = isset($options['darken_background']); | |
| 116 | + $enable_low_image_darken = isset($darken_background['enable_low_image_darken']) ? $darken_background['enable_low_image_darken'] : ""; | |
| 117 | + $low_image_darken_label = isset($darken_background['low_image_darken_label']) ? $darken_background['low_image_darken_label'] : ""; | |
| 118 | + $video_brightness = isset($options['video_brightness']); | |
| 119 | + $enable_low_video_brightness = isset($video_brightness['enable_low_video_brightness']) ? $video_brightness['enable_low_video_brightness'] : ""; | |
| 120 | + $low_video_brightness_label = isset($video_brightness['low_video_brightness_label']) ? $video_brightness['low_video_brightness_label'] : ""; | |
| 121 | + $video_grayscale = isset($options['video_grayscale']); | |
| 122 | + $enable_video_grayscale = isset($video_grayscale['enable_video_grayscale']) ? $video_grayscale['enable_video_grayscale'] : ""; | |
| 126 | 123 | |
| 127 | - // Enqueue the global floating switcher style from settings | |
| 128 | - $enable_dark_mode_switch = isset($options['enable_dark_mode_switch']) ? $options['enable_dark_mode_switch'] : true; | |
| 129 | - if ($enable_dark_mode_switch) { | |
| 130 | - $global_variant = isset($options['enable_dark_switcher']) ? $options['enable_dark_switcher'] : 'classic'; | |
| 131 | - wp_enqueue_style('theme-' . $global_variant); | |
| 132 | - } | |
| 133 | 124 | |
| 134 | - // Detect [darkify] shortcode usage in current post and enqueue its variant | |
| 135 | - $post = get_post(); | |
| 136 | - if ($post && !empty($post->post_content)) { | |
| 137 | - if (has_shortcode($post->post_content, 'darkify')) { | |
| 138 | - preg_match_all('/\[darkify\b[^\]]*\bswitch=["\']([^"\']+)["\'][^\]]*\]/i', $post->post_content, $matches); | |
| 139 | - $used_switches = !empty($matches[1]) ? $matches[1] : []; | |
| 140 | - foreach ($used_switches as $switch_val) { | |
| 141 | - wp_enqueue_style('theme-' . $this->darkify_switch_to_variant($switch_val)); | |
| 142 | - } | |
| 143 | - // Shortcode without switch= attribute defaults to classic | |
| 144 | - preg_match_all('/\[darkify\b/i', $post->post_content, $all_sc); | |
| 145 | - if (count($all_sc[0]) > count($used_switches)) { | |
| 146 | - wp_enqueue_style('theme-classic'); | |
| 147 | - } | |
| 148 | - } | |
| 149 | 125 | |
| 150 | - // Detect darkify/switcher Gutenberg block usage and enqueue its variant | |
| 151 | - if (function_exists('has_block') && has_block('darkify/switcher', $post)) { | |
| 152 | - foreach ($this->darkify_get_block_variants($post->post_content) as $variant) { | |
| 153 | - wp_enqueue_style('theme-' . $variant); | |
| 154 | - } | |
| 155 | - } | |
| 156 | - } | |
| 126 | + wp_enqueue_style("theme-{$enable_dark_switcher}", DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/$enable_dark_switcher.css", array(), DARKIFY_VERSION, 'all'); | |
| 127 | + wp_register_style('theme-around', DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/around.css", array(), DARKIFY_VERSION, 'all'); | |
| 128 | + wp_register_style('theme-classic', DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/classic.css", array(), DARKIFY_VERSION, 'all'); | |
| 129 | + wp_register_style('theme-dark-inner', DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/dark-inner.css", array(), DARKIFY_VERSION, 'all'); | |
| 130 | + wp_register_style('theme-dark-side', DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/dark-side.css", array(), DARKIFY_VERSION, 'all'); | |
| 131 | + wp_register_style('theme-eclipse', DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/eclipse.css", array(), DARKIFY_VERSION, 'all'); | |
| 132 | + wp_register_style('theme-expand', DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/expand.css", array(), DARKIFY_VERSION, 'all'); | |
| 133 | + wp_register_style('theme-half-sun', DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/half-sun.css", array(), DARKIFY_VERSION, 'all'); | |
| 134 | + wp_register_style('theme-horizon', DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/horizon.css", array(), DARKIFY_VERSION, 'all'); | |
| 135 | + wp_register_style('theme-inner-moon', DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/inner-moon.css", array(), DARKIFY_VERSION, 'all'); | |
| 136 | + wp_register_style('theme-lightbulb', DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/lightbulb.css", array(), DARKIFY_VERSION, 'all'); | |
| 137 | + wp_register_style('theme-simple', DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/simple.css", array(), DARKIFY_VERSION, 'all'); | |
| 138 | + wp_register_style('theme-within', DARKIFY_DIR_URL . "src/Frontend/assets/css/switcher/within.css", array(), DARKIFY_VERSION, 'all'); | |
| 139 | + wp_enqueue_style('darkify-client-main', DARKIFY_DIR_URL . 'src/assets/css/client_main.css', array(), DARKIFY_VERSION); | |
| 140 | + wp_enqueue_style('ico-font', DARKIFY_DIR_URL . 'src/assets/css/icofont.css', array(), DARKIFY_VERSION); | |
| 141 | + wp_enqueue_script('darkify-client-main', DARKIFY_DIR_URL . 'src/assets/js/client_main.js', array('jquery'), DARKIFY_VERSION); | |
| 157 | 142 | |
| 158 | - // Detect switcher used in nav menus and enqueue its variant | |
| 159 | - $show_in_menu = isset($options['show_in_menu']) ? $options['show_in_menu'] : []; | |
| 160 | - $enable_in_menu = isset($show_in_menu['enable_switch_in_menu']) ? $show_in_menu['enable_switch_in_menu'] : false; | |
| 161 | - if ($enable_in_menu) { | |
| 162 | - $select_menus = isset($show_in_menu['select_menus']) ? $show_in_menu['select_menus'] : []; | |
| 163 | - foreach ($select_menus as $select_menu) { | |
| 164 | - $shortcode = $select_menu['darkify_menu_shortcode_helper'] ?? ''; | |
| 165 | - if ($shortcode) { | |
| 166 | - preg_match('/\bswitch=["\']([^"\']+)["\']/', $shortcode, $m); | |
| 167 | - $switch_val = $m[1] ?? '1'; | |
| 168 | - wp_enqueue_style('theme-' . $this->darkify_switch_to_variant($switch_val)); | |
| 169 | - } | |
| 170 | - } | |
| 171 | - } | |
| 143 | + $darkify_data = array( | |
| 144 | + 'darkify_switch_unique_id' => $this->unique_id, | |
| 145 | + 'darkify_is_this_admin_panel' => is_admin() ? "1" : "0", | |
| 146 | + 'darkify_enable_default_dark_mode' => $enable_default_dark_mode, | |
| 147 | + 'darkify_enable_os_aware' => $options["enable_os_aware"], | |
| 148 | + 'darkify_enable_keyboard_shortcut' => $options["enable_keyboard_shortcut"], | |
| 149 | + 'darkify_enable_time_based_dark' => $enable_time_based_dark, | |
| 150 | + 'darkify_time_based_dark_start' => $time_based_dark_start ? $time_based_dark_start : "19:00", | |
| 151 | + 'darkify_time_based_dark_stop' => $time_based_dark_stop ? $time_based_dark_stop : "07:00", | |
| 152 | + 'darkify_alternative_dark_mode_switch' => $options["alternative_dark_mode_switcher"], | |
| 153 | + 'darkify_enable_low_image_brightness' => $enable_low_image_brightness, | |
| 154 | + 'darkify_image_brightness_to' => $low_image_brightness_label, | |
| 155 | + 'darkify_enable_image_grayscale' => $enable_image_grayscale, | |
| 156 | + 'darkify_image_grayscale_to' => $image_grayscale_label, | |
| 157 | + 'darkify_disallowed_grayscale_images' => $disallowed_low_grayscale_images, | |
| 158 | + 'darkify_enable_bg_image_darken' => $enable_low_image_darken, | |
| 159 | + 'darkify_bg_image_darken_to' => $low_image_darken_label, | |
| 160 | + 'darkify_enable_invert_inline_svg' => $options["enable_invert_inline_svg"], | |
| 161 | + 'darkify_enable_low_video_brightness' => $enable_low_video_brightness, | |
| 162 | + 'darkify_video_brightness_to' => $low_video_brightness_label, | |
| 163 | + 'darkify_enable_video_grayscale' => $enable_video_grayscale, | |
| 164 | + // Add more variables as needed... | |
| 165 | + ); | |
| 166 | + wp_localize_script('darkify-client-main', 'darkify_data', $darkify_data); | |
| 172 | 167 | } |
| 173 | 168 | } |
| 174 | - | |
| 175 | - private function darkify_switch_to_variant($switch_value) | |
| 176 | - { | |
| 177 | - $map = [ | |
| 178 | - '1' => 'classic', '2' => 'expand', '3' => 'inner-moon', | |
| 179 | - '4' => 'within', '5' => 'orbit', | |
| 180 | - ]; | |
| 181 | - if (is_numeric($switch_value)) { | |
| 182 | - return $map[(string) $switch_value] ?? 'classic'; | |
| 183 | - } | |
| 184 | - return $switch_value ?: 'classic'; | |
| 185 | - } | |
| 186 | - | |
| 187 | - private function darkify_get_block_variants($content) | |
| 188 | - { | |
| 189 | - $variants = []; | |
| 190 | - $this->darkify_collect_block_variants(parse_blocks($content), $variants); | |
| 191 | - return array_unique($variants); | |
| 192 | - } | |
| 193 | - | |
| 194 | - private function darkify_collect_block_variants($blocks, &$variants) | |
| 195 | - { | |
| 196 | - foreach ($blocks as $block) { | |
| 197 | - if ('darkify/switcher' === $block['blockName']) { | |
| 198 | - $variants[] = $block['attrs']['style'] ?? 'classic'; | |
| 199 | - } | |
| 200 | - if (!empty($block['innerBlocks'])) { | |
| 201 | - $this->darkify_collect_block_variants($block['innerBlocks'], $variants); | |
| 202 | - } | |
| 203 | - } | |
| 204 | - | |
| 205 | - $options = get_option('darkify'); | |
| 206 | - $min = (apply_filters('darkify_dev_mode', false) || WP_DEBUG) ? '' : '.min'; | |
| 207 | - | |
| 208 | - // Always register all variant styles so page builders (Elementor, etc.) can reference them. | |
| 209 | - wp_register_style('darkify-client-main', DARKIFY_DIR_URL . 'src/assets/css/client_main' . $min . '.css', array(), DARKIFY_VERSION); | |
| 210 | - wp_register_style('theme-classic', DARKIFY_DIR_URL . 'src/assets/css/switcher/classic' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); | |
| 211 | - wp_register_style('theme-expand', DARKIFY_DIR_URL . 'src/assets/css/switcher/expand' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); | |
| 212 | - wp_register_style('theme-inner-moon', DARKIFY_DIR_URL . 'src/assets/css/switcher/inner-moon' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); | |
| 213 | - wp_register_style('theme-orbit', DARKIFY_DIR_URL . 'src/assets/css/switcher/orbit' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); | |
| 214 | - wp_register_style('theme-within', DARKIFY_DIR_URL . 'src/assets/css/switcher/within' . $min . '.css', array('darkify-client-main'), DARKIFY_VERSION, 'all'); | |
| 215 | - | |
| 216 | - // Determine which variants are actually needed on this page. | |
| 217 | - $variants = $this->darkify_collect_page_variants($options); | |
| 218 | - | |
| 219 | - if (empty($variants)) { | |
| 220 | - return; | |
| 221 | - } | |
| 222 | - | |
| 223 | - // Enqueue main CSS + only the required variant CSS — all resolved before wp_head fires. | |
| 224 | - wp_enqueue_style('darkify-client-main'); | |
| 225 | - foreach ($variants as $variant) { | |
| 226 | - wp_enqueue_style("theme-{$variant}"); | |
| 227 | - } | |
| 228 | - wp_enqueue_script('darkify-client-main', DARKIFY_DIR_URL . 'src/assets/js/client_main' . $min . '.js', array('jquery'), DARKIFY_VERSION); | |
| 229 | - } | |
| 230 | - | |
| 231 | - /** | |
| 232 | - * Collects all switcher variants needed on the current page so they can be | |
| 233 | - * enqueued during wp_enqueue_scripts (before wp_head), guaranteeing CSS loads | |
| 234 | - * in the <head> regardless of page builder or theme. | |
| 235 | - */ | |
| 236 | - private function darkify_collect_page_variants($options) | |
| 237 | - { | |
| 238 | - $all_variants = array('classic', 'expand', 'inner-moon', 'orbit', 'within'); | |
| 239 | - $num_map = array('1' => 'classic', '2' => 'expand', '3' => 'inner-moon', '4' => 'within', '5' => 'orbit'); | |
| 240 | - $variants = array(); | |
| 241 | - | |
| 242 | - // 1. Global / floating switcher variant (always present when dark mode is active). | |
| 243 | - $global_variant = isset($options['enable_dark_switcher']) ? $options['enable_dark_switcher'] : 'classic'; | |
| 244 | - if (in_array($global_variant, $all_variants, true)) { | |
| 245 | - $variants[] = $global_variant; | |
| 246 | - } | |
| 247 | - | |
| 248 | - // 2. Menu switcher — parse variant from each saved shortcode helper string. | |
| 249 | - $show_in_menu = isset($options['show_in_menu']) ? $options['show_in_menu'] : array(); | |
| 250 | - if (!empty($show_in_menu['enable_switch_in_menu']) && !empty($show_in_menu['select_menus'])) { | |
| 251 | - foreach ($show_in_menu['select_menus'] as $menu_item) { | |
| 252 | - $helper = isset($menu_item['darkify_menu_shortcode_helper']) ? $menu_item['darkify_menu_shortcode_helper'] : ''; | |
| 253 | - if ($helper && preg_match('/switch=["\']?([^"\'>\s\]]+)["\']?/', $helper, $m)) { | |
| 254 | - $v = isset($num_map[$m[1]]) ? $num_map[$m[1]] : $m[1]; | |
| 255 | - if (in_array($v, $all_variants, true)) { | |
| 256 | - $variants[] = $v; | |
| 257 | - } | |
| 258 | - } | |
| 259 | - } | |
| 260 | - } | |
| 261 | - | |
| 262 | - // 3. Page-level detection — shortcodes, Gutenberg blocks, Elementor widget. | |
| 263 | - global $post; | |
| 264 | - if ($post) { | |
| 265 | - $content = $post->post_content; | |
| 266 | - | |
| 267 | - // Shortcodes: parse switch attribute from every [darkify] tag. | |
| 268 | - if (has_shortcode($content, 'darkify')) { | |
| 269 | - preg_match_all('/\[darkify[^\]]*switch=["\']?([^"\'>\s\]]+)/', $content, $sc_matches); | |
| 270 | - foreach ($sc_matches[1] as $switch_val) { | |
| 271 | - $v = isset($num_map[$switch_val]) ? $num_map[$switch_val] : $switch_val; | |
| 272 | - if (in_array($v, $all_variants, true)) { | |
| 273 | - $variants[] = $v; | |
| 274 | - } else { | |
| 275 | - $variants[] = 'classic'; // default when no valid variant found | |
| 276 | - } | |
| 277 | - } | |
| 278 | - // If [darkify] exists but has no switch attribute, default to classic. | |
| 279 | - if (empty($sc_matches[1]) && has_shortcode($content, 'darkify')) { | |
| 280 | - $variants[] = 'classic'; | |
| 281 | - } | |
| 282 | - } | |
| 283 | - | |
| 284 | - // Gutenberg blocks: walk the block tree and collect style attributes. | |
| 285 | - if (function_exists('has_block') && has_block('darkify/switcher', $post)) { | |
| 286 | - $blocks = parse_blocks($content); | |
| 287 | - $variants = array_merge($variants, $this->darkify_extract_block_variants($blocks, $all_variants)); | |
| 288 | - } | |
| 289 | - | |
| 290 | - // Elementor widget: if Darkify widget appears in the page's Elementor data, | |
| 291 | - // load all variants — the specific style is stored as a control value that | |
| 292 | - // would require deep JSON parsing to extract reliably. | |
| 293 | - if (class_exists('\Elementor\Plugin')) { | |
| 294 | - $elementor_data = get_post_meta($post->ID, '_elementor_data', true); | |
| 295 | - if ($elementor_data && strpos($elementor_data, '"widgetType":"darkify"') !== false) { | |
| 296 | - return $all_variants; | |
| 297 | - } | |
| 298 | - } | |
| 299 | - } | |
| 300 | - | |
| 301 | - return array_values(array_unique(array_filter($variants))); | |
| 302 | - } | |
| 303 | - | |
| 304 | - /** | |
| 305 | - * Recursively walks a parsed block tree and collects darkify/darkify style values. | |
| 306 | - */ | |
| 307 | - private function darkify_extract_block_variants($blocks, $all_variants) | |
| 308 | - { | |
| 309 | - $variants = array(); | |
| 310 | - foreach ($blocks as $block) { | |
| 311 | - if ('darkify/switcher' === $block['blockName']) { | |
| 312 | - $style = isset($block['attrs']['style']) ? $block['attrs']['style'] : 'classic'; | |
| 313 | - if (in_array($style, $all_variants, true)) { | |
| 314 | - $variants[] = $style; | |
| 315 | - } | |
| 316 | - } | |
| 317 | - if (!empty($block['innerBlocks'])) { | |
| 318 | - $variants = array_merge($variants, $this->darkify_extract_block_variants($block['innerBlocks'], $all_variants)); | |
| 319 | - } | |
| 320 | - } | |
| 321 | - return $variants; | |
| 322 | - } | |
| 323 | 169 | public function darkify_is_dark_mode_allowed() |
| 324 | 170 | { |
| 325 | 171 | $options = get_option('darkify'); |
| 326 | 172 | /* Disable if Oxygen Builder is Opened */ |
| @@ -338,9 +184,9 @@ | ||
| 338 | 184 | |
| 339 | 185 | public function darkify_client_header_script() |
| 340 | 186 | { |
| 341 | 187 | if ($this->darkify_is_dark_mode_allowed()) { |
| 342 | - include_once DarkifyUtils::darkify_locate_template('header_script.php'); | |
| 188 | + include_once Helpers::darkify_locate_template('header_script.php'); | |
| 343 | 189 | } |
| 344 | 190 | } |
| 345 | 191 | |
| 346 | 192 | public function darkify_client_footer_script() |
| @@ -345,34 +191,25 @@ | ||
| 345 | 191 | |
| 346 | 192 | public function darkify_client_footer_script() |
| 347 | 193 | { |
| 348 | 194 | if ($this->darkify_is_dark_mode_allowed()) { |
| 349 | - include_once DarkifyUtils::darkify_locate_template('footer_script.php'); | |
| 195 | + include_once Helpers::darkify_locate_template('footer_script.php'); | |
| 350 | 196 | } |
| 351 | 197 | } |
| 352 | 198 | |
| 353 | - function darkify_switch_in_menu($items, $args) | |
| 199 | + public function darkify_switch_in_menu($items, $args) | |
| 354 | 200 | { |
| 355 | 201 | $options = get_option('darkify'); |
| 356 | 202 | $show_in_menu = $options['show_in_menu']; |
| 357 | - $select_menus = isset($show_in_menu['select_menus']) ? $show_in_menu['select_menus'] : []; | |
| 203 | + $switch_in_menu_location = $show_in_menu['switch_in_menu_location']; | |
| 204 | + $darkify_menu_shortcode_helper = $show_in_menu['darkify_menu_shortcode_helper']; | |
| 358 | 205 | |
| 359 | - $last_shortcode = ''; | |
| 360 | - | |
| 361 | - if ($this->darkify_is_dark_mode_allowed() && isset($args->menu->term_id)) { | |
| 362 | - foreach ($select_menus as $select_menu) { | |
| 363 | - | |
| 364 | - $switch_in_menu_location = $select_menu['switch_in_menu_location'] ?? ''; | |
| 365 | - $darkify_menu_shortcode_helper = $select_menu['darkify_menu_shortcode_helper'] ?? ''; | |
| 366 | - $menu_position = isset($select_menu['select_menu_position']) ? $select_menu['select_menu_position'] : 'after'; | |
| 206 | + if ($this->darkify_is_dark_mode_allowed()) { | |
| 207 | + if (isset($args->menu->term_id)) { | |
| 367 | 208 | if ($args->menu->term_id == $switch_in_menu_location) { |
| 368 | - $last_shortcode = $darkify_menu_shortcode_helper; | |
| 209 | + $items .= '<li class="menu-item">' . do_shortcode($darkify_menu_shortcode_helper) . '</li>'; | |
| 369 | 210 | } |
| 370 | 211 | } |
| 371 | - if ($last_shortcode) { | |
| 372 | - $items = ($menu_position == 'before') ? '<li class="menu-item">' . do_shortcode($last_shortcode) . '</li>' . $items : $items . '<li class="menu-item">' . do_shortcode($last_shortcode) . '</li>'; | |
| 373 | - } | |
| 374 | 212 | } |
| 375 | - | |
| 376 | 213 | return $items; |
| 377 | 214 | } |
| 378 | 215 | } |