-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathclass-multi-value-field-table.php
266 lines (210 loc) · 5.21 KB
/
class-multi-value-field-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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
<?php
/**
* ConvertKit General Settings class
*
* @package ConvertKit
* @author ConvertKit
*/
/**
* Include WP_List_Table if not defined.
*/
if ( ! class_exists( 'WP_List_Table' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
}
/**
* Class Multi_Value_Field_Table
*/
class Multi_Value_Field_Table extends WP_List_Table {
/**
* Holds the supported bulk actions.
*
* @var array
*/
private $bulk_actions = array();
/**
* Holds the table columns.
*
* @var array
*/
private $columns = array();
/**
* Holds the sortable table columns.
*
* @var array
*/
private $sortable_columns = array();
/**
* Holds the table rows and their data.
*
* @var array
*/
private $data = array();
/**
* Constructor.
*
* @since 1.0.0
*/
public function __construct() {
parent::__construct(
array(
'singular' => 'item',
'plural' => 'items',
'ajax' => false,
)
);
}
/**
* Set default column attributes
*
* @since 1.0.0
*
* @param array $item A singular item (one full row's worth of data).
* @param string $column_name The name/slug of the column to be processed.
* @return string Text or HTML to be placed inside the column <td>
*/
public function column_default( $item, $column_name ) {
return $item[ $column_name ];
}
/**
* Provide a callback function to render the checkbox column
*
* @param array $item A row's worth of data.
* @return string The formatted string with a checkbox
*/
public function column_cb( $item ) {
return sprintf(
'<input type="checkbox" name="%1$s[]" value="%2$s" />',
$this->_args['singular'],
$item['id']
);
}
/**
* Get the bulk actions for this table
*
* @return array Bulk actions
*/
public function get_bulk_actions() {
return $this->bulk_actions;
}
/**
* Get a list of columns
*
* @return array
*/
public function get_columns() {
return $this->columns;
}
/**
* Add a column to the table
*
* @param string $key Machine-readable column name.
* @param string $title Title shown to the user.
* @param boolean $sortable Whether or not this is sortable (defaults false).
*/
public function add_column( $key, $title, $sortable = false ) {
$this->columns[ $key ] = $title;
if ( $sortable ) {
$this->sortable_columns[ $key ] = array( $key, false );
}
}
/**
* Add an item (row) to the table
*
* @param array $item A row's worth of data.
*/
public function add_item( $item ) {
array_push( $this->data, $item );
}
/**
* Add a bulk action to the table
*
* @param string $key Machine-readable action name.
* @param string $name Title shown to the user.
*/
public function add_bulk_action( $key, $name ) {
$this->bulk_actions[ $key ] = $name;
}
/**
* Prepares the items (rows) to be rendered
*/
public function prepare_items() {
$total_items = count( $this->data );
$per_page = 25;
$columns = $this->columns;
$hidden = array();
$sortable = $this->sortable_columns;
$this->_column_headers = array( $columns, $hidden, $sortable );
$current_page = $this->get_pagenum();
$sorted_data = $this->reorder( $this->data );
$data = array_slice( $sorted_data, ( ( $current_page - 1 ) * $per_page ), $per_page );
$this->items = $data;
$this->set_pagination_args(
array(
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => (int) ceil( $total_items / $per_page ),
)
);
}
/**
* Reorder the data according to the sort parameters
*
* @param array $data Row data, unsorted.
* @return array Row data, sorted
*/
public function reorder( $data ) {
usort(
$data,
function ( $a, $b ) {
if ( empty( $_REQUEST['orderby'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
$orderby = 'title';
} else {
$orderby = sanitize_sql_orderby( wp_unslash( $_REQUEST['orderby'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
}
if ( empty( $_REQUEST['order'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
$order = 'asc';
} else {
$order = sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
}
$result = strcmp( $a[ $orderby ], $b[ $orderby ] ); // Determine sort order.
return ( 'asc' === $order ) ? $result : -$result; // Send final sort direction to usort.
}
);
return $data;
}
/**
* Display the table without the nonce at the top.
*
* @since 3.1.0
* @access public
*/
public function display_no_nonce() {
$singular = $this->_args['singular'];
$this->display_tablenav( 'bottom' );
$this->screen->render_screen_reader_content( 'heading_list' );
?>
<table class="wp-list-table <?php echo esc_attr( implode( ' ', $this->get_table_classes() ) ); ?>">
<thead>
<tr>
<?php $this->print_column_headers(); ?>
</tr>
</thead>
<tbody id="the-list"
<?php
if ( $singular ) {
echo " data-wp-lists='list:" . esc_attr( $singular ) . "'";
}
?>
>
<?php $this->display_rows_or_placeholder(); ?>
</tbody>
<tfoot>
<tr>
<?php $this->print_column_headers( false ); ?>
</tr>
</tfoot>
</table>
<?php
$this->display_tablenav( 'bottom' );
}
}