| 1 |
<?php |
| 2 |
|
| 3 |
// Thanks to Matt Van Andel (http://www.mattvanandel.com) for its plugin "Custom List Table Example" ! |
| 4 |
|
| 5 |
if(!class_exists('WP_List_Table')){ |
| 6 |
require_once( ABSPATH . 'wp-admin/includes/class-wp-list-table.php' ); // since WP 3.1 |
| 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 |
return $item[$column_name]; |
| 20 |
} |
| 21 |
|
| 22 |
function column_name($item) { |
| 23 |
$edit_link = esc_url(admin_url('admin.php?page=mlang&action=edit&lang='.$item['term_id'])); |
| 24 |
$delete_link = wp_nonce_url('?page=mlang&action=delete&noheader=true&lang=' . $item['term_id'], 'delete-lang'); |
| 25 |
$actions = array( |
| 26 |
'edit' => '<a href="' . $edit_link . '">' . __('Edit','polylang') . '</a>', |
| 27 |
'delete' => '<a href="' . $delete_link .'">' . __('Delete','polylang') .'</a>' |
| 28 |
); |
| 29 |
return $item['name'].$this->row_actions($actions); |
| 30 |
} |
| 31 |
|
| 32 |
function column_cb($item) { |
| 33 |
return sprintf( |
| 34 |
'<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 35 |
/*$1%s*/ esc_attr($this->_args['singular']), |
| 36 |
/*$2%s*/ esc_attr($item['term_id']) |
| 37 |
); |
| 38 |
} |
| 39 |
|
| 40 |
function get_columns() { |
| 41 |
$columns = array( |
| 42 |
// FIXME checkboxes are useles for now |
| 43 |
// 'cb' => '<input type="checkbox" />', //Render a checkbox instead of text |
| 44 |
'name' => __('Full name', 'polylang'), |
| 45 |
'description' => __('Locale', 'polylang'), |
| 46 |
'slug' => __('Code', 'polylang'), |
| 47 |
'term_group' => __('Order', 'polylang'), |
| 48 |
'flag' => __('Flag', 'polylang'), |
| 49 |
'count' => __('Posts', 'polylang') |
| 50 |
); |
| 51 |
return $columns; |
| 52 |
} |
| 53 |
|
| 54 |
function get_sortable_columns() { |
| 55 |
$sortable_columns = array( |
| 56 |
'name' => array('name', true), // sorted by name by default |
| 57 |
'description' => array('description', false), |
| 58 |
'slug' => array('slug', false), |
| 59 |
'term_group' => array('term_group', false), |
| 60 |
'count' => array('count', false) |
| 61 |
); |
| 62 |
return $sortable_columns; |
| 63 |
} |
| 64 |
|
| 65 |
/* |
| 66 |
function get_bulk_actions() { |
| 67 |
return array('delete' => 'Delete'); |
| 68 |
} |
| 69 |
*/ |
| 70 |
|
| 71 |
function prepare_items($data = array()) { |
| 72 |
$per_page = 10; // 10 languages per page |
| 73 |
$columns = $this->get_columns(); |
| 74 |
$hidden = array(); |
| 75 |
$sortable = $this->get_sortable_columns(); |
| 76 |
$this->_column_headers = array($columns, $hidden, $sortable); |
| 77 |
|
| 78 |
function usort_reorder($a,$b){ |
| 79 |
$orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'name'; // if no sort, default to name |
| 80 |
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; // if no order, default to asc |
| 81 |
$result = strcmp($a[$orderby], $b[$orderby]); // determine sort order |
| 82 |
return ($order==='asc') ? $result : -$result; // send final sort direction to usort |
| 83 |
} |
| 84 |
usort($data, 'usort_reorder'); |
| 85 |
|
| 86 |
$current_page = $this->get_pagenum(); |
| 87 |
$total_items = count($data); |
| 88 |
$data = array_slice($data,(($current_page-1)*$per_page),$per_page); |
| 89 |
$this->items = $data; |
| 90 |
|
| 91 |
$this->set_pagination_args(array( |
| 92 |
'total_items' => $total_items, //WE have to calculate the total number of items |
| 93 |
'per_page' => $per_page, //WE have to determine how many items to show on a page |
| 94 |
'total_pages' => ceil($total_items/$per_page) //WE have to calculate the total number of pages |
| 95 |
) ); |
| 96 |
} |
| 97 |
} // class Polylang_List_Table |
| 98 |
|
| 99 |
class Polylang_String_Table extends WP_List_Table { |
| 100 |
function __construct() { |
| 101 |
parent::__construct(array( |
| 102 |
'singular' => __('Strings translation','polylang'), |
| 103 |
'plural' => __('Strings translations','polylang'), |
| 104 |
'ajax'=> false) |
| 105 |
); |
| 106 |
} |
| 107 |
|
| 108 |
function column_default($item, $column_name) { |
| 109 |
return $item[$column_name]; |
| 110 |
} |
| 111 |
|
| 112 |
function column_translations($item) { |
| 113 |
$out = ''; |
| 114 |
foreach($item['translations'] as $key=>$translation) { |
| 115 |
$input_type = $item['multiline'] ? |
| 116 |
'<textarea name="translation[%1$s][%2$s]" id="%1$s-%2$s">%4$s</textarea>' : |
| 117 |
'<input name="translation[%1$s][%2$s]" id="%1$s-%2$s" value="%4$s" />'; |
| 118 |
$out .= sprintf('<div class="translation"><label for="%1$s-%2$s">%3$s</label>'.$input_type.'</div>', |
| 119 |
esc_attr($key), |
| 120 |
esc_attr($item['row']), |
| 121 |
esc_html($key), |
| 122 |
$item['multiline'] ? esc_textarea($translation) : esc_html($translation)); |
| 123 |
} |
| 124 |
return $out; |
| 125 |
} |
| 126 |
|
| 127 |
function get_columns() { |
| 128 |
return array( |
| 129 |
'name' => __('Name', 'polylang'), |
| 130 |
'string' => __('String', 'polylang'), |
| 131 |
'translations' => __('Translations', 'polylang'), |
| 132 |
); |
| 133 |
} |
| 134 |
|
| 135 |
function get_sortable_columns() { |
| 136 |
return array( |
| 137 |
'name' => array('name', false), |
| 138 |
'string' => array('string', false), |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
function prepare_items($data = array()) { |
| 143 |
$per_page = 10; // 10 strings per page |
| 144 |
$columns = $this->get_columns(); |
| 145 |
$hidden = array(); |
| 146 |
$sortable = $this->get_sortable_columns(); |
| 147 |
$this->_column_headers = array($columns, $hidden, $sortable); |
| 148 |
|
| 149 |
function usort_reorder($a,$b){ |
| 150 |
$orderby = $_REQUEST['orderby']; |
| 151 |
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; // if no order, default to asc |
| 152 |
$result = strcmp($a[$orderby], $b[$orderby]); // determine sort order |
| 153 |
return ($order==='asc') ? $result : -$result; // send final sort direction to usort |
| 154 |
} |
| 155 |
if (!empty($_REQUEST['orderby'])) // no sort by default |
| 156 |
usort($data, 'usort_reorder'); |
| 157 |
|
| 158 |
$current_page = $this->get_pagenum(); |
| 159 |
$total_items = count($data); |
| 160 |
$data = array_slice($data,(($current_page-1)*$per_page),$per_page); |
| 161 |
$this->items = $data; |
| 162 |
|
| 163 |
$this->set_pagination_args( array( |
| 164 |
'total_items' => $total_items, |
| 165 |
'per_page' => $per_page, |
| 166 |
'total_pages' => ceil($total_items/$per_page) |
| 167 |
) ); |
| 168 |
} |
| 169 |
} // class Polylang_String_Table |
| 170 |
?> |
| 171 |
|