-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist-table.php
More file actions
435 lines (357 loc) · 13.6 KB
/
list-table.php
File metadata and controls
435 lines (357 loc) · 13.6 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
<?php defined( 'ABSPATH' ) || exit;
/**
* List Table class based on WP_Posts_List_Table class
*
* Using copy of WP_List_Table class as recommended in the docs
* @see https://developer.wordpress.org/reference/classes/wp_list_table/
*/
// WP_List_Table dependencies
if ( ! function_exists( 'convert_to_screen' ) ) {
require_once ABSPATH . 'wp-admin/includes/template.php';
}
if ( ! function_exists( 'get_column_headers' ) ) {
require_once ABSPATH . 'wp-admin/includes/screen.php';
}
// if ( ! class_exists( 'WP_List_Table' ) ) {
// require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
// }
// Base class copied from WP_List_Table
require_once __DIR__.'/list-table-base.php';
class TDB_List_Table extends TDB_List_Table_Base {
/**
* Get things started
*
* @param array $args Optional. Arbitrary display and query arguments to pass through
* the list table. Default empty array.
*/
public function __construct( $args = array() ) {
global $tdb_table;
parent::__construct( array(
'singular' => $tdb_table->labels->singular_name,
'plural' => $tdb_table->labels->plural_name,
'screen' => convert_to_screen( $tdb_table->labels->plural_name )
) );
}
/**
* Show the search field
*
* @param string $text Label for the search box
* @param string $input_id ID of the search box
*
* @return void
*/
public function search_box( $text, $input_id ) {
if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
return;
$input_id = $input_id . '-search-input';
if ( ! empty( $_REQUEST['orderby'] ) )
echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
if ( ! empty( $_REQUEST['order'] ) )
echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
?>
<p class="search-box">
<label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
<input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" />
<?php submit_button( $text, 'button', false, false, array( 'ID' => 'search-submit' ) ); ?>
</p>
<?php
}
/**
* Retrieve the view types
*
* @return array $views All the views available
*/
public function get_views() {
global $wpdb, $tdb_table;
/**
* Utility filter to generate views based on a given field
*
* @since 1.0.0
*
* @param string $field_id The field id to generate the views
*
* @return string
*/
$field_id = apply_filters( "tdb_list_{$tdb_table->name}_views_field", '' );
/**
* Labels for the filed id
*
* @since 1.0.0
*
* @param array $field_labels Field labels in format array( 'field_value' => 'Field Label' )
* Field values that are not listed here won't get displayed
*
* @return array
*/
$field_labels = apply_filters( "tdb_list_{$tdb_table->name}_views_field_labels", array() );
$views = array();
// Check if field ID and labels has been passed and also is field is registered on the table (not as meta)
if( ! empty( $field_id ) && ! empty( $field_labels ) && isset( $tdb_table->db->schema->fields[$field_id] ) ) {
// Get the number of entries per each different field value
$results = $wpdb->get_results( "SELECT {$field_id}, COUNT( * ) AS num_entries FROM {$tdb_table->db->table_name} GROUP BY {$field_id}", ARRAY_A );
$counts = array();
// Loop them to build the counts array
foreach( $results as $result ) {
$counts[$result[$field_id]] = absint( $result['num_entries'] );
}
$list_link = tdb_get_list_link( $tdb_table->name );
$current = isset( $_GET[$field_id] ) ? $_GET[$field_id] : '';
// Setup the 'All' view
$all_count = absint( $wpdb->get_var( "SELECT COUNT( * ) FROM {$tdb_table->db->table_name}" ) );
$views['all'] = '<a href="' . $list_link . '" class="' . ( empty( $current ) ? 'current' : '' ) . '">' . __( 'All', 'tdb' ) . ' <span class="count">(' . $all_count . ')</span></a>';
foreach( $counts as $value => $count ) {
// Skip fields that are not intended to being displayed
if( ! isset( $field_labels[$value] ) ) {
continue;
}
$label = $field_labels[$value];
$url = $list_link . '&' . $field_id . '=' . $value;
$views[$value] = '<a href="' . $url . '" class="' . ( $current === $value ? 'current' : '' ) . '">' . $label . ' <span class="count">(' . $count . ')</span>' . '</a>';
}
}
/**
* Available filter to past custom views
*
* @since 1.0.0
*
* @param array $views An array of views links.
* Array format: array( 'link_id' => 'link' )
* Link format: '<a href="#">{label} <span class="count">({count})</span></a>'
*/
$views = apply_filters( "{$tdb_table->name}_get_views", $views );
return $views;
}
/**
*
* @return array
*/
protected function get_bulk_actions() {
global $tdb_table;
$actions = array();
if ( current_user_can( $tdb_table->cap->delete_items ) ) {
$actions['delete'] = __( 'Delete Permanently' );
}
$actions = apply_filters( "{$tdb_table->name}_bulk_actions", $actions );
return $actions;
}
/**
*
* @return array
*/
protected function get_table_classes() {
global $tdb_table;
return array( 'widefat', 'fixed', 'striped', $tdb_table->name );
}
/**
* Retrieve the table columns
*
* @access public
* @since 1.0.0
* @return array $columns Array of all the list table columns
*/
public function get_columns() {
global $tdb_table;
$columns = array();
$bulk_actions = $this->get_bulk_actions();
if( ! empty( $bulk_actions ) ) {
$columns['cb'] = '<input type="checkbox" />';
}
/**
* Filters the columns displayed in the list table of a specific TDB table.
*
* @since 1.0.0
*
* @param array $posts_columns An array of column names.
* @param TDB_Table $tdb_table The table object.
*/
return apply_filters( "manage_{$tdb_table->name}_columns", $columns, $tdb_table );
}
/**
* Retrieve the table's sortable columns
*
* @access public
* @since 1.0.0
* @return array Array of all the sortable columns
*/
public function get_sortable_columns() {
global $tdb_table;
$sortable_columns = array();
/**
* Filters the sortable columns in the list table of a specific TDB table.
*
* Format:
* 'internal-name' => 'orderby'
* or
* 'internal-name' => array( 'orderby', true )
* The second format will make the initial sorting order be descending
*
* @since 1.0.0
*
* @param array $sortable_columns An array of column names.
* @param TDB_Table $tdb_table The table object.
*/
return apply_filters( "manage_{$tdb_table->name}_sortable_columns", $sortable_columns, $tdb_table );
}
/**
* This function renders most of the columns in the list table.
*
* @access public
* @since 1.0.0
*
* @param stdClass $item The current object.
* @param string $column_name The name of the column
* @return string The column value.
*/
public function column_default( $item, $column_name ) {
global $tdb_table;
$value = isset( $item->$column_name ) ? $item->$column_name : '';
$primary_key = $tdb_table->db->primary_key;
ob_start();
/**
* Fires for each custom column of a specific TDB table in the list table.
*
* The dynamic portion of the hook name, `$tdb_table->name`, refers to the TDB table name.
*
* @since 1.0.0
*
* @param string $column_name The name of the column to display.
* @param int $object_id The current object ID.
* @param stdClass $object The current object.
* @param TDB_Table $tdb_table The TDB table object.
*/
do_action( "manage_{$tdb_table->name}_custom_column", $column_name, $item->$primary_key, $item, $tdb_table );
$custom_output = ob_get_clean();
if( ! empty( $custom_output ) ) {
return $custom_output;
}
$bulk_actions = $this->get_bulk_actions();
$first_column_index = ( ! empty( $bulk_actions ) ) ? 1 : 0;
$can_edit_item = current_user_can( $tdb_table->cap->edit_item, $item->$primary_key );
$columns = $this->get_columns();
$columns_keys = array_keys( $columns );
if( $column_name === $columns_keys[$first_column_index] && $can_edit_item ) {
// Turns first column into a text link with url to edit the item
$value = sprintf( '<a href="%s" aria-label="%s">%s</a>',
tdb_get_edit_link( $tdb_table->name, $item->$primary_key ),
esc_attr( sprintf( __( 'Edit “%s”' ), $value ) ),
$value
);
// Small screens toggle
$value .= '<button type="button" class="toggle-row"><span class="screen-reader-text">' . __( 'Show more details' ) . '</span></button>';
}
return $value;
}
/**
* Generates and displays row action links.
*
* @since 4.3.0
* @access protected
*
* @param object $item The item being acted upon.
* @param string $column_name Current column name.
* @param string $primary Primary column name.
*
* @return string Row actions output for posts.
*/
protected function handle_row_actions( $item, $column_name, $primary ) {
if ( $primary !== $column_name ) {
return '';
}
global $tdb_table;
$primary_key = $tdb_table->db->primary_key;
$actions = array();
if ( $tdb_table->views->edit && current_user_can( $tdb_table->cap->edit_item, $item->$primary_key ) ) {
$actions['edit'] = sprintf(
'<a href="%s" aria-label="%s">%s</a>',
tdb_get_edit_link( $tdb_table->name, $item->$primary_key ),
esc_attr( __( 'Edit' ) ),
__( 'Edit' )
);
}
if ( current_user_can( $tdb_table->cap->delete_item, $item->$primary_key ) ) {
$actions['delete'] = sprintf(
'<a href="%s" class="submitdelete" onclick="%s" aria-label="%s">%s</a>',
tdb_get_delete_link( $tdb_table->name, $item->$primary_key ),
"return confirm('" .
esc_attr( __( "Are you sure you want to delete this item?\\n\\nClick \\'Cancel\\' to go back, \\'OK\\' to confirm the delete." ) ) .
"');",
esc_attr( __( 'Delete permanently' ) ),
__( 'Delete Permanently' )
);
}
/**
* Filters the array of row action links on the Posts list table.
*
* The filter is evaluated only for non-hierarchical post types.
*
* @since 2.8.0
*
* @param array $actions An array of row action links. Defaults are
* 'Edit', 'Quick Edit', 'Restore, 'Trash',
* 'Delete Permanently', 'Preview', and 'View'.
* @param WP_Post $post The post object.
*/
$actions = apply_filters( "{$tdb_table->name}_row_actions", $actions, $item );
return $this->row_actions( $actions );
}
/**
* Handles the checkbox column output.
*
* @param WP_Post $item The current WP_Post object.
*/
public function column_cb( $item ) {
global $tdb_table;
$primary_key = $tdb_table->db->primary_key;
if ( current_user_can( $tdb_table->cap->edit_items ) ): ?>
<label class="screen-reader-text" for="cb-select-<?php echo $item->$primary_key; ?>"><?php
printf( __( 'Select Item #%d' ), $item->$primary_key );
?></label>
<input id="cb-select-<?php echo $item->$primary_key; ?>" type="checkbox" name="item[]" value="<?php echo $item->$primary_key; ?>" />
<div class="locked-indicator">
<span class="locked-indicator-icon" aria-hidden="true"></span>
<span class="screen-reader-text"><?php
printf(
/* translators: %d: item ID */
__( '“Item #%d” is locked' ),
$item->$primary_key
);
?></span>
</div>
<?php endif;
}
/**
* Renders the message to be displayed when there are no results.
*/
function no_items() {
global $tdb_table;
echo $tdb_table->labels->not_found;
}
public function prepare_items() {
global $tdb_table, $tdb_query;
// Get per page setting
$per_page = $this->get_items_per_page( 'edit_' . $tdb_table->name . '_per_page' );
// Update query vars based on settings
$tdb_query->query_vars['items_per_page'] = $per_page;
// Get query results
$this->items = $tdb_query->get_results();
$total_items = $tdb_query->found_results;
// Setup pagination args based on items found and per page settings
$this->set_pagination_args( array(
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil( $total_items / $per_page )
) );
}
public function extra_tablenav($which){
global $tdb_table;
$args = $tdb_table->views->list->args;
switch ( $which ) {
case 'top':
if(isset($args['nav_top']) && is_callable($args['nav_top'])) call_user_func_array($args['nav_top'], [$tdb_table]);
break;
case 'bottom':
if(isset($args['nav_bottom']) && is_callable($args['nav_bottom'])) call_user_func_array($args['nav_bottom'], [$tdb_table]);
break;
}
}
}