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