-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.php
145 lines (126 loc) · 3.35 KB
/
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
<?php
if (!class_exists('WP_List_Table')) {
require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
}
class Table extends WP_List_Table {
public $args;
/**
* Constructor
*/
public function __construct(array $args)
{
// Set the arguments
$this->args = $args;
// Construct the parent
parent::construct($this->args['options']);
}
/**
* Define table classes
*
* @return array
*/
public function get_table_classes()
{
return ['widefat', 'fixed', 'striped', $this->_args['plural']];
}
/**
* Set the table columns
*
* @return array
*/
public function get_columns()
{
return $this->args['columns'];
}
/**
* Define the sortable columns
*
* @return array
*/
public function get_sortable_columns()
{
return $this->args['sortable'];
}
/**
* Render the checkbox column
*
* @param object
* @return string
*/
public function column_cb($item)
{
$column = $this->args['columnCB'];
return sprintf('<input type="checkbox" name="' . $column . '[]" value="%d" />', $item->{$column});
}
/**
* Display the data in the table
*
* @param object $item
* @param string $name
* @return string
*/
public function column_default($item, $name)
{
// Loop through the specificed columns
foreach ($this->args['columnValues'] as $columnName => $columnValue) {
if ($name === $columnName) {
return $item->{$columnValue};
}
}
// switch ($name) {
// case 'ID':
// return $item->meta_id;
// case 'page':
// return $item->post->post_title;
// case 'type':
// return $item->post->post_type;
// default:
// return isset($item->$name ) ? $item->$name : '';
// }
}
/**
* Set the bulk actions
*
* @return array
*/
public function get_bulk_actions() {
return $this->args['bulk']['actions'];
}
/**
* Process bulk actions
*/
public function process_bulk_action()
{
// Only if we got an nonce
if (isset($_POST['_wpnonce']) && !empty($_POST['_wpnonce'])) {
$nonce = filter_input(INPUT_POST, '_wpnonce', FILTER_SANITIZE_STRING);
$action = 'bulk-' . $this->_args['plural'];
// Check nonce
if (!wp_verify_nonce( $nonce, $action)) {
wp_die('Nope! Security check failed!');
}
// Get the bulk action
$action = $this->args['bulk']['methods'][$_POST['action']];
// Call the method
$action[0]->destroy($_POST);
}
}
/**
* Prepare items for the table
*/
public function prepare_items()
{
// Set column headers
$this->_column_headers = [$this->args['columns'], $this->args['hidden'], $this->args['sortable']];
// Set table items
$this->items = $this->args['items'];
// Process bulk actions
$this->process_bulk_action();
// Set pagination arguments
$this->set_pagination_args(array(
'total_items' => (int) $this->args['totalItems'],
'total_pages' => (int) $this->args['totalPages'],
'per_page' => (int) $this->args['perPage'],
));
}
}