PluginProbe
MaxButtons – Create buttons / 6.4
MaxButtons – Create buttons v6.4
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 / admin-class.php

admin-class.php in MaxButtons – Create buttons 6.4, at classes/admin-class.php

317 lines 6.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 defined('ABSPATH') or die('No direct access permitted');
3
4 class maxButtonsAdmin
5 {
6
7 protected $wpdb;
8 protected static $instance = null;
9
10 function __construct()
11 {
12 global $wpdb;
13 $this->wpdb = $wpdb;
14 }
15
16 public static function getInstance()
17 {
18 if (is_null(self::$instance))
19 self::$instance = new maxButtonsAdmin();
20
21 return self::$instance;
22
23 }
24
25 public function loadFonts()
26 {
27 $fonts = array(
28 '' => '',
29 'Arial' => 'Arial',
30 'Courier New' => 'Courier New',
31 'Georgia' => 'Georgia',
32 'Tahoma' => 'Tahoma',
33 'Times New Roman' => 'Times New Roman',
34 'Trebuchet MS' => 'Trebuchet MS',
35 'Verdana' => 'Verdana'
36 );
37 return $fonts;
38 }
39
40 /* Get multiple buttons
41
42 Used for overview pages, retrieve buttons on basis of passed arguments.
43
44 @return array Array of found buttons with argument
45 */
46
47 function getButtons($args = array())
48 {
49
50 $defaults = array(
51 "status" => "publish",
52 "orderby" => "id",
53 "order" => "DESC",
54 "limit" => 20,
55 "paged" => 1,
56 );
57 $args = wp_parse_args($args, $defaults);
58
59 $limit = intval($args["limit"]);
60 $page = intval($args["paged"]);
61 $escape = array();
62 $escape[] = $args["status"];
63
64 // 'white-list' escaping
65 switch ($args["orderby"])
66 {
67 case "id";
68 $orderby = "id";
69 break;
70 case "name":
71 default:
72 $orderby = "name";
73 break;
74
75 }
76
77 switch($args["order"])
78 {
79 case "DESC":
80 case "desc":
81 $order = "DESC";
82 break;
83 case "ASC":
84 case "asc":
85 default:
86 $order = "ASC";
87 break;
88 }
89
90
91 $sql = "SELECT id FROM " . maxUtils::get_table_name() . " WHERE status = '%s'";
92 if ($args["orderby"] != '')
93 {
94 $sql .= " ORDER BY $orderby $order";
95
96 }
97
98 if ($limit > 0)
99 {
100
101 if ($page == 1 )
102 $offset = 0;
103 else
104 $offset = ($page-1) * $limit;
105
106 $sql .= " LIMIT $offset, $limit ";
107 }
108
109 $sql = $this->wpdb->prepare($sql,$escape , ARRAY_A);
110
111 $buttons = $this->wpdb->get_results($sql, ARRAY_A);
112
113
114 return $buttons;
115
116 }
117
118 function getButtonCount($args = array())
119 {
120 $defaults = array(
121 "status" => "publish",
122
123 );
124 $args = wp_parse_args($args, $defaults);
125
126 $sql = "SELECT count(id) FROM " . maxUtils::get_table_name() . " WHERE status = '%s'";
127 $sql = $this->wpdb->prepare($sql, $args["status"] );
128 $result = $this->wpdb->get_var($sql);
129 return $result;
130
131 }
132
133 function getButtonPages($args = array())
134 {
135 $defaults = array(
136 "limit" => 20,
137 "paged" => 1,
138 "status" => "publish",
139 "output" => "list", // not used, future arg.
140 "view" => "all",
141
142 );
143
144 $args = wp_parse_args($args, $defaults);
145
146 $limit = intval($args["limit"]);
147 $page = intval($args["paged"]);
148 $view = $args["view"];
149
150 $total = $this->getButtonCount(array("status" => $args["status"]));
151
152 $num_pages = ceil($total / $limit);
153
154 if ($num_pages == 0) $num_pages = 1; // lowest limit, page 1
155 $output = '';
156 $url = $_SERVER['REQUEST_URI'];
157
158 $url = remove_query_arg("view", $url);
159 $url = add_query_arg("view", $view, $url);
160
161 $first_url = ($page != 1 ) ? add_query_arg("paged", 1, $url) : false;
162 $last_url = ($page != $num_pages) ? add_query_arg("paged", $num_pages, $url) : false;
163 $next_url = ($page != $num_pages) ? add_query_arg("paged", ($page + 1), $url) : false;
164 $next_page = ($page != $num_pages) ? ($page + 1) : false;
165 $prev_page = ($page != 1) ? ($page -1 ) : false;
166 $prev_url = ($page != 1 ) ? add_query_arg("paged", ($page -1), $url) : false;
167
168
169 $return = array(
170 "first" => 1,
171 "base" => remove_query_arg("paged",$url),
172 "first_url" => esc_url($first_url),
173 "last" => $num_pages,
174 "last_url" => esc_url($last_url),
175 "next_url" => esc_url($next_url),
176 "prev_url" => esc_url($prev_url),
177 "prev_page" => $prev_page,
178 "next_page" => $next_page,
179 "total" => $total,
180 "current" => $page,
181
182
183
184 );
185
186 return $return;
187 }
188
189 static function getAjaxButtons()
190 {
191
192 $admin = self::getInstance();
193 $args = array();
194
195 $paged = (isset($_REQUEST["paged"])) ? intval($_REQUEST["paged"]) : 1;
196 if ($paged > 0)
197 $args["paged" ] = $paged;
198
199
200 $button = new MaxButton();
201 $buttons = $admin->getButtons($args);
202
203 echo "<div id='maxbuttons'><div class='preview-buttons'>";
204 echo '<div class="tablenav top"> ';
205
206 do_action('mb-display-pagination', $args);
207 echo '<span class="loading"></span>';
208 echo '</div>';
209
210
211 if (count($buttons) == 0)
212 {
213
214 $url = admin_url('admin.php?page=maxbuttons-controller&action=edit');
215 echo "<p><strong>" . __("You didn't add any buttons yet!","maxbuttons") . "</strong></p>";
216 echo "<P>" . sprintf(__("Click %shere%s to add one", "maxbuttons"),
217 "<a href='$url' target='_blank'>", "</a>") . "</strong></p>";
218
219 }
220
221 foreach($buttons as $b)
222 {
223
224 $button_id = $b["id"];
225 $button->set($button_id);
226 echo "<div class='button-row button-select' data-button='$button_id'>";
227 echo "<span class='col col_insert'> ";
228
229 echo "<span class='small'>[ID: $button_id ]</span>
230 </span> ";
231
232 echo "<span class='col col_button'><div class='shortcode-container'>";
233 $button->display(array("mode" => "preview", "load_css" => "inline" ));
234 echo "</div></span>";
235 echo "<span class='col col_name'>" . $button->getName() . "</span>";
236 echo "</div>";
237 }
238 echo '<div class="tablenav bottom"> ';
239 do_action('mb-display-pagination', $args);
240 echo '<span class="loading"></span>';
241 echo '</div>';
242
243
244 echo "</div></div>";
245 echo "<p>&nbsp;</p>";
246
247 exit();
248
249 }
250 function get_header($args =array() )
251 {
252 $defaults = array(
253 "tabs_active" => false,
254 "title" => "",
255 "action" => "",
256 );
257
258 $args = wp_parse_args($args, $defaults);
259 extract($args);
260
261 include_once(MB()->get_plugin_path() . "includes/admin_header.php");
262
263 }
264
265
266 function get_footer()
267 {
268 include_once(MB()->get_plugin_path() . "includes/admin_footer.php");
269
270 }
271
272 // unified (future way to end ajax requests + feedback
273 function endAjaxRequest($args = array())
274 {
275 $defaults = array(
276 "error" => true, // can have errors and still result true on success
277 "result" => true,
278 "body" => "",
279 "title" => "",
280 "data" => array(),
281 );
282
283 $args = wp_parse_args($args, $defaults);
284
285 echo json_encode($args);
286 die();
287
288
289 }
290
291 function log($action, $message)
292 {
293 if (! defined('MAXBUTTONS_DEBUG') || ! MAXBUTTONS_DEBUG)
294 return;
295
296 $stack = debug_backtrace();
297 $caller = $stack[1]['function'];
298
299 $dir = MB()->get_plugin_path() . "logs";
300 if (! is_dir($dir))
301 @mkdir($dir, 0777, true); // silently fail here.
302
303 if (! is_dir($dir))
304 return false;
305
306 $file = fopen($dir . "/maxbuttons.log", "a+");
307 $now = new DateTime() ;
308 $now_format = $now->format("d/M/Y H:i:s");
309
310 $write_string = "[" . $now_format . "] $action - $message ( $caller )";
311 fwrite($file, $write_string);
312 fclose($file);
313
314
315 }
316 }
317