PluginProbe
MaxButtons – Create buttons / 7.13
MaxButtons – Create buttons v7.13
6.23 6.24 6.25 6.26 6.26.1 6.27 6.28 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7.0 7.1 7.1.1 7.1.2 7.1.3 7.10 7.11 7.13 7.13.1 7.13.2 7.13.3 All 100 releases
maxbuttons / classes / max-utils.php

max-utils.php in MaxButtons – Create buttons 7.13, at classes/max-utils.php

506 lines 12.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace MaxButtons;
3 defined('ABSPATH') or die('No direct access permitted');
4
5 // new class for the future.
6 class maxUtils
7 {
8
9 protected static $timings = array();
10 protected static $time_operations = array();
11 protected static $timer = 0;
12
13
14 /** Callback for array filter to prepend namepaces. **/
15 public static function array_namespace($var)
16 {
17 $namespace = __NAMESPACE__ . '\\'; // PHP 5.3
18 return ($namespace . $var);
19 }
20
21 public static function namespaceit($var)
22 {
23 $namespace = __NAMESPACE__ . '\\'; // PHP 5.3
24 return $namespace . $var;
25 }
26
27 // central ajax action handler
28 public static function ajax_action()
29 {
30 $status = 'error';
31
32 $plugin_action = isset($_POST['plugin_action']) ? sanitize_text_field($_POST['plugin_action']) : '';
33 $nonce = isset($_POST['nonce']) ? $_POST['nonce'] : false;
34 $message = __( sprintf("No Handler found for action %s ", $plugin_action), 'maxbuttons');
35
36
37 if (! wp_verify_nonce($nonce, 'maxajax') )
38 {
39 $message = __('Nonce not verified (' . $nonce . ')', 'maxbuttons');
40 }
41 else
42 {
43 do_action('maxbuttons/ajax/' . $plugin_action, $_POST);
44 }
45
46 /*echo json_encode( array( 'status' => $status,
47 'message' => $message,
48 )
49 ); */
50
51 wp_send_json_error( array('message' => $message) );
52
53 }
54
55 public static function translit($string)
56 {
57 require_once(MB()->get_plugin_path() . "assets/libraries/url_slug.php");
58
59 $string = mb_url_slug($string, array("transliterate" => true));
60
61 return $string;
62 }
63
64 public static function selectify($name, $array, $selected, $target = '', $class = '')
65 {
66 // optional target for js updating
67 if ($target != '' )
68 $target = " data-target='$target' ";
69 if ($class != '')
70 $class = " class='$class' ";
71 $output = "<select name='$name' id='$name' $target $class>";
72
73 foreach($array as $key => $value)
74 {
75 $output .= "<option value='$key' " . selected($key, $selected, false) . ">$value</option>";
76 }
77 $output .= "</select>";
78
79 return $output;
80
81 }
82
83
84 static function hex2rgba($color, $opacity) {
85 // Grab the hex color and remove #
86
87 /* Check if color is already rgba. This can happen with transparency. */
88 if (strpos($color, 'rgba') !== false)
89 return $color;
90
91 $hex = str_replace("#", "", $color);
92
93 // Convert hex to rgb
94 if(strlen($color) == 3) {
95 // If in the #fff variety
96 $r = hexdec(substr($hex, 0, 1).substr($hex, 0, 1));
97 $g = hexdec(substr($hex, 1, 1).substr($hex, 1, 1));
98 $b = hexdec(substr($hex, 2, 1).substr($hex, 2, 1));
99 } else {
100 // If in the #ffffff variety
101 $r = hexdec(substr($hex, 0, 2));
102 $g = hexdec(substr($hex, 2, 2));
103 $b = hexdec(substr($hex, 4, 2));
104 }
105
106 // The array of rgb values
107 $rgb_array = array($r, $g, $b);
108
109 // Catch for opacity when the button has not been saved
110 if($opacity == '') {
111 $alpha = 1;
112 } else {
113 // Alpha value in decimal when an opacity has been set
114 $alpha = $opacity / 100;
115 }
116
117 // The rgb values separated by commas
118 $rgb = implode(", ", $rgb_array);
119
120 // Spits out rgba(0, 0, 0, 0.5) format
121 return 'rgba(' . $rgb . ', ' . $alpha . ')';
122 }
123
124 // test if color value is in RGBA or not.
125 static function isrgba($value)
126 {
127 if (strpos($value, 'rgb') >= 0 )
128 return true;
129 else {
130 return false;
131 }
132 }
133
134 static function strip_px($value) {
135 $value = rtrim( intval($value), 'px');
136 $value = rtrim( intval($value), '%');
137 return $value;
138 }
139
140 static function generate_font_sizes($start, $end, $step = 1)
141 {
142 $sizes = array();
143
144 for ($i = $start; $i <= $end; $i += $step)
145 {
146 $sizes[$i] = $i;
147
148 }
149 return $sizes;
150
151 }
152
153
154 static function get_media_query($get_option = 1)
155 {
156
157 $queries = array("phone" => "only screen and (max-width : 480px)",
158 "phone_land" => "only screen and (min-width : 321px) and (max-width : 480px)",
159 "phone_portrait" => " only screen and (max-width : 320px)",
160 "ipad" => "only screen and (min-width : 768px) and (max-width : 1024px)",
161 "medium_phone" => "only screen and (min-width: 480px) and (max-width: 768px)",
162 "ipad_land" => "only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape)",
163 "ipad_portrait" => "only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait)",
164 "desktop" => "only screen and (min-width : 1224px)",
165 "large_desktop" => "only screen and (min-width : 1824px)",
166 );
167
168 $query_names = array(
169 "phone" => __("Small phones, < 480px","maxbuttons"),
170 "phone_land" => __("Small phones (landscape) ","maxbuttons"),
171 "phone_portrait" => __("Small phones (portrait), < 320px ","maxbuttons"),
172 "medium_phone" => __("Medium-size (smart)phone (480px-768px)","maxbuttons"),
173 "ipad" => __("Ipad (all) / Large phones (768px-1024px)","maxbuttons"),
174 "ipad_land" => __("Ipad landscape","maxbuttons"),
175 "ipad_portrait" => __("Ipad portrait","maxbuttons"),
176 "desktop" => __("Desktop, > 1224px","maxbuttons"),
177 "large_desktop" => __("Large desktops","maxbuttons"),
178 "custom" => __("Custom size","maxbuttons"),
179 );
180
181 $query_descriptions = array(
182 "phone" => __("Optimized for small smartphones ( screen sizes under 480px )","maxbuttons"),
183 "phone_land" => __("Optimzed for small smartphones in landscape and higher ( screen sizes 321px - 480px)","maxbuttons"),
184 "phone_portrait" => __("Optimized for small phones ( screen size max 320px )","maxbuttons"),
185 "ipad" => __("Optimized for devices between 768px and 1024px","maxbuttons"),
186 "medium_phone" => __("Optimized for medium sizes devices between 480px and 768px","maxbuttons"),
187 "ipad_land" => __("Optimized for devices between 768px and 1024px in landscape","maxbuttons"),
188 "ipad_portrait" => __("Optimized for deviced between 768px and 1024 in portrait","maxbuttons"),
189 "desktop" => __("Desktop screens from 1224px","maxbuttons"),
190 "large_desktop" => __("Large desktop screens, from 1824px","maxbuttons"),
191 "custom" => __("Set your own breakpoints","maxbuttons"),
192 );
193
194
195 switch($get_option)
196 {
197 case 1:
198 return $query_names;
199 break;
200 case 2:
201 return $queries;
202 break;
203 case 3:
204 return $query_descriptions;
205 break;
206 }
207
208 }
209
210 static function get_buttons_table_name($old = false)
211 {
212 self::addTime('Legacy Function call : get_buttons_table_name');
213 return self::get_table_name($old);
214 }
215
216 static function get_table_name($old = false) {
217 global $wpdb;
218 if ($old)
219 return $wpdb->prefix . 'maxbuttons_buttons';
220 else
221 return $wpdb->prefix . 'maxbuttonsv3';
222 }
223
224 static function get_collection_table_name() {
225 global $wpdb;
226 return $wpdb->prefix . 'maxbuttons_collections';
227
228 }
229
230 static function get_coltrans_table_name() {
231 global $wpdb;
232 return $wpdb->prefix . 'maxbuttons_collections_trans';
233
234 }
235
236 /* Replacement function for Wordpress' transients and problematic name length. */
237 static function get_transient($name)
238 {
239 global $wpdb;
240 // self::removeExpiredTrans();
241
242 if ($name == '')
243 return false;
244
245 $table = self::get_coltrans_table_name();
246
247 $sql = "SELECT value FROM $table where name= '%s' ";
248 $sql = $wpdb->prepare($sql, $name);
249
250 $var = $wpdb->get_var($sql);
251
252 if (is_null($var))
253 $var = false;
254
255 return $var;
256
257 }
258
259
260 static function set_transient($name, $value , $expire = -1 )
261 {
262 global $wpdb;
263
264
265 if ($expire == -1 )
266 $expire = HOUR_IN_SECONDS * 4;
267
268 if ($name == '')
269 return false;
270
271 $expire_time = time() + $expire;
272
273 $table = self::get_coltrans_table_name();
274
275 // prevent doubles, remove any present by this name
276 self::delete_transient($name);
277
278 $wpdb->insert($table,
279 array("name" => $name,
280 "value" => $value,
281 "expire" => $expire_time
282 ),
283 array("%s","%s","%d"));
284
285
286 }
287
288 static function delete_transient($name)
289 {
290 global $wpdb;
291
292
293 $table = self::get_coltrans_table_name();
294 $wpdb->delete($table, array("name" => $name), array('%s') );
295
296 }
297
298 static function removeExpiredTrans()
299 {
300 global $wpdb;
301
302 $table = self::get_coltrans_table_name();
303 $sql = "DELETE FROM $table WHERE expire < UNIX_TIMESTAMP(NOW())";
304 $return = $wpdb->query($sql);
305
306 if($return === false)
307 {
308 $error = "Database error " . $wpdb->last_error;
309 MB()->add_notice('error', $error);
310 $install = MB()->getClass('install');
311 $install->create_database_table();
312 }
313
314 }
315
316 /** Function will try to unload any FA scripts other than MB from WP. In case of conflict */
317 /* 7.0 note - this function is currently not in use due to dynamic font library loading */
318 static function fixFAConflict()
319 {
320 $forcefa = get_option('maxbuttons_forcefa');
321
322 if ($forcefa != '1')
323 return;
324
325 global $wp_styles;
326
327 $our_fa_there = false;
328
329 foreach($wp_styles->registered as $script => $details)
330 {
331 if ($script == 'mbpro-font-awesome')
332 {
333 $our_fa_there = true;
334
335 break;
336 }
337 }
338
339 // fix nothing on pages where we are not loading.
340 if (! $our_fa_there)
341 {
342 return;
343 }
344
345 // Loop through all registered styles and remove any that appear to be Font Awesome.
346 foreach ( $wp_styles->registered as $script => $details ) {
347 $src = isset($details->src) ? $details->src : false;
348
349 if ($script == 'mbpro-font-awesome')
350 {
351 $mbpro_src = $src;
352 continue; // exclude us
353 }
354
355 // look at script handle
356
357 if ( false !== strpos( $script, 'fontawesome' ) || false !== strpos( $script, 'font-awesome' ) ) {
358 wp_dequeue_style( $script );
359 }
360 // look at file source
361 if ($src && ( false !== strpos($src, 'font-awesome') || false !== strpos($src, 'fontawesome') ) )
362 {
363 wp_dequeue_style( $script );
364 }
365
366 }
367
368 // This is a fix specific for NGGallery since they load their scripts weirdly / wrongly, but do check for the presence of a style named 'fontawesome' .
369 wp_register_style('fontawesome', $src);
370
371 }
372
373
374 static function timeInit()
375 {
376 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
377 return;
378
379 self::$timer = microtime(true);
380
381 if (is_admin())
382 add_filter("admin_footer",array(self::namespaceit('maxUtils'), "showTime"), 100);
383 else
384 add_action("wp_footer",array(self::namespaceit('maxUtils'), "showTime"));
385
386 }
387
388 static function addTime($msg)
389 {
390 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
391 return;
392
393
394 self::$timings[] = array("msg" => $msg,"time" => microtime(true));
395 }
396
397 static function startTime($operation)
398 {
399 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
400 return;
401
402 self::$time_operations[$operation][] = array("start" => microtime(true),
403 "end" => 0,
404 'memory_start' => memory_get_usage(),
405 );
406
407 }
408
409 static function endTime($operation)
410 {
411 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
412 return;
413
414 $timedcount = count(self::$time_operations[$operation]);
415 for ($i = 0; $i < $timedcount; $i++)
416 {
417 if (self::$time_operations[$operation][$i]["end"] == 0)
418 {
419 self::$time_operations[$operation][$i]["end"] = microtime(true);
420 self::$time_operations[$operation][$i]["memory_end"] = memory_get_usage();
421 break;
422 }
423 }
424
425 }
426
427 public static function convert_memory($size)
428 {
429 $unit=array('b','kb','mb','gb','tb','pb');
430 return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
431 }
432
433 public static function showTime()
434 {
435 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
436 return;
437
438 $timer = self::$timer;
439 $text = '';
440 $text .= "<div id='mb-timer'>";
441 $text .= "<p><strong>Timed Operations</strong></p>";
442
443 foreach(self::$time_operations as $operation => $operations)
444 {
445 foreach($operations as $index => $data)
446 {
447 $start = $data["start"];
448 $end = $data["end"];
449 $duration = $end - $start;
450 $mem_start = $data['memory_start'];
451 $mem_end = $data['memory_end'];
452
453
454 $text .= "<span class='first'>$duration</span>
455 <span class='second'>$operation</span>
456 <span class='third'>" . self::convert_memory($mem_start) . " - " . self::convert_memory($mem_end) . "</span><br />
457 ";
458 }
459 }
460
461
462 $text .= "<p><strong>" . __("MaxButtons Loading Time:","maxbuttons") . "</strong></p>";
463 $prev_time =0;
464
465 $time_array = array();
466
467 foreach(self::$timings as $timing)
468 {
469 $cum = ($timing["time"] - $prev_time);
470 $text .= "<span class='first'>" . ($timing["time"] - $timer) . "</span><span class='second'> " . $timing["msg"] . "</span><span class='third'>$cum</span> <br /> ";
471 //$time_array[$cum] = $timing["msg"];
472 $prev_time = $timing["time"];
473 }
474
475 /*ksort($time_array);
476
477 $text .= "<br><br><strong>By time taken:</strong><br>";
478 foreach($time_array as $timeline)
479 {
480 $text .= "$timeline <br />";
481 }
482 */
483 $text .= "</div> ";
484 $text .= "<style>#mb-timer { margin-left: 180px; }
485 #mb-timer span {
486 display: inline-block;
487 font-size: 12px;
488 }
489 #mb-timer span.first {
490 width: 170px;
491 }
492 #mb-timer span.second {
493 width: 300px;
494 }
495 #mb-timer span.third {
496 width: 150px;
497 }
498 </style>";
499
500 echo $text;
501
502
503 //return $filter . $text;
504 }
505 }
506