PluginProbe
MaxButtons – Create buttons / 9.8.2
MaxButtons – Create buttons v9.8.2
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 9.8.2, at classes/max-utils.php

540 lines 14.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 declare(strict_types=1);
3 namespace MaxButtons;
4 defined('ABSPATH') or die('No direct access permitted');
5
6 // new class for the future.
7 class maxUtils
8 {
9
10 protected static $timings = array();
11 protected static $time_operations = array();
12 protected static $timer = 0;
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 public static function getAllowedProcotols($args = array())
78 {
79 $defaults = array(
80 'is_shortcode' => false,
81 );
82
83 $args = wp_parse_args($args, $defaults);
84
85 if (false === $args['is_shortcode'])
86 {
87 // allowed url protocols for esc_url functions
88 $protocols = array('http','https','ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'sms', 'callto', 'fax', 'xmpp', 'javascript', 'file', 'ms-windows-store', 'steam', 'webcal');
89 }
90 else {
91 $protocols = array('http','https','ftp', 'ftps', 'mailto', 'news', 'irc', 'gopher', 'nntp', 'feed', 'telnet', 'mms', 'rtsp', 'svn', 'tel', 'sms', 'callto', 'fax', 'xmpp', 'ms-windows-store', 'steam', 'webcal');
92 }
93
94 $extra_protocols = get_option('maxbuttons_protocol');
95 if (! is_bool($extra_protocols)) // if option is set
96 {
97 $extra_protocols = array_map('trim', array_filter(explode(',', $extra_protocols)));
98 }
99
100 if (is_array($extra_protocols) && count($extra_protocols) > 0)
101 {
102 $protocols = array_merge($protocols, $extra_protocols);
103 }
104
105 return $protocols;
106 }
107
108 public static function hex2rgba($color, $opacity) {
109 // Grab the hex color and remove #
110
111 /* Check if color is already rgba. This can happen with transparency. */
112 if (strpos($color, 'rgba') !== false)
113 return $color;
114
115 $hex = str_replace("#", "", $color);
116
117 // Convert hex to rgb
118 if(strlen($color) == 3) {
119 // If in the #fff variety
120 $r = hexdec(substr($hex, 0, 1).substr($hex, 0, 1));
121 $g = hexdec(substr($hex, 1, 1).substr($hex, 1, 1));
122 $b = hexdec(substr($hex, 2, 1).substr($hex, 2, 1));
123 } else {
124 // If in the #ffffff variety
125 $r = hexdec(substr($hex, 0, 2));
126 $g = hexdec(substr($hex, 2, 2));
127 $b = hexdec(substr($hex, 4, 2));
128 }
129
130 // The array of rgb values
131 $rgb_array = array($r, $g, $b);
132
133 // Catch for opacity when the button has not been saved
134 if($opacity == '') {
135 $alpha = 1;
136 } else {
137 // Alpha value in decimal when an opacity has been set
138 $alpha = $opacity / 100;
139 }
140
141 // The rgb values separated by commas
142 $rgb = implode(", ", $rgb_array);
143
144 // Spits out rgba(0, 0, 0, 0.5) format
145 return 'rgba(' . $rgb . ', ' . $alpha . ')';
146 }
147
148 // test if color value is in RGBA or not.
149 public static function isrgba($value)
150 {
151 // Can't have rgb is not string
152 if (! is_string($value))
153 {
154 return false;
155 }
156 elseif (strpos($value, 'rgb') >= 0 )
157 {
158 return true;
159 }
160
161 return false;
162 }
163
164 public static function strip_px($value) {
165
166 // If not string, can't have this px / % values.
167 if (false === is_string($value))
168 {
169 return $value;
170 }
171
172 $value = rtrim( $value, 'px');
173 $value = rtrim( $value, '%');
174 return $value;
175 }
176
177 // This will be needed for converting from old formats to the new screens.
178 public static function legacy_get_media_query($get_option = 1)
179 {
180
181 $queries = array("phone" => "only screen and (max-width : 480px)",
182 "phone_land" => "only screen and (min-width : 321px) and (max-width : 480px)",
183 "phone_portrait" => " only screen and (max-width : 320px)",
184 "ipad" => "only screen and (min-width : 768px) and (max-width : 1024px)",
185 "medium_phone" => "only screen and (min-width: 480px) and (max-width: 768px)",
186 "ipad_land" => "only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : landscape)",
187 "ipad_portrait" => "only screen and (min-device-width : 768px) and (max-device-width : 1024px) and (orientation : portrait)",
188 "desktop" => "only screen and (min-width : 1224px)",
189 "large_desktop" => "only screen and (min-width : 1824px)",
190 );
191
192 $query_names = array(
193 "phone" => __("Small phones, < 480px","maxbuttons"),
194 "phone_land" => __("Small phones (landscape) ","maxbuttons"),
195 "phone_portrait" => __("Small phones (portrait), < 320px ","maxbuttons"),
196 "medium_phone" => __("Medium-size (smart)phone (480px-768px)","maxbuttons"),
197 "ipad" => __("Ipad (all) / Large phones (768px-1024px)","maxbuttons"),
198 "ipad_land" => __("Ipad landscape","maxbuttons"),
199 "ipad_portrait" => __("Ipad portrait","maxbuttons"),
200 "desktop" => __("Desktop, > 1224px","maxbuttons"),
201 "large_desktop" => __("Large desktops","maxbuttons"),
202 "custom" => __("Custom size","maxbuttons"),
203 );
204
205 $query_descriptions = array(
206 "phone" => __("Optimized for small smartphones ( screen sizes under 480px )","maxbuttons"),
207 "phone_land" => __("Optimzed for small smartphones in landscape and higher ( screen sizes 321px - 480px)","maxbuttons"),
208 "phone_portrait" => __("Optimized for small phones ( screen size max 320px )","maxbuttons"),
209 "ipad" => __("Optimized for devices between 768px and 1024px","maxbuttons"),
210 "medium_phone" => __("Optimized for medium sizes devices between 480px and 768px","maxbuttons"),
211 "ipad_land" => __("Optimized for devices between 768px and 1024px in landscape","maxbuttons"),
212 "ipad_portrait" => __("Optimized for deviced between 768px and 1024 in portrait","maxbuttons"),
213 "desktop" => __("Desktop screens from 1224px","maxbuttons"),
214 "large_desktop" => __("Large desktop screens, from 1824px","maxbuttons"),
215 "custom" => __("Set your own breakpoints","maxbuttons"),
216 );
217
218
219 switch($get_option)
220 {
221 case 1:
222 return $query_names;
223 break;
224 case 2:
225 return $queries;
226 break;
227 case 3:
228 return $query_descriptions;
229 break;
230 default:
231 return $query_names;
232 }
233
234 }
235
236 public static function get_buttons_table_name($old = false)
237 {
238 self::addTime('Legacy Function call : get_buttons_table_name');
239 return self::get_table_name($old);
240 }
241
242 public static function get_table_name($old = false) {
243 global $wpdb;
244 if ($old)
245 return $wpdb->prefix . 'maxbuttons_buttons';
246 else
247 return $wpdb->prefix . 'maxbuttonsv3';
248 }
249
250 public static function get_collection_table_name() {
251 global $wpdb;
252 return $wpdb->prefix . 'maxbuttons_collections';
253
254 }
255
256 public static function get_coltrans_table_name() {
257 global $wpdb;
258 return $wpdb->prefix . 'maxbuttons_collections_trans';
259
260 }
261
262 /* Replacement function for Wordpress' transients and problematic name length. */
263 public static function get_transient($name)
264 {
265 global $wpdb;
266 // self::removeExpiredTrans();
267
268 if ($name == '')
269 return false;
270
271 $table = self::get_coltrans_table_name();
272
273 $sql = "SELECT value FROM $table where name= '%s' ";
274 $sql = $wpdb->prepare($sql, $name);
275
276 $var = $wpdb->get_var($sql);
277
278 if (is_null($var))
279 $var = false;
280
281 return $var;
282
283 }
284
285
286 public static function set_transient($name, $value , $expire = -1 )
287 {
288 global $wpdb;
289
290
291 if ($expire == -1 )
292 $expire = HOUR_IN_SECONDS * 4;
293
294 if ($name == '')
295 return false;
296
297 $expire_time = time() + $expire;
298
299 $table = self::get_coltrans_table_name();
300
301 // prevent doubles, remove any present by this name
302 self::delete_transient($name);
303
304 $wpdb->insert($table,
305 array("name" => $name,
306 "value" => $value,
307 "expire" => $expire_time
308 ),
309 array("%s","%s","%d"));
310 }
311
312 public static function delete_transient($name)
313 {
314 global $wpdb;
315
316 $table = self::get_coltrans_table_name();
317 $wpdb->delete($table, array("name" => $name), array('%s') );
318
319 }
320
321 public static function removeExpiredTrans()
322 {
323 global $wpdb;
324
325 $table = self::get_coltrans_table_name();
326 $sql = "DELETE FROM $table WHERE expire < UNIX_TIMESTAMP(NOW())";
327 $return = $wpdb->query($sql);
328
329 if($return === false)
330 {
331 $error = "Database error " . $wpdb->last_error;
332 MB()->add_notice('error', $error);
333 $install = MB()->getClass('install');
334 $install->create_database_table();
335 }
336
337 }
338
339 /** Function will try to unload any FA scripts other than MB from WP. In case of conflict */
340 /* 7.0 note - this function is currently not in use due to dynamic font library loading */
341 public static function fixFAConflict()
342 {
343
344 $forcefa = get_option('maxbuttons_forcefa');
345
346 if ($forcefa != '1')
347 return;
348
349 global $wp_styles;
350
351 $our_fa_there = false;
352
353 foreach($wp_styles->registered as $script => $details)
354 {
355 if ($script == 'mbpro-font-awesome')
356 {
357 $our_fa_there = true;
358
359 break;
360 }
361 }
362
363 // fix nothing on pages where we are not loading.
364 if (! $our_fa_there)
365 {
366 return;
367 }
368
369 // Loop through all registered styles and remove any that appear to be Font Awesome.
370 foreach ( $wp_styles->registered as $script => $details ) {
371 $src = isset($details->src) ? $details->src : false;
372
373 if ($script == 'mbpro-font-awesome')
374 {
375 $mbpro_src = $src;
376 continue; // exclude us
377 }
378
379 // look at script handle
380
381 if ( false !== strpos( $script, 'fontawesome' ) || false !== strpos( $script, 'font-awesome' ) ) {
382 wp_dequeue_style( $script );
383 }
384 // look at file source
385 if ($src && ( false !== strpos($src, 'font-awesome') || false !== strpos($src, 'fontawesome') ) )
386 {
387 wp_dequeue_style( $script );
388 }
389
390 }
391
392 // 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' .
393 wp_register_style('fontawesome', $src);
394
395 }
396
397 public static function debugLog($string)
398 {
399 $upload_dir = wp_upload_dir();
400 $path = $upload_dir['path'];
401
402 $file = fopen( trailingslashit($path) . 'maxbuttons-debug.log','a+');
403
404 fwrite($file, var_export($string, true) );
405 fclose($file);
406
407 }
408
409 public static function timeInit()
410 {
411 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
412 return;
413
414 self::$timer = microtime(true);
415
416 if (is_admin())
417 add_filter("admin_footer",array(self::namespaceit('maxUtils'), "showTime"), 100);
418 else
419 add_action("wp_footer",array(self::namespaceit('maxUtils'), "showTime"));
420
421 }
422
423 public static function addTime($msg)
424 {
425 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
426 return;
427
428
429 self::$timings[] = array("msg" => $msg,"time" => microtime(true));
430 }
431
432 public static function startTime($operation)
433 {
434 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
435 return;
436
437 self::$time_operations[$operation][] = array("start" => microtime(true),
438 "end" => 0,
439 'memory_start' => memory_get_usage(),
440 );
441 }
442
443 public static function endTime($operation)
444 {
445 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
446 return;
447
448 $timedcount = count(self::$time_operations[$operation]);
449 for ($i = 0; $i < $timedcount; $i++)
450 {
451 if (self::$time_operations[$operation][$i]["end"] == 0)
452 {
453 self::$time_operations[$operation][$i]["end"] = microtime(true);
454 self::$time_operations[$operation][$i]["memory_end"] = memory_get_usage();
455 break;
456 }
457 }
458
459 }
460
461 public static function convert_memory($size)
462 {
463 $unit=array('b','kb','mb','gb','tb','pb');
464 return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
465 }
466
467 public static function showTime()
468 {
469 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
470 return;
471
472 $timer = self::$timer;
473 $text = '';
474 $text .= "<div id='mb-timer'>";
475 $text .= "<p><strong>Timed Operations</strong></p>";
476
477 foreach(self::$time_operations as $operation => $operations)
478 {
479 foreach($operations as $index => $data)
480 {
481 $start = $data["start"];
482 $end = $data["end"];
483 $duration = $end - $start;
484 $mem_start = $data['memory_start'];
485 $mem_end = $data['memory_end'];
486
487
488 $text .= "<span class='first'>$duration</span>
489 <span class='second'>$operation</span>
490 <span class='third'>" . self::convert_memory($mem_start) . " - " . self::convert_memory($mem_end) . "</span><br />
491 ";
492 }
493 }
494
495
496 $text .= "<p><strong>" . __("MaxButtons Loading Time:","maxbuttons") . "</strong></p>";
497 $prev_time =0;
498
499 $time_array = array();
500
501 foreach(self::$timings as $timing)
502 {
503 $cum = ($timing["time"] - $prev_time);
504 $text .= "<span class='first'>" . ($timing["time"] - $timer) . "</span><span class='second'> " . $timing["msg"] . "</span><span class='third'>$cum</span> <br /> ";
505 //$time_array[$cum] = $timing["msg"];
506 $prev_time = $timing["time"];
507 }
508
509 /*ksort($time_array);
510
511 $text .= "<br><br><strong>By time taken:</strong><br>";
512 foreach($time_array as $timeline)
513 {
514 $text .= "$timeline <br />";
515 }
516 */
517 $text .= "</div> ";
518 $text .= "<style>#mb-timer { margin-left: 180px; }
519 #mb-timer span {
520 display: inline-block;
521 font-size: 12px;
522 }
523 #mb-timer span.first {
524 width: 170px;
525 }
526 #mb-timer span.second {
527 width: 300px;
528 }
529 #mb-timer span.third {
530 width: 150px;
531 }
532 </style>";
533
534 echo $text;
535
536
537 //return $filter . $text;
538 }
539 }
540