|
1
|
|
|
<?php |
|
2
|
|
|
namespace NirjharLo\WP_Plugin_Framework\Lib; |
|
3
|
|
|
|
|
4
|
|
|
/** |
|
5
|
|
|
* Implimentation of WordPress inbuilt functions for creating an extension of a default table class. |
|
6
|
|
|
* |
|
7
|
|
|
* $my_plugin_name_table = new PLUGIN_TABLE(); |
|
8
|
|
|
* $my_plugin_name_table->prepare_items(); |
|
9
|
|
|
* $my_plugin_name_table->display(); |
|
10
|
|
|
* |
|
11
|
|
|
* @author Nirjhar Lo |
|
12
|
|
|
* @package wp-plugin-framework |
|
13
|
|
|
*/ |
|
14
|
|
|
if ( ! class_exists( 'Table' ) ) { |
|
15
|
|
|
|
|
16
|
|
|
if ( ! class_exists( 'WP_List_Table' ) ) { |
|
17
|
|
|
require_once( ABSPATH . 'wp-admin/includes/screen.php' ); |
|
|
|
|
|
|
18
|
|
|
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
final class Table extends WP_List_Table { |
|
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Instantiate the table |
|
26
|
|
|
* |
|
27
|
|
|
* @return Void |
|
28
|
|
|
*/ |
|
29
|
|
|
public function __construct() { |
|
30
|
|
|
|
|
31
|
|
|
parent::__construct( [ |
|
32
|
|
|
'singular' => __( 'Name', 'textdomain' ), |
|
|
|
|
|
|
33
|
|
|
'plural' => __( 'Names', 'textdomain' ), |
|
34
|
|
|
'ajax' => false, |
|
35
|
|
|
] ); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Fetch the data using custom named method function |
|
41
|
|
|
* |
|
42
|
|
|
* @return Array |
|
43
|
|
|
*/ |
|
44
|
|
|
public static function get_Table( $per_page = 5, $page_number = 1 ) { |
|
45
|
|
|
|
|
46
|
|
|
global $wpdb; |
|
47
|
|
|
|
|
48
|
|
|
//Take pivotal from URL |
|
49
|
|
|
$link = ( isset( $_GET['link'] ) ? $_GET['link'] : 'link' ); |
|
50
|
|
|
|
|
51
|
|
|
//Build the db query base |
|
52
|
|
|
$sql = "SELECT * FROM {$wpdb->prefix}wordpress_table"; |
|
53
|
|
|
$sql .= " QUERIES with $link'"; |
|
54
|
|
|
|
|
55
|
|
|
//Set filters in the query using $_REQUEST |
|
56
|
|
|
if ( ! empty( $_REQUEST['orderby'] ) ) { |
|
57
|
|
|
$sql .= ' ORDER BY ' . esc_sql( $_REQUEST['orderby'] ); |
|
|
|
|
|
|
58
|
|
|
$sql .= ! empty( $_REQUEST['order'] ) ? ' ' . esc_sql( $_REQUEST['order'] ) : ' ASC'; |
|
59
|
|
|
} |
|
60
|
|
|
$sql .= " LIMIT $per_page"; |
|
61
|
|
|
$sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page; |
|
62
|
|
|
|
|
63
|
|
|
//get the data from database |
|
64
|
|
|
$result = $wpdb->get_results( $sql, 'ARRAY_A' ); |
|
65
|
|
|
|
|
66
|
|
|
return $result; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Delete individual data |
|
72
|
|
|
* |
|
73
|
|
|
* @return Void |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function delete_url( $id ) { |
|
76
|
|
|
|
|
77
|
|
|
global $wpdb; |
|
78
|
|
|
|
|
79
|
|
|
$wpdb->delete( "{$wpdb->prefix}wordpress_table", array( 'ID' => $id ), array( '%s' ) ); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* If there is no data to show |
|
85
|
|
|
* |
|
86
|
|
|
* @return String |
|
87
|
|
|
*/ |
|
88
|
|
|
public function no_items() { |
|
89
|
|
|
|
|
90
|
|
|
_e( 'No Items Added yet.', 'textDomain' ); |
|
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* How many rows are present there |
|
96
|
|
|
* |
|
97
|
|
|
* @return Int |
|
98
|
|
|
*/ |
|
99
|
|
|
public static function record_count() { |
|
100
|
|
|
|
|
101
|
|
|
global $wpdb; |
|
102
|
|
|
|
|
103
|
|
|
//Take pivotal from URL |
|
104
|
|
|
$link = ( isset( $_GET['link'] ) ? $_GET['link'] : 'link' ); |
|
105
|
|
|
|
|
106
|
|
|
//Build the db query base |
|
107
|
|
|
$sql = "SELECT COUNT(*) FROM {$wpdb->prefix}wordpress_table"; |
|
108
|
|
|
$sql .= " QUERIES with $link'"; |
|
109
|
|
|
|
|
110
|
|
|
return $wpdb->get_var( $sql ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Display columns content |
|
116
|
|
|
* |
|
117
|
|
|
* @return Html |
|
|
|
|
|
|
118
|
|
|
*/ |
|
119
|
|
|
public function column_name( $item ) { |
|
120
|
|
|
|
|
121
|
|
|
$delete_nonce = wp_create_nonce( 'delete_url' ); |
|
|
|
|
|
|
122
|
|
|
$title = sprintf( '<strong>%s</strong>', $item['item_name'] ); |
|
123
|
|
|
|
|
124
|
|
|
//Change the page instruction where you want to show it |
|
125
|
|
|
$actions = array( |
|
126
|
|
|
'delete' => sprintf( '<a href="?page=%s&action=%s&instruction=%s&_wpnonce=%s">%s</a>', esc_attr( $_REQUEST['page'] ), 'delete', absint( $item['ID'] ), $delete_nonce, __( 'Delete', 'textdomain' ) ) |
|
|
|
|
|
|
127
|
|
|
); |
|
128
|
|
|
|
|
129
|
|
|
return $title . $this->row_actions( $actions ); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* set coulmns name |
|
135
|
|
|
* |
|
136
|
|
|
* @return Html |
|
137
|
|
|
*/ |
|
138
|
|
|
public function column_default( $item, $column_name ) { |
|
139
|
|
|
|
|
140
|
|
|
switch ( $column_name ) { |
|
141
|
|
|
|
|
142
|
|
|
case 'name': |
|
143
|
|
|
//This is the first column |
|
144
|
|
|
return $this->column_name( $item ); |
|
145
|
|
|
case 'caseOne': |
|
146
|
|
|
case 'caseTwo': |
|
147
|
|
|
case 'caseThree': |
|
148
|
|
|
return $item[ $column_name ]; |
|
149
|
|
|
|
|
150
|
|
|
default: |
|
151
|
|
|
|
|
152
|
|
|
//Show the whole array for troubleshooting purposes |
|
153
|
|
|
return print_r( $item, true ); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
/** |
|
159
|
|
|
* Set checkboxes to delete |
|
160
|
|
|
* |
|
161
|
|
|
* @return Html |
|
162
|
|
|
*/ |
|
163
|
|
|
public function column_cb( $item ) { |
|
164
|
|
|
|
|
165
|
|
|
return sprintf( '<input type="checkbox" name="bulk-select[]" value="%s" />', $item['ID'] ); |
|
|
|
|
|
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Columns callback |
|
171
|
|
|
* |
|
172
|
|
|
* @return Array |
|
173
|
|
|
*/ |
|
174
|
|
|
public function get_columns() { |
|
175
|
|
|
|
|
176
|
|
|
$columns = array( |
|
177
|
|
|
'cb' => '<input type="checkbox" />', |
|
178
|
|
|
'name' => __( 'Name', 'textdomain' ), |
|
|
|
|
|
|
179
|
|
|
'caseOne' => __( 'Case One', 'textdomain' ), |
|
180
|
|
|
'caseTwo' => __( 'Case Two', 'textdomain' ), |
|
181
|
|
|
'caseThree' => __( 'Case Three', 'textdomain' ), |
|
182
|
|
|
); |
|
183
|
|
|
|
|
184
|
|
|
return $columns; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Decide columns to be sortable by array input |
|
190
|
|
|
* |
|
191
|
|
|
* @return Array |
|
192
|
|
|
*/ |
|
193
|
|
|
public function get_sortable_columns() { |
|
194
|
|
|
|
|
195
|
|
|
$sortable_columns = array( |
|
196
|
|
|
'name' => array( 'name', true ), |
|
197
|
|
|
'caseOne' => array( 'caseOne', false ), |
|
198
|
|
|
'caseTwo' => array( 'caseTwo', false ), |
|
199
|
|
|
); |
|
200
|
|
|
|
|
201
|
|
|
return $sortable_columns; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Determine bulk actions in the table dropdown |
|
207
|
|
|
* |
|
208
|
|
|
* @return Array |
|
209
|
|
|
*/ |
|
210
|
|
|
public function get_bulk_actions() { |
|
211
|
|
|
|
|
212
|
|
|
$actions = array( 'bulk-delete' => 'Delete' ); |
|
213
|
|
|
|
|
214
|
|
|
return $actions; |
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Prapare the display variables for screen options |
|
220
|
|
|
* |
|
221
|
|
|
* @return Void |
|
222
|
|
|
*/ |
|
223
|
|
|
public function prepare_items() { |
|
224
|
|
|
|
|
225
|
|
|
$this->_column_headers = $this->get_column_info(); |
|
|
|
|
|
|
226
|
|
|
|
|
227
|
|
|
/** Process bulk action */ |
|
228
|
|
|
$this->process_bulk_action(); |
|
229
|
|
|
$per_page = $this->get_items_per_page( 'option_name_per_page', 5 ); |
|
230
|
|
|
$current_page = $this->get_pagenum(); |
|
231
|
|
|
$total_items = self::record_count(); |
|
232
|
|
|
$this->_column_headers = [ |
|
233
|
|
|
$this->get_columns(), |
|
234
|
|
|
[], // hidden columns |
|
235
|
|
|
$this->get_sortable_columns(), |
|
236
|
|
|
$this->get_primary_column_name(), |
|
237
|
|
|
]; |
|
238
|
|
|
$this->set_pagination_args( array( |
|
239
|
|
|
'total_items' => $total_items, |
|
240
|
|
|
'per_page' => $per_page, |
|
241
|
|
|
) ); |
|
242
|
|
|
|
|
243
|
|
|
$this->items = self::get_Table( $per_page, $current_page ); |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
|
|
247
|
|
|
/** |
|
248
|
|
|
* Process bulk action |
|
249
|
|
|
* |
|
250
|
|
|
* @return Void |
|
251
|
|
|
*/ |
|
252
|
|
|
public function process_bulk_action() { |
|
253
|
|
|
|
|
254
|
|
|
//Detect when a bulk action is being triggered... |
|
255
|
|
|
if ( 'delete' === $this->current_action() ) { |
|
256
|
|
|
|
|
257
|
|
|
//In our file that handles the request, verify the nonce. |
|
258
|
|
|
$nonce = esc_attr( $_REQUEST['_wpnonce'] ); |
|
|
|
|
|
|
259
|
|
|
|
|
260
|
|
|
if ( ! wp_verify_nonce( $nonce, 'delete_url' ) ) { |
|
|
|
|
|
|
261
|
|
|
die( 'Go get a live script kiddies' ); |
|
|
|
|
|
|
262
|
|
|
} else { |
|
263
|
|
|
self::delete_url( absint( $_GET['instruction'] ) ); //Remember the instruction param from column_name method |
|
|
|
|
|
|
264
|
|
|
} |
|
265
|
|
|
} |
|
266
|
|
|
|
|
267
|
|
|
//If the delete bulk action is triggered |
|
268
|
|
|
if ( isset( $_POST['action'] ) ) { |
|
269
|
|
|
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) ) { |
|
270
|
|
|
$delete_ids = esc_sql( $_POST['bulk-select'] ); |
|
|
|
|
|
|
271
|
|
|
foreach ( $delete_ids as $id ) { |
|
272
|
|
|
self::delete_url( $id ); |
|
273
|
|
|
} |
|
274
|
|
|
} |
|
275
|
|
|
} |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
} ?> |
|
|
|
|
|
|
279
|
|
|
|