| 1 |
<?php |
| 2 |
|
| 3 |
if(!class_exists('WP_List_Table')){ |
| 4 |
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); // since WP 3.1 |
| 5 |
} |
| 6 |
|
| 7 |
/* |
| 8 |
* a class to create the languages table in Polylang settings |
| 9 |
* Thanks to Matt Van Andel (http://www.mattvanandel.com) for its plugin "Custom List Table Example" ! |
| 10 |
* |
| 11 |
* @since 0.1 |
| 12 |
*/ |
| 13 |
class PLL_Table_Languages extends WP_List_Table { |
| 14 |
|
| 15 |
/* |
| 16 |
* constructor |
| 17 |
* |
| 18 |
* @since 0.1 |
| 19 |
*/ |
| 20 |
function __construct() { |
| 21 |
parent::__construct(array( |
| 22 |
'plural' => 'Languages', // do not translate (used for css class) |
| 23 |
'ajax' => false |
| 24 |
)); |
| 25 |
} |
| 26 |
|
| 27 |
/* |
| 28 |
* displays the item information in a column (default case) |
| 29 |
* |
| 30 |
* @since 0.1 |
| 31 |
* |
| 32 |
* @param object $item |
| 33 |
* @param string $column_name |
| 34 |
* @return string |
| 35 |
*/ |
| 36 |
function column_default($item, $column_name) { |
| 37 |
return $item->$column_name; |
| 38 |
} |
| 39 |
|
| 40 |
/* |
| 41 |
* displays the item information in the column 'name' |
| 42 |
* displays the edit and delete links |
| 43 |
* |
| 44 |
* @since 0.1 |
| 45 |
* |
| 46 |
* @param object $item |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
function column_name($item) { |
| 50 |
return $item->name . $this->row_actions(array( |
| 51 |
'edit' => sprintf( |
| 52 |
'<a href="%s">%s</a>', |
| 53 |
esc_url(admin_url('admin.php?page=mlang&pll_action=edit&lang=' . $item->term_id)), |
| 54 |
__('Edit','polylang') |
| 55 |
), |
| 56 |
'delete' => sprintf( |
| 57 |
'<a href="%s" onclick = "return confirm(\'%s\');">%s</a>', |
| 58 |
wp_nonce_url('?page=mlang&pll_action=delete&noheader=true&lang=' . $item->term_id, 'delete-lang'), |
| 59 |
__('You are about to permanently delete this language. Are you sure?', 'polylang'), |
| 60 |
__('Delete','polylang') |
| 61 |
) |
| 62 |
)); |
| 63 |
} |
| 64 |
|
| 65 |
/* |
| 66 |
* gets the list of columns |
| 67 |
* |
| 68 |
* @since 0.1 |
| 69 |
* |
| 70 |
* @return array the list of column titles |
| 71 |
*/ |
| 72 |
function get_columns() { |
| 73 |
return array( |
| 74 |
'name' => __('Full name', 'polylang'), |
| 75 |
'locale' => __('Locale', 'polylang'), |
| 76 |
'slug' => __('Code', 'polylang'), |
| 77 |
'term_group' => __('Order', 'polylang'), |
| 78 |
'flag' => __('Flag', 'polylang'), |
| 79 |
'count' => __('Posts', 'polylang') |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
/* |
| 84 |
* gets the list of sortable columns |
| 85 |
* |
| 86 |
* @since 0.1 |
| 87 |
* |
| 88 |
* @return array |
| 89 |
*/ |
| 90 |
function get_sortable_columns() { |
| 91 |
return array( |
| 92 |
'name' => array('name', true), // sorted by name by default |
| 93 |
'locale' => array('locale', false), |
| 94 |
'slug' => array('slug', false), |
| 95 |
'term_group' => array('term_group', false), |
| 96 |
'count' => array('count', false) |
| 97 |
); |
| 98 |
} |
| 99 |
|
| 100 |
/* |
| 101 |
* prepares the list of items for displaying |
| 102 |
* |
| 103 |
* @since 0.1 |
| 104 |
* |
| 105 |
* @param array $data |
| 106 |
*/ |
| 107 |
function prepare_items($data = array()) { |
| 108 |
$per_page = $this->get_items_per_page('pll_lang_per_page'); |
| 109 |
$this->_column_headers = array($this->get_columns(), array(), $this->get_sortable_columns()); |
| 110 |
|
| 111 |
function usort_reorder($a, $b){ |
| 112 |
$orderby = !empty($_REQUEST['orderby']) ? $_REQUEST['orderby'] : 'name'; |
| 113 |
$result = strcmp($a->$orderby, $b->$orderby); // determine sort order |
| 114 |
return (empty($_REQUEST['order']) || $_REQUEST['order'] == 'asc') ? $result : -$result; // send final sort direction to usort |
| 115 |
}; |
| 116 |
|
| 117 |
usort($data, 'usort_reorder'); |
| 118 |
|
| 119 |
$total_items = count($data); |
| 120 |
$this->items = array_slice($data, ($this->get_pagenum() - 1) * $per_page, $per_page); |
| 121 |
|
| 122 |
$this->set_pagination_args(array( |
| 123 |
'total_items' => $total_items, |
| 124 |
'per_page' => $per_page, |
| 125 |
'total_pages' => ceil($total_items/$per_page) |
| 126 |
)); |
| 127 |
} |
| 128 |
} |
| 129 |
|