PluginProbe
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) / 1.4.20
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) v1.4.20
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.4.20, at src/Includes/Darkify.php

223 lines 10.2 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
78 /**
79 * Run the loader to execute all of the hooks with WordPress.
80 *
81 * @since 1.0.0
82 */
83 public function run()
84 {
85 $this->loader->run();
86 }
87
88 public function darkify_redirect_to($plugin)
89 {
90 if (DARKIFY_BASENAME === $plugin) {
91 $redirect_url = esc_url(admin_url('admin.php?page=darkify'));
92 exit(wp_kses_post(wp_safe_redirect($redirect_url)));
93 }
94 }
95
96 /**
97 * The slug of the plugin used to uniquely identify it within the context of
98 * WordPress and to define internationalization functionality.
99 *
100 * @since 1.0.0
101 * @return string The name of the plugin.
102 */
103 public function get_plugin_name()
104 {
105 return $this->plugin_slug;
106 }
107
108 /**
109 * Retrieve the version number of the plugin.
110 *
111 * @since 1.0.0
112 * @return string The version number of the plugin.
113 */
114 public function get_version()
115 {
116 return $this->version;
117 }
118
119 /**
120 * Define the constants
121 *
122 * @return void
123 */
124 public function define_constants()
125 {
126 define('DARKIFY_URL', plugins_url('', DARKIFY_FILE));
127 define('DARKIFY_ASSETS', DARKIFY_URL . '/src/assets/');
128 define('DARKIFY_ADMIN', DARKIFY_URL . '/src/Admin');
129 }
130
131 /**
132 * Load the plugin after all plugins are loaded.
133 *
134 * @return void
135 */
136 public function init_plugin()
137 {
138 do_action('darkify_loaded');
139 load_plugin_textdomain('darkify', false, DARKIFY_DIR_NAME . "/languages");
140 }
141
142 /**
143 * Load the required dependencies for this plugin.
144 *
145 * Include the following files that make up the plugin:
146 *
147 * - Loader. Orchestrates the hooks of the plugin.
148 * - Teamproi18n. Defines internationalization functionality.
149 * - Admin. Defines all hooks for the admin area.
150 * - Frontend. Defines all hooks for the public side of the site.
151 *
152 * Create an instance of the loader which will be used to register the hooks
153 * with WordPress.
154 *
155 * @since 1.0.0
156 * @access private
157 */
158 private function load_dependencies()
159 {
160 /**
161 * The class responsible for orchestrating the actions and filters of the
162 * core plugin.
163 */
164 $this->loader = new Loader();
165 }
166
167 /**
168 * Register all of the hooks related to the admin dashboard functionality
169 * of the plugin.
170 *
171 * @since 1.0.0
172 * @access private
173 */
174 private function define_admin_hooks()
175 {
176 $plugin_admin = new Admin($this->get_plugin_name(), $this->get_version());
177 $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_styles');
178 $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'enqueue_scripts');
179 $this->loader->add_action('admin_enqueue_scripts', $plugin_admin, 'admin_dequeue_for_specefic_pages', 999);
180 }
181
182 /**
183 * Register all of the hooks related to the public-facing functionality
184 * of the plugin.
185 *
186 * @since 1.0.0
187 * @access private
188 */
189 private function define_public_hooks()
190 {
191 $plugin_public = new Frontend($this->get_plugin_name(), $this->get_version());
192 }
193
194 public static function switcher($variant)
195 {
196 $style = "theme-$variant";
197 wp_enqueue_style($style);
198 if ('classic' == $variant) {
199 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"><path d="M0-5h30a1 1 0 0 0 9 13v24H0Z" /></clipPath><g clip-path="url(#theme-toggle__classic__cutout)"><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>';
200 } else if ('expand' == $variant) {
201 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"><path d="M0-11h25a1 1 0 0017 13v30H0Z" /></clipPath><g clip-path="url(#theme-toggle__expand__cutout)"><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>';
202 } else if ('inner-moon' === $variant) {
203 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>';
204 } else if ('within' === $variant) {
205 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"><path d="M0 0h32v32h-32ZM6 16A1 1 0 0026 16 1 1 0 006 16" /></clipPath><g clip-path="url(#theme-toggle__within__clip)"><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" />
206 <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>';
207 } elseif ('orbit' === $variant) {
208 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-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><span class="toggle-thumb"></span>';
209 }
210 }
211
212 public static function switcher_wrapper($type, $unique_id, $variant, $position_class = '', $floating_switch_position = '', $tooltip_classes = '', $hide_dark_mode_on_mobile_screen = '', $floating_switch_width = '', $tooltip_on_floating_switch = '')
213 {
214
215 return '<div 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="position: relative;"' : '') . '><div class="theme-toggle">' . self::switcher($variant) . '</div>' . ($type !== 'shortcode' && $floating_switch_width ? '<span class="darkify_tooltiptext">' . esc_attr($tooltip_on_floating_switch) . '</span>' : '') . '</div>';
216 }
217
218 private function init_action()
219 {
220 add_action('activated_plugin', [$this, 'darkify_redirect_to']);
221 }
222 }
223