| 1 |
<?php |
| 2 |
|
| 3 |
// Thanks to Matt Van Andel (http://www.mattvanandel.com) for most of this code ! |
| 4 |
|
| 5 |
if(!class_exists('WP_List_Table')){ |
| 6 |
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); |
| 7 |
} |
| 8 |
|
| 9 |
class Polylang_List_Table extends WP_List_Table { |
| 10 |
function __construct() { |
| 11 |
parent::__construct( array( |
| 12 |
'singular' => __('Language','polylang'), |
| 13 |
'plural' => __('Languages','polylang'), |
| 14 |
'ajax'=> false) |
| 15 |
); |
| 16 |
} |
| 17 |
|
| 18 |
function column_default( $item, $column_name){ |
| 19 |
switch($column_name){ |
| 20 |
case 'description': |
| 21 |
case 'slug': |
| 22 |
case 'count': |
| 23 |
return $item[$column_name]; |
| 24 |
default: |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
function column_name($item){ |
| 29 |
$actions = array( |
| 30 |
'edit' => sprintf('<a href="?page=mlang&action=edit&lang=%s">%s</a>',$item['term_id'],__('Edit','polylang')), |
| 31 |
'delete' => '<a href="' . wp_nonce_url('?page=mlang&action=delete&noheader=true&lang=' . $item['term_id'], 'delete-lang') .'">' . __('Delete','polylang') .'</a>' |
| 32 |
); |
| 33 |
|
| 34 |
return $item['name'].$this->row_actions($actions); |
| 35 |
} |
| 36 |
|
| 37 |
function column_cb($item){ |
| 38 |
return sprintf( |
| 39 |
'<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 40 |
/*$1%s*/ $this->_args['singular'], |
| 41 |
/*$2%s*/ $item['term_id'] |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
function get_columns(){ |
| 46 |
$columns = array( |
| 47 |
'cb' => '<input type="checkbox" />', //Render a checkbox instead of text |
| 48 |
'name' => __('Full name', 'polylang'), |
| 49 |
'description' => __('Locale', 'polylang'), |
| 50 |
'slug' => __('Code', 'polylang'), |
| 51 |
'count' => __('Posts', 'polylang') |
| 52 |
); |
| 53 |
return $columns; |
| 54 |
} |
| 55 |
|
| 56 |
function get_sortable_columns() { |
| 57 |
$sortable_columns = array( |
| 58 |
'name' => array('name',true), // sorted by name by default |
| 59 |
'description' => array('description',false), |
| 60 |
'slug' => array('slug',false), |
| 61 |
'count' => array('count',false) |
| 62 |
); |
| 63 |
return $sortable_columns; |
| 64 |
} |
| 65 |
|
| 66 |
/* |
| 67 |
function get_bulk_actions() { |
| 68 |
return array('delete' => 'Delete'); |
| 69 |
} |
| 70 |
*/ |
| 71 |
|
| 72 |
function prepare_items($data = array()) { |
| 73 |
$per_page = 5; // 5 languages per page |
| 74 |
$columns = $this->get_columns(); |
| 75 |
$hidden = array(); |
| 76 |
$sortable = $this->get_sortable_columns(); |
| 77 |
|
| 78 |
$this->_column_headers = array($columns, $hidden, $sortable); |
| 79 |
|
| 80 |
function usort_reorder($a,$b){ |
| 81 |
$orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'name'; //If no sort, default to title |
| 82 |
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc |
| 83 |
$result = strcmp($a[$orderby], $b[$orderby]); //Determine sort order |
| 84 |
return ($order==='asc') ? $result : -$result; //Send final sort direction to usort |
| 85 |
} |
| 86 |
usort($data, 'usort_reorder'); |
| 87 |
|
| 88 |
$current_page = $this->get_pagenum(); |
| 89 |
$total_items = count($data); |
| 90 |
$data = array_slice($data,(($current_page-1)*$per_page),$per_page); |
| 91 |
$this->items = $data; |
| 92 |
|
| 93 |
$this->set_pagination_args( array( |
| 94 |
'total_items' => $total_items, //WE have to calculate the total number of items |
| 95 |
'per_page' => $per_page, //WE have to determine how many items to show on a page |
| 96 |
'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages |
| 97 |
) ); |
| 98 |
} |
| 99 |
} |
| 100 |
?> |
| 101 |
|