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

586 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
207 if ($limit > 0)
208 {
209
210 if ($page == 1 )
211 $offset = 0;
212 else
213 $offset = ($page-1) * $limit;
214
215 $sql .= " LIMIT $offset, $limit ";
216 }
217
218 $sql = $this->wpdb->prepare($sql,$escape);
219
220 $buttons = $this->wpdb->get_results($sql, ARRAY_A);
221
222
223 return $buttons;
224
225 }
226
227 function getButtonCount($args = array())
228 {
229 $defaults = array(
230 "status" => "publish",
231
232 );
233 $args = wp_parse_args($args, $defaults);
234
235 $sql = "SELECT count(id) FROM " . maxUtils::get_table_name() . " WHERE status = '%s'";
236 $sql = $this->wpdb->prepare($sql, $args["status"] );
237 $result = $this->wpdb->get_var($sql);
238 return $result;
239
240 }
241
242 function getButtonPages($args = array())
243 {
244 $defaults = array(
245 "limit" => 20,
246 "paged" => 1,
247 "status" => "publish",
248 "output" => "list", // not used, future arg.
249 "view" => "all",
250
251 );
252
253 $args = wp_parse_args($args, $defaults);
254
255 $limit = intval($args["limit"]);
256 $page = intval($args["paged"]);
257 $view = $args["view"];
258
259 $total = $this->getButtonCount(array("status" => $args["status"]));
260
261 $num_pages = ceil($total / $limit);
262
263 if ($num_pages == 0) $num_pages = 1; // lowest limit, page 1
264 $output = '';
265 $url = $_SERVER['REQUEST_URI'];
266
267 $url = remove_query_arg("view", $url);
268 $url = add_query_arg("view", $view, $url);
269
270 $first_url = ($page != 1 ) ? add_query_arg("paged", 1, $url) : false;
271 $last_url = ($page != $num_pages) ? add_query_arg("paged", $num_pages, $url) : false;
272 $next_url = ($page != $num_pages) ? add_query_arg("paged", ($page + 1), $url) : false;
273 $next_page = ($page != $num_pages) ? ($page + 1) : false;
274 $prev_page = ($page != 1) ? ($page -1 ) : false;
275 $prev_url = ($page != 1 ) ? add_query_arg("paged", ($page -1), $url) : false;
276
277
278 $return = array(
279 "first" => 1,
280 "base" => remove_query_arg("paged",$url),
281 "first_url" => esc_url($first_url),
282 "last" => $num_pages,
283 "last_url" => esc_url($last_url),
284 "next_url" => esc_url($next_url),
285 "prev_url" => esc_url($prev_url),
286 "prev_page" => $prev_page,
287 "next_page" => $next_page,
288 "total" => $total,
289 "current" => $page,
290
291
292
293 );
294
295 return $return;
296 }
297
298 static public function getAjaxButtons($post)
299 {
300
301 $admin = self::getInstance();
302 $args = array();
303
304 $paged = (isset($post["page"])) ? intval($post["page"]) : 1;
305 if ($paged > 0)
306 $args["paged" ] = $paged;
307
308 $button = MB()->getClass('button');
309 $buttons = $admin->getButtons($args);
310
311 ob_start();
312 echo "<div class='ajax-content'>";
313
314 // echo '<div class="tablenav top"> ';
315 echo "<span class='hint'>" . __('Click on a button to select it and add the shortcode to the editor', 'maxbuttons') . "</span>";
316 do_action('mb-display-pagination', $args, 'top');
317 echo '<span class="loading"></span>';
318 // echo '</div>';
319
320
321 if (count($buttons) == 0)
322 {
323
324 $url = admin_url('admin.php?page=maxbuttons-controller&action=edit');
325 echo "<h3 class='nobuttons-header'>" . __("You didn't create any buttons yet!","maxbuttons") . "</h3>";
326 echo "<p class='nobuttons-link'>" . sprintf(__("Click %shere%s to add one", "maxbuttons"),
327 "<a href='$url' target='_blank'>", "</a>") . "</strong></p>";
328
329 }
330
331 foreach($buttons as $b)
332 {
333
334 $button_id = $b["id"];
335 $button->set($button_id);
336 echo "<div class='button-list button-select' data-button='$button_id'>";
337 echo "<span class='button-id'> ";
338
339 echo "<span class='small'>[ID: $button_id ]</span>
340 </span> ";
341
342 echo "<span class='button-preview'><div class='shortcode-container'>";
343 $button->display(array("mode" => "preview", "load_css" => "inline" ));
344 echo "</div></span>";
345 echo "<span class='button-name'>" . $button->getName() . "</span>";
346 echo "</div>";
347 }
348 //echo '<div class="tablenav bottom"> ';
349 do_action('mb-display-pagination', $args, 'bottom');
350 echo '<span class="loading"></span>';
351 //echo '</div>';
352
353
354 echo "</div>";
355 echo "<p style='height:80px;'>&nbsp;</p>";
356
357 $output = ob_get_contents();
358 ob_end_clean();
359
360 $result = array('output' => $output,
361 'action' => 'buttons_load');
362
363 echo json_encode($result);
364
365 exit();
366
367 }
368
369
370 static public function mediaShortcodeOptions($post)
371 {
372 $button_id = isset($post['button_id']) ? intval($post['button_id']) : false;
373
374 $result = array('output' => '',
375 'action' => 'shortcode_options');
376
377 $button = MB()->getClass("button");
378 $button->set($button_id);
379
380 //$data = $button->getd
381
382 $data = array(
383 'url' => maxBlocks::getValue('url'),
384 'text' => maxBlocks::getValue('text'),
385 'new_window' => maxBlocks::getValue('new_window'),
386 'nofollow' => maxBlocks::getValue('nofollow'),
387 'link_title' => maxBlocks::getValue('link_title'),
388 );
389
390 // hook spec. qtrans.
391 $mode = 'preview';
392 $preview = true;
393 $compile = false;
394 $data = apply_filters('mb/button/data_before_display', $data, $mode, array('preview' => $preview, 'compile' => $compile) );
395
396 $admin = self::getInstance();
397 $shortcode_data = array(); // data array to build shortcodes and check default values
398
399 $display_args = array('echo'=> false, 'load_css' => 'inline');
400 $preview = new maxField('generic');
401 $preview->name = 'button-preview';
402 $preview->content = '<h3>' . __('Shortcode Options') . '</h3>
403 <p>' . $button->display($display_args) . '</p>
404 <p>' . __('Change the options to add shortcode attributes. ', 'maxbuttons') . '</p>';
405
406 $admin->addField($preview, 'start','end');
407
408 $url = new maxField('text');
409 $url->id = 'shortcode_url';
410 $url->name = $url->id;
411 $url->placeholder = 'http://';
412 $url->label = __('Button URL', 'maxbuttons');
413 $url->value = $data['url'];
414 $shortcode_data[] = array(
415 'name' => $url->name,
416 'original' => $url->value,
417 'shortcode' => 'url',
418 );
419
420 $admin->addField($url, 'start','end');
421
422 $text = new maxField('text');
423 $text->id = 'shortcode_text';
424 $text->name = $text->id;
425 $text->label = __('Button Text', 'maxbuttons');
426 $text->value = $data['text'];
427 $shortcode_data[] = array(
428 'name' => $text->name,
429 'original' => $text->value,
430 'shortcode' => 'text',
431 );
432
433 $admin->addField($text, 'start', 'end');
434
435 $more = new maxField('generic');
436 $more->name = 'more';
437 $more->content = '<div class="more-options"><a href="#">' . __('More Options', 'maxbuttons') . '</a></div>';
438
439 $admin->addField($more, 'start', 'end');
440
441 $new_window = new maxField('checkbox');
442 $new_window->id = 'shortcode_window';
443 $new_window->name = $new_window->id;
444 $new_window->label = __('Open in New Window', 'maxbuttons');
445 $new_window->value = 1;
446 $new_window->main_class = 'option more-field';
447 $new_window->checked = checked( $data['new_window'], 1, false);
448 $shortcode_data[] = array(
449 'name' => $new_window->name,
450 'original' => ( $data['new_window'] == 1) ? true : false,
451 'shortcode' => 'window',
452 'checked' => 'new',
453 'unchecked' => 'same',
454 );
455
456 $admin->addField($new_window, 'start', 'end');
457
458 $ffollow = new maxField('checkbox');
459 $ffollow->label = __('Use rel="nofollow"', 'maxbuttons');
460 $ffollow->value = 1;
461 $ffollow->name = 'shortcode_nofollow';
462 $ffollow->id = $ffollow->name;
463 $ffollow->main_class = 'option more-field';
464 $ffollow->checked = checked( $data['nofollow'] , 1, false);
465 $shortcode_data[] = array(
466 'name' => $ffollow->name,
467 'original' => ( $data['nofollow'] == 1) ? true : false,
468 'shortcode' => 'nofollow',
469 'checked' => 'true',
470 'unchecked' => 'false',
471 );
472
473
474 $admin->addField($ffollow, 'start','end');
475
476 $field_title = new maxField('text');
477 $field_title->label = __('Button Tooltip', 'maxbuttons');
478 $field_title->name = 'shortcode_link_title'; // title is too generic
479 $field_title->id = $field_title->name;
480 $field_title->main_class = 'option more-field';
481 $field_title->value = $data['link_title'];
482
483 $shortcode_data[] = array(
484 'name' => $field_title->name,
485 'original' => $field_title->value,
486 'shortcode' => 'linktitle',
487 );
488
489 $admin->addField($field_title, 'start','end');
490
491 $class = new maxField();
492 $class->id = 'shortcode_extra_classes';
493 $class->name = $class->id;
494 $class->label = __("Extra classes","maxbuttons");
495 $class->value = '';
496 $class->main_class = 'option more-field';
497
498 $shortcode_data[] = array(
499 'name' => $class->name,
500 'original' => $class->value,
501 'shortcode' => 'extraclass',
502 );
503
504 $admin->addField($class, 'start', 'end');
505
506 $shortcode_data = apply_filters('mb/media/shortcode_data', $shortcode_data);
507
508 $result['output'] = '<div class="ajax-content shortcode-options">' . $admin->display_fields(true,true) . '</div>';
509 $result['shortcodeData'] = $shortcode_data;
510 $result['button_id'] = $button_id;
511
512 echo json_encode($result);
513
514 exit();
515
516 }
517
518
519 function get_header($args =array() )
520 {
521 $defaults = array(
522 "tabs_active" => false,
523 "title" => "",
524 "action" => "",
525 );
526
527 $args = wp_parse_args($args, $defaults);
528 extract($args);
529
530 include_once(MB()->get_plugin_path() . "includes/admin_header.php");
531
532 }
533
534
535 function get_footer()
536 {
537 include_once(MB()->get_plugin_path() . "includes/admin_footer.php");
538
539 }
540
541 // unified (future way to end ajax requests + feedback
542 function endAjaxRequest($args = array())
543 {
544 $defaults = array(
545 "error" => true, // can have errors and still result true on success
546 "result" => true,
547 "body" => "",
548 "title" => "",
549 "data" => array(),
550 );
551
552 $args = wp_parse_args($args, $defaults);
553
554 echo json_encode($args);
555 die();
556
557
558 }
559
560 function log($action, $message)
561 {
562 if (! defined('MAXBUTTONS_DEBUG') || ! MAXBUTTONS_DEBUG)
563 return;
564
565 $stack = debug_backtrace();
566 $caller = $stack[1]['function'];
567
568 $dir = MB()->get_plugin_path() . "logs";
569 if (! is_dir($dir))
570 @mkdir($dir, 0777, true); // silently fail here.
571
572 if (! is_dir($dir))
573 return false;
574
575 $file = fopen( trailingslashit($dir) . "/maxbuttons.log", "a+");
576 $now = new \DateTime();
577 $now_format = $now->format("d/M/Y H:i:s");
578
579 $write_string = "[" . $now_format . "] $action - $message ( $caller )";
580 fwrite($file, $write_string);
581 fclose($file);
582
583
584 }
585 }
586