-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlist-view.php
More file actions
441 lines (325 loc) · 12.5 KB
/
list-view.php
File metadata and controls
441 lines (325 loc) · 12.5 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
436
437
438
439
440
441
<?php defined( 'ABSPATH' ) || exit;
class TDB_List_View extends TDB_View {
protected $per_page = 20;
protected $columns = array();
public function __construct( $name, $args ) {
parent::__construct( $name, $args );
$this->per_page = isset( $args['per_page'] ) ? $args['per_page'] : 20;
$this->columns = isset( $args['columns'] ) ? $args['columns'] : array();
}
public function add_hooks() {
parent::add_hooks();
add_filter( "manage_{$this->name}_columns", array( $this, 'get_columns' ) );
add_filter( "manage_{$this->name}_sortable_columns", array( $this, 'get_sortable_columns' ) );
}
/**
* Set columns passed from view columns arg.
*
* @param array $columns
*
* @return array
*/
public function get_columns( $columns ) {
foreach( $this->columns as $column => $column_args ) {
if( is_array( $column_args ) && isset( $column_args['label'] ) ) {
// 'column_name' => array( 'label' => 'Column Label' )
$columns[$column] = $column_args['label'];
} else if( gettype( $column_args ) === 'string' ) {
// 'column_name' => 'Column Label'
$columns[$column] = $column_args;
}
}
return $columns;
}
/**
* Set columns passed from view columns arg.
*
* @since 1.0.0
*
* @param array $sortable_columns
*
* @return array
*/
public function get_sortable_columns( $sortable_columns ) {
foreach( $this->columns as $column => $column_args ) {
if( is_array( $column_args ) && isset( $column_args['sortable'] ) ) {
// 'column_name' => array( 'sortable' => 'sortable_setup' )
$sortable_columns[$column] = $column_args['sortable'];
}
}
return $sortable_columns;
}
public function init() {
global $tdb_registered_tables, $tdb_table, $tdb_query, $tdb_list_table;
if( ! isset( $tdb_registered_tables[$this->name] ) ) {
return;
}
// Setup TDB_Table
$tdb_table = $tdb_registered_tables[$this->name];
// Check for bulk delete
if( isset( $_GET['action'] ) ) {
if( $_GET['action'] === 'delete' ) {
// Deleting
$this->bulk_delete();
}
}
// Check for delete action
if( isset( $_GET['tdb-action'] ) ) {
if( $_GET['tdb-action'] === 'delete' ) {
// Deleting
$this->delete();
}
}
// Setup the query and the list table objects
$tdb_query = new TDB_Query( $_GET );
$tdb_list_table = new TDB_List_Table();
}
/**
* Screen settings text displayed in the Screen Options tab.
*
* @since 1.0.0
*
* @param string $screen_settings Screen settings.
* @param WP_Screen $screen WP_Screen object.
*/
public function screen_settings( $screen_settings, $screen ) {
$this->render_list_table_columns_preferences();
$this->render_per_page_options();
}
/**
* Render the list table columns preferences.
*
* @since 1.0.0
*/
public function render_list_table_columns_preferences() {
global $tdb_table, $tdb_list_table;
// Set up vars
$columns = $tdb_list_table->get_columns();
$hidden = get_hidden_columns( $tdb_table->name );
if ( ! $columns )
return;
$legend = ! empty( $columns['_title'] ) ? $columns['_title'] : __( 'Columns' );
?>
<fieldset class="metabox-prefs">
<legend><?php echo $legend; ?></legend>
<?php
$special = array( '_title', 'cb' );
foreach ( $columns as $column => $title ) {
// Can't hide these for they are special
if ( in_array( $column, $special ) || empty( $title ) ) {
continue;
}
$id = "$column-hide";
echo '<label>';
echo '<input class="hide-column-tog" name="' . $id . '" type="checkbox" id="' . $id . '" value="' . $column . '"' . checked( ! in_array( $column, $hidden ), true, false ) . ' />';
echo "$title</label>\n";
}
?>
</fieldset>
<?php
}
/**
* Render the items per page option
*
* @since 1.0.0
*/
public function render_per_page_options() {
global $tdb_table;
if ( ! $this->per_page ) {
return;
}
// Set up vars
$per_page_label = __( 'Number of items per page:' );
$option = str_replace( '-', '_', "edit_{$tdb_table->name}_per_page" );
$per_page = (int) get_user_option( $option );
if ( empty( $per_page ) || $per_page < 1 )
$per_page = $this->per_page;
$per_page = apply_filters( "{$option}", $per_page );
// This needs a submit button
add_filter( 'screen_options_show_submit', '__return_true' );
?>
<fieldset class="screen-options">
<legend><?php _e( 'Pagination' ); ?></legend>
<?php if ( $per_page_label ) : ?>
<label for="<?php echo esc_attr( $option ); ?>"><?php echo $per_page_label; ?></label>
<input type="number" step="1" min="1" max="999" class="screen-per-page" name="wp_screen_options[value]"
id="<?php echo esc_attr( $option ); ?>" maxlength="3"
value="<?php echo esc_attr( $per_page ); ?>" />
<?php endif; ?>
<input type="hidden" name="wp_screen_options[option]" value="<?php echo esc_attr( $option ); ?>" />
</fieldset>
<?php
}
/**
* Screen option value before it is set.
*
* @since 1.0.0
*
* @param bool|int $value_to_set Screen option value to set. Default false to skip.
* @param string $option The option name.
* @param int $value The option value.
*
* @return bool|mixed False to skip or any other value to set as option value
*/
public function set_screen_settings( $value_to_set, $option, $value ) {
global $tdb_table, $tdb_list_table;
if( ! $tdb_table ) {
return $value_to_set;
}
if( ! $tdb_list_table ) {
return $value_to_set;
}
$view_settings = array(
str_replace( '-', '_', "edit_{$tdb_table->name}_per_page" ) // Per page
);
// Columns hidden setting
$columns = $tdb_list_table->get_columns();
$special = array( '_title', 'cb' );
foreach ( $columns as $column => $title ) {
// Can't hide these for they are special
if ( in_array( $column, $special ) || empty( $title ) )
continue;
$view_settings[] = "$column-hide";
}
// If option is on this view settings list, then save it
if( in_array( $option, $view_settings ) ) {
$value_to_set = $value;
}
return $value_to_set;
}
public function bulk_delete() {
global $tdb_table;
// If not TDB object, die
if ( ! $tdb_table )
wp_die( __( 'Invalid item type.' ) );
// If not TDB object allow ui, die
if ( ! $tdb_table->show_ui ) {
wp_die( __( 'Sorry, you are not allowed to delete items of this type.' ) );
}
// Nonce check
if ( ! isset( $_REQUEST['_wpnonce'] ) ) {
wp_die( __( 'Sorry, you are not allowed to delete items of this type.' ) );
}
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'bulk-' . sanitize_key( $tdb_table->labels->plural_name ) ) ) {
wp_die( __( 'Sorry, you are not allowed to delete items of this type.' ) );
}
$object_ids = array();
// Check received items
if ( ! empty( $_REQUEST['item'] ) ) {
$object_ids = array_map('intval', $_REQUEST['item']);
}
$deleted = 0;
foreach ( (array) $object_ids as $object_id ) {
// If not current user can delete, die
if ( ! current_user_can( $tdb_table->cap->delete_item, $object_id ) ) {
wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
}
if ( ! tdb_delete_object( $object_id, true ) )
wp_die( __( 'Error in deleting.' ) );
$deleted++;
}
$location = add_query_arg( array( 'deleted' => $deleted ), $this->get_link() );
wp_redirect( $location );
exit;
}
public function delete() {
global $tdb_table;
// If not TDB object, die
if ( ! $tdb_table ) {
wp_die( __( 'Invalid item type.' ) );
}
// If not TDB object allow ui, die
if ( ! $tdb_table->show_ui ) {
wp_die( __( 'Sorry, you are not allowed to delete items of this type.' ) );
}
$primary_key = $tdb_table->db->primary_key;
// Object ID is required
if( ! isset( $_GET[$primary_key] ) ) {
wp_die( __( 'Sorry, you are not allowed to delete items of this type.' ) );
}
$object_id = (int) $_GET[$primary_key];
// Nonce check
if ( ! isset( $_REQUEST['_wpnonce'] ) ) {
wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
}
if ( ! wp_verify_nonce( $_REQUEST['_wpnonce'], 'tdb_delete_' . $object_id ) ) {
wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
}
// If user can not delete it, bail
if ( ! current_user_can( $tdb_table->cap->delete_item, $object_id ) ) {
wp_die( __( 'Sorry, you are not allowed to delete this item.' ) );
}
if ( ! tdb_delete_object( $object_id ) )
wp_die( __( 'Error in deleting.' ) );
$location = add_query_arg( array( 'deleted' => 1 ), $this->get_link() );
wp_redirect( $location );
exit;
}
/**
* View content.
*
* @since 1.0.0
*/
public function render() {
global $tdb_table, $tdb_list_table;
$tdb_list_table->prepare_items();
$bulk_counts = array(
'updated' => isset( $_REQUEST['updated'] ) ? absint( $_REQUEST['updated'] ) : 0,
'locked' => isset( $_REQUEST['locked'] ) ? absint( $_REQUEST['locked'] ) : 0,
'deleted' => isset( $_REQUEST['deleted'] ) ? absint( $_REQUEST['deleted'] ) : 0,
'trashed' => isset( $_REQUEST['trashed'] ) ? absint( $_REQUEST['trashed'] ) : 0,
'untrashed' => isset( $_REQUEST['untrashed'] ) ? absint( $_REQUEST['untrashed'] ) : 0,
);
$bulk_messages = array(
'updated' => _n( '%s item updated.', '%s items updated.', $bulk_counts['updated'] ),
'locked' => ( 1 == $bulk_counts['locked'] ) ? __( '1 item not updated, somebody is editing it.' ) :
_n( '%s item not updated, somebody is editing it.', '%s items not updated, somebody is editing them.', $bulk_counts['locked'] ),
'deleted' => _n( '%s item permanently deleted.', '%s items permanently deleted.', $bulk_counts['deleted'] ),
'trashed' => _n( '%s item moved to the Trash.', '%s items moved to the Trash.', $bulk_counts['trashed'] ),
'untrashed' => _n( '%s item restored from the Trash.', '%s items restored from the Trash.', $bulk_counts['untrashed'] ),
);
/**
* Filters the bulk action updated messages.
*
* @since 1.0.0
*
* @param array $bulk_messages Arrays of messages. Messages are keyed with 'updated', 'locked', 'deleted', 'trashed', and 'untrashed'.
* @param array $bulk_counts Array of item counts for each message, used to build internationalized strings.
*/
$bulk_messages = apply_filters( 'bulk_object_updated_messages', $bulk_messages, $bulk_counts );
$bulk_counts = array_filter( $bulk_counts );
?>
<div class="wrap">
<h1 class="wp-heading-inline"><?php echo $tdb_table->labels->plural_name; ?></h1>
<?php if ( property_exists( $tdb_table->views, 'add' ) && $tdb_table->views->add && current_user_can( $tdb_table->cap->create_items ) ) :
echo ' <a href="' . esc_url( $tdb_table->views->add->get_link() ) . '" class="page-title-action">' . esc_html( $tdb_table->labels->add_new_item ) . '</a>';
endif; ?>
<hr class="wp-header-end">
<?php
// If we have a bulk message to issue:
$messages = array();
foreach ( $bulk_counts as $message => $count ) {
if ( isset( $bulk_messages[ $message ] ) )
$messages[] = sprintf( $bulk_messages[ $message ], number_format_i18n( $count ) );
//if ( $message == 'trashed' && isset( $_REQUEST['ids'] ) ) {
//$ids = preg_replace( '/[^0-9,]/', '', $_REQUEST['ids'] );
//$messages[] = '<a href="' . esc_url( wp_nonce_url( "edit.php?post_type=$post_type&doaction=undo&action=untrash&ids=$ids", "bulk-posts" ) ) . '">' . __('Undo') . '</a>';
//}
}
if ( $messages )
echo '<div id="message" class="updated notice is-dismissible"><p>' . join( ' ', $messages ) . '</p></div>';
unset( $messages );
$_SERVER['REQUEST_URI'] = remove_query_arg( array( 'locked', 'skipped', 'updated', 'deleted', 'trashed', 'untrashed' ), $_SERVER['REQUEST_URI'] );
?>
<?php $tdb_list_table->views(); ?>
<form id="ct-list-filter" method="get">
<input type="hidden" name="page" value="<?php echo esc_attr( $this->args['menu_slug'] ); ?>" />
<?php $tdb_list_table->search_box( $tdb_table->labels->search_items, $tdb_table->name ); ?>
<?php $tdb_list_table->display(); ?>
</form>
<div id="ajax-response"></div>
<br class="clear" />
</div>
<?php
}
}