-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-metronet-log-list-table.php
176 lines (151 loc) · 5.6 KB
/
class-metronet-log-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
173
174
175
176
<?php
/*Code inspired by http://wp.smashingmagazine.com/2011/11/03/native-admin-tables-wordpress/
Technically shouldn't be using list table according to this comment: http://wpengineer.com/2426/wp_list_table-a-step-by-step-guide/#comment-9617
This class could use some work, notably searching by user ID and to/from dates - Feel free to contribute here: https://github.com/ronalfy/metronet-log
*/
class Metronet_Logs_List_Table extends WP_List_Table {
public function __construct() {
parent::__construct( array(
'singular'=> 'log',
'plural' => 'logs',
'ajax' => false //We won't support Ajax for this table
) );
} //end constructor
public function display_rows() {
static $alternate;
//Get the records registered in the prepare_items method
$records = $this->items;
//Get the columns registered in the get_columns and get_sortable_columns methods
list( $columns, $hidden ) = $this->get_column_info();
//Loop for each record
if( !empty( $records ) ){
foreach( $records as $rec ){
$alternate = 'alternate' == $alternate ? '' : 'alternate';
//Open the line
echo sprintf( '<tr id="record_%d" class="%s">', $rec->log_id, $alternate );
foreach ( $columns as $column_name => $column_display_name ) {
//Style attributes for each col
$class = "class='$column_name column-$column_name'";
$style = "";
$attributes = $class;
//Display the cell
switch ( $column_name ) {
case 'cb':
$attributes = "class='$column_name column-$column_name check-column'";
printf( '<th %1$s><input id="cb-select-%2$d" type="checkbox" name="log[]" value="%2$d"></th>', $attributes, $rec->log_id );
break;
case 'user':
$user = get_user_by( 'id', $rec->user_id );
echo sprintf( '<td %s>', $attributes );
if ( !$user )
echo __( "User doesn't exist", 'metronet_log' );
else
echo esc_html( $user->user_nicename );
echo '</td>';
break;
case 'type':
echo sprintf( '<td %s>', $attributes );
echo esc_html( $rec->type );
echo '</td>';
break;
case 'value':
echo sprintf( '<td %s>', $attributes );
$value = maybe_unserialize( $rec->value );
//If objects or arrays, perform a filter so the dev can determine how best to output
if ( is_object( $value ) || is_array( $value ) ) {
ob_start();
do_action( 'metronet_log_obj_arr', $rec->user_id, $rec->type, $value ); //For other plugin devs - output $value how you see fit
$output = ob_get_clean();
if ( empty( $output ) ) echo $value;
else echo $output;
} else {
echo esc_html( $value ); //Assumes string or int
}
echo '</td>';
break;
case 'date':
echo sprintf( '<td %s>', $attributes );
$date = sprintf( __( '%s ago', 'metronet_log' ), human_time_diff( strtotime( $rec->date ) ) );
echo apply_filters( 'metronet_log_date', esc_html( $date ), $rec->date ); //Filter for devs
echo '</td>';
break;
}
}
//Close the line
echo'</tr>';
}
}
}
public function extra_tablenav( $which ) {
if ( $which == 'top' ) {
} else {
}
} //end extra_tablenav
public function get_columns() {
$columns = array(
'cb' => '<input type="checkbox" />',
'user' => esc_html__( 'User', 'metronet_log' ),
'type' => esc_html__( 'Type', 'metronet_log' ),
'value' => esc_html__( 'Value', 'metronet_log' ),
'date' => esc_html__( 'Date', 'metronet_log' )
);
return $columns;
} //end get_columns
//Display Bulk Actions Dropdown
public function get_bulk_actions() {
$actions = array(
'delete' => 'Delete'
);
return $actions;
}
public function prepare_items() {
global $wpdb, $_wp_column_headers;
$screen = get_current_screen();
$this->_column_headers = array(
$this->get_columns(), // columns
array(), // hidden
$this->get_sortable_columns(), // sortable
);
//Tablename
$tablename = Metronet_Log::get_table_name();
//Handle bulk actions
//$this->process_bulk_action();
/* -- Preparing your query -- */
$query = "SELECT * FROM {$tablename} WHERE 1=1"; //todo - This doesn't seem effecient
if ( isset( $_GET[ 's' ] ) ) {
$search = esc_sql( $_GET[ 's' ] );
$query .= " AND type = '$search'";
}
if ( !isset( $_GET[ 'orderby' ] ) ) {
$query .= ' ORDER BY date DESC';
}
//Number of elements in the table
$totalitems = $wpdb->query( $query ); //return the total number of affected rows
//How many to display per page?
$perpage = 50;
//Which page is this?
$paged = !empty($_GET["paged"]) ? mysql_real_escape_string($_GET["paged"]) : '';
//Page Number
if(empty($paged) || !is_numeric($paged) || $paged<=0 ){ $paged=1; }
//How many pages do we have in total?
$totalpages = ceil($totalitems/$perpage);
//adjust the query to take pagination into account
if(!empty($paged) && !empty($perpage)){
$offset=($paged-1)*$perpage;
$query.=' LIMIT '.(int)$offset.','.(int)$perpage;
}
/* -- Register the pagination -- */
$this->set_pagination_args( array(
"total_items" => $totalitems,
"total_pages" => $totalpages,
"per_page" => $perpage,
) );
//The pagination links are automatically built according to those parameters
/* -- Register the Columns -- */
$columns = $this->get_columns();
$_wp_column_headers[$screen->id]=$columns;
/* -- Fetch the items -- */
$this->items = $wpdb->get_results($query);
} //end prepare
} //end Metronet_Logs_List_Table
?>