-
Notifications
You must be signed in to change notification settings - Fork 0
/
bluecadet-log-table.php
179 lines (144 loc) · 5.04 KB
/
bluecadet-log-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
<?php
class BluecadetLog_Table extends WP_List_Table {
/**
* Constructor, we override the parent to pass our own arguments
* We usually focus on three parameters: singular and plural labels, as well as whether the class supports AJAX.
*/
function __construct() {
parent::__construct( array(
'singular'=> 'wp_list_text_link', //Singular label
'plural' => 'wp_list_test_links', //plural label, also this well be one of the table css class
'ajax' => false //We won't support Ajax for this table
) );
}
/**
* Add extra markup in the toolbars before or after the list
* @param string $which, helps you decide if you add the markup after (bottom) or before (top) the list
*/
function extra_tablenav( $which ) {
if ( $which == "top" ){
//The code that goes before the table is here
echo "Hello, I'm before the table";
}
if ( $which == "bottom" ){
//The code that goes after the table is there
echo "Hi, I'm after the table";
}
}
/**
* Define the columns that are going to be used in the table
* @return array $columns, the array of columns to use with the table
*/
function get_columns() {
$columns = array(
'log_id' => __('Log ID'),
'user_id' => __('User ID'),
'type' => __('Type'),
'message' => __('Message'),
'severity' => __('Severity'),
'timestamp' => __('Timestamp'),
);
return $columns;
}
function get_hidden_columns() {
$columns = [];
return $columns;
}
/**
* Decide which columns to activate the sorting functionality on
* @return array $sortable, the array of columns that can be sorted by the user
*/
public function get_sortable_columns() {
$sortable = array(
'log_id' => array('log_id', true ),
'user_id' => array('user_id', true ),
'type' => array('type', true ),
'severity' => array('severity', true ),
'timestamp' => array('timestamp', true ),
);
return $sortable;
}
/**
* Retrieve log data from the database
*
* @param int $per_page
* @param int $page_number
*
* @return mixed
*/
public static function get_logs( $per_page = 5, $page_number = 1 ) {
global $wpdb;
$sql = "SELECT * FROM $wpdb->bc_log_activity_log";
if ( ! empty( $_REQUEST['orderby'] ) ) {
$sql .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] );
$sql .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' ASC';
}
$sql .= " LIMIT $per_page";
$sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
$result = $wpdb->get_results( $sql );
return $result;
}
// Get Full Count.
public static function get_count() {
global $wpdb;
$sql = "SELECT COUNT(*) as count FROM $wpdb->bc_log_activity_log";
$result = $wpdb->get_results( $sql );
return current($result)->count;
}
/**
* Prepare the table with different parameters, pagination, columns and table elements
*/
function prepare_items() {
$columns = $this->get_columns();
$hidden = $this->get_hidden_columns();
$sortable = $this->get_sortable_columns();
$perPage = 50;
$currentPage = $this->get_pagenum();
$this->set_pagination_args( array(
'total_items' => $this->get_count(),
'per_page' => $perPage
) );
$this->_column_headers = array($columns, $hidden, $sortable);
$this->items = self::get_logs( $perPage, $currentPage );
}
/**
* Generate the table rows
*
* @since 3.1.0
* @access public
*/
public function display_rows() {
//Get the records registered in the prepare_items method
$records = $this->items;
list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
$severity_labels = bcl_severity_labels();
//Loop for each record
if(!empty($records)){
foreach($records as $rec){
//Open the line
echo '<tr id="record_'.$rec->log_id.'">';
foreach ( $columns as $column_name => $column_display_name ) {
//Style attributes for each col
$class = "class='$column_name column-$column_name'";
$style = "";
if ( in_array( $column_name, $hidden ) ) $style = ' style="display:none;"';
$attributes = $class . $style;
//edit link
$editlink = '/wp-admin/link.php?action=edit&link_id='.(int)$rec->log_id;
//Display the cell
switch ( $column_name ) {
case "log_id": echo '<td '.$attributes.'>'.stripslashes($rec->log_id).'</td>'; break;
case "user_id": echo '<td '.$attributes.'>'.stripslashes($rec->user_id).'</td>'; break;
case "type": echo '<td '.$attributes.'>'.stripslashes($rec->type).'</td>'; break;
case "message": echo '<td '.$attributes.'>'.stripslashes($rec->message).'</td>'; break;
case "severity": echo '<td '.$attributes.'>'.$severity_labels[$rec->severity].'</td>'; break;
case "timestamp": echo '<td '.$attributes.'>'.date('Y-m-d H:i:s', $rec->timestamp).'</td>'; break;
}
}
//Close the line
echo'</tr>';
}
}
}
}
?>