PluginProbe
MaxButtons – Create buttons / 7.13.1
MaxButtons – Create buttons v7.13.1
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.1, at classes/max-utils.php

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