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