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

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