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

471 lines 19.5 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
21 public function isMobile()
22 {
23 if (function_exists("wp_is_mobile")) {
24 return wp_is_mobile();
25 } else {
26 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"]);
27 }
28 }
29 public function is_hidden_by_user_agent($hide_on_desktop, $hide_dark_mode_on_mobile, $type_of_hide_by)
30 {
31 if ($this->isMobile()) {
32 if ($hide_dark_mode_on_mobile) {
33 if ($type_of_hide_by == "user_agent" || $type_of_hide_by == "both") {
34 return True;
35 }
36 }
37 } else {
38 if ($hide_on_desktop) {
39 return True;
40 }
41 }
42 return False;
43 }
44
45 public function generateSwitchStyles($options)
46 {
47 $switch_border_radius = $options["switch_border_radius"];
48 $styles = array(
49
50 "--darkify_switch_width_height" => $options["switch_width_height"]["width"] . "px",
51 "--darkify_switch_icon_size" => $options["switch_icon_size"]["width"] . "px",
52 "--darkify_switch_border_radius" => $switch_border_radius['all'] . "px",
53 "--darkify_switch_light_mode_bg" => $options["switch_light_mode_bg"],
54 "--darkify_switch_dark_mode_bg" => $options["switch_dark_mode_bg"],
55 "--darkify_switch_light_mode_color" => $options["switch_light_mode_color"],
56 "--darkify_switch_dark_mode_color" => $options["switch_dark_mode_color"],
57 );
58 return $styles;
59 }
60
61
62 public function generateSwitchStylesForShortcode($atts)
63 {
64 $styles = array();
65 foreach ($atts as $key => $value) {
66 if ($key == "switch") {
67 continue;
68 }
69 $styles["--darkify_switch_" . $key] = $value;
70 }
71 return $styles;
72 }
73
74
75 public function string_contains_any($string, $words, $caseSensitive = true)
76 {
77 foreach ($words as $word) {
78 $position = $caseSensitive ? stripos($string, $word) : strpos($string, $word);
79 if ($position !== false) {
80 return true;
81 }
82 }
83 return false;
84 }
85
86 public static function extractCustomCssSelectorsForAutoExcluding($css, $disallowed_elements_force_to_correct)
87 {
88 $pseudo_classes = array(":after", ":before", ":first-letter", ":first-line", ":selection", ":not-disallowed");
89 $css = str_replace(array("\r", "\n"), "", $css);
90 /* Convert to Array */
91 $exclude_elements = array();
92 $results = array();
93 preg_match_all('/(.+?)\s?\{\s?(.+?)\s?\}/', $css, $matches);
94 foreach ($matches[0] as $i => $original) {
95 foreach (explode(';', $matches[2][$i]) as $attr) {
96 if (strlen(trim($attr)) > 0) // for missing semicolon on last element, which is legal
97 {
98 list($name, $value) = explode(':', $attr);
99 $results[$matches[1][$i]][trim($name)] = trim($value);
100 }
101 }
102 }
103 /* Array to processed css string */
104 foreach ($results as $single_selector => $rules) {
105 $single_selector_arr = explode(",", $single_selector);
106 for ($i = 0; $i < sizeof($single_selector_arr); $i++) {
107 if (!$this->string_contains_any(trim($single_selector_arr[$i]), $pseudo_classes, false)) {
108 $exclude_elements[] = trim($single_selector_arr[$i]);
109 if ($disallowed_elements_force_to_correct) {
110 $exclude_elements[] = trim($single_selector_arr[$i]) . " *";
111 }
112 }
113 }
114 }
115 return $exclude_elements;
116 }
117
118 public static function parseAndProcessCustomCSS($css, $disallowed_elements_force_to_correct)
119 {
120 /* Replace : on https:// or http:// for detecting as background:black type */
121 $css = str_replace("://", "--colon--//", $css);
122
123 $css = str_replace(array("\r", "\n"), "", $css);
124 /* Convert to Array */
125 $results = array();
126 preg_match_all('/(.+?)\s?\{\s?(.+?)\s?\}/', $css, $matches);
127 foreach ($matches[0] as $i => $original) {
128 foreach (explode(';', $matches[2][$i]) as $attr) {
129 if (strlen(trim($attr)) > 0) // for missing semicolon on last element, which is legal
130 {
131 list($name, $value) = explode(':', $attr);
132 $results[$matches[1][$i]][trim($name)] = trim($value);
133 }
134 }
135 }
136 /* Array to processed css string */
137 $updated_css = "";
138 foreach ($results as $single_selector => $rules) {
139 $updated_single_selector = "";
140 $single_selector_arr = explode(",", $single_selector);
141 for ($i = 0; $i < sizeof($single_selector_arr); $i++) {
142 if ($i > 0) {
143 $updated_single_selector .= ", ";
144 }
145 $updated_single_selector .= ".darkify_dark_mode_enabled " . trim($single_selector_arr[$i]);
146 if ($disallowed_elements_force_to_correct) {
147 $updated_single_selector .= ", .darkify_dark_mode_enabled " . trim($single_selector_arr[$i]) . " *";
148 }
149 }
150
151 $updated_single_selector = str_replace(":not-disallowed", "", $updated_single_selector);
152 $updated_css .= $updated_single_selector . "{";
153
154 foreach ($rules as $key => $value) {
155 if (strpos($value, "important") == false) {
156 $value = $value . " !important";
157 }
158 $updated_css .= $key . ": " . $value . ";";
159 }
160
161 $updated_css .= "}";
162 }
163 /* Replace --colon--// back to https:// or http:// format */
164 $updated_css = str_replace("--colon--//", "://", $updated_css);
165 return $updated_css;
166 }
167
168
169 public static function parseAndProcessNormalCustomCSS($css)
170 {
171 /* Replace : on https:// or http:// for detecting as background:black type */
172 $css = str_replace("://", "--colon--//", $css);
173
174 $css = str_replace(array("\r", "\n"), "", $css);
175 /* Convert to Array */
176 $results = array();
177 preg_match_all('/(.+?)\s?\{\s?(.+?)\s?\}/', $css, $matches);
178 foreach ($matches[0] as $i => $original) {
179 foreach (explode(';', $matches[2][$i]) as $attr) {
180 if (strlen(trim($attr)) > 0) // for missing semicolon on last element, which is legal
181 {
182 list($name, $value) = explode(':', $attr);
183 $results[$matches[1][$i]][trim($name)] = trim($value);
184 }
185 }
186 }
187 /* Array to processed css string */
188 $updated_css = "";
189 foreach ($results as $single_selector => $rules) {
190 $updated_css .= $single_selector . "{";
191
192 foreach ($rules as $key => $value) {
193 if (strpos($value, "important") == false) {
194 $value = $value . " !important";
195 }
196 $updated_css .= $key . ": " . $value . ";";
197 }
198
199 $updated_css .= "}";
200 }
201 /* Replace --colon--// back to https:// or http:// format */
202 $updated_css = str_replace("--colon--//", "://", $updated_css);
203 return $updated_css;
204 }
205
206
207 public static function generateAllowedElementsStr($options)
208 {
209 $allowed_elements = isset($options["allowed_elements"]) ? $options["allowed_elements"] : "";
210 $allowed_elements = "";
211 if (strlen(trim($allowed_elements)) > 0) {
212 $allowed_elements_arr = explode(',', $allowed_elements);
213 if (is_array($allowed_elements_arr)) {
214 if (sizeof($allowed_elements_arr) > 0) {
215 foreach ($allowed_elements_arr as $single_element) {
216 $allowed_elements .= ($allowed_elements != "" ? ", " : "") . trim($single_element);
217 $allowed_elements .= ($allowed_elements != "" ? ", " : "") . trim($single_element) . ' *';
218 }
219 }
220 }
221 }
222 return $allowed_elements;
223 }
224
225 public static function generateDisallowedElementsStr($options, $external_support_class_obj)
226 {
227 $disallowed_element = isset($options["disallowed_elements"]) ? $options["disallowed_elements"] : "";
228 $disallowed_elements_force_to_correct = isset($options["disallowed_elements_force_to_correct"]) ? $options["disallowed_elements_force_to_correct"] : "";
229 $custom_css = isset($options["custom_css"]) ? $options["custom_css"] : "";
230 $disallowed_elements = "";
231 if (strlen(trim($disallowed_element)) > 0) {
232 $disallowed_elements_arr = explode(',', $disallowed_element);
233 if (is_array($disallowed_elements_arr)) {
234 if (sizeof($disallowed_elements_arr) > 0) {
235 foreach ($disallowed_elements_arr as $single_element) {
236 $disallowed_elements .= ($disallowed_elements != "" ? ", " : "") . trim($single_element);
237 $disallowed_elements .= ($disallowed_elements != "" ? ", " : "") . trim($single_element) . ' *';
238 }
239 }
240 }
241 }
242
243
244
245 // Get Disallowed Elements from External Plugins
246 $disallowed_from_external = $external_support_class_obj->getDisallowedElementsByAvailablePlugins();
247 if (sizeof($disallowed_from_external) > 0) {
248 foreach ($disallowed_from_external as $single_element) {
249 $disallowed_elements .= ($disallowed_elements != "" ? ", " : "") . trim($single_element);
250 }
251 }
252
253 // Exclude selectors specified in Custom CSS
254 $custom_css_selectors = self::extractCustomCssSelectorsForAutoExcluding($custom_css, $disallowed_elements_force_to_correct);
255 if (is_array($custom_css_selectors)) {
256 if (sizeof($custom_css_selectors) > 0) {
257 foreach ($custom_css_selectors as $single_element) {
258 $disallowed_elements .= ($disallowed_elements != "" ? ", " : "") . trim($single_element);
259 }
260 }
261 }
262 return $disallowed_elements;
263 }
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
285 public function getWpNavMenus()
286 {
287 $results = array();
288 $results[] = array("id" => "0", "text" => "Choose Menu");
289 $menus = wp_get_nav_menus();
290 foreach ($menus as $menu) {
291 $results[] = array("id" => $menu->term_id, "text" => $menu->name);
292 }
293 return $results;
294 }
295
296 public function getWpPages()
297 {
298 $results = array();
299 $results[] = array("id" => "0", "text" => "Homepage");
300 $results[] = array("id" => "lr", "text" => "WP Login / Registration Page");
301 $args = array('post_type' => 'page', 'posts_per_page' => -1, 'orderby' => 'title', 'order' => 'ASC');
302 foreach (get_posts($args) as $page) {
303 $results[] = array("id" => $page->ID, "text" => $page->post_title);
304 }
305 return $results;
306 }
307
308 public function searchWpPosts($search = "")
309 {
310 global $wpdb;
311 $results = array();
312 $sql = $wpdb->prepare("SELECT ID, post_title FROM {$wpdb->prefix}posts WHERE post_type = 'post' AND post_title LIKE %s GROUP BY ID ORDER BY ID DESC LIMIT 10", array('%' . $search . '%'));
313 $listPosts = $wpdb->get_results($sql);
314 if (sizeof($listPosts) > 0) {
315 foreach ($listPosts as $singlePost) {
316 $results[] = array("id" => $singlePost->ID, "text" => $singlePost->post_title);
317 }
318 }
319 return $results;
320 }
321
322
323 public function getWpPosts($post_ids = array())
324 {
325 $results = array();
326 if (sizeof($post_ids) > 0) {
327 $args = array('post_type' => 'post', 'posts_per_page' => -1, 'orderby' => 'ID', 'order' => 'DESC', 'post__in' => $post_ids);
328 foreach (get_posts($args) as $page) {
329 $results[] = array("id" => $page->ID, "text" => $page->post_title);
330 }
331 }
332 return $results;
333 }
334
335
336
337 public function isRestrictedByDisallowedAdminPages($disallowed_admin_pages)
338 {
339 if (strlen(trim($disallowed_admin_pages)) > 0) {
340 if (function_exists("get_current_screen")) {
341 $current_screen = get_current_screen();
342 $parts = explode('_page_', $current_screen->id);
343 if (count($parts) !== 2) {
344 return False;
345 } /* means it's something like invalid page slug */
346 $page_slug = $parts[1];
347
348 $disallowed_admin_pages_arr = explode(',', $disallowed_admin_pages);
349 if (is_array($disallowed_admin_pages_arr)) {
350 if (sizeof($disallowed_admin_pages_arr) > 0) {
351 foreach ($disallowed_admin_pages_arr as $single_page) {
352 if ($page_slug == trim($single_page)) {
353 return True;
354 }
355 }
356 }
357 }
358 }
359 }
360 return False;
361 }
362
363
364 public function isRestrictedByDisallowedPages($disallowed_pages)
365 {
366 if (is_array($disallowed_pages)) {
367 if (sizeof($disallowed_pages) > 0) {
368 if (function_exists("is_page") && function_exists("get_the_ID")) {
369 if (is_page() || is_front_page()) {
370 $current_page_id = is_front_page() ? "0" : get_the_ID();
371 if (in_array($current_page_id, $disallowed_pages)) {
372 return True;
373 }
374 }
375 }
376 if (class_exists('WooCommerce')) {
377 if (function_exists("is_shop") && function_exists("wc_get_page_id")) {
378 if (is_shop()) {
379 $current_page_id = wc_get_page_id('shop');
380 if (in_array($current_page_id, $disallowed_pages)) {
381 return True;
382 }
383 }
384 }
385 }
386 if (isset($GLOBALS['pagenow'])) {
387 if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) {
388 $current_page_id = "lr";
389 if (in_array($current_page_id, $disallowed_pages)) {
390 return True;
391 }
392 }
393 }
394 }
395 }
396 return False;
397 }
398
399 public function isRestrictedByAllowedPages($allowed_pages)
400 {
401 if (is_array($allowed_pages)) {
402 if (sizeof($allowed_pages) > 0) {
403 if (function_exists("is_page") && function_exists("get_the_ID")) {
404 if (is_page() || is_front_page()) {
405 $current_page_id = is_front_page() ? "0" : get_the_ID();
406 if (!in_array($current_page_id, $allowed_pages)) {
407 return True;
408 }
409 }
410 }
411 if (class_exists('WooCommerce')) {
412 if (function_exists("is_shop") && function_exists("wc_get_page_id")) {
413 if (is_shop()) {
414 $current_page_id = wc_get_page_id('shop');
415 if (!in_array($current_page_id, $allowed_pages)) {
416 return True;
417 }
418 }
419 }
420 }
421 if (isset($GLOBALS['pagenow'])) {
422 if (in_array($GLOBALS['pagenow'], array('wp-login.php', 'wp-register.php'))) {
423 $current_page_id = "lr";
424 if (!in_array($current_page_id, $allowed_pages)) {
425 return True;
426 }
427 }
428 }
429 }
430 }
431 return False;
432 }
433
434
435 public function isRestrictedByDisallowedPosts($disallowed_posts)
436 {
437 if (is_array($disallowed_posts)) {
438 if (sizeof($disallowed_posts) > 0) {
439 if (function_exists("is_singular") && function_exists("get_the_ID")) {
440 if (is_singular("post")) {
441 $current_post_id = get_the_ID();
442 if (in_array($current_post_id, $disallowed_posts)) {
443 return True;
444 }
445 }
446 }
447 }
448 }
449 return False;
450 }
451
452
453 public function isRestrictedByAllowedPosts($allowed_posts)
454 {
455 if (is_array($allowed_posts)) {
456 if (sizeof($allowed_posts) > 0) {
457 if (function_exists("is_singular") && function_exists("get_the_ID")) {
458 if (is_singular("post")) {
459 $current_post_id = get_the_ID();
460 if (!in_array($current_post_id, $allowed_posts)) {
461 return True;
462 }
463 }
464 }
465 }
466 }
467 return False;
468 }
469 }
470 }
471