| 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); |
| 60 |
} |
| 61 |
|
| 62 |
public function display_fields($clean = true, $return = false) |
| 63 |
{ |
| 64 |
$fields = apply_filters('mb/display_fields', $this->fields); |
| 65 |
$output = ''; |
| 66 |
|
| 67 |
|
| 68 |
foreach($fields as $id => $item) |
| 69 |
{ |
| 70 |
$field = $item['field']; |
| 71 |
$output .= $field->output($item['start'], $item['end']); |
| 72 |
} |
| 73 |
|
| 74 |
// auto-update system |
| 75 |
$updatable = $this->defined_updatable; |
| 76 |
foreach($updatable as $index => $field) |
| 77 |
{ |
| 78 |
$updatable[$index] = '#' . $field; |
| 79 |
} |
| 80 |
|
| 81 |
$output .= ' <span class="updatables hidden">' . implode(',', $updatable) . '</span>'; |
| 82 |
|
| 83 |
if ($clean) |
| 84 |
{ |
| 85 |
$this->fields = array(); |
| 86 |
$this->defined_updatable = array(); |
| 87 |
} |
| 88 |
|
| 89 |
if (! $return) |
| 90 |
echo $output; |
| 91 |
else |
| 92 |
return $output; |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
/* Get multiple buttons |
| 97 |
|
| 98 |
Used for overview pages, retrieve buttons on basis of passed arguments. |
| 99 |
|
| 100 |
@return array Array of found buttons with argument |
| 101 |
*/ |
| 102 |
|
| 103 |
function getButtons($args = array()) |
| 104 |
{ |
| 105 |
$defaults = array( |
| 106 |
"status" => "publish", |
| 107 |
"orderby" => "id", |
| 108 |
"order" => "DESC", |
| 109 |
"limit" => 20, |
| 110 |
"paged" => 1, |
| 111 |
); |
| 112 |
$args = wp_parse_args($args, $defaults); |
| 113 |
|
| 114 |
$limit = intval($args["limit"]); |
| 115 |
$page = intval($args["paged"]); |
| 116 |
$escape = array(); |
| 117 |
$escape[] = $args["status"]; |
| 118 |
|
| 119 |
// 'white-list' escaping |
| 120 |
switch ($args["orderby"]) |
| 121 |
{ |
| 122 |
case "id"; |
| 123 |
$orderby = "id"; |
| 124 |
break; |
| 125 |
case "name": |
| 126 |
default: |
| 127 |
$orderby = "name"; |
| 128 |
break; |
| 129 |
|
| 130 |
} |
| 131 |
|
| 132 |
switch($args["order"]) |
| 133 |
{ |
| 134 |
case "DESC": |
| 135 |
case "desc": |
| 136 |
$order = "DESC"; |
| 137 |
break; |
| 138 |
case "ASC": |
| 139 |
case "asc": |
| 140 |
default: |
| 141 |
$order = "ASC"; |
| 142 |
break; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
$sql = "SELECT id FROM " . maxUtils::get_table_name() . " WHERE status = '%s'"; |
| 147 |
if ($args["orderby"] != '') |
| 148 |
{ |
| 149 |
$sql .= " ORDER BY $orderby $order"; |
| 150 |
|
| 151 |
} |
| 152 |
|
| 153 |
if ($limit > 0) |
| 154 |
{ |
| 155 |
|
| 156 |
if ($page == 1 ) |
| 157 |
$offset = 0; |
| 158 |
else |
| 159 |
$offset = ($page-1) * $limit; |
| 160 |
|
| 161 |
$sql .= " LIMIT $offset, $limit "; |
| 162 |
} |
| 163 |
|
| 164 |
$sql = $this->wpdb->prepare($sql,$escape); |
| 165 |
|
| 166 |
$buttons = $this->wpdb->get_results($sql, ARRAY_A); |
| 167 |
|
| 168 |
|
| 169 |
return $buttons; |
| 170 |
|
| 171 |
} |
| 172 |
|
| 173 |
function getButtonCount($args = array()) |
| 174 |
{ |
| 175 |
$defaults = array( |
| 176 |
"status" => "publish", |
| 177 |
|
| 178 |
); |
| 179 |
$args = wp_parse_args($args, $defaults); |
| 180 |
|
| 181 |
$sql = "SELECT count(id) FROM " . maxUtils::get_table_name() . " WHERE status = '%s'"; |
| 182 |
$sql = $this->wpdb->prepare($sql, $args["status"] ); |
| 183 |
$result = $this->wpdb->get_var($sql); |
| 184 |
return $result; |
| 185 |
|
| 186 |
} |
| 187 |
|
| 188 |
function getButtonPages($args = array()) |
| 189 |
{ |
| 190 |
$defaults = array( |
| 191 |
"limit" => 20, |
| 192 |
"paged" => 1, |
| 193 |
"status" => "publish", |
| 194 |
"output" => "list", // not used, future arg. |
| 195 |
"view" => "all", |
| 196 |
|
| 197 |
); |
| 198 |
|
| 199 |
$args = wp_parse_args($args, $defaults); |
| 200 |
|
| 201 |
$limit = intval($args["limit"]); |
| 202 |
$page = intval($args["paged"]); |
| 203 |
$view = $args["view"]; |
| 204 |
|
| 205 |
$total = $this->getButtonCount(array("status" => $args["status"])); |
| 206 |
|
| 207 |
$num_pages = ceil($total / $limit); |
| 208 |
|
| 209 |
if ($num_pages == 0) $num_pages = 1; // lowest limit, page 1 |
| 210 |
$output = ''; |
| 211 |
$url = $_SERVER['REQUEST_URI']; |
| 212 |
|
| 213 |
$url = remove_query_arg("view", $url); |
| 214 |
$url = add_query_arg("view", $view, $url); |
| 215 |
|
| 216 |
$first_url = ($page != 1 ) ? add_query_arg("paged", 1, $url) : false; |
| 217 |
$last_url = ($page != $num_pages) ? add_query_arg("paged", $num_pages, $url) : false; |
| 218 |
$next_url = ($page != $num_pages) ? add_query_arg("paged", ($page + 1), $url) : false; |
| 219 |
$next_page = ($page != $num_pages) ? ($page + 1) : false; |
| 220 |
$prev_page = ($page != 1) ? ($page -1 ) : false; |
| 221 |
$prev_url = ($page != 1 ) ? add_query_arg("paged", ($page -1), $url) : false; |
| 222 |
|
| 223 |
|
| 224 |
$return = array( |
| 225 |
"first" => 1, |
| 226 |
"base" => remove_query_arg("paged",$url), |
| 227 |
"first_url" => esc_url($first_url), |
| 228 |
"last" => $num_pages, |
| 229 |
"last_url" => esc_url($last_url), |
| 230 |
"next_url" => esc_url($next_url), |
| 231 |
"prev_url" => esc_url($prev_url), |
| 232 |
"prev_page" => $prev_page, |
| 233 |
"next_page" => $next_page, |
| 234 |
"total" => $total, |
| 235 |
"current" => $page, |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
); |
| 240 |
|
| 241 |
return $return; |
| 242 |
} |
| 243 |
|
| 244 |
static function getAjaxButtons() |
| 245 |
{ |
| 246 |
|
| 247 |
$admin = self::getInstance(); |
| 248 |
$args = array(); |
| 249 |
|
| 250 |
$paged = (isset($_REQUEST["paged"])) ? intval($_REQUEST["paged"]) : 1; |
| 251 |
if ($paged > 0) |
| 252 |
$args["paged" ] = $paged; |
| 253 |
|
| 254 |
|
| 255 |
$button = MB()->getClass('button'); |
| 256 |
$buttons = $admin->getButtons($args); |
| 257 |
|
| 258 |
echo "<div id='maxbuttons'><div class='preview-buttons'>"; |
| 259 |
|
| 260 |
echo '<div class="tablenav top"> '; |
| 261 |
echo "<span class='hint'>" . __('Click on a button to select it and add the shortcode to the editor', 'maxbuttons') . "</span>"; |
| 262 |
do_action('mb-display-pagination', $args); |
| 263 |
echo '<span class="loading"></span>'; |
| 264 |
echo '</div>'; |
| 265 |
|
| 266 |
|
| 267 |
if (count($buttons) == 0) |
| 268 |
{ |
| 269 |
|
| 270 |
$url = admin_url('admin.php?page=maxbuttons-controller&action=edit'); |
| 271 |
echo "<p><strong>" . __("You didn't add any buttons yet!","maxbuttons") . "</strong></p>"; |
| 272 |
echo "<P>" . sprintf(__("Click %shere%s to add one", "maxbuttons"), |
| 273 |
"<a href='$url' target='_blank'>", "</a>") . "</strong></p>"; |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
foreach($buttons as $b) |
| 278 |
{ |
| 279 |
|
| 280 |
$button_id = $b["id"]; |
| 281 |
$button->set($button_id); |
| 282 |
echo "<div class='button-row button-select' data-button='$button_id'>"; |
| 283 |
echo "<span class='col col_insert'> "; |
| 284 |
|
| 285 |
echo "<span class='small'>[ID: $button_id ]</span> |
| 286 |
</span> "; |
| 287 |
|
| 288 |
echo "<span class='col col_button'><div class='shortcode-container'>"; |
| 289 |
$button->display(array("mode" => "preview", "load_css" => "inline" )); |
| 290 |
echo "</div></span>"; |
| 291 |
echo "<span class='col col_name'>" . $button->getName() . "</span>"; |
| 292 |
echo "</div>"; |
| 293 |
} |
| 294 |
echo '<div class="tablenav bottom"> '; |
| 295 |
do_action('mb-display-pagination', $args); |
| 296 |
echo '<span class="loading"></span>'; |
| 297 |
echo '</div>'; |
| 298 |
|
| 299 |
|
| 300 |
echo "</div></div>"; |
| 301 |
echo "<p> </p>"; |
| 302 |
|
| 303 |
exit(); |
| 304 |
|
| 305 |
} |
| 306 |
function get_header($args =array() ) |
| 307 |
{ |
| 308 |
$defaults = array( |
| 309 |
"tabs_active" => false, |
| 310 |
"title" => "", |
| 311 |
"action" => "", |
| 312 |
); |
| 313 |
|
| 314 |
$args = wp_parse_args($args, $defaults); |
| 315 |
extract($args); |
| 316 |
|
| 317 |
include_once(MB()->get_plugin_path() . "includes/admin_header.php"); |
| 318 |
|
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
function get_footer() |
| 323 |
{ |
| 324 |
include_once(MB()->get_plugin_path() . "includes/admin_footer.php"); |
| 325 |
|
| 326 |
} |
| 327 |
|
| 328 |
// unified (future way to end ajax requests + feedback |
| 329 |
function endAjaxRequest($args = array()) |
| 330 |
{ |
| 331 |
$defaults = array( |
| 332 |
"error" => true, // can have errors and still result true on success |
| 333 |
"result" => true, |
| 334 |
"body" => "", |
| 335 |
"title" => "", |
| 336 |
"data" => array(), |
| 337 |
); |
| 338 |
|
| 339 |
$args = wp_parse_args($args, $defaults); |
| 340 |
|
| 341 |
echo json_encode($args); |
| 342 |
die(); |
| 343 |
|
| 344 |
|
| 345 |
} |
| 346 |
|
| 347 |
function log($action, $message) |
| 348 |
{ |
| 349 |
if (! defined('MAXBUTTONS_DEBUG') || ! MAXBUTTONS_DEBUG) |
| 350 |
return; |
| 351 |
|
| 352 |
$stack = debug_backtrace(); |
| 353 |
$caller = $stack[1]['function']; |
| 354 |
|
| 355 |
$dir = MB()->get_plugin_path() . "logs"; |
| 356 |
if (! is_dir($dir)) |
| 357 |
@mkdir($dir, 0777, true); // silently fail here. |
| 358 |
|
| 359 |
if (! is_dir($dir)) |
| 360 |
return false; |
| 361 |
|
| 362 |
$file = fopen( trailingslashit($dir) . "/maxbuttons.log", "a+"); |
| 363 |
$now = new DateTime() ; |
| 364 |
$now_format = $now->format("d/M/Y H:i:s"); |
| 365 |
|
| 366 |
$write_string = "[" . $now_format . "] $action - $message ( $caller )"; |
| 367 |
fwrite($file, $write_string); |
| 368 |
fclose($file); |
| 369 |
|
| 370 |
|
| 371 |
} |
| 372 |
} |
| 373 |
|