PluginProbe
MaxButtons – Create buttons / 6.28
MaxButtons – Create buttons v6.28
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 6.28, at classes/max-utils.php

471 lines 11.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', '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 static function fixFAConflict()
297 {
298 $forcefa = get_option('maxbuttons_forcefa');
299
300 if ($forcefa != '1')
301 return;
302
303 global $wp_styles;
304
305 $our_fa_there = false;
306
307 foreach($wp_styles->registered as $script => $details)
308 {
309 if ($script == 'mbpro-font-awesome')
310 {
311 $our_fa_there = true;
312 break;
313 }
314 }
315
316 // fix nothing on pages where we are not loading.
317 if (! $our_fa_there)
318 return;
319
320
321 // Loop through all registered styles and remove any that appear to be Font Awesome.
322 foreach ( $wp_styles->registered as $script => $details ) {
323 $src = isset($details->src) ? $details->src : false;
324
325 if ($script == 'mbpro-font-awesome')
326 {
327 $mbpro_src = $src;
328 continue; // exclude us
329 }
330
331 if ( false !== strpos( $script, 'fontawesome' ) || false !== strpos( $script, 'font-awesome' ) ) {
332 wp_dequeue_style( $script );
333 }
334 if ($src && ( false !== strpos($src, 'font-awesome') || false !== strpos($src, 'fontawesome') ) )
335 {
336 wp_dequeue_style( $script );
337 }
338
339 }
340
341 // 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' .
342 wp_register_style('fontawesome', $src);
343
344 }
345
346
347 static function timeInit()
348 {
349 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
350 return;
351
352 self::$timer = microtime(true);
353
354 if (is_admin())
355 add_filter("admin_footer",array(self::namespaceit('maxUtils'), "showTime"), 100);
356 else
357 add_action("wp_footer",array(self::namespaceit('maxUtils'), "showTime"));
358
359 }
360
361 static function addTime($msg)
362 {
363 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
364 return;
365
366
367 self::$timings[] = array("msg" => $msg,"time" => microtime(true));
368 }
369
370 static function startTime($operation)
371 {
372 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
373 return;
374
375 self::$time_operations[$operation][] = array("start" => microtime(true),
376 "end" => 0,
377 );
378
379 }
380
381 static function endTime($operation)
382 {
383 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
384 return;
385
386 $timedcount = count(self::$time_operations[$operation]);
387 for ($i = 0; $i < $timedcount; $i++)
388 {
389 if (self::$time_operations[$operation][$i]["end"] == 0)
390 {
391 self::$time_operations[$operation][$i]["end"] = microtime(true);
392 break;
393 }
394 }
395
396 }
397
398 static function showTime()
399 {
400 if ( ! defined('MAXBUTTONS_BENCHMARK') || MAXBUTTONS_BENCHMARK !== true)
401 return;
402
403 $timer = self::$timer;
404 $text = '';
405 $text .= "<div id='mb-timer'>";
406 $text .= "<p><strong>Timed Operations</strong></p>";
407
408 foreach(self::$time_operations as $operation => $operations)
409 {
410 foreach($operations as $index => $data)
411 {
412 $start = $data["start"];
413 $end = $data["end"];
414 $duration = $end - $start;
415
416
417 $text .= "<span class='first'>$duration</span>
418 <span class='second'>$operation</span>
419 <span class='third'>&nbsp; </span><br />
420 ";
421
422
423 }
424 }
425
426
427 $text .= "<p><strong>" . __("MaxButtons Loading Time:","maxbuttons") . "</strong></p>";
428 $prev_time =0;
429
430 $time_array = array();
431
432 foreach(self::$timings as $timing)
433 {
434 $cum = ($timing["time"] - $prev_time);
435 $text .= "<span class='first'>" . ($timing["time"] - $timer) . "</span><span class='second'> " . $timing["msg"] . "</span><span class='third'>$cum</span> <br /> ";
436 //$time_array[$cum] = $timing["msg"];
437 $prev_time = $timing["time"];
438 }
439
440 /*ksort($time_array);
441
442 $text .= "<br><br><strong>By time taken:</strong><br>";
443 foreach($time_array as $timeline)
444 {
445 $text .= "$timeline <br />";
446 }
447 */
448 $text .= "</div> ";
449 $text .= "<style>#mb-timer { margin-left: 180px; }
450 #mb-timer span {
451 display: inline-block;
452 font-size: 12px;
453 }
454 #mb-timer span.first {
455 width: 170px;
456 }
457 #mb-timer span.second {
458 width: 300px;
459 }
460 #mb-timer span.third {
461 width: 150px;
462 }
463 </style>";
464
465 echo $text;
466
467
468 //return $filter . $text;
469 }
470 }
471