| 1 |
<?php |
| 2 |
|
| 3 |
namespace MasterAddons\Inc\Classes; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; |
| 7 |
} |
| 8 |
|
| 9 |
class Utils |
| 10 |
{ |
| 11 |
public static function get_options($key, $network_override = true) |
| 12 |
{ |
| 13 |
if (is_network_admin()) { |
| 14 |
$value = get_site_option($key); |
| 15 |
} elseif (!$network_override && is_multisite()) { |
| 16 |
$value = get_site_option($key); |
| 17 |
} elseif ($network_override && is_multisite()) { |
| 18 |
$value = get_option($key); |
| 19 |
$value = (false === $value || (is_array($value) && in_array('disabled', $value))) ? get_site_option($key) : $value; |
| 20 |
} else { |
| 21 |
$value = get_option($key); |
| 22 |
} |
| 23 |
|
| 24 |
return $value; |
| 25 |
} |
| 26 |
|
| 27 |
public static function check_options($option_name) |
| 28 |
{ |
| 29 |
return isset($option_name) ? esc_attr($option_name) : false; |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Allowed option-name prefixes for plugin writes. Prevents this generic |
| 34 |
* wrapper from being used to write arbitrary WordPress options. |
| 35 |
* |
| 36 |
* @var string[] |
| 37 |
*/ |
| 38 |
private static $jltma_allowed_option_prefixes = array('jltma_', 'ma_el_', 'master_addons', 'master-addons'); |
| 39 |
|
| 40 |
public static function update_options($option_name, $option_value) |
| 41 |
{ |
| 42 |
if (!is_string($option_name) || '' === $option_name) { |
| 43 |
return false; |
| 44 |
} |
| 45 |
|
| 46 |
$is_allowed = false; |
| 47 |
foreach (self::$jltma_allowed_option_prefixes as $prefix) { |
| 48 |
if (0 === strpos($option_name, $prefix)) { |
| 49 |
$is_allowed = true; |
| 50 |
break; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
if (!$is_allowed) { |
| 55 |
return false; |
| 56 |
} |
| 57 |
|
| 58 |
if (JLTMA_NETWORK_ACTIVATED == true) { |
| 59 |
return update_site_option($option_name, $option_value); |
| 60 |
} else { |
| 61 |
return update_option($option_name, $option_value); |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
public static function is_plugin_active($plugin_basename) |
| 66 |
{ |
| 67 |
include_once(ABSPATH . 'wp-admin/includes/plugin.php'); |
| 68 |
return \is_plugin_active($plugin_basename); |
| 69 |
} |
| 70 |
|
| 71 |
public static function is_woocommerce_active() |
| 72 |
{ |
| 73 |
return self::is_plugin_active('woocommerce/woocommerce.php'); |
| 74 |
} |
| 75 |
|
| 76 |
public static function is_site_wide($plugin) |
| 77 |
{ |
| 78 |
if (!is_multisite()) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
|
| 82 |
$plugins = get_site_option('active_sitewide_plugins'); |
| 83 |
if (isset($plugins[$plugin])) { |
| 84 |
return true; |
| 85 |
} |
| 86 |
|
| 87 |
return false; |
| 88 |
} |
| 89 |
|
| 90 |
public static function pretty_number($x = 0) |
| 91 |
{ |
| 92 |
$x = (int) $x; |
| 93 |
|
| 94 |
if ($x > 1000000) { |
| 95 |
return floor($x / 1000000) . 'M'; |
| 96 |
} |
| 97 |
|
| 98 |
if ($x > 10000) { |
| 99 |
return floor($x / 1000) . 'k'; |
| 100 |
} |
| 101 |
return $x; |
| 102 |
} |
| 103 |
|
| 104 |
public static function get_site_domain() |
| 105 |
{ |
| 106 |
return str_ireplace('www.', '', (string) wp_parse_url(home_url() ?? '', PHP_URL_HOST)); |
| 107 |
} |
| 108 |
|
| 109 |
public static function human_readable_num($size) |
| 110 |
{ |
| 111 |
$l = substr($size, -1); |
| 112 |
$ret = substr($size, 0, -1); |
| 113 |
|
| 114 |
switch (strtoupper($l)) { |
| 115 |
case 'P': |
| 116 |
$ret *= 1024; |
| 117 |
case 'T': |
| 118 |
$ret *= 1024; |
| 119 |
case 'G': |
| 120 |
$ret *= 1024; |
| 121 |
case 'M': |
| 122 |
$ret *= 1024; |
| 123 |
case 'K': |
| 124 |
$ret *= 1024; |
| 125 |
} |
| 126 |
return $ret; |
| 127 |
} |
| 128 |
|
| 129 |
public static function array_flatten($array) |
| 130 |
{ |
| 131 |
if (!is_array($array)) { |
| 132 |
return false; |
| 133 |
} |
| 134 |
$result = array(); |
| 135 |
foreach ($array as $key => $value) { |
| 136 |
if (is_array($value)) { |
| 137 |
$result[$key] = $value[0]; |
| 138 |
} else { |
| 139 |
$result[$key] = $value; |
| 140 |
} |
| 141 |
} |
| 142 |
return $result; |
| 143 |
} |
| 144 |
|
| 145 |
public static function hex2rgb_array($hex) |
| 146 |
{ |
| 147 |
$hex = str_replace('#', '', $hex); |
| 148 |
if (strlen($hex) == 3) { |
| 149 |
$r = hexdec(substr($hex, 0, 1) . substr($hex, 0, 1)); |
| 150 |
$g = hexdec(substr($hex, 1, 1) . substr($hex, 1, 1)); |
| 151 |
$b = hexdec(substr($hex, 2, 1) . substr($hex, 2, 1)); |
| 152 |
} else { |
| 153 |
$r = hexdec(substr($hex, 0, 2)); |
| 154 |
$g = hexdec(substr($hex, 2, 2)); |
| 155 |
$b = hexdec(substr($hex, 4, 2)); |
| 156 |
} |
| 157 |
$rgb = array($r, $g, $b); |
| 158 |
return $rgb; |
| 159 |
} |
| 160 |
|
| 161 |
public static function hex2Rgb($hex, $alpha = false) |
| 162 |
{ |
| 163 |
$hex = str_replace('#', '', $hex); |
| 164 |
$length = strlen($hex); |
| 165 |
$rgb['r'] = hexdec($length == 6 ? substr($hex, 0, 2) : ($length == 3 ? str_repeat(substr($hex, 0, 1), 2) : 0)); |
| 166 |
$rgb['g'] = hexdec($length == 6 ? substr($hex, 2, 2) : ($length == 3 ? str_repeat(substr($hex, 1, 1), 2) : 0)); |
| 167 |
$rgb['b'] = hexdec($length == 6 ? substr($hex, 4, 2) : ($length == 3 ? str_repeat(substr($hex, 2, 1), 2) : 0)); |
| 168 |
if ($alpha) { |
| 169 |
$rgb['a'] = $alpha; |
| 170 |
} |
| 171 |
return $rgb; |
| 172 |
} |
| 173 |
|
| 174 |
public static function image_filter_gallery_categories($gallery_items) |
| 175 |
{ |
| 176 |
if (!is_array($gallery_items)) { |
| 177 |
return false; |
| 178 |
} |
| 179 |
|
| 180 |
$gallery_category_names = array(); |
| 181 |
$gallery_category_names_final = array(); |
| 182 |
|
| 183 |
foreach ($gallery_items as $gallery_item) { |
| 184 |
$gallery_category_names[] = $gallery_item['gallery_category_name']; |
| 185 |
} |
| 186 |
|
| 187 |
if (is_array($gallery_category_names) && !empty($gallery_category_names)) { |
| 188 |
foreach ($gallery_category_names as $gallery_category_name) { |
| 189 |
$gallery_category_names_final[] = explode(',', $gallery_category_name); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
if (is_array($gallery_category_names_final) && !empty($gallery_category_names_final)) { |
| 194 |
$gallery_category_names_final = self::image_filter_gallery_array_flatten($gallery_category_names_final); |
| 195 |
return array_unique(array_filter($gallery_category_names_final)); |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
public static function image_filter_gallery_category_classes($gallery_classes, $id) |
| 200 |
{ |
| 201 |
if (!($gallery_classes)) { |
| 202 |
return false; |
| 203 |
} |
| 204 |
|
| 205 |
$gallery_cat_classes = array(); |
| 206 |
$gallery_classes = explode(',', $gallery_classes); |
| 207 |
|
| 208 |
if (is_array($gallery_classes) && !empty($gallery_classes)) { |
| 209 |
foreach ($gallery_classes as $gallery_class) { |
| 210 |
$gallery_cat_classes[] = sanitize_title($gallery_class) . '-' . $id; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
return implode(' ', $gallery_cat_classes); |
| 215 |
} |
| 216 |
|
| 217 |
public static function image_filter_gallery_categories_parts($gallery_classes) |
| 218 |
{ |
| 219 |
if (!($gallery_classes)) { |
| 220 |
return false; |
| 221 |
} |
| 222 |
|
| 223 |
$gallery_cat_classes = array(); |
| 224 |
$gallery_classes = explode(',', $gallery_classes); |
| 225 |
|
| 226 |
if (is_array($gallery_classes) && !empty($gallery_classes)) { |
| 227 |
foreach ($gallery_classes as $gallery_class) { |
| 228 |
$gallery_cat_classes[] = '<div class="ma-el-label ma-el-added ma-el-image-filter-cat">' . sanitize_title($gallery_class) . '</div>'; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return implode(' ', $gallery_cat_classes); |
| 233 |
} |
| 234 |
|
| 235 |
public static function image_filter_gallery_array_flatten($array) |
| 236 |
{ |
| 237 |
if (!is_array($array)) { |
| 238 |
return false; |
| 239 |
} |
| 240 |
|
| 241 |
$result = array(); |
| 242 |
|
| 243 |
foreach ($array as $key => $value) { |
| 244 |
if (is_array($value)) { |
| 245 |
$result = array_merge($result, self::image_filter_gallery_array_flatten($value)); |
| 246 |
} else { |
| 247 |
$result[$key] = $value; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
return $result; |
| 252 |
} |
| 253 |
|
| 254 |
public static function multi_dimension_flatten($array, $prefix = '') |
| 255 |
{ |
| 256 |
$result = array(); |
| 257 |
foreach ($array as $key => $value) { |
| 258 |
if (is_array($value)) { |
| 259 |
$result = $result + self::multi_dimension_flatten($value, $prefix . $key . '.'); |
| 260 |
} else { |
| 261 |
$result[$key] = $value; |
| 262 |
} |
| 263 |
} |
| 264 |
return $result; |
| 265 |
} |
| 266 |
|
| 267 |
public static function get_environment_info() |
| 268 |
{ |
| 269 |
$curl_version = ''; |
| 270 |
if (function_exists('curl_version')) { |
| 271 |
$curl_version = curl_version(); |
| 272 |
$curl_version = $curl_version['version'] . ', ' . $curl_version['ssl_version']; |
| 273 |
} |
| 274 |
|
| 275 |
$wp_memory_limit = self::human_readable_num(WP_MEMORY_LIMIT); |
| 276 |
if (function_exists('memory_get_usage')) { |
| 277 |
$wp_memory_limit = max($wp_memory_limit, self::human_readable_num(@ini_get('memory_limit'))); |
| 278 |
} |
| 279 |
|
| 280 |
return array( |
| 281 |
'home_url' => get_option('home'), |
| 282 |
'site_url' => get_option('siteurl'), |
| 283 |
'version' => defined('JLTMA_VER') ? JLTMA_VER : '', |
| 284 |
'wp_version' => get_bloginfo('version'), |
| 285 |
'wp_multisite' => is_multisite(), |
| 286 |
'wp_memory_limit' => $wp_memory_limit, |
| 287 |
'wp_debug_mode' => (defined('WP_DEBUG') && WP_DEBUG), |
| 288 |
'wp_cron' => !(defined('DISABLE_WP_CRON') && DISABLE_WP_CRON), |
| 289 |
'language' => get_locale(), |
| 290 |
'external_object_cache' => wp_using_ext_object_cache(), |
| 291 |
'server_info' => isset($_SERVER['SERVER_SOFTWARE']) ? sanitize_text_field( wp_unslash($_SERVER['SERVER_SOFTWARE']) ) : '', |
| 292 |
'php_version' => phpversion(), |
| 293 |
'php_post_max_size' => self::human_readable_num(ini_get('post_max_size')), |
| 294 |
'php_max_execution_time' => ini_get('max_execution_time'), |
| 295 |
'php_max_input_vars' => ini_get('max_input_vars'), |
| 296 |
'curl_version' => $curl_version, |
| 297 |
'suhosin_installed' => extension_loaded('suhosin'), |
| 298 |
'max_upload_size' => wp_max_upload_size(), |
| 299 |
'default_timezone' => date_default_timezone_get(), |
| 300 |
'fsockopen_or_curl_enabled' => (function_exists('fsockopen') || function_exists('curl_init')), |
| 301 |
'soapclient_enabled' => class_exists('SoapClient'), |
| 302 |
'domdocument_enabled' => class_exists('DOMDocument'), |
| 303 |
'gzip_enabled' => is_callable('gzopen'), |
| 304 |
'mbstring_enabled' => extension_loaded('mbstring'), |
| 305 |
); |
| 306 |
} |
| 307 |
} |
| 308 |
|