| 1 |
<?php |
| 2 |
ob_start(); |
| 3 |
|
| 4 |
class Pma_Categories_List_Table extends WP_List_Table { |
| 5 |
private $plugin_name; |
| 6 |
private $title_length; |
| 7 |
|
| 8 |
/** Class constructor */ |
| 9 |
public function __construct( $plugin_name ) { |
| 10 |
$this->plugin_name = $plugin_name; |
| 11 |
$this->title_length = Poll_Maker_Ays_Admin::get_listtables_title_length('categories'); |
| 12 |
parent::__construct(array( |
| 13 |
'singular' =>esc_html__('Category', "poll-maker"), //singular name of the listed records |
| 14 |
'plural' =>esc_html__('Categories', "poll-maker"), //plural name of the listed records |
| 15 |
'ajax' => false, //does this table support ajax? |
| 16 |
)); |
| 17 |
add_action('admin_notices', array($this, 'poll_category_notices')); |
| 18 |
|
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Retrieve customers data from the database |
| 23 |
* |
| 24 |
* @param int $per_page |
| 25 |
* @param int $page_number |
| 26 |
* |
| 27 |
* @return mixed |
| 28 |
*/ |
| 29 |
public static function get_poll_categories( $per_page = 20, $page_number = 1, $search = '' ) { |
| 30 |
|
| 31 |
global $wpdb; |
| 32 |
$cat_table = esc_sql($wpdb->prefix."ayspoll_categories"); |
| 33 |
$sql = "SELECT * FROM ".$cat_table; |
| 34 |
$args = array(); |
| 35 |
|
| 36 |
if( $search != '' ){ |
| 37 |
$where[] = $search; |
| 38 |
} |
| 39 |
|
| 40 |
// Get where condition to filter |
| 41 |
$where = self::get_where_condition(); |
| 42 |
$sql .= $where; |
| 43 |
|
| 44 |
if ( ! empty( $_REQUEST['orderby'] ) ) { |
| 45 |
|
| 46 |
$order_by = ( isset( $_REQUEST['orderby'] ) && sanitize_text_field( $_REQUEST['orderby'] ) != '' ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'id'; |
| 47 |
$order_by .= ( ! empty( $_REQUEST['order'] ) && strtolower( $_REQUEST['order'] ) == 'asc' ) ? ' ASC' : ' DESC'; |
| 48 |
|
| 49 |
$sql_orderby = sanitize_sql_orderby($order_by); |
| 50 |
|
| 51 |
if ( $sql_orderby ) { |
| 52 |
$sql .= ' ORDER BY ' . $sql_orderby; |
| 53 |
} else { |
| 54 |
$sql .= ' ORDER BY id DESC'; |
| 55 |
} |
| 56 |
}else{ |
| 57 |
$sql .= ' ORDER BY id DESC'; |
| 58 |
} |
| 59 |
|
| 60 |
$sql .= " LIMIT %d"; |
| 61 |
$args[] = $per_page; |
| 62 |
$offset = ($page_number - 1) * $per_page; |
| 63 |
$sql .= " OFFSET %d"; |
| 64 |
$args[] = $offset; |
| 65 |
|
| 66 |
$result = $wpdb->get_results( |
| 67 |
$wpdb->prepare( $sql, $args), |
| 68 |
'ARRAY_A' |
| 69 |
); |
| 70 |
|
| 71 |
return $result; |
| 72 |
} |
| 73 |
|
| 74 |
public function get_poll_category( $id ) { |
| 75 |
global $wpdb; |
| 76 |
|
| 77 |
$cat_id = absint(sanitize_text_field($id)); |
| 78 |
$cat_table = esc_sql($wpdb->prefix."ayspoll_categories"); |
| 79 |
$sql = "SELECT * FROM ".$cat_table." WHERE id=%d"; |
| 80 |
$result = $wpdb->get_row( |
| 81 |
$wpdb->prepare( $sql, $cat_id), |
| 82 |
'ARRAY_A' |
| 83 |
); |
| 84 |
|
| 85 |
return $result; |
| 86 |
} |
| 87 |
|
| 88 |
public function add_edit_poll_category( $data, $id = null, $ays_change_type = "" ) { |
| 89 |
global $wpdb; |
| 90 |
$cats_table = $wpdb->prefix . 'ayspoll_categories'; |
| 91 |
|
| 92 |
if (isset($data["poll_category_action"]) && wp_verify_nonce($data["poll_category_action"], 'poll_category_action')) { |
| 93 |
|
| 94 |
$poll_allowed_html = Poll_Maker_Data::ays_poll_custom_allowed_html(); |
| 95 |
|
| 96 |
$title = isset($data['ays_title']) && $data['ays_title'] != "" ? stripslashes(sanitize_text_field($data['ays_title'])) : "Category"; |
| 97 |
|
| 98 |
$description = isset( $data['ays_description'] ) && $data['ays_description'] != '' ? wp_kses( $data['ays_description'], $poll_allowed_html ) : ''; |
| 99 |
|
| 100 |
$skip_poll = isset($data['ays_poll_allow_skip']) && $data['ays_poll_allow_skip'] == "allow" ? sanitize_text_field($data['ays_poll_allow_skip']) : ""; |
| 101 |
$next_text = isset($data['ays_poll_next_text']) && $data['ays_poll_next_text'] != "" ? sanitize_text_field($data['ays_poll_next_text']) : "Next"; |
| 102 |
$default_message = 'The polls that belong to this category are expired or unpublished'; |
| 103 |
$exp_message = (isset($data['ays_poll_cat_message']) && $data['ays_poll_cat_message'] != '') ? wp_kses( $data['ays_poll_cat_message'], $poll_allowed_html ) : $default_message; |
| 104 |
$previous_text = (isset($data['ays_poll_previous_text']) && $data['ays_poll_previous_text'] != '') ? sanitize_text_field($data['ays_poll_previous_text']) : 'Previous'; |
| 105 |
$message = ''; |
| 106 |
$options = array( |
| 107 |
"allow_skip" => $skip_poll, |
| 108 |
"next_text" => $next_text, |
| 109 |
"exp_message" => $exp_message, |
| 110 |
"previous_text" => $previous_text |
| 111 |
); |
| 112 |
if ($id == null) { |
| 113 |
$result = $wpdb->insert( |
| 114 |
$cats_table, |
| 115 |
array( |
| 116 |
'title' => $title, |
| 117 |
'description' => $description, |
| 118 |
'options' => json_encode($options, true), |
| 119 |
), |
| 120 |
array('%s', '%s', '%s') |
| 121 |
); |
| 122 |
$message = 'created'; |
| 123 |
$last_id = $wpdb->insert_id; |
| 124 |
} else { |
| 125 |
$result = $wpdb->update( |
| 126 |
$cats_table, |
| 127 |
array( |
| 128 |
'title' => $title, |
| 129 |
'description' => $description, |
| 130 |
'options' => json_encode($options, true), |
| 131 |
), |
| 132 |
array('id' => $id), |
| 133 |
array('%s', '%s', '%s'), |
| 134 |
array('%d') |
| 135 |
); |
| 136 |
$message = 'updated'; |
| 137 |
} |
| 138 |
|
| 139 |
if ($result >= 0) { |
| 140 |
if ('' != $ays_change_type) { |
| 141 |
if($id == null){ |
| 142 |
$url = esc_url_raw( add_query_arg( array( |
| 143 |
"action" => "edit", |
| 144 |
"poll_category" => $last_id, |
| 145 |
"status" => $message |
| 146 |
) ) ); |
| 147 |
} |
| 148 |
else{ |
| 149 |
$url = esc_url_raw( remove_query_arg(false) ) . '&status=' . $message; |
| 150 |
} |
| 151 |
wp_redirect($url); |
| 152 |
} else { |
| 153 |
$url = esc_url_raw(remove_query_arg(array('action', 'poll_category'))) . '&status=' . $message; |
| 154 |
wp_redirect($url); |
| 155 |
} |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Delete a customer record. |
| 162 |
* |
| 163 |
* @param int $id customer ID |
| 164 |
*/ |
| 165 |
public static function delete_poll_categories( $id ) { |
| 166 |
global $wpdb; |
| 167 |
$cat_table = esc_sql($wpdb->prefix."ayspoll_categories"); |
| 168 |
$wpdb->delete( |
| 169 |
$cat_table, |
| 170 |
array('id' => $id), |
| 171 |
array('%d') |
| 172 |
); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Returns the count of records in the database. |
| 177 |
* |
| 178 |
* @return null|string |
| 179 |
*/ |
| 180 |
public static function record_count() { |
| 181 |
global $wpdb; |
| 182 |
$filter = array(); |
| 183 |
$cat_table = esc_sql($wpdb->prefix."ayspoll_categories"); |
| 184 |
$sql = "SELECT COUNT(*) FROM ".$cat_table; |
| 185 |
|
| 186 |
$search = ( isset( $_REQUEST['s'] ) ) ? sanitize_text_field( $_REQUEST['s'] ) : false; |
| 187 |
if( $search ){ |
| 188 |
$filter[] = sprintf(" title LIKE '%%%s%%' ", esc_sql( $wpdb->esc_like( $search ) ) ); |
| 189 |
} |
| 190 |
|
| 191 |
if(count($filter) !== 0){ |
| 192 |
$sql .= " WHERE ".implode(" AND ", $filter); |
| 193 |
} |
| 194 |
|
| 195 |
return $wpdb->get_var($sql); |
| 196 |
} |
| 197 |
|
| 198 |
/** Text displayed when no customer data is available */ |
| 199 |
public function no_items() { |
| 200 |
_e('There are no poll categories yet.', "poll-maker"); |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Render a column when no column specific method exist. |
| 205 |
* |
| 206 |
* @param array $item |
| 207 |
* @param string $column_name |
| 208 |
* |
| 209 |
* @return mixed |
| 210 |
*/ |
| 211 |
public function column_default( $item, $column_name ) { |
| 212 |
switch ( $column_name ) { |
| 213 |
case 'title': |
| 214 |
case 'description': |
| 215 |
case 'polls': |
| 216 |
case 'id': |
| 217 |
return $item[$column_name]; |
| 218 |
break; |
| 219 |
default: |
| 220 |
return print_r($item, true); //Show the whole array for troubleshooting purposes |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* Render the bulk edit checkbox |
| 226 |
* |
| 227 |
* @param array $item |
| 228 |
* |
| 229 |
* @return string |
| 230 |
*/ |
| 231 |
function column_cb( $item ) { |
| 232 |
if(intval($item['id']) === 1){ |
| 233 |
return; |
| 234 |
} |
| 235 |
return sprintf( |
| 236 |
'<input type="checkbox" name="bulk-delete[]" value="%s">', $item['id'] |
| 237 |
); |
| 238 |
} |
| 239 |
|
| 240 |
function column_shortcode( $item ) { |
| 241 |
return sprintf('<input type="text" onClick="this.setSelectionRange(0, this.value.length)" readonly value="[ays_poll cat_id=%s]" />', $item["id"]); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* Method for name column |
| 246 |
* |
| 247 |
* @param array $item an array of DB data |
| 248 |
* |
| 249 |
* @return string |
| 250 |
*/ |
| 251 |
function column_title( $item ) { |
| 252 |
$delete_nonce = wp_create_nonce($this->plugin_name . '-delete-poll-category'); |
| 253 |
|
| 254 |
$category_title = stripslashes(esc_attr($item['title'])); |
| 255 |
|
| 256 |
$categories_title_length = intval( $this->title_length ); |
| 257 |
|
| 258 |
$restitle = Poll_Maker_Ays_Admin::ays_restriction_string("word", $category_title, $categories_title_length); |
| 259 |
|
| 260 |
$title = sprintf('<a href="?page=%s&action=%s&poll_category=%d"><strong>%s</strong></a>', esc_attr($_REQUEST['page']), 'edit', absint($item['id']) ,$restitle); |
| 261 |
|
| 262 |
|
| 263 |
$actions = array( |
| 264 |
'edit' => sprintf('<a href="?page=%s&action=%s&poll_category=%d">' .esc_html__('Edit', "poll-maker") . '</a>', esc_attr($_REQUEST['page']), 'edit', absint($item['id'])), |
| 265 |
// 'delete' => sprintf('<a href="?page=%s&action=%s&poll_category=%s&_wpnonce=%s">' .esc_html__('Delete', $this->plugin_name) . '</a>', esc_attr($_REQUEST['page']), 'delete', absint($item['id']), $delete_nonce), |
| 266 |
); |
| 267 |
|
| 268 |
if(intval($item['id']) !== 1){ |
| 269 |
$actions['delete'] = sprintf('<a href="?page=%s&action=%s&poll_category=%s&_wpnonce=%s">' .esc_html__('Delete', "poll-maker") . '</a>', esc_attr($_REQUEST['page']), 'delete', absint($item['id']), $delete_nonce); |
| 270 |
} |
| 271 |
|
| 272 |
return $title . $this->row_actions($actions); |
| 273 |
} |
| 274 |
|
| 275 |
function column_polls( $item ) { |
| 276 |
global $wpdb; |
| 277 |
$polls_table = $wpdb->prefix . "ayspoll_polls"; |
| 278 |
$categories_table = $wpdb->prefix . "ayspoll_categories"; |
| 279 |
|
| 280 |
$sql = "SELECT COUNT(*) FROM " . $polls_table . " p WHERE p.categories LIKE '%," . esc_sql(absint($item['id'])) . ",%' OR p.categories LIKE '" . esc_sql(absint($item['id'])) . ",%' OR p.categories LIKE '%," . esc_sql(absint($item['id'])) . "' OR p.categories = '" . esc_sql(absint($item['id'])) . "'"; |
| 281 |
|
| 282 |
$result = $wpdb->get_var($sql); |
| 283 |
|
| 284 |
if ( isset($result) && $result > 0 ) { |
| 285 |
$result = sprintf( '<a href="?page=%s&filterby=%d" target="_blank">%s</a>', 'poll-maker-ays', $item['id'], $result ); |
| 286 |
} |
| 287 |
|
| 288 |
return "<p style='font-size:14px;'>" . $result . "</p>"; |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Associative array of columns |
| 293 |
* |
| 294 |
* @return array |
| 295 |
*/ |
| 296 |
function get_columns() { |
| 297 |
$columns = array( |
| 298 |
'cb' => '<input type="checkbox">', |
| 299 |
'title' =>esc_html__('Title', "poll-maker"), |
| 300 |
'description' =>esc_html__('Description', "poll-maker"), |
| 301 |
'shortcode' =>esc_html__('Shortcode', "poll-maker"), |
| 302 |
'polls' =>esc_html__('Polls', "poll-maker"), |
| 303 |
'id' =>esc_html__('ID', "poll-maker"), |
| 304 |
); |
| 305 |
|
| 306 |
return $columns; |
| 307 |
} |
| 308 |
|
| 309 |
/** |
| 310 |
* Columns to make sortable. |
| 311 |
* |
| 312 |
* @return array |
| 313 |
*/ |
| 314 |
public function get_sortable_columns() { |
| 315 |
$sortable_columns = array( |
| 316 |
'title' => array('title', true), |
| 317 |
'id' => array('id', true), |
| 318 |
); |
| 319 |
|
| 320 |
return $sortable_columns; |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* Returns an associative array containing the bulk action |
| 325 |
* |
| 326 |
* @return array |
| 327 |
*/ |
| 328 |
public function get_bulk_actions() { |
| 329 |
$actions = array( |
| 330 |
'bulk-delete' =>esc_html__('Delete', "poll-maker"), |
| 331 |
); |
| 332 |
|
| 333 |
return $actions; |
| 334 |
} |
| 335 |
|
| 336 |
/** |
| 337 |
* Handles data query and filter, sorting, and pagination. |
| 338 |
*/ |
| 339 |
public function prepare_items() { |
| 340 |
|
| 341 |
$this->_column_headers = $this->get_column_info(); |
| 342 |
|
| 343 |
/** Process bulk action */ |
| 344 |
$this->process_bulk_action(); |
| 345 |
|
| 346 |
$per_page = $this->get_items_per_page('poll_cats_per_page', 20); |
| 347 |
$current_page = $this->get_pagenum(); |
| 348 |
$total_items = self::record_count(); |
| 349 |
|
| 350 |
$this->set_pagination_args(array( |
| 351 |
'total_items' => $total_items, //WE have to calculate the total number of items |
| 352 |
'per_page' => $per_page, //WE have to determine how many items to show on a page |
| 353 |
)); |
| 354 |
|
| 355 |
$search = ( isset( $_REQUEST['s'] ) ) ? sanitize_text_field( $_REQUEST['s'] ) : false; |
| 356 |
|
| 357 |
$do_search = ( $search ) ? sprintf(" title LIKE '%%%s%%' ", $search ) : ''; |
| 358 |
|
| 359 |
$this->items = self::get_poll_categories($per_page, $current_page, $do_search); |
| 360 |
|
| 361 |
} |
| 362 |
|
| 363 |
public function process_bulk_action() { |
| 364 |
//Detect when a bulk action is being triggered... |
| 365 |
if ('delete' === $this->current_action()) { |
| 366 |
|
| 367 |
// In our file that handles the request, verify the nonce. |
| 368 |
$nonce = esc_attr($_REQUEST['_wpnonce']); |
| 369 |
|
| 370 |
if (!wp_verify_nonce($nonce, $this->plugin_name . '-delete-poll-category')) { |
| 371 |
die('Go get a life script kiddies'); |
| 372 |
} else { |
| 373 |
self::delete_poll_categories(absint($_GET['poll_category'])); |
| 374 |
|
| 375 |
// esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 376 |
// add_query_arg() return the current url |
| 377 |
|
| 378 |
$url = esc_url_raw(remove_query_arg(['action', 'poll_category', '_wpnonce'])) . '&status=deleted'; |
| 379 |
wp_redirect($url); |
| 380 |
} |
| 381 |
|
| 382 |
} |
| 383 |
|
| 384 |
// If the delete bulk action is triggered |
| 385 |
if ((isset($_POST['action']) && 'bulk-delete' == $_POST['action']) |
| 386 |
|| (isset($_POST['action2']) && 'bulk-delete' == $_POST['action2']) |
| 387 |
) { |
| 388 |
|
| 389 |
$delete_ids = ( isset( $_POST['bulk-delete'] ) && ! empty( $_POST['bulk-delete'] ) ) ? esc_sql( $_POST['bulk-delete'] ) : array(); |
| 390 |
|
| 391 |
// loop over the array of record IDs and delete them |
| 392 |
foreach ( $delete_ids as $id ) { |
| 393 |
self::delete_poll_categories($id); |
| 394 |
|
| 395 |
} |
| 396 |
|
| 397 |
// esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 398 |
// add_query_arg() return the current url |
| 399 |
$url = esc_url_raw(remove_query_arg(['action', 'poll_category', '_wpnonce'])) . '&status=deleted'; |
| 400 |
wp_redirect($url); |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
public function poll_category_notices() { |
| 405 |
$status = (isset($_REQUEST['status'])) ? sanitize_text_field($_REQUEST['status']) : ''; |
| 406 |
|
| 407 |
if (empty($status)) { |
| 408 |
return; |
| 409 |
} |
| 410 |
|
| 411 |
if ('created' == $status) { |
| 412 |
$updated_message = esc_html( esc_html__('Poll category created.', "poll-maker")); |
| 413 |
} elseif ('updated' == $status) { |
| 414 |
$updated_message = esc_html( esc_html__('Poll category saved.', "poll-maker")); |
| 415 |
} elseif ('deleted' == $status) { |
| 416 |
$updated_message = esc_html( esc_html__('Poll category deleted.', "poll-maker")); |
| 417 |
} |
| 418 |
|
| 419 |
if (empty($updated_message)) { |
| 420 |
return; |
| 421 |
} |
| 422 |
|
| 423 |
?> |
| 424 |
<div class="ays-poll-admin-notice notice notice-success is-dismissible"> |
| 425 |
<p> <?php echo $updated_message; ?> </p> |
| 426 |
</div> |
| 427 |
<?php |
| 428 |
} |
| 429 |
|
| 430 |
public static function get_where_condition(){ |
| 431 |
global $wpdb; |
| 432 |
|
| 433 |
$where = array(); |
| 434 |
$sql = ''; |
| 435 |
|
| 436 |
// Search by title |
| 437 |
$search = ( isset( $_REQUEST['s'] ) ) ? esc_sql( sanitize_text_field( $_REQUEST['s'] ) ) : false; |
| 438 |
if( $search ){ |
| 439 |
$where[] = sprintf("title LIKE '%%%s%%'", esc_sql( $wpdb->esc_like( $search ) ) ); |
| 440 |
} |
| 441 |
|
| 442 |
if( !empty($where) ){ |
| 443 |
$sql = " WHERE " . implode( " AND ", $where ); |
| 444 |
} |
| 445 |
|
| 446 |
return $sql; |
| 447 |
} |
| 448 |
|
| 449 |
} |