PluginProbe
Redirection / 2.6.3
Redirection v2.6.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.6.3, at models/pager.php

486 lines 15.3 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_Module_Table extends WP_List_Table {
422 private $token = false;
423
424 function __construct( $token ) {
425 $this->token = $token;
426
427 //Set parent defaults
428 parent::__construct( array(
429 'singular' => 'item', //singular name of the listed records
430 'plural' => 'items', //plural name of the listed records
431 'ajax' => false, //does this table support ajax?
432 ) );
433 }
434
435 function get_columns() {
436 $columns = array(
437 'name' => __( 'Module', 'redirection' ),
438 'total' => __( 'Redirects', 'redirection' ),
439 );
440
441 return $columns;
442 }
443
444 function column_name( $item ) {
445 $config = $item->get_config();
446
447 if ( $item->can_edit_config() )
448 $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() );
449
450 if ( $item->get_id() === WordPress_Module::MODULE_ID && $this->token )
451 $actions['rss'] = sprintf( '<a href="%s">RSS</a>', '?page=redirection.php&amp;token='.$this->token.'&amp;sub=rss&amp;module='.intval( $item->get_id() ) );
452
453 $actions['csv'] = sprintf( '<a href="%s">CSV</a>', '?page=redirection.php&amp;token='.$this->token.'&amp;sub=csv&amp;module='.intval( $item->get_id() ) );
454 $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' ) );
455 $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' ) );
456
457 if ( count( $config ) > 0 )
458 $config = '<div class="module-config">'.join( '<br/>', $config ).'</div>';
459 else
460 $config = '';
461
462 return '<p><strong>'.esc_html( $item->get_name() ).'</strong></p>'.$item->get_description().$config.$this->row_actions( $actions );
463 }
464
465 function column_total( $item ) {
466 return esc_html( $item->get_total_redirects() );
467 }
468
469 function prepare_items( $type = '', $id = 0 ) {
470 global $wpdb;
471
472 $options = red_get_options();
473 $columns = $this->get_columns();
474 $sortable = $this->get_sortable_columns();
475
476 $this->_column_headers = array( $columns, array(), $sortable );
477 $this->items = Red_Module::get_for_select();
478 $this->total_items = count( $this->items );
479 $this->set_pagination_args( array(
480 'total_items' => $this->total_items,
481 'per_page' => 100,
482 'total_pages' => ceil( $this->total_items / 100 ),
483 ) );
484 }
485 }
486