PluginProbe
MaxButtons – Create buttons / 7.13
MaxButtons – Create buttons v7.13
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 7.13, at classes/admin-class.php

584 lines 14.0 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 class maxButtonsAdmin
6 {
7
8 protected $wpdb;
9 protected static $instance = null;
10
11 protected $fields = array();
12 protected $defined_fields = array();
13 protected $defined_updatable = array();
14
15 function __construct()
16 {
17 global $wpdb;
18 $this->wpdb = $wpdb;
19 }
20
21 public static function getInstance()
22 {
23 if (is_null(self::$instance))
24 self::$instance = new maxButtonsAdmin();
25
26 return self::$instance;
27
28 }
29
30 public function loadFonts()
31 {
32 $fonts = array(
33 '' => __('[Site Default]','maxbuttons'),
34 'Arial' => 'Arial',
35 'Courier New' => 'Courier New',
36 'Georgia' => 'Georgia',
37 'Tahoma' => 'Tahoma',
38 'Times New Roman' => 'Times New Roman',
39 'Trebuchet MS' => 'Trebuchet MS',
40 'Verdana' => 'Verdana'
41 );
42 return $fonts;
43 }
44
45
46 // add a maxfield to be displayed on the admin.
47 public function addfield($field, $start = '', $end = '')
48 {
49 $field_id = isset($field->id) ? $field->id : $field->template . \rand(0,1000);
50
51 $field->publish = false; // Output fields via class - never output.
52
53 $this->fields[$field_id] = array('field' => $field,
54 'start' => $start,
55 'end' => $end);
56 $this->fields = apply_filters('mb/editor/addfield', $this->fields, $field);
57
58 $this->defined_fields[] = $field_id;
59 do_action('mb/editor/afterfield/'. $field_id, $field);
60 }
61
62 /** Insert a field before or after a existing field
63 *
64 * @param String $insert_field Existing field to perform operation on
65 * @param Object $field The new field to insert
66 * @param String $start Start Template
67 * @param String $end End template
68 * @param String $op Insert before of after specified insert field
69 */
70 public function insertField($insert_field, $field, $start = 'start', $end = 'end', $op = 'before')
71 {
72 $insert_pos = 0;
73
74 $this->addField($field, $start, $end); // add the field to the array
75
76 foreach($this->fields as $field_id => $array)
77 {
78 if ($field_id == $insert_field)
79 {
80
81 break;
82 }
83 $insert_pos++;
84 }
85
86 // Find inserted field and remove it from array.
87 $new_field_id = $field->id;
88 $new_field_ar = $this->fields[$new_field_id];
89 unset($this->fields[$new_field_id]);
90
91 // Find position to insert new field
92 // Yes this could be more efficient.
93 $new_fields = array();
94 $i = 0;
95 foreach($this->fields as $field_id => $array)
96 {
97 if ($i == $insert_pos && $op == 'before')
98 {
99 $new_fields[$new_field_id] = $new_field_ar;
100 }
101 $new_fields[$field_id] = $array;
102 if ($i == $insert_pos && $op == 'after')
103 {
104 $new_fields[$new_field_id] = $new_field_ar;
105 }
106 $i++;
107 }
108 $this->fields = $new_fields;
109 }
110
111 public function getFields()
112 {
113 return $this->fields;
114 }
115
116 public function display_fields($clean = true, $return = false)
117 {
118 $fields = apply_filters('mb/display_fields', $this->fields);
119 $output = '';
120
121
122 foreach($fields as $id => $item)
123 {
124 $field = $item['field'];
125 $output .= $field->output($item['start'], $item['end']);
126 }
127
128 // auto-update system
129 $updatable = $this->defined_updatable;
130 foreach($updatable as $index => $field)
131 {
132 $updatable[$index] = '#' . $field;
133 }
134
135 $output .= ' <span class="updatables hidden">' . implode(',', $updatable) . '</span>';
136
137 if ($clean)
138 {
139 $this->fields = array();
140 $this->defined_updatable = array();
141 }
142
143 if (! $return)
144 echo $output;
145 else
146 return $output;
147
148 }
149
150 /* Get multiple buttons
151
152 Used for overview pages, retrieve buttons on basis of passed arguments.
153
154 @return array Array of found buttons with argument
155 */
156
157 function getButtons($args = array())
158 {
159 $defaults = array(
160 "status" => "publish",
161 "orderby" => "id",
162 "order" => "DESC",
163 "limit" => 20,
164 "paged" => 1,
165 );
166 $args = wp_parse_args($args, $defaults);
167
168 $limit = intval($args["limit"]);
169 $page = intval($args["paged"]);
170 $escape = array();
171 $escape[] = $args["status"];
172
173 // 'white-list' escaping
174 switch ($args["orderby"])
175 {
176 case "id";
177 $orderby = "id";
178 break;
179 case "name":
180 default:
181 $orderby = "name";
182 break;
183
184 }
185
186 switch($args["order"])
187 {
188 case "DESC":
189 case "desc":
190 $order = "DESC";
191 break;
192 case "ASC":
193 case "asc":
194 default:
195 $order = "ASC";
196 break;
197 }
198
199
200 $sql = "SELECT id FROM " . maxUtils::get_table_name() . " WHERE status = '%s'";
201 if ($args["orderby"] != '')
202 {
203 $sql .= " ORDER BY $orderby $order";
204 }
205
206 if ($limit > 0)
207 {
208
209 if ($page == 1 )
210 $offset = 0;
211 else
212 $offset = ($page-1) * $limit;
213
214 $sql .= " LIMIT $offset, $limit ";
215 }
216
217 $sql = $this->wpdb->prepare($sql,$escape);
218
219 $buttons = $this->wpdb->get_results($sql, ARRAY_A);
220
221 return $buttons;
222
223 }
224
225 function getButtonCount($args = array())
226 {
227 $defaults = array(
228 "status" => "publish",
229
230 );
231 $args = wp_parse_args($args, $defaults);
232
233 $sql = "SELECT count(id) FROM " . maxUtils::get_table_name() . " WHERE status = '%s'";
234 $sql = $this->wpdb->prepare($sql, $args["status"] );
235 $result = $this->wpdb->get_var($sql);
236 return $result;
237
238 }
239
240 function getButtonPages($args = array())
241 {
242 $defaults = array(
243 "limit" => 20,
244 "paged" => 1,
245 "status" => "publish",
246 "output" => "list", // not used, future arg.
247 "view" => "all",
248
249 );
250
251 $args = wp_parse_args($args, $defaults);
252
253 $limit = intval($args["limit"]);
254 $page = intval($args["paged"]);
255 $view = $args["view"];
256
257 $total = $this->getButtonCount(array("status" => $args["status"]));
258
259 $num_pages = ceil($total / $limit);
260
261 if ($num_pages == 0) $num_pages = 1; // lowest limit, page 1
262 $output = '';
263 $url = $_SERVER['REQUEST_URI'];
264
265 $url = remove_query_arg("view", $url);
266 $url = add_query_arg("view", $view, $url);
267
268 $first_url = ($page != 1 ) ? add_query_arg("paged", 1, $url) : false;
269 $last_url = ($page != $num_pages) ? add_query_arg("paged", $num_pages, $url) : false;
270 $next_url = ($page != $num_pages) ? add_query_arg("paged", ($page + 1), $url) : false;
271 $next_page = ($page != $num_pages) ? ($page + 1) : false;
272 $prev_page = ($page != 1) ? ($page -1 ) : false;
273 $prev_url = ($page != 1 ) ? add_query_arg("paged", ($page -1), $url) : false;
274
275
276 $return = array(
277 "first" => 1,
278 "base" => remove_query_arg("paged",$url),
279 "first_url" => esc_url($first_url),
280 "last" => $num_pages,
281 "last_url" => esc_url($last_url),
282 "next_url" => esc_url($next_url),
283 "prev_url" => esc_url($prev_url),
284 "prev_page" => $prev_page,
285 "next_page" => $next_page,
286 "total" => $total,
287 "current" => $page,
288
289
290
291 );
292
293 return $return;
294 }
295
296 static public function getAjaxButtons($post)
297 {
298
299 $admin = self::getInstance();
300 $args = array();
301
302 $paged = (isset($post["page"])) ? intval($post["page"]) : 1;
303 if ($paged > 0)
304 $args["paged" ] = $paged;
305
306 $button = MB()->getClass('button');
307 $buttons = $admin->getButtons($args);
308
309 ob_start();
310 echo "<div class='ajax-content'>";
311
312 // echo '<div class="tablenav top"> ';
313 echo "<span class='hint'>" . __('Click on a button to select it and add the shortcode to the editor', 'maxbuttons') . "</span>";
314 do_action('mb-display-pagination', $args, 'top');
315 echo '<span class="loading"></span>';
316 // echo '</div>';
317
318
319 if (count($buttons) == 0)
320 {
321
322 $url = admin_url('admin.php?page=maxbuttons-controller&action=edit');
323 echo "<h3 class='nobuttons-header'>" . __("You didn't create any buttons yet!","maxbuttons") . "</h3>";
324 echo "<p class='nobuttons-link'>" . sprintf(__("Click %shere%s to add one", "maxbuttons"),
325 "<a href='$url' target='_blank'>", "</a>") . "</strong></p>";
326
327 }
328
329 foreach($buttons as $b)
330 {
331
332 $button_id = $b["id"];
333 $button->set($button_id);
334 echo "<div class='button-list button-select' data-button='$button_id'>";
335 echo "<span class='button-id'> ";
336
337 echo "<span class='small'>[ID: $button_id ]</span>
338 </span> ";
339
340 echo "<span class='button-preview'><div class='shortcode-container'>";
341 $button->display(array("mode" => "preview", "load_css" => "inline" ));
342 echo "</div></span>";
343 echo "<span class='button-name'>" . $button->getName() . "</span>";
344 echo "</div>";
345 }
346 //echo '<div class="tablenav bottom"> ';
347 do_action('mb-display-pagination', $args, 'bottom');
348 echo '<span class="loading"></span>';
349 //echo '</div>';
350
351
352 echo "</div>";
353 echo "<p style='height:80px;'>&nbsp;</p>";
354
355 $output = ob_get_contents();
356 ob_end_clean();
357
358 $result = array('output' => $output,
359 'action' => 'buttons_load');
360
361 echo json_encode($result);
362
363 exit();
364
365 }
366
367
368 static public function mediaShortcodeOptions($post)
369 {
370 $button_id = isset($post['button_id']) ? intval($post['button_id']) : false;
371
372 $result = array('output' => '',
373 'action' => 'shortcode_options');
374
375 $button = MB()->getClass("button");
376 $button->set($button_id);
377
378 //$data = $button->getd
379
380 $data = array(
381 'url' => maxBlocks::getValue('url'),
382 'text' => maxBlocks::getValue('text'),
383 'new_window' => maxBlocks::getValue('new_window'),
384 'nofollow' => maxBlocks::getValue('nofollow'),
385 'link_title' => maxBlocks::getValue('link_title'),
386 );
387
388 // hook spec. qtrans.
389 $mode = 'preview';
390 $preview = true;
391 $compile = false;
392 $data = apply_filters('mb/button/data_before_display', $data, $mode, array('preview' => $preview, 'compile' => $compile) );
393
394 $admin = self::getInstance();
395 $shortcode_data = array(); // data array to build shortcodes and check default values
396
397 $display_args = array('echo'=> false, 'load_css' => 'inline');
398 $preview = new maxField('generic');
399 $preview->name = 'button-preview';
400 $preview->content = '<h3>' . __('Shortcode Options') . '</h3>
401 <p>' . $button->display($display_args) . '</p>
402 <p>' . __('Change the options to add shortcode attributes. ', 'maxbuttons') . '</p>';
403
404 $admin->addField($preview, 'start','end');
405
406 $url = new maxField('text');
407 $url->id = 'shortcode_url';
408 $url->name = $url->id;
409 $url->placeholder = 'http://';
410 $url->label = __('Button URL', 'maxbuttons');
411 $url->value = $data['url'];
412 $shortcode_data[] = array(
413 'name' => $url->name,
414 'original' => $url->value,
415 'shortcode' => 'url',
416 );
417
418 $admin->addField($url, 'start','end');
419
420 $text = new maxField('text');
421 $text->id = 'shortcode_text';
422 $text->name = $text->id;
423 $text->label = __('Button Text', 'maxbuttons');
424 $text->value = $data['text'];
425 $shortcode_data[] = array(
426 'name' => $text->name,
427 'original' => $text->value,
428 'shortcode' => 'text',
429 );
430
431 $admin->addField($text, 'start', 'end');
432
433 $more = new maxField('generic');
434 $more->name = 'more';
435 $more->content = '<div class="more-options"><a href="#">' . __('More Options', 'maxbuttons') . '</a></div>';
436
437 $admin->addField($more, 'start', 'end');
438
439 $new_window = new maxField('checkbox');
440 $new_window->id = 'shortcode_window';
441 $new_window->name = $new_window->id;
442 $new_window->label = __('Open in New Window', 'maxbuttons');
443 $new_window->value = 1;
444 $new_window->main_class = 'option more-field';
445 $new_window->checked = checked( $data['new_window'], 1, false);
446 $shortcode_data[] = array(
447 'name' => $new_window->name,
448 'original' => ( $data['new_window'] == 1) ? true : false,
449 'shortcode' => 'window',
450 'checked' => 'new',
451 'unchecked' => 'same',
452 );
453
454 $admin->addField($new_window, 'start', 'end');
455
456 $ffollow = new maxField('checkbox');
457 $ffollow->label = __('Use rel="nofollow"', 'maxbuttons');
458 $ffollow->value = 1;
459 $ffollow->name = 'shortcode_nofollow';
460 $ffollow->id = $ffollow->name;
461 $ffollow->main_class = 'option more-field';
462 $ffollow->checked = checked( $data['nofollow'] , 1, false);
463 $shortcode_data[] = array(
464 'name' => $ffollow->name,
465 'original' => ( $data['nofollow'] == 1) ? true : false,
466 'shortcode' => 'nofollow',
467 'checked' => 'true',
468 'unchecked' => 'false',
469 );
470
471
472 $admin->addField($ffollow, 'start','end');
473
474 $field_title = new maxField('text');
475 $field_title->label = __('Button Tooltip', 'maxbuttons');
476 $field_title->name = 'shortcode_link_title'; // title is too generic
477 $field_title->id = $field_title->name;
478 $field_title->main_class = 'option more-field';
479 $field_title->value = $data['link_title'];
480
481 $shortcode_data[] = array(
482 'name' => $field_title->name,
483 'original' => $field_title->value,
484 'shortcode' => 'linktitle',
485 );
486
487 $admin->addField($field_title, 'start','end');
488
489 $class = new maxField();
490 $class->id = 'shortcode_extra_classes';
491 $class->name = $class->id;
492 $class->label = __("Extra classes","maxbuttons");
493 $class->value = '';
494 $class->main_class = 'option more-field';
495
496 $shortcode_data[] = array(
497 'name' => $class->name,
498 'original' => $class->value,
499 'shortcode' => 'extraclass',
500 );
501
502 $admin->addField($class, 'start', 'end');
503
504 $shortcode_data = apply_filters('mb/media/shortcode_data', $shortcode_data);
505
506 $result['output'] = '<div class="ajax-content shortcode-options">' . $admin->display_fields(true,true) . '</div>';
507 $result['shortcodeData'] = $shortcode_data;
508 $result['button_id'] = $button_id;
509
510 echo json_encode($result);
511
512 exit();
513
514 }
515
516
517 function get_header($args =array() )
518 {
519 $defaults = array(
520 "tabs_active" => false,
521 "title" => "",
522 "action" => "",
523 );
524
525 $args = wp_parse_args($args, $defaults);
526 extract($args);
527
528 include_once(MB()->get_plugin_path() . "includes/admin_header.php");
529
530 }
531
532
533 function get_footer()
534 {
535 include_once(MB()->get_plugin_path() . "includes/admin_footer.php");
536
537 }
538
539 // unified (future way to end ajax requests + feedback
540 function endAjaxRequest($args = array())
541 {
542 $defaults = array(
543 "error" => true, // can have errors and still result true on success
544 "result" => true,
545 "body" => "",
546 "title" => "",
547 "data" => array(),
548 );
549
550 $args = wp_parse_args($args, $defaults);
551
552 echo json_encode($args);
553 die();
554
555
556 }
557
558 function log($action, $message)
559 {
560 if (! defined('MAXBUTTONS_DEBUG') || ! MAXBUTTONS_DEBUG)
561 return;
562
563 $stack = debug_backtrace();
564 $caller = $stack[1]['function'];
565
566 $dir = MB()->get_plugin_path() . "logs";
567 if (! is_dir($dir))
568 @mkdir($dir, 0777, true); // silently fail here.
569
570 if (! is_dir($dir))
571 return false;
572
573 $file = fopen( trailingslashit($dir) . "/maxbuttons.log", "a+");
574 $now = new \DateTime();
575 $now_format = $now->format("d/M/Y H:i:s");
576
577 $write_string = "[" . $now_format . "] $action - $message ( $caller )";
578 fwrite($file, $write_string);
579 fclose($file);
580
581
582 }
583 }
584