| 1 |
<?php |
| 2 |
|
| 3 |
// If this file is called directly, abort. |
| 4 |
if (!defined('WPINC')) { |
| 5 |
die; |
| 6 |
} |
| 7 |
|
| 8 |
if (!class_exists('DarkifyUtils')) { |
| 9 |
class DarkifyUtils |
| 10 |
{ |
| 11 |
|
| 12 |
public $base_admin; |
| 13 |
public function __construct($base_admin) |
| 14 |
{ |
| 15 |
$this->base_admin = $base_admin; |
| 16 |
} |
| 17 |
|
| 18 |
|
| 19 |
public function isMobile() |
| 20 |
{ |
| 21 |
if (function_exists("wp_is_mobile")) { |
| 22 |
return wp_is_mobile(); |
| 23 |
} else { |
| 24 |
$user_agent = isset( $_SERVER["HTTP_USER_AGENT"] ) ? sanitize_text_field( wp_unslash( $_SERVER["HTTP_USER_AGENT"] ) ) : ''; |
| 25 |
$pattern = "/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos)/i"; |
| 26 |
$escaped_pattern = preg_quote($pattern, '/'); |
| 27 |
return preg_match(esc_attr($escaped_pattern), esc_attr($user_agent)); |
| 28 |
} |
| 29 |
} |
| 30 |
public function is_hidden_by_user_agent($hide_on_desktop, $hide_dark_mode_on_mobile, $type_of_hide_by) |
| 31 |
{ |
| 32 |
if ($this->isMobile()) { |
| 33 |
if ($hide_dark_mode_on_mobile) { |
| 34 |
if ($type_of_hide_by == "user_agent" || $type_of_hide_by == "both") { |
| 35 |
return True; |
| 36 |
} |
| 37 |
} |
| 38 |
} else { |
| 39 |
if ($hide_on_desktop) { |
| 40 |
return True; |
| 41 |
} |
| 42 |
} |
| 43 |
return False; |
| 44 |
} |
| 45 |
|
| 46 |
public function generateSwitchStyles($options) |
| 47 |
{ |
| 48 |
$switch_border_radius = $options["switch_border_radius"]; |
| 49 |
$styles = array( |
| 50 |
|
| 51 |
"--darkify_switch_width_height" => $options["switch_width_height"]["width"] . "px", |
| 52 |
"--darkify_switch_icon_size" => $options["switch_icon_size"]["width"] . "px", |
| 53 |
"--darkify_switch_border_radius" => $switch_border_radius['all'] . "px", |
| 54 |
"--darkify_switch_light_mode_bg" => $options["switch_light_mode_bg"], |
| 55 |
"--darkify_switch_dark_mode_bg" => $options["switch_dark_mode_bg"], |
| 56 |
"--darkify_switch_light_mode_color" => $options["switch_light_mode_color"], |
| 57 |
"--darkify_switch_dark_mode_color" => $options["switch_dark_mode_color"], |
| 58 |
); |
| 59 |
return $styles; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
public function generateSwitchStylesForShortcode($atts) |
| 64 |
{ |
| 65 |
$styles = array(); |
| 66 |
foreach ($atts as $key => $value) { |
| 67 |
if ($key == "switch") { |
| 68 |
continue; |
| 69 |
} |
| 70 |
$styles["--darkify_switch_" . $key] = $value; |
| 71 |
} |
| 72 |
return $styles; |
| 73 |
} |
| 74 |
|
| 75 |
public function minify_string($buffer) |
| 76 |
{ |
| 77 |
$search = array( |
| 78 |
'/\>[^\S ]+/s', // strip whitespaces after tags, except space |
| 79 |
'/[^\S ]+\</s', // strip whitespaces before tags, except space |
| 80 |
'/(\s)+/s', // shorten multiple whitespace sequences |
| 81 |
'/<!--(.|\s)*?-->/' // Remove HTML comments |
| 82 |
); |
| 83 |
$replace = array( |
| 84 |
'>', |
| 85 |
'<', |
| 86 |
'\\1', |
| 87 |
'' |
| 88 |
); |
| 89 |
$buffer = preg_replace($search, $replace, $buffer); |
| 90 |
return $buffer; |
| 91 |
} |
| 92 |
|
| 93 |
public function isRestrictedByDisallowedAdminPages($disallowed_admin_pages) |
| 94 |
{ |
| 95 |
if (strlen(trim($disallowed_admin_pages)) > 0) { |
| 96 |
if (function_exists("get_current_screen")) { |
| 97 |
$current_screen = get_current_screen(); |
| 98 |
$parts = explode('_page_', $current_screen->id); |
| 99 |
if (count($parts) !== 2) { |
| 100 |
return False; |
| 101 |
} /* means it's something like invalid page slug */ |
| 102 |
$page_slug = $parts[1]; |
| 103 |
|
| 104 |
$disallowed_admin_pages_arr = explode(',', $disallowed_admin_pages); |
| 105 |
if (is_array($disallowed_admin_pages_arr)) { |
| 106 |
if (sizeof($disallowed_admin_pages_arr) > 0) { |
| 107 |
foreach ($disallowed_admin_pages_arr as $single_page) { |
| 108 |
if ($page_slug == trim($single_page)) { |
| 109 |
return True; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
} |
| 115 |
} |
| 116 |
return False; |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|