-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathclass-wpwsl-list-table.php
executable file
·172 lines (151 loc) · 4.73 KB
/
class-wpwsl-list-table.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/* -------------------------------------------- *
* Class Definition *
* -------------------------------------------- */
//This part is our very own WP_List_Table
class WPWSL_List_Table extends WP_List_Table
{
private $rawData = [];
private $found_data = [];
public function __construct($data)
{
global $status, $page;
$this->rawData = $data;
parent::__construct([
'singular' => 'tpl', //singular name of the listed records
'plural' => 'tpls', //plural name of the listed records
'ajax' => false //does this table support ajax?
]);
}
public function no_items()
{
_e('No Item found.', 'WPWSL');
}
public function column_default($item, $column_name)
{
switch ($column_name)
{
case 'title':
case 'type':
case 'date':
case 'trigger_by':
return $item[$column_name];
default:
return print_r($item, true); //Show the whole array for troubleshooting purposes
}
}
public function get_sortable_columns()
{
$sortable_columns = [
'title' => [
'title',
false],
'type' => [
'type',
false],
'date' => [
'date',
false],
'trigger_by' => [
'trigger_by',
false]
];
return $sortable_columns;
}
public function get_columns()
{
$columns = [
'cb' => '<input type="checkbox" />',
'title' => __('Title', 'WPWSL'),
'type' => __('Type', 'WPWSL'),
'date' => __('Date', 'WPWSL'),
'trigger_by' => __('Trigger by', 'WPWSL'),
];
return $columns;
}
public function usort_reorder($a, $b)
{
// If no sort, default to title
$orderby = (!empty($_GET['orderby']) ) ? $_GET['orderby'] : 'ID';
// If no order, default to asc
$order = (!empty($_GET['order']) ) ? $_GET['order'] : 'desc';
// Determine sort order
$result = strcmp($a[$orderby], $b[$orderby]);
// Send final sort direction to usort
return ( $order === 'asc' ) ? $result : -$result;
}
public function column_title($item)
{
$actions = [
'edit' => sprintf('<a href="' . menu_page_url(WPWSL_GENERAL_PAGE, false) . '&edit=%s">' . __('Edit', 'WPWSL') . '</a>', $item['ID']),
'delete' => sprintf('<a href="' . menu_page_url(WPWSL_GENERAL_PAGE, false) . '&delete=%s">' . __('Delete', 'WPWSL') . '</a>', $item['ID']),
];
return sprintf('%1$s %2$s', $item['title'], $this->row_actions($actions));
}
public function get_bulk_actions()
{
$actions = [
'delete' => __('Delete', 'WPWSL')
];
return $actions;
}
// public function process_bulk_action() {
//
// if ( 'delete' === $this->current_action() ) {
// if(isset($_GET['tpl'])){
// foreach($_GET['tpl'] as $tpl){
// foreach($this->rawData as $key=>$dt){
// if($dt['ID']==$tpl){
// unset($this->rawData[$key]);
// }
// }
//
// }
// }
// }
// }
public function column_cb($item)
{
return sprintf(
'<input type="checkbox" name="tpl[]" value="%s" />', $item['ID']
);
}
public function extra_tablenav($which)
{
if ($which == "top") {
//The code that goes before the table is here
//echo 'top';
}
if ($which == "bottom") {
//The code that goes after the table is there
//echo 'bottom';
}
}
public function prepare_items()
{
//$this->process_bulk_action();
$columns = $this->get_columns();
$hidden = [];
$sortable = $this->get_sortable_columns();
$this->_column_headers = [
$columns,
$hidden,
$sortable];
usort($this->rawData, [
$this,
'usort_reorder']);
$per_page = 100;
$current_page = $this->get_pagenum();
$total_items = count($this->rawData);
// only ncessary because we have sample data
$current_page_idx = ( $current_page - 1 ) * $per_page;
$this->found_data = array_slice($this->rawData, $current_page_idx, $per_page);
$this->set_pagination_args([
'total_items' => $total_items,
//WE have to calculate the total number of items
'per_page' => $per_page
//WE have to determine how many items to show on a page
]);
$this->items = $this->found_data;
}
}