| 1 |
<?php |
| 2 |
|
| 3 |
class Form_List extends WP_List_Table |
| 4 |
{ |
| 5 |
|
| 6 |
public static $TABLE_NAME = 'webling_forms'; |
| 7 |
|
| 8 |
/** Class constructor */ |
| 9 |
public function __construct() |
| 10 |
{ |
| 11 |
parent::__construct([ |
| 12 |
'singular' => __('Anmeldeformular', 'webling'), //singular name of the listed records |
| 13 |
'plural' => __('Anmeldeformulare', 'webling'), //plural name of the listed records |
| 14 |
'ajax' => false //should this table support ajax? |
| 15 |
]); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Retrieve data from the database |
| 20 |
* |
| 21 |
* @param int $per_page |
| 22 |
* @param int $page_number |
| 23 |
* |
| 24 |
* @return mixed |
| 25 |
*/ |
| 26 |
public static function get_records($per_page = 5, $page_number = 1) |
| 27 |
{ |
| 28 |
|
| 29 |
global $wpdb; |
| 30 |
|
| 31 |
$sql = "SELECT * FROM {$wpdb->prefix}" . self::$TABLE_NAME; |
| 32 |
|
| 33 |
if (!empty($_REQUEST['orderby'])) { |
| 34 |
$sql .= ' ORDER BY ' . esc_sql($_REQUEST['orderby']); |
| 35 |
$sql .= !empty($_REQUEST['order']) ? ' ' . esc_sql($_REQUEST['order']) : ' ASC'; |
| 36 |
} |
| 37 |
|
| 38 |
$sql .= " LIMIT $per_page"; |
| 39 |
$sql .= ' OFFSET ' . ($page_number - 1) * $per_page; |
| 40 |
|
| 41 |
$result = $wpdb->get_results($sql, 'ARRAY_A'); |
| 42 |
|
| 43 |
return $result; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Delete a record. |
| 48 |
* |
| 49 |
* @param int $id record ID |
| 50 |
*/ |
| 51 |
public static function delete_record($id) |
| 52 |
{ |
| 53 |
global $wpdb; |
| 54 |
|
| 55 |
$wpdb->delete( |
| 56 |
"{$wpdb->prefix}" . self::$TABLE_NAME, |
| 57 |
['id' => $id], |
| 58 |
['%d'] |
| 59 |
); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Returns the count of records in the database. |
| 64 |
* |
| 65 |
* @return null|string |
| 66 |
*/ |
| 67 |
public static function record_count() |
| 68 |
{ |
| 69 |
global $wpdb; |
| 70 |
|
| 71 |
$sql = "SELECT COUNT(*) FROM {$wpdb->prefix}" . self::$TABLE_NAME; |
| 72 |
|
| 73 |
return $wpdb->get_var($sql); |
| 74 |
} |
| 75 |
|
| 76 |
/** Text displayed when no data is available */ |
| 77 |
public function no_items() |
| 78 |
{ |
| 79 |
_e('No records avaliable.', 'webling'); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Method for name column |
| 84 |
* |
| 85 |
* @param array $item an array of DB data |
| 86 |
* |
| 87 |
* @return string |
| 88 |
*/ |
| 89 |
function column_name($item) |
| 90 |
{ |
| 91 |
// create a nonce |
| 92 |
$delete_nonce = wp_create_nonce('sp_delete_' . self::$TABLE_NAME); |
| 93 |
|
| 94 |
$title = '<strong>' . $item['name'] . '</strong>'; |
| 95 |
|
| 96 |
$actions = [ |
| 97 |
'delete' => sprintf('<a href="?page=%s&action=%s&record=%s&_wpnonce=%s">Delete</a>', esc_attr($_REQUEST['page']), 'delete', absint($item['id']), $delete_nonce) |
| 98 |
]; |
| 99 |
|
| 100 |
return $title . $this->row_actions($actions); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Render a column when no column specific method exists. |
| 105 |
* |
| 106 |
* @param array $item |
| 107 |
* @param string $column_name |
| 108 |
* |
| 109 |
* @return mixed |
| 110 |
*/ |
| 111 |
public function column_default($item, $column_name) |
| 112 |
{ |
| 113 |
switch ($column_name) { |
| 114 |
case 'title': |
| 115 |
return '<a href="'.admin_url('admin.php?page=webling_page_form_edit&form_id='.$item['id']).'">' . $item['title'] . '</a>'; |
| 116 |
case 'shortcode': |
| 117 |
return '[webling_form id="'.$item['id'].'"]'; |
| 118 |
default: |
| 119 |
return print_r($item, true); //Show the whole array for troubleshooting purposes |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Render the bulk edit checkbox |
| 125 |
* |
| 126 |
* @param array $item |
| 127 |
* |
| 128 |
* @return string |
| 129 |
*/ |
| 130 |
function column_cb($item) |
| 131 |
{ |
| 132 |
return sprintf( |
| 133 |
'<input type="checkbox" name="webling_form_bulk_delete[]" value="%s" />', $item['id'] |
| 134 |
); |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Associative array of columns |
| 139 |
* |
| 140 |
* @return array |
| 141 |
*/ |
| 142 |
function get_columns() |
| 143 |
{ |
| 144 |
$columns = [ |
| 145 |
'cb' => '<input type="checkbox" />', |
| 146 |
'title' => __('Title', 'webling'), |
| 147 |
'shortcode' => __('Shortcode', 'webling'), |
| 148 |
]; |
| 149 |
|
| 150 |
return $columns; |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Columns to make sortable. |
| 155 |
* |
| 156 |
* @return array |
| 157 |
*/ |
| 158 |
public function get_sortable_columns() |
| 159 |
{ |
| 160 |
$sortable_columns = array( |
| 161 |
'title' => array('title', false) |
| 162 |
); |
| 163 |
|
| 164 |
return $sortable_columns; |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Returns an associative array containing the bulk action |
| 169 |
* |
| 170 |
* @return array |
| 171 |
*/ |
| 172 |
public function get_bulk_actions() |
| 173 |
{ |
| 174 |
$actions = [ |
| 175 |
'webling_form_bulk_delete' => 'Delete' |
| 176 |
]; |
| 177 |
|
| 178 |
return $actions; |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* Handles data query and filter, sorting, and pagination. |
| 183 |
*/ |
| 184 |
public function prepare_items() |
| 185 |
{ |
| 186 |
|
| 187 |
$columns = $this->get_columns(); |
| 188 |
$hidden = array(); |
| 189 |
$sortable = $this->get_sortable_columns(); |
| 190 |
$this->_column_headers = array($columns, $hidden, $sortable); |
| 191 |
|
| 192 |
/** Process bulk action */ |
| 193 |
//$this->process_bulk_action(); |
| 194 |
|
| 195 |
$per_page = $this->get_items_per_page('records_per_page', 30); |
| 196 |
$current_page = $this->get_pagenum(); |
| 197 |
$total_items = self::record_count(); |
| 198 |
|
| 199 |
$this->set_pagination_args([ |
| 200 |
'total_items' => $total_items, //WE have to calculate the total number of items |
| 201 |
'per_page' => $per_page //WE have to determine how many items to show on a page |
| 202 |
]); |
| 203 |
|
| 204 |
$this->items = self::get_records($per_page, $current_page); |
| 205 |
} |
| 206 |
|
| 207 |
public function process_bulk_action() |
| 208 |
{ |
| 209 |
|
| 210 |
//Detect when a bulk action is being triggered... |
| 211 |
if ('delete' === $this->current_action()) { |
| 212 |
|
| 213 |
// In our file that handles the request, verify the nonce. |
| 214 |
$nonce = esc_attr($_REQUEST['_wpnonce']); |
| 215 |
|
| 216 |
if (!wp_verify_nonce($nonce, 'sp_delete_'.self::$TABLE_NAME)) { |
| 217 |
die('Go get a life script kiddies'); |
| 218 |
} else { |
| 219 |
self::delete_record(absint($_GET['record'])); |
| 220 |
|
| 221 |
wp_redirect(esc_url(add_query_arg([]))); |
| 222 |
exit; |
| 223 |
} |
| 224 |
|
| 225 |
} |
| 226 |
|
| 227 |
// If the delete bulk action is triggered |
| 228 |
if ((isset($_POST['action']) && $_POST['action'] == 'webling_form_bulk_delete') |
| 229 |
|| (isset($_POST['action2']) && $_POST['action2'] == 'webling_form_bulk_delete') |
| 230 |
) { |
| 231 |
|
| 232 |
$delete_ids = esc_sql($_POST['webling_form_bulk_delete']); |
| 233 |
|
| 234 |
// loop over the array of record IDs and delete them |
| 235 |
foreach ($delete_ids as $id) { |
| 236 |
self::delete_record($id); |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
wp_redirect(esc_url(add_query_arg([]))); |
| 241 |
exit; |
| 242 |
} |
| 243 |
} |
| 244 |
} |
| 245 |
|