| 1 |
<?php |
| 2 |
namespace MaxButtons; |
| 3 |
|
| 4 |
class listController extends MaxController |
| 5 |
{ |
| 6 |
protected $view_template = 'maxbuttons-list'; |
| 7 |
|
| 8 |
protected $mbadmin; |
| 9 |
protected $button; |
| 10 |
|
| 11 |
public function __construct() |
| 12 |
{ |
| 13 |
$this->mbadmin = MB()->getClass('admin'); |
| 14 |
$this->button = MB()->getClass('button'); |
| 15 |
parent::__construct(); |
| 16 |
} |
| 17 |
|
| 18 |
// view Loader. |
| 19 |
public function view() |
| 20 |
{ |
| 21 |
|
| 22 |
if (isset($_POST) && isset($_POST["mb-list-nonce"]) ) { |
| 23 |
$this->handlePost(); |
| 24 |
} |
| 25 |
$this->loadView(); |
| 26 |
|
| 27 |
parent::view(); |
| 28 |
} |
| 29 |
|
| 30 |
|
| 31 |
public function loadView() |
| 32 |
{ |
| 33 |
|
| 34 |
if (! isset($this->view->listView)) // Can be set by handlePost |
| 35 |
$this->view->listView = (isset($_GET["view"])) ? sanitize_text_field($_GET["view"]) : "all"; |
| 36 |
|
| 37 |
$this->loadButtons(); |
| 38 |
$this->view->published_buttons_count = $this->mbadmin->getButtonCount(array()); |
| 39 |
$this->view->trashed_buttons_count = $this->mbadmin->getButtonCount(array("status" => "trash")); |
| 40 |
|
| 41 |
} |
| 42 |
|
| 43 |
protected function loadButtons() |
| 44 |
{ |
| 45 |
$args = array(); |
| 46 |
|
| 47 |
$args['orderby'] = isset($_GET["orderby"]) ? sanitize_text_field($_GET["orderby"]) : 'id'; |
| 48 |
$args['order'] = isset($_GET["order"]) ? sanitize_text_field($_GET["order"]) : 'DESC'; |
| 49 |
|
| 50 |
|
| 51 |
if (isset($_GET["paged"]) && $_GET["paged"] != '') |
| 52 |
{ |
| 53 |
$page = intval($_GET["paged"]); |
| 54 |
$args["paged"] = $page; |
| 55 |
} |
| 56 |
|
| 57 |
if ($this->view->listView == 'trash') |
| 58 |
$args["status"] = "trash"; |
| 59 |
|
| 60 |
$published_buttons = $this->mbadmin->getButtons($args); |
| 61 |
$this->view->published_buttons = $published_buttons; |
| 62 |
$this->view->pageArgs = $args; |
| 63 |
} |
| 64 |
|
| 65 |
protected function handlePost() |
| 66 |
{ |
| 67 |
$verify = wp_verify_nonce( $_POST['mb-list-nonce'], 'mb-list' ); |
| 68 |
if (! $verify ) |
| 69 |
{ |
| 70 |
$this->messages[] = __('Something went wrong with the form, nonce not verified', 'maxbuttons'); |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
$bulk_action = isset($_POST['bulk-action-select']) ? $_POST['bulk-action-select'] : false; |
| 75 |
$button_id = isset($_POST['button-id']) ? $_POST['button-id'] : false; |
| 76 |
|
| 77 |
if ($button_id && $bulk_action == 'trash') { |
| 78 |
$count = 0; |
| 79 |
foreach ($button_id as $id) { |
| 80 |
$id = intval($id); |
| 81 |
$this->button->set($id); |
| 82 |
$this->button->setStatus('trash'); |
| 83 |
$count++; |
| 84 |
} |
| 85 |
|
| 86 |
if ($count == 1) { |
| 87 |
$this->messages[] = __('Moved 1 button to the trash.', 'maxbuttons'); |
| 88 |
} |
| 89 |
|
| 90 |
if ($count > 1) { |
| 91 |
$this->messages[] = __('Moved ', 'maxbuttons') . $count . __(' buttons to the trash.', 'maxbuttons'); |
| 92 |
} |
| 93 |
} |
| 94 |
elseif ($button_id && $bulk_action == 'restore') { |
| 95 |
$count = 0; |
| 96 |
|
| 97 |
foreach ($button_id as $id) { |
| 98 |
$id = intval($id); |
| 99 |
$set = $this->button->set($id,'','trash'); |
| 100 |
$this->button->setStatus('publish'); |
| 101 |
|
| 102 |
//maxbuttons_button_restore($id); |
| 103 |
$count++; |
| 104 |
} |
| 105 |
|
| 106 |
if ($count == 1) { |
| 107 |
$this->messages[] = __('Restored 1 button.', 'maxbuttons'); |
| 108 |
} |
| 109 |
|
| 110 |
if ($count > 1) { |
| 111 |
$this->messages[] = __('Restored ', 'maxbuttons') . $count . __(' buttons.', 'maxbuttons'); |
| 112 |
} |
| 113 |
$this->view->listView = 'all'; // switch to normal list. |
| 114 |
} |
| 115 |
|
| 116 |
if ($button_id && $bulk_action == 'delete') { |
| 117 |
$count = 0; |
| 118 |
|
| 119 |
foreach ($button_id as $id) { |
| 120 |
$id = intval($id); |
| 121 |
$this->button->delete($id); |
| 122 |
$count++; |
| 123 |
} |
| 124 |
|
| 125 |
if ($count == 1) { |
| 126 |
$this->messages[] = __('Deleted 1 button.', 'maxbuttons'); |
| 127 |
} |
| 128 |
|
| 129 |
if ($count > 1) { |
| 130 |
$this->messages[] = __('Deleted ', 'maxbuttons') . $count . __(' buttons.', 'maxbuttons'); |
| 131 |
} |
| 132 |
} |
| 133 |
} // handlePost |
| 134 |
|
| 135 |
|
| 136 |
protected function handleMessages() |
| 137 |
{ |
| 138 |
if (isset($_GET['message']) && $_GET['message'] == '1') { |
| 139 |
$this->messages[] = __('Moved 1 button to the trash.', 'maxbuttons'); |
| 140 |
} |
| 141 |
|
| 142 |
if (isset($_GET['message']) && $_GET['message'] == '1restore') { |
| 143 |
$this->messages[] = __('Restored 1 button.', 'maxbuttons'); |
| 144 |
} |
| 145 |
|
| 146 |
if (isset($_GET['message']) && $_GET['message'] == '1delete') { |
| 147 |
$this->messages[] = __('Deleted 1 button.', 'maxbuttons'); |
| 148 |
} |
| 149 |
|
| 150 |
if (isset($_GET['message']) && $_GET['message'] == 'empty-trash') |
| 151 |
{ |
| 152 |
$this->messages[] = __('Emptied Trash', 'maxbuttons'); |
| 153 |
} |
| 154 |
|
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
} // class listController |
| 159 |
|