PluginProbe
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) / 1.4.4
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) v1.4.4
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 / Helpers / DarkifyUtils.php

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

361 lines 15.9 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\Helpers;
4
5 // If this file is called directly, abort.
6 if (!defined('WPINC')) {
7 die;
8 }
9
10 if (!class_exists('DarkifyUtils')) {
11 class DarkifyUtils
12 {
13
14 public $base_admin;
15 public function __construct($base_admin)
16 {
17 $this->base_admin = $base_admin;
18 }
19
20 public function isMobile()
21 {
22 if (function_exists("wp_is_mobile")) {
23 return wp_is_mobile();
24 } else {
25 return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i", $_SERVER["HTTP_USER_AGENT"]);
26 }
27 }
28 public function is_hidden_by_user_agent($hide_on_desktop, $hide_dark_mode_on_mobile, $type_of_hide_by)
29 {
30 if ($this->isMobile()) {
31 if ($hide_dark_mode_on_mobile) {
32 if ($type_of_hide_by == "user_agent" || $type_of_hide_by == "both") {
33 return True;
34 }
35 }
36 } else {
37 if ($hide_on_desktop) {
38 return True;
39 }
40 }
41 return False;
42 }
43
44 public function generateSwitchStyles($options)
45 {
46 $switch_width_height = isset($options['switch_width_height']) ? $options['switch_width_height'] : [];
47 $switch_width = !empty($switch_width_height['width']) ? $switch_width_height['width'] : '60';
48 $switch_height = !empty($switch_width_height['height']) ? $switch_width_height['height'] : '60';
49 $switch_icon_size = isset($options["switch_icon_size"]) ? $options["switch_icon_size"] : [];
50 $switch_icon_size_width = !empty($switch_icon_size['width']) ? $switch_icon_size['width'] : '40';
51 $switch_background = isset($options['switch_background']) ? $options['switch_background'] : [];
52 $switch_light_mode_bg = !empty($switch_background) ? $switch_background['switch_light_mode_bg'] : '#121116';
53 $switch_dark_mode_bg = !empty($switch_background) ? $switch_background['switch_dark_mode_bg'] : '#ffffff';
54 $switch_icon_color = isset($options['switch_icon_color']) ? $options['switch_icon_color'] : [];
55
56 $switch_light_mode_color = !empty($switch_icon_color) ? $switch_icon_color['switch_light_mode_color'] : '#ffffff';
57 $switch_dark_mode_color = !empty($switch_icon_color) ? $switch_icon_color['switch_dark_mode_color'] : '#121116';
58 $switch_border = isset($options['switch_border']) ? $options['switch_border'] : [];
59 $switch_border_all = !empty($switch_border['all']) ? $switch_border['all'] : "0";
60 $switch_border_light_color = !empty($switch_border['color']) ? $switch_border['color'] : "#ffffff";
61 $switch_border_dark_color = !empty($switch_border['color3']) ? $switch_border['color3'] : "#121116";
62 $switch_border_radius = !empty($switch_border['radius']) ? $switch_border['radius'] : "7";
63
64 $styles = array(
65 "--darkify_switch_width" => $switch_width . "px",
66 "--darkify_switch_height" => $switch_height . "px",
67 "--darkify_switch_icon_size" => $switch_icon_size_width . "px",
68 "--darkify_switch_border" => $switch_border_all . "px",
69 "--darkify_switch_border_light_color" => $switch_border_light_color,
70 "--darkify_switch_border_dark_color" => $switch_border_dark_color,
71 "--darkify_switch_border_radius" => $switch_border_radius . "px",
72 "--darkify_switch_light_mode_bg" => $switch_light_mode_bg,
73 "--darkify_switch_dark_mode_bg" => $switch_dark_mode_bg,
74 "--darkify_switch_light_mode_color" => $switch_light_mode_color,
75 "--darkify_switch_dark_mode_color" => $switch_dark_mode_color,
76 );
77 return $styles;
78 }
79
80 public function generateSwitchStylesForShortcode($atts)
81 {
82 $styles = array();
83 foreach ($atts as $key => $value) {
84 if ($key == "switch") {
85 continue;
86 }
87 $styles["--darkify_switch_" . $key] = $value;
88 }
89 return $styles;
90 }
91
92 public function string_contains_any($string, $words, $caseSensitive = true)
93 {
94 foreach ($words as $word) {
95 $position = $caseSensitive ? stripos($string, $word) : strpos($string, $word);
96 if ($position !== false) {
97 return true;
98 }
99 }
100 return false;
101 }
102
103 public function extractCustomCssSelectorsForAutoExcluding($css, $custom_css_rules)
104 {
105 $pseudo_classes = array(":after", ":before", ":first-letter", ":first-line", ":selection", ":not-disallowed");
106 $css = str_replace(array("\r", "\n"), "", $css);
107 /* Convert to Array */
108 $exclude_elements = array();
109 $results = array();
110 preg_match_all('/(.+?)\s?\{\s?(.+?)\s?\}/', $css, $matches);
111 foreach ($matches[0] as $i => $original) {
112 foreach (explode(';', $matches[2][$i]) as $attr) {
113 if (strlen(trim($attr)) > 0) // for missing semicolon on last element, which is legal
114 {
115 list($name, $value) = explode(':', $attr);
116 $results[$matches[1][$i]][trim($name)] = trim($value);
117 }
118 }
119 }
120 /* Array to processed css string */
121 foreach ($results as $single_selector => $rules) {
122 $single_selector_arr = explode(",", $single_selector);
123 for ($i = 0; $i < sizeof($single_selector_arr); $i++) {
124 if (!$this->string_contains_any(trim($single_selector_arr[$i]), $pseudo_classes, false)) {
125 $exclude_elements[] = trim($single_selector_arr[$i]);
126 if ($custom_css_rules) {
127 $exclude_elements[] = trim($single_selector_arr[$i]) . " *";
128 }
129 }
130 }
131 }
132 return $exclude_elements;
133 }
134
135 public static function parseAndProcessCustomCSS($css)
136 {
137 /* Replace : on https:// or http:// for detecting as background:black type */
138 $css = str_replace("://", "--colon--//", $css);
139
140 $css = str_replace(array("\r", "\n"), "", $css);
141 /* Convert to Array */
142 $results = array();
143 preg_match_all('/(.+?)\s?\{\s?(.+?)\s?\}/', $css, $matches);
144 foreach ($matches[0] as $i => $original) {
145 foreach (explode(';', $matches[2][$i]) as $attr) {
146 if (strlen(trim($attr)) > 0) // for missing semicolon on last element, which is legal
147 {
148 list($name, $value) = explode(':', $attr);
149 $results[$matches[1][$i]][trim($name)] = trim($value);
150 }
151 }
152 }
153 /* Array to processed css string */
154 $updated_css = "";
155 foreach ($results as $single_selector => $rules) {
156 $updated_single_selector = "";
157 $single_selector_arr = explode(",", $single_selector);
158 for ($i = 0; $i < sizeof($single_selector_arr); $i++) {
159 $selector = ".darkify_dark_mode_enabled " . trim($single_selector_arr[$i]);
160 if ($i > 0) {
161 $updated_single_selector .= ", ";
162 }
163
164 // Check if selector has :not-disallowed
165 if (strpos($selector, ':not-disallowed') !== false) {
166 // Remove :not-disallowed and add the selector without darkify_dark_mode_enabled
167 $updated_single_selector .= str_replace(':not-disallowed', '', $selector);
168 } else {
169 // Add darkify_dark_mode_enabled prefix for normal selectors
170 $updated_single_selector .= $selector;
171 }
172 }
173
174 $updated_css .= $updated_single_selector . "{";
175
176 foreach ($rules as $key => $value) {
177 if (strpos($value, "important") == false) {
178 $value = $value . " !important";
179 }
180 $updated_css .= $key . ": " . $value . ";";
181 }
182
183 $updated_css .= "}";
184 }
185 /* Replace --colon--// back to https:// or http:// format */
186 $updated_css = str_replace("--colon--//", "://", $updated_css);
187 return $updated_css;
188 }
189
190
191 public static function parseAndProcessNormalCustomCSS($css)
192 {
193 /* Replace : on https:// or http:// for detecting as background:black type */
194 $css = str_replace("://", "--colon--//", $css);
195
196 $css = str_replace(array("\r", "\n"), "", $css);
197 /* Convert to Array */
198 $results = array();
199 preg_match_all('/(.+?)\s?\{\s?(.+?)\s?\}/', $css, $matches);
200 foreach ($matches[0] as $i => $original) {
201 foreach (explode(';', $matches[2][$i]) as $attr) {
202 if (strlen(trim($attr)) > 0) // for missing semicolon on last element, which is legal
203 {
204 list($name, $value) = explode(':', $attr);
205 $results[$matches[1][$i]][trim($name)] = trim($value);
206 }
207 }
208 }
209 /* Array to processed css string */
210 $updated_css = "";
211 foreach ($results as $single_selector => $rules) {
212 $updated_css .= $single_selector . "{";
213
214 foreach ($rules as $key => $value) {
215 if (strpos($value, "important") == false) {
216 $value = $value . " !important";
217 }
218 $updated_css .= $key . ": " . $value . ";";
219 }
220
221 $updated_css .= "}";
222 }
223 /* Replace --colon--// back to https:// or http:// format */
224 $updated_css = str_replace("--colon--//", "://", $updated_css);
225 return $updated_css;
226 }
227
228 public function generateDisallowedElementsStr($options, $external_support_class_obj)
229 {
230 $disallowed_element = isset($options["disallowed_elements"]) ? self::normalize_restriction_class($options['disallowed_elements']) : "";
231 $custom_css_rules = isset($options["custom_css_rules"]) ? $options["custom_css_rules"] : "";
232 $custom_css = isset($options["custom_css"]) ? $options["custom_css"] : "";
233 $disallowed_elements = "";
234 if (strlen(trim($disallowed_element)) > 0) {
235 $disallowed_elements_arr = explode(',', $disallowed_element);
236 if (is_array($disallowed_elements_arr)) {
237 if (sizeof($disallowed_elements_arr) > 0) {
238 foreach ($disallowed_elements_arr as $single_element) {
239 $disallowed_elements .= ($disallowed_elements != "" ? ", " : "") . trim($single_element);
240 $disallowed_elements .= ($disallowed_elements != "" ? ", " : "") . trim($single_element) . ' *';
241 }
242 }
243 }
244 }
245
246 // Get Disallowed Elements from External Plugins
247 $disallowed_from_external = $external_support_class_obj->getDisallowedElementsByAvailablePlugins();
248 if (sizeof($disallowed_from_external) > 0) {
249 foreach ($disallowed_from_external as $single_element) {
250 $disallowed_elements .= ($disallowed_elements != "" ? ", " : "") . trim($single_element);
251 }
252 }
253
254 // Exclude selectors specified in Custom CSS
255 $custom_css_selectors = $this->extractCustomCssSelectorsForAutoExcluding($custom_css, $custom_css_rules);
256 if (is_array($custom_css_selectors)) {
257 if (sizeof($custom_css_selectors) > 0) {
258 foreach ($custom_css_selectors as $single_element) {
259 $disallowed_elements .= ($disallowed_elements != "" ? ", " : "") . trim($single_element);
260 }
261 }
262 }
263 return $disallowed_elements;
264 }
265
266 public function minify_string($buffer)
267 {
268 $search = array(
269 '/\>[^\S ]+/s', // strip whitespaces after tags, except space
270 '/[^\S ]+\</s', // strip whitespaces before tags, except space
271 '/(\s)+/s', // shorten multiple whitespace sequences
272 '/<!--(.|\s)*?-->/' // Remove HTML comments
273 );
274 $replace = array(
275 '>',
276 '<',
277 '\\1',
278 ''
279 );
280 $buffer = preg_replace($search, $replace, $buffer);
281 return $buffer;
282 }
283
284 public function isRestrictedByDisallowedAdminPages($disallowed_admin_pages)
285 {
286 if (strlen(trim($disallowed_admin_pages)) > 0) {
287 if (function_exists("get_current_screen")) {
288 $current_screen = get_current_screen();
289 $parts = explode('_page_', $current_screen->id);
290 if (count($parts) !== 2) {
291 return False;
292 } /* means it's something like invalid page slug */
293 $page_slug = $parts[1];
294
295 $disallowed_admin_pages_arr = explode(',', $disallowed_admin_pages);
296 if (is_array($disallowed_admin_pages_arr)) {
297 if (sizeof($disallowed_admin_pages_arr) > 0) {
298 foreach ($disallowed_admin_pages_arr as $single_page) {
299 if ($page_slug == trim($single_page)) {
300 return True;
301 }
302 }
303 }
304 }
305 }
306 }
307 return False;
308 }
309
310 /**
311 * Custom Template locator .
312 *
313 * @param mixed $template_name template name .
314 * @param mixed $template_path template path .
315 * @param mixed $default_path default path .
316 * @return string
317 */
318 public static function darkify_locate_template($template_name, $default_path = '')
319 {
320 if (!$default_path) {
321 $default_path = DARKIFY_PATH . 'src/Frontend/Templates/';
322 }
323 $template = locate_template($template_name);
324 // Get default template.
325 if (!$template) {
326 $template = $default_path . $template_name;
327 }
328 // Return what we found.
329 return $template;
330 }
331
332 /**
333 * Cleans up that sloppy list of URLs—tightens it up like a true warrior.
334 *
335 * @param string $input A comma-separated string of URLs, possibly with extra space.
336 * @return string Refined and battle-ready URL list—no excess baggage.
337 *
338 * @bardock No room for weakness. Clean your inputs before they get you killed.
339 */
340 public static function normalize_restriction_class($input)
341 {
342 $urls = preg_replace('/,\s+/', ',', $input);
343 return $urls;
344 }
345
346 /* =============== Darkify Add Button Class =============== */
347 public function addButtonClassByDarkify($options)
348 {
349 $darkfy_button_class = [
350 "elementor-button",
351 "gspb-buttonbox",
352 "fusion-button",
353 "wp-element-button",
354 "ct-header-search",
355 "stk-button"
356 ];
357 return $darkfy_button_class;
358 }
359 }
360 }
361