PluginProbe
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) / 1.4.9
Darkify – Dark Mode & Night Mode for Website & Admin (Dark Theme Included) v1.4.9
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.9, at src/Helpers/DarkifyUtils.php

369 lines 16.1 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, $custom_css_rules)
136 {
137 /* Decode any escaped HTML entities like &gt;, &lt;, &quot; */
138 $css = html_entity_decode($css, ENT_QUOTES | ENT_HTML5, 'UTF-8');
139
140 /* Replace : on https:// or http:// for detecting as background:black type */
141 $css = str_replace("://", "--colon--//", $css);
142
143 $css = str_replace(array("\r", "\n"), "", $css);
144
145 /* Convert to Array */
146 $results = array();
147 preg_match_all('/(.+?)\s?\{\s?(.+?)\s?\}/', $css, $matches);
148 foreach ($matches[0] as $i => $original) {
149 foreach (explode(';', $matches[2][$i]) as $attr) {
150 if (strlen(trim($attr)) > 0) {
151 if (strpos($attr, ':') !== false) {
152 list($name, $value) = explode(':', $attr, 2); // FIXED
153 $results[$matches[1][$i]][trim($name)] = trim($value);
154 }
155 }
156 }
157 }
158
159 /* Array to processed css string */
160 $updated_css = "";
161 foreach ($results as $single_selector => $rules) {
162 $updated_single_selector = "";
163 $single_selector_arr = explode(",", $single_selector);
164 for ($i = 0; $i < sizeof($single_selector_arr); $i++) {
165 $selector = ".darkify_dark_mode_enabled " . trim($single_selector_arr[$i]);
166 if ($i > 0) {
167 $updated_single_selector .= ", ";
168 }
169
170 // Check if selector has :not-disallowed
171 if (strpos($selector, ':not-disallowed') !== false) {
172 $updated_single_selector .= str_replace(':not-disallowed', '', $selector);
173 } else {
174 $updated_single_selector .= $selector;
175 if ($custom_css_rules && !preg_match('/:{1,2}(after|before)\b/i', $selector)) {
176 $updated_single_selector .= ", .darkify_dark_mode_enabled " . $selector . " *";
177 }
178 }
179 }
180
181 $updated_css .= $updated_single_selector . "{";
182
183 foreach ($rules as $key => $value) {
184 if (strpos($value, "important") == false) {
185 $value = $value . " !important";
186 }
187 $updated_css .= $key . ": " . $value . ";";
188 }
189
190 $updated_css .= "}";
191 }
192
193 /* Replace --colon--// back to https:// or http:// format */
194 $updated_css = str_replace("--colon--//", "://", $updated_css);
195
196 return $updated_css;
197 }
198
199 public static function parseAndProcessNormalCustomCSS($css)
200 {
201 /* Replace : on https:// or http:// for detecting as background:black type */
202 $css = str_replace("://", "--colon--//", $css);
203
204 $css = str_replace(array("\r", "\n"), "", $css);
205 /* Convert to Array */
206 $results = array();
207 preg_match_all('/(.+?)\s?\{\s?(.+?)\s?\}/', $css, $matches);
208 foreach ($matches[0] as $i => $original) {
209 foreach (explode(';', $matches[2][$i]) as $attr) {
210 if (strlen(trim($attr)) > 0) // for missing semicolon on last element, which is legal
211 {
212 list($name, $value) = explode(':', $attr);
213 $results[$matches[1][$i]][trim($name)] = trim($value);
214 }
215 }
216 }
217 /* Array to processed css string */
218 $updated_css = "";
219 foreach ($results as $single_selector => $rules) {
220 $updated_css .= $single_selector . "{";
221
222 foreach ($rules as $key => $value) {
223 if (strpos($value, "important") == false) {
224 $value = $value . " !important";
225 }
226 $updated_css .= $key . ": " . $value . ";";
227 }
228
229 $updated_css .= "}";
230 }
231 /* Replace --colon--// back to https:// or http:// format */
232 $updated_css = str_replace("--colon--//", "://", $updated_css);
233 return $updated_css;
234 }
235
236 public function generateDisallowedElementsStr($options, $external_support_class_obj)
237 {
238 $disallowed_element = isset($options["disallowed_elements"]) ? self::normalize_restriction_class($options['disallowed_elements']) : "";
239 $custom_css_rules = isset($options["custom_css_rules"]) ? $options["custom_css_rules"] : "";
240 $custom_css = isset($options["custom_css"]) ? $options["custom_css"] : "";
241 $disallowed_elements = "";
242 if (strlen(trim($disallowed_element)) > 0) {
243 $disallowed_elements_arr = explode(',', $disallowed_element);
244 if (is_array($disallowed_elements_arr)) {
245 if (sizeof($disallowed_elements_arr) > 0) {
246 foreach ($disallowed_elements_arr as $single_element) {
247 $disallowed_elements .= ($disallowed_elements != "" ? ", " : "") . trim($single_element);
248 $disallowed_elements .= ($disallowed_elements != "" ? ", " : "") . trim($single_element) . ' *';
249 }
250 }
251 }
252 }
253
254 // Get Disallowed Elements from External Plugins
255 $disallowed_from_external = $external_support_class_obj->getDisallowedElementsByAvailablePlugins();
256 if (sizeof($disallowed_from_external) > 0) {
257 foreach ($disallowed_from_external as $single_element) {
258 $disallowed_elements .= ($disallowed_elements != "" ? ", " : "") . trim($single_element);
259 }
260 }
261
262 // Exclude selectors specified in Custom CSS
263 $custom_css_selectors = $this->extractCustomCssSelectorsForAutoExcluding($custom_css, $custom_css_rules);
264 if (is_array($custom_css_selectors)) {
265 if (sizeof($custom_css_selectors) > 0) {
266 foreach ($custom_css_selectors as $single_element) {
267 $disallowed_elements .= ($disallowed_elements != "" ? ", " : "") . trim($single_element);
268 }
269 }
270 }
271 return $disallowed_elements;
272 }
273
274 public function minify_string($buffer)
275 {
276 $search = array(
277 '/\>[^\S ]+/s', // strip whitespaces after tags, except space
278 '/[^\S ]+\</s', // strip whitespaces before tags, except space
279 '/(\s)+/s', // shorten multiple whitespace sequences
280 '/<!--(.|\s)*?-->/' // Remove HTML comments
281 );
282 $replace = array(
283 '>',
284 '<',
285 '\\1',
286 ''
287 );
288 $buffer = preg_replace($search, $replace, $buffer);
289 return $buffer;
290 }
291
292 public function isRestrictedByDisallowedAdminPages($disallowed_admin_pages)
293 {
294 if (strlen(trim($disallowed_admin_pages)) > 0) {
295 if (function_exists("get_current_screen")) {
296 $current_screen = get_current_screen();
297 $parts = explode('_page_', $current_screen->id);
298 if (count($parts) !== 2) {
299 return False;
300 } /* means it's something like invalid page slug */
301 $page_slug = $parts[1];
302
303 $disallowed_admin_pages_arr = explode(',', $disallowed_admin_pages);
304 if (is_array($disallowed_admin_pages_arr)) {
305 if (sizeof($disallowed_admin_pages_arr) > 0) {
306 foreach ($disallowed_admin_pages_arr as $single_page) {
307 if ($page_slug == trim($single_page)) {
308 return True;
309 }
310 }
311 }
312 }
313 }
314 }
315 return False;
316 }
317
318 /**
319 * Custom Template locator .
320 *
321 * @param mixed $template_name template name .
322 * @param mixed $template_path template path .
323 * @param mixed $default_path default path .
324 * @return string
325 */
326 public static function darkify_locate_template($template_name, $default_path = '')
327 {
328 if (!$default_path) {
329 $default_path = DARKIFY_PATH . 'src/Frontend/Templates/';
330 }
331 $template = locate_template($template_name);
332 // Get default template.
333 if (!$template) {
334 $template = $default_path . $template_name;
335 }
336 // Return what we found.
337 return $template;
338 }
339
340 /**
341 * Cleans up that sloppy list of URLs—tightens it up like a true warrior.
342 *
343 * @param string $input A comma-separated string of URLs, possibly with extra space.
344 * @return string Refined and battle-ready URL list—no excess baggage.
345 *
346 * @bardock No room for weakness. Clean your inputs before they get you killed.
347 */
348 public static function normalize_restriction_class($input)
349 {
350 $urls = preg_replace('/,\s+/', ',', $input);
351 return $urls;
352 }
353
354 /* =============== Darkify Add Button Class =============== */
355 public function addButtonClassByDarkify($options)
356 {
357 $darkfy_button_class = [
358 "elementor-button",
359 "gspb-buttonbox",
360 "fusion-button",
361 "wp-element-button",
362 "ct-header-search",
363 "stk-button"
364 ];
365 return $darkfy_button_class;
366 }
367 }
368 }
369