| 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 |
/** |
| 8 |
* Class MultiValueFieldTable |
| 9 |
*/ |
| 10 |
class MultiValueFieldTable extends WP_List_Table { |
| 11 |
private $_bulk_actions = array(); |
| 12 |
private $_columns = array(); |
| 13 |
private $_sortable_columns = array(); |
| 14 |
private $_data = array(); |
| 15 |
|
| 16 |
function __construct() { |
| 17 |
global $status, $page; |
| 18 |
|
| 19 |
parent::__construct(array( |
| 20 |
'singular' => 'item', |
| 21 |
'plural' => 'items', |
| 22 |
'ajax' => false |
| 23 |
)); |
| 24 |
} |
| 25 |
|
| 26 |
/** |
| 27 |
* Set default column attributes |
| 28 |
* |
| 29 |
* @param array $item A singular item (one full row's worth of data) |
| 30 |
* @param array $column_name The name/slug of the column to be processed |
| 31 |
* @return string Text or HTML to be placed inside the column <td> |
| 32 |
*/ |
| 33 |
public function column_default($item, $column_name) { |
| 34 |
return $item[$column_name]; |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Provide a callback function to render the checkbox column |
| 39 |
* |
| 40 |
* @param array $item A row's worth of data |
| 41 |
* @return string The formatted string with a checkbox |
| 42 |
*/ |
| 43 |
public function column_cb($item) { |
| 44 |
return sprintf( |
| 45 |
'<input type="checkbox" name="%1$s[]" value="%2$s" />', |
| 46 |
$this->_args['singular'], |
| 47 |
$item['id'] |
| 48 |
); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Get the bulk actions for this table |
| 53 |
* |
| 54 |
* @return array Bulk actions |
| 55 |
*/ |
| 56 |
public function get_bulk_actions() { |
| 57 |
return $this->_bulk_actions; |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Get a list of columns |
| 62 |
* |
| 63 |
* @return array |
| 64 |
*/ |
| 65 |
public function get_columns() { |
| 66 |
return $this->_columns; |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Add a column to the table |
| 71 |
* |
| 72 |
* @param string $key Machine-readable column name |
| 73 |
* @param string $title Title shown to the user |
| 74 |
* @param boolean $sortable Whether or not this is sortable (defaults false) |
| 75 |
*/ |
| 76 |
public function add_column($key, $title, $sortable = false) { |
| 77 |
$this->_columns[$key] = $title; |
| 78 |
|
| 79 |
if ($sortable) $this->_sortable_columns[$key] = array($key, false); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Add an item (row) to the table |
| 84 |
* |
| 85 |
* @param array $item A row's worth of data |
| 86 |
*/ |
| 87 |
public function add_item($item) { |
| 88 |
array_push($this->_data, $item); |
| 89 |
} |
| 90 |
|
| 91 |
/** |
| 92 |
* Add a bulk action to the table |
| 93 |
* |
| 94 |
* @param string $key Machine-readable action name |
| 95 |
* @param string $name Title shown to the user |
| 96 |
*/ |
| 97 |
public function add_bulk_action($key, $name) { |
| 98 |
$this->_bulk_actions[$key] = $name; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Prepares the items (rows) to be rendered |
| 103 |
*/ |
| 104 |
public function prepare_items() { |
| 105 |
$total_items = count($this->_data); |
| 106 |
$per_page = 25; |
| 107 |
|
| 108 |
$columns = $this->_columns; |
| 109 |
$hidden = array(); |
| 110 |
$sortable = $this->_sortable_columns; |
| 111 |
|
| 112 |
$this->_column_headers = array($columns, $hidden, $sortable); |
| 113 |
|
| 114 |
$current_page = $this->get_pagenum(); |
| 115 |
|
| 116 |
$sorted_data = $this->reorder($this->_data); |
| 117 |
|
| 118 |
$data = array_slice($sorted_data, (($current_page-1)*$per_page),$per_page); |
| 119 |
|
| 120 |
$this->items = $data; |
| 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 |
/** |
| 130 |
* Reorder the data according to the sort parameters |
| 131 |
* |
| 132 |
* @return array Row data, sorted |
| 133 |
*/ |
| 134 |
public function reorder($data) { |
| 135 |
function usort_reorder($a, $b) { |
| 136 |
$orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'title'; //If no sort, default to title |
| 137 |
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc'; //If no order, default to asc |
| 138 |
$result = strcmp($a[$orderby], $b[$orderby]); //Determine sort order |
| 139 |
return ($order==='asc') ? $result : -$result; //Send final sort direction to usort |
| 140 |
} |
| 141 |
usort($data, 'usort_reorder'); |
| 142 |
|
| 143 |
return $data; |
| 144 |
} |
| 145 |
} |
| 146 |
|