PluginProbe
Redirection / 2.4.5
Redirection v2.4.5
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.5, at models/pager.php

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