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

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