Letter Templates Add New'; // } } public function prepare_items() { $hidden = $this->get_hidden_columns(); $sortable = $this->get_sortable_columns(); $data = $this->table_data(); usort( $data, array( &$this, 'sort_data' ) ); $perPage = $this->perPage; $currentPage = $this->get_pagenum(); $totalItems = $this->total_elements; $this->set_pagination_args( array( 'total_items' => $totalItems, 'per_page' => $perPage ) ); $data = array_slice($data,(($currentPage-1)*$perPage),$perPage); $this->_column_headers = array($this->columns, $hidden, $sortable); $this->items = $data; } /** * Override the parent columns method. Defines the columns to use in your listing table * * @return Array */ public function get_columns() { return $this->columns; } /** * Define which columns are hidden * * @return Array */ public function get_hidden_columns() { return array(); } /** * Define the sortable columns * * @return Array */ public function get_sortable_columns() { $sortable = array(); // if($this->total_elements and count($this->data_table) > 0) { // foreach($this->data_table[0] as $k => $v) { // $sortable[$k] = array($k, false); // } // } return $this->sortable_custom_columns; } /** * Get the table data * * @return Array */ private function table_data() { return $this->data_table; } /** * Define what data to show on each column of the table * * @param Array $item Data * @param String $column_name - Current column name * * @return Mixed */ public function column_default( $item, $column_name ) { return $item[ $column_name ]; } public function column_name($item){ $item_json = json_decode(json_encode($item), true); $copy = $this->custom_actions; foreach($this->custom_actions as $k => $action) { foreach($item_json as $key => $d) { $action = str_replace("[".$key."]", $item_json[$key], $action); } $this->custom_actions[$k] = $action; } $result = sprintf('%s %s', $item_json[$this->default_column_name], $this->row_actions($this->custom_actions)); $this->custom_actions = $copy; return $result; } /** * Allows you to sort the data by the variables set in the $_GET * * @return Mixed */ private function sort_data( $a, $b ) { // Set defaults $orderby = 'title'; $order = 'asc'; // If orderby is set, use this as the sort column if(!empty($_GET['orderby'])) { $orderby = sanitize_text_field($_GET['orderby']); } // If order is set use this as the order if(!empty($_GET['order'])) { $order = sanitize_text_field($_GET['order']); } $result = ''; if(isset($a[$orderby])) { $result = strcmp( $a[$orderby], $b[$orderby] ); } if($order === 'asc') { return $result; } return -$result; } }