PluginProbe
Redirection / 2.4.3
Redirection v2.4.3
5.10.0 5.9.0 5.8.1 5.8.0 3.7.2 3.7.3 4.0 4.0.1 4.1 4.1.1 4.2 4.2.1 4.2.2 4.2.3 4.3 4.3.1 4.3.2 4.3.3 4.4 4.4.1 4.4.2 4.5 4.5.1 4.6.2 4.7.1 All 130 releases
redirection / models / pager.php

pager.php in Redirection 2.4.3, at models/pager.php

757 lines 24.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( !class_exists( 'WP_List_Table' ) )
4 require_once ABSPATH . 'wp-admin/includes/class-wp-list-table.php';
5
6 class Redirection_Table extends WP_List_Table {
7 private $groups;
8 private $current_group;
9 private $total_items;
10 private $current_group_id;
11
12 function __construct( array $groups, $current_group_id ) {
13 $this->groups = $groups;
14 $this->current_group_id = $current_group_id;
15
16 //Set parent defaults
17 parent::__construct( array(
18 'singular' => 'item', //singular name of the listed records
19 'plural' => 'items', //plural name of the listed records
20 'ajax' => false //does this table support ajax?
21 ) );
22 }
23
24 function get_columns(){
25 $columns = array(
26 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
27 'type' => __( 'Type', 'redirection' ),
28 'url' => __( 'URL', 'redirection' ),
29 'hits' => __( 'Hits', 'redirection' ),
30 'last_access' => __( 'Last Access', 'redirection' ),
31 );
32
33 return $columns;
34 }
35
36 function column_type( $item ) {
37 return esc_html( $item->type() );
38 }
39
40 function column_last_access( $item ) {
41 if ( $item->get_last_hit() == 0 )
42 return '&mdash;';
43 return date_i18n( get_option( 'date_format' ), $item->get_last_hit() );
44 }
45
46 function column_hits( $item ) {
47 return esc_html( number_format_i18n( $item->get_hits(), 0 ) );
48 }
49
50 function column_url( $item ) {
51 $actions = array(
52 'edit' => sprintf( '<a class="red-ajax" data-action="%s" data-nonce="%s" data-id="%s" href="#">'.__( 'Edit', 'redirection' ).'</a>', 'red_redirect_edit', wp_create_nonce( 'red-edit_'.$item->get_id() ), $item->get_id() ),
53 'delete' => sprintf( '<a class="red-auto" data-action="%s" href="#">'.__( 'Delete', 'redirection' ).'</a>', 'delete', $item->get_id() ),
54 );
55
56 $before = $after = '';
57 if ( $item->is_enabled() )
58 $actions['disable'] = sprintf( '<a class="red-auto" data-action="%s" href="#">'.__( 'Disable', 'redirection' ).'</a>', 'disable', $item->get_id() );
59 else {
60 $actions['enable'] = sprintf( '<a class="red-auto" data-action="%s" href="#">'.__( 'Enable', 'redirection' ).'</a>', 'enable', $item->get_id() );
61 $before = '<span class="red-disabled">';
62 $after = '</span>';
63 }
64
65 $title = $item->get_url();
66 if ( $item->get_title() )
67 $title = $item->get_title();
68
69 return sprintf( '%1$s %2$s', $before.'<a href="'.esc_url( $item->get_url() ).'">'.esc_html( $title ).'</a>'.$after, $this->row_actions( $actions ) );
70 }
71
72 function column_cb($item){
73 return sprintf(
74 '<input type="checkbox" name="%1$s[]" value="%2$s" />',
75 /*$1%s*/ $this->_args['singular'], //Let's simply repurpose the table's singular label ("movie")
76 /*$2%s*/ $item->get_id() //The value of the checkbox should be the record's id
77 );
78 }
79
80 function get_row( $item ) {
81 ob_start();
82
83 $this->single_row( $item );
84 $output = ob_get_contents();
85
86 ob_end_clean();
87
88 return $output;
89 }
90
91 function get_sortable_columns() {
92 $sortable_columns = array(
93 'url' => array( 'url', false),
94 );
95 return $sortable_columns;
96 }
97
98 function get_bulk_actions() {
99 $actions = array(
100 'delete' => __( 'Delete', 'redirection' ),
101 'enable' => __( 'Enable', 'redirection' ),
102 'disable' => __( 'Disable', 'redirection' ),
103 'reset' => __( 'Reset Hits', 'redirection' ),
104 );
105
106 return $actions;
107 }
108
109 function process_bulk_action() {
110 if ( !isset( $_POST['item'] ) )
111 return;
112
113 if ( in_array( $this->current_action(), array( 'reset', 'enable', 'disable', 'delete' ) ) ) {
114 $redirections = array();
115 $flush = array();
116
117 foreach( (array)$_POST['item'] AS $id ) {
118 $redirect = Red_Item::get_by_id( intval( $id ) );
119
120 if ( $redirect ) {
121 if ( $this->current_action() === 'reset' )
122 $redirect->reset();
123 elseif ( $this->current_action() === 'enable' ) {
124 $redirect->enable();
125 $flush[] = $redirect->get_group_id();
126 }
127 elseif ( $this->current_action() === 'disable' ) {
128 $redirect->disable();
129 $flush[] = $redirect->get_group_id();
130 }
131 elseif ( $this->current_action() === 'delete' )
132 $redirect->delete();
133 }
134 }
135
136 $flush = array_unique( $flush );
137 foreach ( $flush AS $group_id ) {
138 Red_Module::flush( $group_id );
139 }
140 }
141 }
142
143 function extra_tablenav( $which ) {
144 if ( $which == 'bottom' )
145 return;
146
147 ?>
148 <div class="alignleft actions">
149 <select name="id">
150 <option value="0"<?php selected( 0, $this->current_group_id ); ?>><?php _e( 'No group filter', 'redirection' ); ?></option>
151
152 <?php foreach ( $this->groups AS $module_name => $groups ) : ?>
153 <optgroup label="<?php echo esc_attr( $module_name ); ?>">
154 <?php foreach ( $groups AS $group_id => $group ) : ?>
155 <option value="<?php echo esc_attr( $group_id ); ?>"<?php selected( $group_id, $this->current_group_id ); ?>>
156 <?php echo esc_html( $group ); ?>
157 </option>
158 <?php endforeach; ?>
159 </optgroup>
160 <?php endforeach; ?>
161 </select>
162
163 <?php submit_button( __( 'Filter' ), 'button', false, false, array( 'id' => 'post-query-submit' ) ); ?>
164 </div>
165 <?php
166 }
167
168 function prepare_items( $type = '', $id = 0 ) {
169 global $wpdb, $current_user;
170
171 $screen = get_current_screen();
172
173 $per_page = 25;
174 if ( $screen && $screen->get_option( 'per_page', 'option' ) ) {
175 $per_page = intval( get_user_meta( $current_user->ID, $screen->get_option( 'per_page', 'option' ), true ) );
176 if ( $per_page === 0 )
177 $per_page = 25;
178 }
179
180 $columns = $this->get_columns();
181 $sortable = $this->get_sortable_columns();
182
183 $this->_column_headers = array( $columns, array(), $sortable );
184
185 // Process any stuff
186 $this->process_bulk_action();
187
188 $orderby = ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id';
189 $order = ( ! empty( $_GET['order'] ) ) ? strtolower( $_GET['order'] ) : 'desc';
190
191 if ( !in_array( $orderby, array_keys( $sortable ) ) )
192 $orderby = 'id';
193
194 if ( !in_array( $order, array( 'asc', 'desc' ) ) )
195 $order = 'desc';
196
197 $where = array();
198 if ( isset( $_GET['s'] ) && strlen( $_GET['s'] ) > 0 )
199 $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_GET['s'] ).'%' );
200
201 if ( isset( $_REQUEST['id'] ) && intval( $_REQUEST['id'] ) > 0 )
202 $where[] = $wpdb->prepare( "group_id=%d", intval( $_REQUEST['id'] ) );
203
204 $where_cond = "";
205 if ( count( $where ) > 0 )
206 $where_cond = " WHERE ".implode( ' AND ', $where );
207
208 $table = $wpdb->prefix.'redirection_items';
209 $rows = $wpdb->get_results( "SELECT * FROM {$table} ".$where_cond.$wpdb->prepare( " ORDER BY $orderby $order LIMIT %d,%d", ( $this->get_pagenum() - 1 ) * $per_page, $per_page ) );
210 $this->total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$table}".$where_cond );
211
212 $this->items = array();
213 foreach ( (array)$rows AS $row ) {
214 $this->items[] = new Red_Item( $row );
215 }
216
217 $this->set_pagination_args( array(
218 'total_items' => $this->total_items,
219 'per_page' => $per_page,
220 'total_pages' => ceil( $this->total_items / $per_page )
221 ) );
222 }
223 }
224
225 class Redirection_Group_Table extends WP_List_Table {
226 private $modules;
227
228 function __construct( $modules ) {
229 $this->modules = $modules;
230
231 //Set parent defaults
232 parent::__construct( array(
233 'singular' => 'item', //singular name of the listed records
234 'plural' => 'items', //plural name of the listed records
235 'ajax' => false //does this table support ajax?
236 ) );
237 }
238
239 function get_columns(){
240 $columns = array(
241 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
242 'name' => __( 'Name', 'redirection' ),
243 'redirects' => __( 'Redirects', 'redirection' ),
244 'module' => __( 'Module', 'redirection' ),
245 );
246
247 return $columns;
248 }
249
250 function column_name( $item ) {
251 $actions = array(
252 'edit' => sprintf( '<a class="red-ajax" data-action="%s" data-nonce="%s" data-id="%s" href="#">'.__( 'Edit', 'redirection' ).'</a>', 'red_group_edit', wp_create_nonce( 'red-edit_'.$item->get_id() ), $item->get_id() ),
253 'delete' => sprintf( '<a class="red-auto" data-action="%s" href="#">'.__( 'Delete', 'redirection' ).'</a>', 'delete', $item->get_id() ),
254 'view' => '<a href="tools.php?page=redirection.php&amp;id='.$item->get_id().'">'.__( 'View Redirects', 'redirection' ).'</a>',
255 );
256
257 $after = $before = '';
258 if ( $item->is_enabled() )
259 $actions['disable'] = sprintf( '<a class="red-auto" data-action="%s" href="#">'.__( 'Disable', 'redirection' ).'</a>', 'disable', $item->get_id() );
260 else {
261 $actions['enable'] = sprintf( '<a class="red-auto" data-action="%s" href="#">'.__( 'Enable', 'redirection' ).'</a>', 'enable', $item->get_id() );
262 $before = '<span class="red-disabled">';
263 $after = '</span>';
264 }
265
266 return sprintf( '%1$s %2$s', $before.esc_html( $item->get_name() ).$after, $this->row_actions( $actions ) );
267 }
268
269 function column_redirects( $item ) {
270 return esc_html( $item->get_total_redirects() );
271 }
272
273 function column_module( $item ) {
274 $module = Red_Module::get( $item->get_module_id() );
275
276 if ( $module )
277 return esc_html( $module->get_name() );
278 return esc_html( __( 'Unknown', 'redirection' ) );
279 }
280
281 function column_cb($item){
282 return sprintf(
283 '<input type="checkbox" name="%1$s[]" value="%2$s" />',
284 /*$1%s*/ $this->_args['singular'], //Let's simply repurpose the table's singular label ("movie")
285 /*$2%s*/ $item->id //The value of the checkbox should be the record's id
286 );
287 }
288
289 function get_sortable_columns() {
290 $sortable_columns = array(
291 'name' => array( 'name', false ),
292 );
293
294 return $sortable_columns;
295 }
296
297 function get_bulk_actions() {
298 $actions = array(
299 'delete' => __( 'Delete', 'redirection' ),
300 'enable' => __( 'Enable', 'redirection' ),
301 'disable' => __( 'Disable', 'redirection' ),
302 );
303
304 return $actions;
305 }
306
307 function process_bulk_action() {
308 if ( !isset( $_POST['item'] ) )
309 return;
310
311 if ( in_array( $this->current_action(), array( 'delete', 'enable', 'disable' ) ) ) {
312 $groups = array();
313
314 foreach( (array)$_POST['item'] AS $id ) {
315 $group = Red_Group::get( intval( $id ) );
316
317 if ( $group ) {
318 if ( $this->current_action() === 'delete' )
319 $group->delete();
320 else if ( $this->current_action() === 'enable' ) {
321 $group->enable();
322 Red_Module::flush( $group->get_id() );
323 }
324 else if ( $this->current_action() === 'disable' ) {
325 $group->disable();
326 Red_Module::flush( $group->get_id() );
327 }
328 }
329 }
330 }
331 }
332
333 private function delete( $item ) {
334 $item->delete();
335 }
336
337 private function enable( $item ) {
338 $item->enable();
339 }
340
341 private function disable( $item ) {
342 $item->disable();
343 }
344
345 function extra_tablenav( $which ) {
346 if ( $which == 'bottom' )
347 return;
348
349 $selected = 0;
350 if ( isset( $_POST['id'] ) )
351 $selected = intval( $_POST['id'] );
352 ?>
353 <div class="alignleft actions">
354 <select name="id">
355 <option value="0"<?php selected( 0, $selected ); ?>><?php _e( 'All modules', 'redirection' ); ?></option>
356 <?php foreach ( $this->modules AS $module_id => $module ) : ?>
357 <option value="<?php echo esc_attr( $module_id ); ?>"<?php selected( $module_id, $selected ); ?>>
358 <?php echo esc_html( $module->get_name() ); ?>
359 </option>
360 <?php endforeach; ?>
361 </select>
362
363 <?php submit_button( __( 'Filter' ), 'button', false, false, array( 'id' => 'post-query-submit' ) ); ?>
364 </div>
365 <?php
366 }
367
368 function prepare_items( $type = '', $id = 0 ) {
369 global $wpdb, $current_user;
370
371 $screen = get_current_screen();
372 $per_page = get_user_meta( $current_user->ID, $screen->get_option( 'per_page', 'option' ), true );
373
374 $per_page = $per_page ? $per_page : 25;
375 $columns = $this->get_columns();
376 $sortable = $this->get_sortable_columns();
377
378 $this->_column_headers = array( $columns, array(), $sortable );
379
380 // Process any stuff
381 $this->process_bulk_action();
382
383 $orderby = ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id';
384 $order = ( ! empty( $_GET['order'] ) ) ? strtolower( $_GET['order'] ) : 'desc';
385
386 if ( !in_array( $orderby, array_keys( $sortable ) ) )
387 $orderby = $wpdb->prefix.'redirection_groups.name';
388
389 if ( !in_array( $order, array( 'asc', 'desc' ) ) )
390 $order = 'desc';
391
392 $where = array();
393 if ( isset( $_GET['s'] ) && strlen( $_GET['s'] ) > 0 )
394 $where[] = $wpdb->prepare( 'name LIKE %s', '%'.$wpdb->esc_like( $_GET['s'] ).'%' );
395
396 if ( isset( $_REQUEST['id'] ) && intval( $_REQUEST['id'] ) > 0 )
397 $where[] = $wpdb->prepare( "module_id=%d", intval( $_REQUEST['id'] ) );
398
399 $where_cond = "";
400 if ( count( $where ) > 0 )
401 $where_cond = " WHERE ".implode( ' AND ', $where );
402
403 $table = $wpdb->prefix.'redirection_groups';
404 $rows = $wpdb->get_results( "SELECT * FROM {$table} ".$where_cond.$wpdb->prepare( " ORDER BY $orderby $order LIMIT %d,%d", ( $this->get_pagenum() - 1 ) * $per_page, $per_page ) );
405 $this->total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$table}".$where_cond );
406
407 $this->items = array();
408 foreach ( (array)$rows AS $row ) {
409 $this->items[] = new Red_Group( $row );
410 }
411
412 $this->set_pagination_args( array(
413 'total_items' => $this->total_items,
414 'per_page' => $per_page,
415 'total_pages' => ceil( $this->total_items / $per_page )
416 ) );
417 }
418 }
419
420 class Redirection_Log_Table extends WP_List_Table {
421 const REFERRER_MAX = 120;
422 const TARGET_MAX = 80;
423 private $lookup;
424
425 function __construct( $options ) {
426 $this->lookup = $options['lookup'];
427
428 //Set parent defaults
429 parent::__construct( array(
430 'singular' => 'item', //singular name of the listed records
431 'plural' => 'items', //plural name of the listed records
432 'ajax' => false //does this table support ajax?
433 ) );
434 }
435
436 function column_created( $item ) {
437 $actions = array();
438
439 if ( $item->sent_to == '' ) {
440 $actions['add'] = '<a href="'.esc_url( $item->url ).'" class="add-log">'.__( 'Add redirect', 'redirection' ).'</a>';
441 }
442
443 return sprintf( '%1$s %2$s', date_i18n( get_option( 'date_format' ), $item->created ).' '.gmdate( get_option( 'time_format' ), $item->created ), $this->row_actions( $actions ) );
444 }
445
446 function column_ip( $item ) {
447 return '<a href="'.esc_attr( $this->lookup ).esc_attr( $item->ip ).'">'.esc_html( $item->ip ).'</a>';
448 }
449
450 function column_url( $item ) {
451 $actions = array(
452 'target' => esc_html( substr( $item->sent_to, 0, self::TARGET_MAX ) ),
453 );
454
455 return sprintf( '%1$s %2$s', '<a href="'.esc_url( $item->url ).'">'.esc_html( $item->show_url( $item->url ) ).'</a>', $this->row_actions( $actions ) );
456 }
457
458 function column_referrer( $item ) {
459 $actions = array(
460 'agent' => esc_html( substr( $item->agent, 0, self::REFERRER_MAX ) ),
461 );
462
463 return sprintf( '%1$s %2$s', '<a href="'.esc_url( $item->referrer ).'">'.esc_html( parse_url( $item->referrer, PHP_URL_HOST ) ).'</a>', $this->row_actions( $actions ) );
464 }
465
466 function column_cb($item){
467 return sprintf(
468 '<input type="checkbox" name="%1$s[]" value="%2$s" />',
469 /*$1%s*/ $this->_args['singular'], //Let's simply repurpose the table's singular label ("movie")
470 /*$2%s*/ $item->id //The value of the checkbox should be the record's id
471 );
472 }
473
474 function get_columns(){
475 $columns = array(
476 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
477 'created' => __( 'Date', 'redirection' ),
478 'url' => __( 'Source URL', 'redirection' ),
479 'referrer' => __( 'Referrer', 'redirection' ),
480 'ip' => __( 'IP', 'redirection' ),
481 );
482 return $columns;
483 }
484
485 function get_sortable_columns() {
486 $sortable_columns = array(
487 'created' => array( 'id', true ),
488 'url' => array( 'url', false),
489 'referrer' => array( 'referrer', false ),
490 'ip' => array( 'item_id', false ),
491 );
492 return $sortable_columns;
493 }
494
495 function get_bulk_actions() {
496 $actions = array(
497 'delete' => __( 'Delete', 'redirection' )
498 );
499 return $actions;
500 }
501
502 function process_bulk_action() {
503 if ( 'delete' === $this->current_action() ) {
504 foreach( $_POST['item'] AS $id ) {
505 RE_Log::delete( intval( $id ) );
506 }
507 }
508 }
509
510 function prepare_items( $type = '', $id = 0 ) {
511 global $wpdb, $current_user;
512
513 $screen = get_current_screen();
514 $per_page = get_user_meta( $current_user->ID, $screen->get_option( 'per_page', 'option' ), true );
515
516 $per_page = $per_page ? $per_page : 25;
517 $columns = $this->get_columns();
518 $sortable = $this->get_sortable_columns();
519
520 $this->_column_headers = array( $columns, array(), $sortable );
521
522 // Process any stuff
523 $this->process_bulk_action();
524
525 $orderby = ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id';
526 $order = ( ! empty( $_GET['order'] ) ) ? strtolower( $_GET['order'] ) : 'desc';
527
528 if ( !in_array( $orderby, array_keys( $sortable ) ) )
529 $orderby = 'id';
530
531 if ( !in_array( $order, array( 'asc', 'desc' ) ) )
532 $order = 'desc';
533
534 $where = array();
535 if ( isset( $_GET['s'] ) && strlen( $_GET['s'] ) > 0 )
536 $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_GET['s'] ).'%' );
537
538 $where_cond = "";
539 if ( count( $where ) > 0 )
540 $where_cond = " WHERE ".implode( ' AND ', $where );
541
542 $table = $wpdb->prefix.'redirection_logs';
543 $rows = $wpdb->get_results( "SELECT * FROM {$table} ".$where_cond.$wpdb->prepare( " ORDER BY $orderby $order LIMIT %d,%d", ( $this->get_pagenum() - 1 ) * $per_page, $per_page ) );
544 $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$table}".$where_cond );
545
546 $this->items = array();
547 foreach ( (array)$rows AS $row ) {
548 $this->items[] = new RE_Log( $row );
549 }
550
551 $this->set_pagination_args( array(
552 'total_items' => $total_items,
553 'per_page' => $per_page,
554 'total_pages' => ceil( $total_items / $per_page )
555 ) );
556 }
557 }
558
559 class Redirection_404_Table extends WP_List_Table {
560 private $lookup;
561
562 function __construct( $options ) {
563 $this->lookup = $options['lookup'];
564
565 //Set parent defaults
566 parent::__construct( array(
567 'singular' => 'item', //singular name of the listed records
568 'plural' => 'items', //plural name of the listed records
569 'ajax' => false //does this table support ajax?
570 ) );
571 }
572
573 function column_created( $item ) {
574 $actions['add'] = '<a href="'.esc_url( $item->url ).'" class="add-log">'.__( 'Add redirect', 'redirection' ).'</a>';
575
576 return sprintf( '%1$s%2$s', date_i18n( get_option( 'date_format' ), $item->created ).'<br/>'.gmdate( get_option( 'time_format' ), $item->created ), $this->row_actions( $actions ) );
577 }
578
579 function column_ip( $item ) {
580 $actions['add'] = '<a href="'.admin_url( 'tools.php?page=redirection.php&sub=404s&ip='.esc_attr( long2ip( $item->ip ) ) ).'">'.__( 'Show only this IP', 'redirection' ).'</a>';
581
582 return sprintf( '%1$s %2$s', '<a href="'.esc_attr( $this->lookup ).esc_attr( long2ip( $item->ip ) ).'">'.long2ip( $item->ip ).'</a>', $this->row_actions( $actions ) );
583 }
584
585 function column_url( $item ) {
586 return '<a href="'.esc_url( $item->url ).'">'.esc_html( $item->show_url( $item->url ) ).'</a>';
587 }
588
589 function column_referrer( $item ) {
590 $actions = array(
591 'agent' => esc_html( $item->agent ),
592 );
593
594 return sprintf( '%1$s %2$s', '<a href="'.esc_url( $item->referrer ).'">'.esc_html( parse_url( $item->referrer, PHP_URL_HOST ) ).'</a>', $this->row_actions( $actions ) );
595 }
596
597 function column_cb($item){
598 return sprintf(
599 '<input type="checkbox" name="%1$s[]" value="%2$s" />',
600 /*$1%s*/ $this->_args['singular'], //Let's simply repurpose the table's singular label ("movie")
601 /*$2%s*/ $item->id //The value of the checkbox should be the record's id
602 );
603 }
604
605 function get_columns(){
606 $columns = array(
607 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text
608 'created' => __( 'Date', 'redirection' ),
609 'url' => __( 'Source URL', 'redirection' ),
610 'referrer' => __( 'Referrer', 'redirection' ),
611 'ip' => __( 'IP', 'redirection' ),
612 );
613 return $columns;
614 }
615
616 function get_sortable_columns() {
617 $sortable_columns = array(
618 'created' => array( 'id', true ),
619 'url' => array( 'url', false),
620 'referrer' => array( 'referrer', false ),
621 );
622 return $sortable_columns;
623 }
624
625 function get_bulk_actions() {
626 $actions = array(
627 'delete' => __( 'Delete', 'redirection' )
628 );
629 return $actions;
630 }
631
632 function process_bulk_action() {
633 if ( 'delete' === $this->current_action() ) {
634 foreach( $_POST['item'] AS $id ) {
635 RE_404::delete( intval( $id ) );
636 }
637 }
638 }
639
640 function prepare_items( $restrict_by_ip = false ) {
641 global $wpdb, $current_user;
642
643 $screen = get_current_screen();
644 $per_page = get_user_meta( $current_user->ID, $screen->get_option( 'per_page', 'option' ), true );
645
646 $per_page = $per_page ? $per_page : 25;
647 $columns = $this->get_columns();
648 $sortable = $this->get_sortable_columns();
649
650 $this->_column_headers = array( $columns, array(), $sortable );
651
652 // Process any stuff
653 $this->process_bulk_action();
654
655 $orderby = ( ! empty( $_GET['orderby'] ) ) ? $_GET['orderby'] : 'id';
656 $order = ( ! empty( $_GET['order'] ) ) ? strtolower( $_GET['order'] ) : 'desc';
657
658 if ( !in_array( $orderby, array_keys( $sortable ) ) )
659 $orderby = 'id';
660
661 if ( !in_array( $order, array( 'asc', 'desc' ) ) )
662 $order = 'desc';
663
664 $where = array();
665 if ( isset( $_GET['s'] ) && strlen( $_GET['s'] ) > 0 )
666 $where[] = $wpdb->prepare( 'url LIKE %s', '%'.$wpdb->esc_like( $_GET['s'] ).'%' );
667
668 if ( $restrict_by_ip !== false )
669 $where[] = $wpdb->prepare( 'ip=INET_ATON(%s)', $restrict_by_ip );
670
671 $where_cond = "";
672 if ( count( $where ) > 0 )
673 $where_cond = " WHERE ".implode( ' AND ', $where );
674
675 $table = $wpdb->prefix.'redirection_404';
676 $rows = $wpdb->get_results( "SELECT * FROM {$table} ".$where_cond.$wpdb->prepare( " ORDER BY $orderby $order LIMIT %d,%d", ( $this->get_pagenum() - 1 ) * $per_page, $per_page ) );
677 $total_items = $wpdb->get_var( "SELECT COUNT(*) FROM {$table}".$where_cond );
678
679 $this->items = array();
680 foreach ( (array)$rows AS $row ) {
681 $this->items[] = new RE_Log( $row );
682 }
683
684 $this->set_pagination_args( array(
685 'total_items' => $total_items,
686 'per_page' => $per_page,
687 'total_pages' => ceil( $total_items / $per_page )
688 ) );
689 }
690 }
691
692 class Redirection_Module_Table extends WP_List_Table {
693 private $token = false;
694
695 function __construct( $token ) {
696 $this->token = $token;
697
698 //Set parent defaults
699 parent::__construct( array(
700 'singular' => 'item', //singular name of the listed records
701 'plural' => 'items', //plural name of the listed records
702 'ajax' => false //does this table support ajax?
703 ) );
704 }
705
706 function get_columns() {
707 $columns = array(
708 'name' => __( 'Module', 'redirection' ),
709 'total' => __( 'Redirects', 'redirection' ),
710 );
711
712 return $columns;
713 }
714
715 function column_name( $item ) {
716 $config = $item->get_config();
717
718 if ( $item->can_edit_config() )
719 $actions['edit'] = sprintf( '<a href="#" class="red-ajax" data-action="%s" data-nonce="%s" data-id="%s">'.__( 'Configure', 'redirection' ).'</a>', 'red_module_edit', wp_create_nonce( 'red_edit-'.$item->get_id() ), $item->get_id() );
720
721 if ( $item->get_id() === WordPress_Module::MODULE_ID && $this->token )
722 $actions['rss'] = sprintf( '<a href="%s">RSS</a>', '?page=redirection.php&amp;token='.$this->token.'&amp;sub=rss&amp;module='.intval( $item->get_id() ) );
723
724 $actions['csv'] = sprintf( '<a href="%s">CSV</a>', '?page=redirection.php&amp;token='.$this->token.'&amp;sub=csv&amp;module='.intval( $item->get_id() ) );
725 $actions['view-htaccess'] = sprintf( '<a href="#" class="red-ajax" data-id="%d" data-action="red_get_htaccess" data-nonce="%s">.htaccess</a>', $item->get_id(), wp_create_nonce( 'red_get_htaccess' ) );
726 $actions['view-nginx'] = sprintf( '<a href="#" class="red-ajax" data-id="%d" data-action="red_get_nginx" data-nonce="%s">Nginx</a>', $item->get_id(), wp_create_nonce( 'red_get_nginx' ) );
727
728 if ( count( $config ) > 0 )
729 $config = '<div class="module-config">'.join( '<br/>', $config ).'</div>';
730 else
731 $config = '';
732
733 return '<p><strong>'.esc_html( $item->get_name() ).'</strong></p>'.$item->get_description().$config.$this->row_actions( $actions );
734 }
735
736 function column_total( $item ) {
737 return esc_html( $item->get_total_redirects() );
738 }
739
740 function prepare_items( $type = '', $id = 0 ) {
741 global $wpdb;
742
743 $options = red_get_options();
744 $columns = $this->get_columns();
745 $sortable = $this->get_sortable_columns();
746
747 $this->_column_headers = array( $columns, array(), $sortable );
748 $this->items = Red_Module::get_for_select();
749 $this->total_items = count( $this->items );
750 $this->set_pagination_args( array(
751 'total_items' => $this->total_items,
752 'per_page' => 100,
753 'total_pages' => ceil( $this->total_items / 100 )
754 ) );
755 }
756 }
757