PluginProbe
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) / 2.1.3
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) v2.1.3
2.1.3 2.1.2 2.1.1 2.1.0 2.0.4 2.0.3 2.0.2 2.0.1 2.0.0 1.5.5 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 trunk 1.0.1 1.1.0 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 All 57 releases
darkify / src / Frontend / DarkifyShortcode.php

DarkifyShortcode.php in Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) 2.1.3, at src/Frontend/DarkifyShortcode.php

111 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ThemeAtelier\Darkify\Frontend;
4
5 use ThemeAtelier\Darkify\Includes\Darkify;
6
7 // If this file is called directly, abort.
8 if (!defined('WPINC')) {
9 die;
10 }
11
12 if (!class_exists('DarkifyShortcode')) {
13 class DarkifyShortcode
14 {
15
16 public $base_client;
17
18 public function __construct($base_client)
19 {
20 $this->base_client = $base_client;
21
22 add_shortcode('darkify', array($this, 'darkify_shortcode_parser'));
23 }
24
25 public function darkify_shortcode_parser($atts, $content = null)
26 {
27 // With Frontend Dark Mode off, client_main.css/.js are never
28 // enqueued on the front end, so rendering the switch markup anyway
29 // leaves an unstyled toggle behind that does nothing when clicked.
30 // Gate every switch entry point here: the Gutenberg block and the
31 // Elementor widget both render through this shortcode. The floating
32 // switch, menu switch and inline scripts are already gated in
33 // Frontend.php.
34 if (! $this->base_client->darkify_is_dark_mode_allowed()) {
35 return '';
36 }
37
38 $options = get_option('darkify');
39
40 // Background
41 $switch_background = isset($options['switch_background']) ? $options['switch_background'] : [];
42 $switch_light_mode_bg = !empty($switch_background) ? $switch_background['switch_light_mode_bg'] : '#121116';
43 $switch_dark_mode_bg = !empty($switch_background) ? $switch_background['switch_dark_mode_bg'] : '#ffffff';
44
45 // Border
46 $switch_border = isset($options['switch_border']) ? $options['switch_border'] : [];
47 $switch_border_all = !empty($switch_border['all']) ? $switch_border['all'] : "0px";
48 $switch_border_light_color = !empty($switch_border['color']) ? $switch_border['color'] : "#ffffff";
49 $switch_border_dark_color = !empty($switch_border['color3']) ? $switch_border['color3'] : "#121116";
50 $switch_border_radius = !empty($switch_border['radius']) ? $switch_border['radius'] . 'px' : "7px";
51
52 // Color
53 $switch_icon_color = isset($options['switch_icon_color']) ? $options['switch_icon_color'] : [];
54 $switch_light_mode_color = !empty($switch_icon_color) ? $switch_icon_color['switch_light_mode_color'] : '#ffffff';
55 $switch_dark_mode_color = !empty($switch_icon_color) ? $switch_icon_color['switch_dark_mode_color'] : '#121116';
56
57 $atts = shortcode_atts(array(
58 'switch' => '1',
59 'switch_size' => '100',
60 'light_mode_bg' => $switch_light_mode_bg,
61 'dark_mode_bg' => $switch_dark_mode_bg,
62 'border' => $switch_border_all,
63 'border_light_color' => $switch_border_light_color,
64 'border_dark_color' => $switch_border_dark_color,
65 'border_radius' => $switch_border_radius,
66 'light_mode_color' => $switch_light_mode_color,
67 'dark_mode_color' => $switch_dark_mode_color,
68 ), $atts);
69
70 return $this->darkify_client_view_maker($atts);
71 }
72 public function darkify_client_view_maker($atts)
73 {
74 if (function_exists('wp_rand')) {
75 $unique_id = wp_rand() . "_shortcode";
76 }
77 $switch_styles = $this->base_client->utils->generateSwitchStylesForShortcode($atts);
78 ob_start();
79
80 if (sizeof($switch_styles) > 0) { ?>
81 <style type="text/css">
82 #darkify_switch_<?php echo esc_attr($unique_id); ?> {
83 <?php foreach ($switch_styles as $key => $value) { ?><?php echo esc_attr($key); ?>: <?php echo esc_attr($value); ?>;
84 <?php } ?>
85 }
86 </style>
87 <?php }
88
89 $switcher_number = $atts['switch'];
90
91 $switcher_number = $atts['switch'];
92 if ($switcher_number === '1') {
93 $switcher_number = 'classic';
94 } elseif ($switcher_number === '2') {
95 $switcher_number = 'expand';
96 } elseif ($switcher_number === '3') {
97 $switcher_number = 'inner-moon';
98 } elseif ($switcher_number === '4') {
99 $switcher_number = 'within';
100 } elseif ($switcher_number === '5') {
101 $switcher_number = 'orbit';
102 }
103 $switch_size = $atts['switch_size'] / 100;
104 // switcher_wrapper() escapes each dynamic value internally, so its
105 // returned markup is safe to print as-is.
106 echo Darkify::switcher_wrapper('shortcode', $unique_id, $switcher_number, $switch_size); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
107 return ob_get_clean();
108 }
109 }
110 }
111