PluginProbe
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) / 1.5.3
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) v1.5.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 / Includes / Darkify.php

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

305 lines 13.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The file of the Darkify class.
5 *
6 * @link https://themeatelier.net
7 * @since 1.0.0
8 *
9 * @package Darkify
10 */
11
12 namespace ThemeAtelier\Darkify\Includes;
13
14 use ThemeAtelier\Darkify\Admin\Admin;
15 use ThemeAtelier\Darkify\Frontend\Frontend;
16
17
18 // don't call the file directly.
19 if (!defined('ABSPATH')) {
20 exit;
21 }
22
23 /**
24 * The main class of the plugin.
25 *
26 * Handle all the class and methods of the plugin.
27 *
28 * @author ThemeAtelier <themeatelierbd@gmail.com>
29 */
30 class Darkify
31 {
32 /**
33 * Plugin version
34 *
35 * @since 1.0.0
36 * @access protected
37 * @var string
38 */
39 protected $version;
40
41 /**
42 * Plugin slug
43 *
44 * @since 1.0.0
45 * @access protected
46 * @var string
47 */
48 protected $plugin_slug;
49
50 /**
51 * Main Loader.
52 *
53 * The loader that's responsible for maintaining and registering all hooks that empowers
54 * the plugin.
55 *
56 * @since 1.0.0
57 * @access protected
58 * @var object
59 */
60 protected $loader;
61 /**
62 * Constructor for the Darkify class.
63 *
64 * Sets up all the appropriate hooks and actions within the plugin.
65 */
66 public function __construct()
67 {
68 $this->version = DARKIFY_VERSION;
69 $this->plugin_slug = 'darkify';
70 $this->load_dependencies();
71 $this->define_constants();
72 $this->define_admin_hooks();
73 $this->define_public_hooks();
74 $this->init_action();
75 add_action('plugin_loaded', array($this, 'init_plugin'));
76
77 add_action('elementor/controls/register', [$this, 'register_controls']);
78 add_action('elementor/widgets/register', array($this, 'register_darkify_widget'));
79
80 add_action('init', [$this, 'create_darkify_block_init']);
81 }
82
83 /**
84 * Run the loader to execute all of the hooks with WordPress.
85 *
86 * @since 1.0.0
87 */
88 public function run()
89 {
90 $this->loader->run();
91 }
92
93 public function darkify_redirect_to($plugin)
94 {
95 if (DARKIFY_BASENAME === $plugin) {
96 $redirect_url = esc_url(admin_url('admin.php?page=darkify'));
97 exit(wp_kses_post(wp_safe_redirect($redirect_url)));
98 }
99 }
100
101 /**
102 * The slug of the plugin used to uniquely identify it within the context of
103 * WordPress and to define internationalization functionality.
104 *
105 * @since 1.0.0
106 * @return string The name of the plugin.
107 */
108 public function get_plugin_name()
109 {
110 return $this->plugin_slug;
111 }
112
113 /**
114 * Retrieve the version number of the plugin.
115 *
116 * @since 1.0.0
117 * @return string The version number of the plugin.
118 */
119 public function get_version()
120 {
121 return $this->version;
122 }
123
124
125 public function register_controls($controls_manager)
126 {
127 require_once DARKIFY_PATH . 'src/Admin/elementor/controls/class-elementor-control-switch.php';
128
129 $controls_manager->register_control(
130 'darkify_switch',
131 new \Elementor_Darkify_Switcher()
132 );
133 }
134 public function register_darkify_widget($widgets_manager)
135 {
136 require_once DARKIFY_PATH . 'src/Admin/elementor/widgets/elementor-widget.php';
137 $widgets_manager->register(new \Elementor_Darkify());
138 }
139
140 /**
141 * Define the constants
142 *
143 * @return void
144 */
145 public function define_constants()
146 {
147 define('DARKIFY_URL', plugins_url('', DARKIFY_FILE));
148 define('DARKIFY_ASSETS', DARKIFY_URL . '/src/assets/');
149 define('DARKIFY_ADMIN', DARKIFY_URL . '/src/Admin');
150 }
151
152 /**
153 * Load the plugin after all plugins are loaded.
154 *
155 * @return void
156 */
157 public function init_plugin()
158 {
159 do_action('darkify_loaded');
160 load_plugin_textdomain('darkify', false, DARKIFY_DIR_NAME . "/languages");
161 }
162
163 /**
164 * Load the required dependencies for this plugin.
165 *
166 * Include the following files that make up the plugin:
167 *
168 * - Loader. Orchestrates the hooks of the plugin.
169 * - Teamproi18n. Defines internationalization functionality.
170 * - Admin. Defines all hooks for the admin area.
171 * - Frontend. Defines all hooks for the public side of the site.
172 *
173 * Create an instance of the loader which will be used to register the hooks
174 * with WordPress.
175 *
176 * @since 1.0.0
177 * @access private
178 */
179 private function load_dependencies()
180 {
181 /**
182 * The class responsible for orchestrating the actions and filters of the
183 * core plugin.
184 */
185 $this->loader = new Loader();
186 }
187
188 /**
189 * Register all of the hooks related to the admin dashboard functionality
190 * of the plugin.
191 *
192 * @since 1.0.0
193 * @access private
194 */
195 private function define_admin_hooks()
196 {
197 $plugin_admin = new Admin($this->get_plugin_name(), $this->get_version());
198 $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');
199 $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');
200 $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'admin_dequeue_for_specefic_pages', 999);
201 }
202
203 /**
204 * Register all of the hooks related to the public-facing functionality
205 * of the plugin.
206 *
207 * @since 1.0.0
208 * @access private
209 */
210 private function define_public_hooks()
211 {
212 $plugin_public = new Frontend($this->get_plugin_name(), $this->get_version());
213 }
214
215 public static function switcher($variant, $unique_id)
216 {
217 if ('classic' == $variant) {
218 return '<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="0px" height="0px" fill="currentColor" stroke-linecap="round" class="theme-toggle__classic" viewBox="0 0 32 32"><clipPath id="theme-toggle__classic__cutout_' . esc_attr($unique_id) . '"><path d="M0-5h30a1 1 0 0 0 9 13v24H0Z" /></clipPath><g clip-path="url(#theme-toggle__classic__cutout_' . esc_attr($unique_id) . ')"><circle cx="16" cy="16" r="9.34" /><g stroke="currentColor" stroke-width="1.5"><path d="M16 5.5v-4" /><path d="M16 30.5v-4" /><path d="M1.5 16h4" /><path d="M26.5 16h4" /><path d="m23.4 8.6 2.8-2.8" /><path d="m5.7 26.3 2.9-2.9" /><path d="m5.8 5.8 2.8 2.8" /><path d="m23.4 23.4 2.9 2.9" /></g></g></svg>';
219 } else if ('expand' == $variant) {
220 return '<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="0px" height="0px" fill="currentColor" class="theme-toggle__expand" viewBox="0 0 32 32"><clipPath id="theme-toggle__expand__cutout_' . esc_attr($unique_id) . '"><path d="M0-11h25a1 1 0 0017 13v30H0Z" /></clipPath><g clip-path="url(#theme-toggle__expand__cutout_' . esc_attr($unique_id) . ')"><circle cx="16" cy="16" r="8.4" /><path d="M18.3 3.2c0 1.3-1 2.3-2.3 2.3s-2.3-1-2.3-2.3S14.7.9 16 .9s2.3 1 2.3 2.3zm-4.6 25.6c0-1.3 1-2.3 2.3-2.3s2.3 1 2.3 2.3-1 2.3-2.3 2.3-2.3-1-2.3-2.3zm15.1-10.5c-1.3 0-2.3-1-2.3-2.3s1-2.3 2.3-2.3 2.3 1 2.3 2.3-1 2.3-2.3 2.3zM3.2 13.7c1.3 0 2.3 1 2.3 2.3s-1 2.3-2.3 2.3S.9 17.3.9 16s1-2.3 2.3-2.3zm5.8-7C9 7.9 7.9 9 6.7 9S4.4 8 4.4 6.7s1-2.3 2.3-2.3S9 5.4 9 6.7zm16.3 21c-1.3 0-2.3-1-2.3-2.3s1-2.3 2.3-2.3 2.3 1 2.3 2.3-1 2.3-2.3 2.3zm2.4-21c0 1.3-1 2.3-2.3 2.3S23 7.9 23 6.7s1-2.3 2.3-2.3 2.4 1 2.4 2.3zM6.7 23C8 23 9 24 9 25.3s-1 2.3-2.3 2.3-2.3-1-2.3-2.3 1-2.3 2.3-2.3z" /></g></svg>';
221 } else if ('inner-moon' === $variant) {
222 return '<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" width="0px" height="0px" fill="currentColor" class="theme-toggle__inner-moon" viewBox="0 0 32 32"><path d="M27.5 11.5v-7h-7L16 0l-4.5 4.5h-7v7L0 16l4.5 4.5v7h7L16 32l4.5-4.5h7v-7L32 16l-4.5-4.5zM16 25.4a9.39 9.39 0 1 1 0-18.8 9.39 9.39 0 1 1 0 18.8z" /><circle cx="16" cy="16" r="8.1" /></svg>';
223 } else if ('within' === $variant) {
224 return '<svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" class="theme-toggle__within" height="1em" width="1em" viewBox="0 0 32 32" fill="currentColor"><clipPath id="theme-toggle__within__clip_' . esc_attr($unique_id) . '"><path d="M0 0h32v32h-32ZM6 16A1 1 0 0026 16 1 1 0 006 16" /></clipPath><g clip-path="url(#theme-toggle__within__clip_' . esc_attr($unique_id) . ')"><path d="M30.7 21.3 27.1 16l3.7-5.3c.4-.5.1-1.3-.6-1.4l-6.3-1.1-1.1-6.3c-.1-.6-.8-.9-1.4-.6L16 5l-5.4-3.7c-.5-.4-1.3-.1-1.4.6l-1 6.3-6.4 1.1c-.6.1-.9.9-.6 1.3L4.9 16l-3.7 5.3c-.4.5-.1 1.3.6 1.4l6.3 1.1 1.1 6.3c.1.6.8.9 1.4.6l5.3-3.7 5.3 3.7c.5.4 1.3.1 1.4-.6l1.1-6.3 6.3-1.1c.8-.1 1.1-.8.7-1.4zM16 25.1c-5.1 0-9.1-4.1-9.1-9.1 0-5.1 4.1-9.1 9.1-9.1s9.1 4.1 9.1 9.1c0 5.1-4 9.1-9.1 9.1z" /></g><path class="theme-toggle__within__circle" d="M16 7.7c-4.6 0-8.2 3.7-8.2 8.2s3.6 8.4 8.2 8.4 8.2-3.7 8.2-8.2-3.6-8.4-8.2-8.4zm0 14.4c-3.4 0-6.1-2.9-6.1-6.2s2.7-6.1 6.1-6.1c3.4 0 6.1 2.9 6.1 6.2s-2.7 6.1-6.1 6.1z" />
225 <path class="theme-toggle__within__inner" d="M16 9.5c-3.6 0-6.4 2.9-6.4 6.4s2.8 6.5 6.4 6.5 6.4-2.9 6.4-6.4-2.8-6.5-6.4-6.5z" /></svg>';
226 } elseif ('orbit' === $variant) {
227 return '<span class="toggle-track"><span class="toggle-sun"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 512 512" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M277.3 32h-42.7v64h42.7V32zm129.1 43.7L368 114.1l29.9 29.9 38.4-38.4-29.9-29.9zm-300.8 0l-29.9 29.9 38.4 38.4 29.9-29.9-38.4-38.4zM256 128c-70.4 0-128 57.6-128 128s57.6 128 128 128 128-57.6 128-128-57.6-128-128-128zm224 106.7h-64v42.7h64v-42.7zm-384 0H32v42.7h64v-42.7zM397.9 368L368 397.9l38.4 38.4 29.9-29.9-38.4-38.4zm-283.8 0l-38.4 38.4 29.9 29.9 38.4-38.4-29.9-29.9zm163.2 48h-42.7v64h42.7v-64z"></path></svg></span><span class="toggle-thumb"></span><span class="toggle-moon"><svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 384 512" height="1em" width="1em" xmlns="http://www.w3.org/2000/svg"><path d="M223.5 32C100 32 0 132.3 0 256S100 480 223.5 480c60.6 0 115.5-24.2 155.8-63.4c5-4.9 6.3-12.5 3.1-18.7s-10.1-9.7-17-8.5c-9.8 1.7-19.8 2.6-30.1 2.6c-96.9 0-175.5-78.8-175.5-176c0-65.8 36-123.1 89.3-153.3c6.1-3.5 9.2-10.5 7.7-17.3s-7.3-11.9-14.3-12.5c-6.3-.5-12.6-.8-19-.8z"></path></svg></span></span>';
228 }
229 }
230
231 public static function switcher_wrapper($type, $unique_id, $variant, $switch_size, $position_class = '', $floating_switch_position = '', $tooltip_classes = '', $hide_dark_mode_on_mobile_screen = '', $floating_switch_width = '', $tooltip_on_floating_switch = '')
232 {
233
234 return '<div style="--darkify-switch-scale: ' . esc_attr($switch_size) . ';" id="darkify_switch_' . esc_attr($unique_id) . '" onclick="darkify_switch_trigger()" class="darkify_switch darkify_switch_style switch-' . esc_attr($variant) . ' ' . ($type !== 'shortcode' ? esc_attr($position_class) . ' ' . esc_attr($floating_switch_position) . ' ' . esc_attr($tooltip_classes) . ' ' . esc_attr($hide_dark_mode_on_mobile_screen) : '') . '" ' . ($type === 'shortcode' ? 'style="--darkify-switch-scale: ' . esc_attr($switch_size) . '; position: relative;"' : '') . '><div class="theme-toggle">' . self::switcher($variant, $unique_id) . '</div>' . ($type !== 'shortcode' && $floating_switch_width ? '<span class="darkify_tooltiptext">' . esc_attr($tooltip_on_floating_switch) . '</span>' : '') . '</div>';
235 }
236
237 private function init_action()
238 {
239 add_action('activated_plugin', [$this, 'darkify_redirect_to']);
240 }
241
242
243 public function create_darkify_block_init()
244 {
245 register_block_type_from_metadata(
246 DARKIFY_PATH . 'src/Admin/blocks/',
247 array(
248 'render_callback' => [$this, 'render_darkify_block_callback'],
249 )
250 );
251
252 wp_localize_script(
253 'darkify-switcher-editor-script',
254 'darkify',
255 array(
256 'dir_url' => DARKIFY_DIR_URL,
257 )
258 );
259 }
260
261 /**
262 * Render callback for the Darkify Pro block.
263 */
264 public function render_darkify_block_callback($attributes)
265 {
266 $style = $attributes['style'] ?? 'classic';
267 $size = $attributes['size'] ?? '1.0';
268 $custom_size = $attributes['custom_size'] ?? 100;
269 $bg_light = $attributes['switch_bg_light'] ?? '#121116';
270 $bg_dark = $attributes['switch_bg_dark'] ?? '#ffffff';
271 $light_icon_color = $attributes['light_icon_color'] ?? '#ffffff';
272 $dark_icon_color = $attributes['dark_icon_color'] ?? '#121116';
273 $light_text_color = $attributes['light_text_color'] ?? '#121116';
274 $dark_text_color = $attributes['dark_text_color'] ?? '#ffffff';
275 $border_width = $attributes['border_width'] ?? 0;
276 $border_light_color = $attributes['border_light_color'] ?? '#121116';
277 $border_dark_color = $attributes['border_dark_color'] ?? '#ffffff';
278 $border_radius = $attributes['border_radius'] ?? 7;
279 $alignment = $attributes['alignment'] ?? 'left';
280
281 // Size logic
282 if ('custom' === $size) {
283 $switcher_size = $custom_size;
284 } else {
285 $switcher_size = (float) $size * 100;
286 }
287
288 return '<div style="display: flex; justify-content: ' . esc_attr($alignment) . '">' . do_shortcode(wp_sprintf(
289 '[darkify switch="%s" icon_size="40px" light_mode_bg="%s" dark_mode_bg="%s" light_mode_color="%s" dark_mode_color="%s" border="%spx" border_light_color="%s" border_dark_color="%s" border_radius="%spx" text_light_mode_color="%s" text_dark_mode_color="%s" switch_size="%s"]',
290 $style,
291 $bg_light,
292 $bg_dark,
293 $light_icon_color,
294 $dark_icon_color,
295 $border_width,
296 $border_light_color,
297 $border_dark_color,
298 $border_radius,
299 $light_text_color,
300 $dark_text_color,
301 $switcher_size
302 )) . '</div>';
303 }
304 }
305