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