Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. <?php
  2.  
  3. require_once(dirname(__FILE__).'/ListTable.php');
  4.  
  5. class SGPB_Table extends SGPB_ListTable
  6. {
  7. protected $id = '';
  8. protected $columns = array();
  9. protected $displayColumns = array();
  10. protected $sortableColumns = array();
  11. protected $tablename = '';
  12. protected $rowsPerPage = 10;
  13. protected $initialOrder = array();
  14. private $previewPopup = false;
  15.  
  16. public function __construct($id, $popupPreviewId = false)
  17. {
  18. $this->id = $id;
  19. $this->previewPopup = $popupPreviewId;
  20. parent::__construct(array(
  21. 'singular'=> 'wp_'.$id, //singular label
  22. 'plural' => 'wp_'.$id.'s', //plural label
  23. 'ajax' => false
  24. ));
  25. }
  26.  
  27. public function setId($id)
  28. {
  29. $this->id = $id;
  30. }
  31.  
  32. public function setRowsPerPage($rowsPerPage)
  33. {
  34. $this->rowsPerPage = $rowsPerPage;
  35. }
  36.  
  37. public function setColumns($columns)
  38. {
  39. $this->columns = $columns;
  40. }
  41.  
  42. public function getColumns()
  43. {
  44. return $this->columns;
  45. }
  46.  
  47. public function setDisplayColumns($displayColumns)
  48. {
  49. $this->displayColumns = $displayColumns;
  50. }
  51.  
  52. public function setSortableColumns($sortableColumns)
  53. {
  54. $this->sortableColumns = $sortableColumns;
  55. }
  56.  
  57. public function setTablename($tablename)
  58. {
  59. $this->tablename = $tablename;
  60. }
  61.  
  62. public function setInitialSort($orderableColumns)
  63. {
  64. $this->initialOrder = $orderableColumns;
  65. }
  66.  
  67. public function get_columns()
  68. {
  69. return $this->displayColumns;
  70. }
  71.  
  72. public function prepare_items()
  73. {
  74. global $wpdb;
  75. $table = $this->tablename;
  76.  
  77. $query = "SELECT ".implode(', ', $this->columns)." FROM ".$table;
  78. $this->customizeQuery($query);
  79.  
  80. $totalItems = count($wpdb->get_results($query)); //return the total number of affected rows
  81.  
  82. if($this->previewPopup) {
  83. $totalItems -= 1;
  84. }
  85. $perPage = $this->rowsPerPage;
  86.  
  87. $totalPages = ceil($totalItems/$perPage);
  88.  
  89. $orderby = isset($_GET["orderby"]) ? sanitize_text_field($_GET["orderby"]) : 'ASC';
  90. $order = isset($_GET["order"]) ? sanitize_text_field($_GET["order"]) : '';
  91.  
  92. if(isset($this->initialOrder) && empty($order)){
  93. foreach($this->initialOrder as $key=>$val){
  94. $order = $val;
  95. $orderby = $key;
  96. }
  97. }
  98.  
  99. if (!empty($orderby) && !empty($order)) {
  100. if($orderby != 'id' && $orderby != 'title') {
  101. $orderby = 'id';
  102. }
  103. if($order != 'asc' && $order != 'desc') {
  104. $order = 'desc';
  105. }
  106. $query .= ' ORDER BY '.$orderby.' '.$order;
  107. }
  108.  
  109. $paged = isset($_GET["paged"]) ? (int)$_GET["paged"] : '';
  110.  
  111. if (empty($paged) || !is_numeric($paged) || $paged<=0) {
  112. $paged = 1;
  113. }
  114.  
  115. //adjust the query to take pagination into account
  116. if(!empty($paged) && !empty($perPage)) {
  117. $offset = ($paged-1) * $perPage;
  118. $query .= ' LIMIT '.(int)$offset.','.(int)$perPage;
  119. }
  120.  
  121. $this->set_pagination_args(array(
  122. "total_items" => $totalItems,
  123. "total_pages" => $totalPages,
  124. "per_page" => $perPage,
  125. ));
  126.  
  127. $columns = $this->get_columns();
  128. $hidden = array();
  129. $sortable = $this->get_sortable_columns();
  130. $this->_column_headers = array($columns, $hidden, $sortable);
  131. $items = $wpdb->get_results($query, ARRAY_N);
  132. /*Remove popup data when its class does not exist.*/
  133. $this->customizeRowsData($items);
  134.  
  135. $this->items = $items;
  136. }
  137.  
  138. public function customizeRowsData(&$items) {
  139.  
  140. }
  141.  
  142. public function get_sortable_columns() {
  143. return $this->sortableColumns;
  144. }
  145.  
  146. public function display_rows()
  147. {
  148. //get the records registered in the prepare_items method
  149. $records = $this->items;
  150.  
  151. //get the columns registered in the get_columns and get_sortable_columns methods
  152. list($columns, $hidden) = $this->get_column_info();
  153.  
  154. if (!empty($records)) {
  155. foreach($records as $rec) {
  156. echo '<tr>';
  157.  
  158. $this->customizeRow($rec);
  159. for ($i = 0; $i<count($rec); $i++) {
  160. echo '<td>'.stripslashes($rec[$i]).'</td>';
  161. }
  162.  
  163. echo '</tr>';
  164. }
  165. }
  166. }
  167.  
  168. public function customizeRow(&$row)
  169. {
  170.  
  171. }
  172.  
  173. public function customizeQuery(&$query)
  174. {
  175.  
  176. }
  177.  
  178. public function __toString()
  179. {
  180. $this->prepare_items(); ?>
  181. <form method="post">
  182. <input type="hidden" name="page" value="my_list_test" />
  183. <?php $this->search_box('search', 'search_id'); ?>
  184. </form>
  185. <?php
  186. $this->display();
  187. return '';
  188. }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement