| 1 |
<?php |
| 2 |
defined('ABSPATH') or die('No direct access permitted'); |
| 3 |
|
| 4 |
// new class for the future. |
| 5 |
class maxUtils |
| 6 |
{ |
| 7 |
|
| 8 |
protected static $timings = array(); |
| 9 |
protected static $time_operations = array(); |
| 10 |
protected static $timer = 0; |
| 11 |
|
| 12 |
static function translit($string) |
| 13 |
{ |
| 14 |
require_once(MB()->get_plugin_path() . "assets/libraries/url_slug.php"); |
| 15 |
$string = mb_url_slug($string, array("transliterate" => true)); |
| 16 |
return $string; |
| 17 |
} |
| 18 |
|
| 19 |
static function selectify($name, $array, $selected, $target = '', $class = '') |
| 20 |
{ |
| 21 |
// optional target for js updating |
| 22 |
if ($target != '' ) |
| 23 |
$target = " data-target='$target' "; |
| 24 |
if ($class != '') |
| 25 |
$class = " class='$class' "; |
| 26 |
$output = "<select name='$name' id='$name' $target $class>"; |
| 27 |
|
| 28 |
foreach($array as $key => $value) |
| 29 |
{ |
| 30 |
$output .= "<option value='$key' " . selected($key, $selected, false) . ">$value</option>"; |
| 31 |
} |
| 32 |
$output .= "</select>"; |
| 33 |
|
| 34 |
return $output; |
| 35 |
|
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
static function hex2rgba($color, $opacity) { |
| 40 |
// Grab the hex color and remove # |
| 41 |
$hex = str_replace("#", "", $color); |
| 42 |
|
| 43 |
// Convert hex to rgb |
| 44 |
if(strlen($color) == 3) { |
| 45 |
// If in the #fff variety |
| 46 |
$r = hexdec(substr($hex, 0, 1).substr($hex, 0, 1)); |
| 47 |
$g = hexdec(substr($hex, 1, 1).substr($hex, 1, 1)); |
| 48 |
$b = hexdec(substr($hex, 2, 1).substr($hex, 2, 1)); |
| 49 |
} else { |
| 50 |
// If in the #ffffff variety |
| 51 |
$r = hexdec(substr($hex, 0, 2)); |
| 52 |
$g = hexdec(substr($hex, 2, 2)); |
| 53 |
$b = hexdec(substr($hex, 4, 2)); |
| 54 |
} |
| 55 |
|
| 56 |
// The array of rgb values |
| 57 |
$rgb_array = array($r, $g, $b); |
| 58 |
|
| 59 |
// Catch for opacity when the button has not been saved |
| 60 |
if($opacity == '') { |
| 61 |
$alpha = 1; |
| 62 |
} else { |
| 63 |
// Alpha value in decimal when an opacity has been set |
| 64 |
$alpha = $opacity / 100; |
| 65 |
} |
| 66 |
|
| 67 |
// The rgb values separated by commas |
| 68 |
$rgb = implode(", ", $rgb_array); |
| 69 |
|
| 70 |
// Spits out rgba(0, 0, 0, 0.5) format |
| 71 |
return 'rgba(' . $rgb . ', ' . $alpha . ')'; |
| 72 |
} |
| 73 |
|
| 74 |
static function strip_px($value) { |
| 75 |
return rtrim( intval($value), 'px'); |
| 76 |
} |
| 77 |
|
| 78 |
static function generate_font_sizes($start, $end, $step = 1) |
| 79 |
{ |
| 80 |
$sizes = array(); |
| 81 |
|
| 82 |
for ($i = $start; $i <= $end; $i += $step) |
| 83 |
{ |
| 84 |
$sizes[$i] = $i; |
| 85 |
|
| 86 |
} |
| 87 |
return $sizes; |
| 88 |
|
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
static function get_media_query($get_option = 1) |
| 93 |
{ |
| 94 |
|
| 95 |
$queries = array("phone" => "only screen and (max-width : 480px)", |
| 96 |
"phone_land" => "only screen and (min-width : 321px) and (max-width : 480px)", |
| 97 |
"phone_portrait" => " only screen and (max-width : 320px)", |
| 98 |
"ipad" => "only screen and (min-width : 768px) and (max-width : 1024px)", |
| 99 |
"medium_phone" => "only screen and (min-width: 480px) and (max-width: 768px)", |
| 100 |
"ipad_land" => "only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape)", |
| 101 |
"ipad_portrait" => "only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait)", |
| 102 |
"desktop" => "only screen and (min-width : 1224px)", |
| 103 |
"large_desktop" => "only screen and (min-width : 1824px)", |
| 104 |
); |
| 105 |
|
| 106 |
$query_names = array( |
| 107 |
"phone" => __("Small phones","maxbuttons"), |
| 108 |
"phone_land" => __("Small phones (landscape)","maxbuttons"), |
| 109 |
"phone_portrait" => __("Small phones (portrait)","maxbuttons"), |
| 110 |
"medium_phone" => __("Medium-size (smart)phone","maxbuttons"), |
| 111 |
"ipad" => __("Ipad (all) / Large phones","maxbuttons"), |
| 112 |
"ipad_land" => __("Ipad landscape","maxbuttons"), |
| 113 |
"ipad_portrait" => __("Ipad portrait","maxbuttons"), |
| 114 |
"desktop" => __("Desktop","maxbuttons"), |
| 115 |
"large_desktop" => __("Large desktops","maxbuttons"), |
| 116 |
"custom" => __("Custom size","maxbuttons"), |
| 117 |
); |
| 118 |
|
| 119 |
$query_descriptions = array( |
| 120 |
"phone" => __("Optimized for small smartphones ( screen sizes under 480px )","maxbuttons"), |
| 121 |
"phone_land" => __("Optimzed for small smartphones in landscape and higher ( screen sizes 321px - 480px)","maxbuttons"), |
| 122 |
"phone_portrait" => __("Optimized for small phones ( screen size max 320px )","maxbuttons"), |
| 123 |
"ipad" => __("Optimized for devices between 768px and 1024px","maxbuttons"), |
| 124 |
"medium_phone" => __("Optimized for medium sizes devices between 480px and 768px","maxbuttons"), |
| 125 |
"ipad_land" => __("Optimized for devices between 768px and 1024px in landscape","maxbuttons"), |
| 126 |
"ipad_portrait" => __("Optimized for deviced between 768px and 1024 in portrait","maxbuttons"), |
| 127 |
"desktop" => __("Desktop screens from 1224px","maxbuttons"), |
| 128 |
"large_desktop" => __("Large desktop screens, from 1824px","maxbuttons"), |
| 129 |
"custom" => __("Set your own breakpoints","maxbuttons"), |
| 130 |
); |
| 131 |
|
| 132 |
|
| 133 |
switch($get_option) |
| 134 |
{ |
| 135 |
case 1: |
| 136 |
return $query_names; |
| 137 |
break; |
| 138 |
case 2: |
| 139 |
return $queries; |
| 140 |
break; |
| 141 |
case 3: |
| 142 |
return $query_descriptions; |
| 143 |
break; |
| 144 |
} |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
static function get_buttons_table_name($old = false) |
| 149 |
{ |
| 150 |
self::addTime('Legacy Function call : get_buttons_table_name'); |
| 151 |
return self::get_table_name($old); |
| 152 |
} |
| 153 |
|
| 154 |
static function get_table_name($old = false) { |
| 155 |
global $wpdb; |
| 156 |
if ($old) |
| 157 |
return $wpdb->prefix . 'maxbuttons_buttons'; |
| 158 |
else |
| 159 |
return $wpdb->prefix . 'maxbuttonsv3'; |
| 160 |
} |
| 161 |
|
| 162 |
static function get_collection_table_name() { |
| 163 |
global $wpdb; |
| 164 |
return $wpdb->prefix . 'maxbuttons_collections'; |
| 165 |
|
| 166 |
} |
| 167 |
|
| 168 |
static function get_coltrans_table_name() { |
| 169 |
global $wpdb; |
| 170 |
return $wpdb->prefix . 'maxbuttons_collections_trans'; |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
/* Replacement function for Wordpress' transients and problematic name length. */ |
| 175 |
static function get_transient($name) |
| 176 |
{ |
| 177 |
global $wpdb; |
| 178 |
// self::removeExpiredTrans(); |
| 179 |
|
| 180 |
if ($name == '') |
| 181 |
return false; |
| 182 |
|
| 183 |
$table = self::get_coltrans_table_name(); |
| 184 |
|
| 185 |
$sql = "SELECT value FROM $table where name= '%s' "; |
| 186 |
$sql = $wpdb->prepare($sql, $name); |
| 187 |
|
| 188 |
$var = $wpdb->get_var($sql); |
| 189 |
|
| 190 |
if (is_null($var)) |
| 191 |
$var = false; |
| 192 |
|
| 193 |
return $var; |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
static function set_transient($name, $value , $expire = -1 ) |
| 199 |
{ |
| 200 |
global $wpdb; |
| 201 |
|
| 202 |
|
| 203 |
if ($expire == -1 ) |
| 204 |
$expire = HOUR_IN_SECONDS * 4; |
| 205 |
|
| 206 |
if ($name == '') |
| 207 |
return false; |
| 208 |
|
| 209 |
$expire_time = time() + $expire; |
| 210 |
|
| 211 |
$table = self::get_coltrans_table_name(); |
| 212 |
|
| 213 |
// prevent doubles, remove any present by this name |
| 214 |
self::delete_transient($name); |
| 215 |
|
| 216 |
$wpdb->insert($table, |
| 217 |
array("name" => $name, |
| 218 |
"value" => $value, |
| 219 |
"expire" => $expire_time |
| 220 |
), |
| 221 |
array("%s","%s","%d")); |
| 222 |
|
| 223 |
|
| 224 |
} |
| 225 |
|
| 226 |
static function delete_transient($name) |
| 227 |
{ |
| 228 |
global $wpdb; |
| 229 |
|
| 230 |
|
| 231 |
$table = self::get_coltrans_table_name(); |
| 232 |
$wpdb->delete($table, array("name" => $name), array('%s') ); |
| 233 |
|
| 234 |
} |
| 235 |
|
| 236 |
static function removeExpiredTrans() |
| 237 |
{ |
| 238 |
global $wpdb; |
| 239 |
|
| 240 |
$table = self::get_coltrans_table_name(); |
| 241 |
$sql = "DELETE FROM $table WHERE expire < UNIX_TIMESTAMP(NOW())"; |
| 242 |
$return = $wpdb->query($sql); |
| 243 |
|
| 244 |
if($return === false) |
| 245 |
{ |
| 246 |
$error = "Database error " . $wpdb->last_error; |
| 247 |
MB()->add_notice('error', $error); |
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
static function timeInit() |
| 254 |
{ |
| 255 |
if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true) |
| 256 |
return; |
| 257 |
|
| 258 |
self::$timer = microtime(true); |
| 259 |
|
| 260 |
if (is_admin()) |
| 261 |
add_filter("admin_footer",array('maxUtils', "showTime"), 100); |
| 262 |
else |
| 263 |
add_action("wp_footer",array('maxUtils', "showTime")); |
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
static function addTime($msg) |
| 268 |
{ |
| 269 |
if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true) |
| 270 |
return; |
| 271 |
|
| 272 |
/*if (count(self::$timings) == 0) |
| 273 |
{ |
| 274 |
self::timeInit(); |
| 275 |
} */ |
| 276 |
|
| 277 |
self::$timings[] = array("msg" => $msg,"time" => microtime(true)); |
| 278 |
} |
| 279 |
|
| 280 |
static function startTime($operation) |
| 281 |
{ |
| 282 |
if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true) |
| 283 |
return; |
| 284 |
|
| 285 |
self::$time_operations[$operation][] = array("start" => microtime(true), |
| 286 |
"end" => 0, |
| 287 |
); |
| 288 |
|
| 289 |
} |
| 290 |
|
| 291 |
static function endTime($operation) |
| 292 |
{ |
| 293 |
if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true) |
| 294 |
return; |
| 295 |
|
| 296 |
$timedcount = count(self::$time_operations[$operation]); |
| 297 |
for ($i = 0; $i < $timedcount; $i++) |
| 298 |
{ |
| 299 |
if (self::$time_operations[$operation][$i]["end"] == 0) |
| 300 |
{ |
| 301 |
self::$time_operations[$operation][$i]["end"] = microtime(true); |
| 302 |
break; |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
} |
| 307 |
|
| 308 |
static function showTime() |
| 309 |
{ |
| 310 |
if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true) |
| 311 |
return; |
| 312 |
|
| 313 |
$timer = self::$timer; |
| 314 |
$text = ''; |
| 315 |
$text .= "<div id='mb-timer'>"; |
| 316 |
$text .= "<p><strong>Timed Operations</strong></p>"; |
| 317 |
|
| 318 |
foreach(self::$time_operations as $operation => $operations) |
| 319 |
{ |
| 320 |
foreach($operations as $index => $data) |
| 321 |
{ |
| 322 |
$start = $data["start"]; |
| 323 |
$end = $data["end"]; |
| 324 |
$duration = $end - $start; |
| 325 |
|
| 326 |
|
| 327 |
$text .= "<span class='first'>$duration</span> |
| 328 |
<span class='second'>$operation</span> |
| 329 |
<span class='third'> </span><br /> |
| 330 |
"; |
| 331 |
|
| 332 |
|
| 333 |
} |
| 334 |
} |
| 335 |
|
| 336 |
|
| 337 |
$text .= "<p><strong>" . __("MaxButtons Loading Time:","maxbuttons") . "</strong></p>"; |
| 338 |
$prev_time =0; |
| 339 |
|
| 340 |
$time_array = array(); |
| 341 |
|
| 342 |
foreach(self::$timings as $timing) |
| 343 |
{ |
| 344 |
$cum = ($timing["time"] - $prev_time); |
| 345 |
$text .= "<span class='first'>" . ($timing["time"] - $timer) . "</span><span class='second'> " . $timing["msg"] . "</span><span class='third'>$cum</span> <br /> "; |
| 346 |
//$time_array[$cum] = $timing["msg"]; |
| 347 |
$prev_time = $timing["time"]; |
| 348 |
} |
| 349 |
|
| 350 |
/*ksort($time_array); |
| 351 |
|
| 352 |
$text .= "<br><br><strong>By time taken:</strong><br>"; |
| 353 |
foreach($time_array as $timeline) |
| 354 |
{ |
| 355 |
$text .= "$timeline <br />"; |
| 356 |
} |
| 357 |
*/ |
| 358 |
$text .= "</div> "; |
| 359 |
$text .= "<style>#mb-timer { margin-left: 180px; } |
| 360 |
#mb-timer span { |
| 361 |
display: inline-block; |
| 362 |
font-size: 12px; |
| 363 |
} |
| 364 |
#mb-timer span.first { |
| 365 |
width: 170px; |
| 366 |
} |
| 367 |
#mb-timer span.second { |
| 368 |
width: 300px; |
| 369 |
} |
| 370 |
#mb-timer span.third { |
| 371 |
width: 150px; |
| 372 |
} |
| 373 |
</style>"; |
| 374 |
|
| 375 |
echo $text; |
| 376 |
|
| 377 |
|
| 378 |
//return $filter . $text; |
| 379 |
} |
| 380 |
} |
| 381 |
|