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

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