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

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