| 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 |
|
| 205 |
echo '<div class="tablenav top"> '; |
| 206 |
echo "<span class='hint'>" . __('Click on a button to select it and add the shortcode to the editor', 'maxbuttons') . "</span>"; |
| 207 |
do_action('mb-display-pagination', $args); |
| 208 |
echo '<span class="loading"></span>'; |
| 209 |
echo '</div>'; |
| 210 |
|
| 211 |
|
| 212 |
if (count($buttons) == 0) |
| 213 |
{ |
| 214 |
|
| 215 |
$url = admin_url('admin.php?page=maxbuttons-controller&action=edit'); |
| 216 |
echo "<p><strong>" . __("You didn't add any buttons yet!","maxbuttons") . "</strong></p>"; |
| 217 |
echo "<P>" . sprintf(__("Click %shere%s to add one", "maxbuttons"), |
| 218 |
"<a href='$url' target='_blank'>", "</a>") . "</strong></p>"; |
| 219 |
|
| 220 |
} |
| 221 |
|
| 222 |
foreach($buttons as $b) |
| 223 |
{ |
| 224 |
|
| 225 |
$button_id = $b["id"]; |
| 226 |
$button->set($button_id); |
| 227 |
echo "<div class='button-row button-select' data-button='$button_id'>"; |
| 228 |
echo "<span class='col col_insert'> "; |
| 229 |
|
| 230 |
echo "<span class='small'>[ID: $button_id ]</span> |
| 231 |
</span> "; |
| 232 |
|
| 233 |
echo "<span class='col col_button'><div class='shortcode-container'>"; |
| 234 |
$button->display(array("mode" => "preview", "load_css" => "inline" )); |
| 235 |
echo "</div></span>"; |
| 236 |
echo "<span class='col col_name'>" . $button->getName() . "</span>"; |
| 237 |
echo "</div>"; |
| 238 |
} |
| 239 |
echo '<div class="tablenav bottom"> '; |
| 240 |
do_action('mb-display-pagination', $args); |
| 241 |
echo '<span class="loading"></span>'; |
| 242 |
echo '</div>'; |
| 243 |
|
| 244 |
|
| 245 |
echo "</div></div>"; |
| 246 |
echo "<p> </p>"; |
| 247 |
|
| 248 |
exit(); |
| 249 |
|
| 250 |
} |
| 251 |
function get_header($args =array() ) |
| 252 |
{ |
| 253 |
$defaults = array( |
| 254 |
"tabs_active" => false, |
| 255 |
"title" => "", |
| 256 |
"action" => "", |
| 257 |
); |
| 258 |
|
| 259 |
$args = wp_parse_args($args, $defaults); |
| 260 |
extract($args); |
| 261 |
|
| 262 |
include_once(MB()->get_plugin_path() . "includes/admin_header.php"); |
| 263 |
|
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
function get_footer() |
| 268 |
{ |
| 269 |
include_once(MB()->get_plugin_path() . "includes/admin_footer.php"); |
| 270 |
|
| 271 |
} |
| 272 |
|
| 273 |
// unified (future way to end ajax requests + feedback |
| 274 |
function endAjaxRequest($args = array()) |
| 275 |
{ |
| 276 |
$defaults = array( |
| 277 |
"error" => true, // can have errors and still result true on success |
| 278 |
"result" => true, |
| 279 |
"body" => "", |
| 280 |
"title" => "", |
| 281 |
"data" => array(), |
| 282 |
); |
| 283 |
|
| 284 |
$args = wp_parse_args($args, $defaults); |
| 285 |
|
| 286 |
echo json_encode($args); |
| 287 |
die(); |
| 288 |
|
| 289 |
|
| 290 |
} |
| 291 |
|
| 292 |
function log($action, $message) |
| 293 |
{ |
| 294 |
if (! defined('MAXBUTTONS_DEBUG') || ! MAXBUTTONS_DEBUG) |
| 295 |
return; |
| 296 |
|
| 297 |
$stack = debug_backtrace(); |
| 298 |
$caller = $stack[1]['function']; |
| 299 |
|
| 300 |
$dir = MB()->get_plugin_path() . "logs"; |
| 301 |
if (! is_dir($dir)) |
| 302 |
@mkdir($dir, 0777, true); // silently fail here. |
| 303 |
|
| 304 |
if (! is_dir($dir)) |
| 305 |
return false; |
| 306 |
|
| 307 |
$file = fopen($dir . "/maxbuttons.log", "a+"); |
| 308 |
$now = new DateTime() ; |
| 309 |
$now_format = $now->format("d/M/Y H:i:s"); |
| 310 |
|
| 311 |
$write_string = "[" . $now_format . "] $action - $message ( $caller )"; |
| 312 |
fwrite($file, $write_string); |
| 313 |
fclose($file); |
| 314 |
|
| 315 |
|
| 316 |
} |
| 317 |
} |
| 318 |
|