PluginProbe
Poll Maker by AYS – Versus Polls, Anonymous Polls, Image Polls / 5.6.0
Poll Maker by AYS – Versus Polls, Anonymous Polls, Image Polls v5.6.0
6.5.0 6.4.9 6.4.8 6.4.7 6.4.6 6.4.5 6.4.4 6.4.3 6.4.2 6.4.1 6.4.0 6.3.9 6.3.8 6.3.7 6.3.6 6.3.5 6.3.4 6.3.3 5.6.0 5.6.1 5.6.2 5.6.3 5.6.4 5.6.5 5.6.6 All 153 releases
poll-maker / includes / lists / class-poll-maker-results-list-table.php

class-poll-maker-results-list-table.php in Poll Maker by AYS – Versus Polls, Anonymous Polls, Image Polls 5.6.0, at includes/lists/class-poll-maker-results-list-table.php

573 lines 16.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 ob_start();
3
4 class Pma_Results_List_Table extends WP_List_Table {
5 private $plugin_name;
6 private $results_obj;
7 private $title_length;
8
9 /** Class constructor */
10 public function __construct( $plugin_name ) {
11 $this->plugin_name = $plugin_name;
12 $this->title_length = Poll_Maker_Ays_Admin::get_listtables_title_length('results');
13 parent::__construct(array(
14 'singular' => __('Result', "poll-maker"), //singular name of the listed records
15 'plural' => __('Results', "poll-maker"), //plural name of the listed records
16 'ajax' => false, //does this table support ajax?
17 ));
18 add_action('admin_notices', array($this, 'results_notices'));
19
20 }
21
22 /**
23 * Retrieve customers data from the database
24 *
25 * @param int $per_page
26 * @param int $page_number
27 *
28 * @return mixed
29 */
30 public static function get_reports( $per_page = 50, $page_number = 1 ) {
31
32 global $wpdb;
33
34 $sql = "SELECT
35 {$wpdb->prefix}ayspoll_polls.id,
36 SUM({$wpdb->prefix}ayspoll_answers.votes) AS voted,
37 {$wpdb->prefix}ayspoll_answers.poll_id,
38 {$wpdb->prefix}ayspoll_polls.categories
39 FROM
40 {$wpdb->prefix}ayspoll_answers
41 INNER JOIN
42 {$wpdb->prefix}ayspoll_polls
43 ON {$wpdb->prefix}ayspoll_answers.poll_id = {$wpdb->prefix}ayspoll_polls.id ";
44
45 if (isset($_REQUEST['orderbypoll']) && $_REQUEST['orderbypoll'] > 0) {
46 $poll_id = absint(sanitize_text_field( $_REQUEST['orderbypoll'] ));
47
48 $sql .= " AND {$wpdb->prefix}ayspoll_reports.answer_id IN (SELECT {$wpdb->prefix}ayspoll_answers.id FROM {$wpdb->prefix}ayspoll_answers WHERE {$wpdb->prefix}ayspoll_answers.poll_id='$poll_id')";
49 }
50
51 if (isset($_REQUEST['orderbycat']) && $_REQUEST['orderbycat'] > 0) {
52 $cat_id = absint(sanitize_text_field( $_REQUEST['orderbycat'] ));
53 $sql .= " AND {$wpdb->prefix}ayspoll_polls.categories LIKE('%,{$cat_id},%')";
54 }
55
56 $sql .= "GROUP BY {$wpdb->prefix}ayspoll_answers.poll_id";
57 if (!empty($_REQUEST['orderby'])) {
58 $order_by = ( isset( $_REQUEST['orderby'] ) && sanitize_text_field( $_REQUEST['orderby'] ) != '' ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'id';
59 $order_by .= ( ! empty( $_REQUEST['order'] ) && strtolower( $_REQUEST['order'] ) == 'asc' ) ? ' ASC' : ' DESC';
60
61 $sql_orderby = sanitize_sql_orderby($order_by);
62
63 if ( $sql_orderby ) {
64 $sql .= ' ORDER BY ' . $sql_orderby;
65 } else {
66 $sql .= ' ORDER BY id DESC';
67 }
68 } else {
69 $sql .= ' ORDER BY id DESC';
70 }
71
72 $sql .= " LIMIT %d";
73 $args[] = $per_page;
74 $offset = ($page_number - 1) * $per_page;
75 $sql .= " OFFSET %d";
76 $args[] = $offset;
77
78 $result = $wpdb->get_results(
79 $wpdb->prepare( $sql, $args),
80 'ARRAY_A'
81 );
82
83 return $result;
84 }
85
86 public function display_tablenav( $which ) {
87 ?>
88 <div class="tablenav <?php echo esc_attr( $which ); ?>">
89
90 <div class="alignleft actions">
91 <?php $this->bulk_actions( $which ); ?>
92 </div>
93 <?php
94 $this->extra_tablenav( $which );
95 $this->pagination( $which );
96 ?>
97 <br class="clear" />
98 </div>
99 <?php
100 }
101
102 public function extra_tablenav($which) {
103 $this->ays_category_filter_content();
104 }
105
106 public function get_report_by_id( $id ) {
107 global $wpdb;
108 $report_id = absint(sanitize_text_field($id));
109 $report_table = esc_sql($wpdb->prefix."ayspoll_reports");
110 $sql = "SELECT * FROM ".$report_table." WHERE id=%d";
111 $result = $wpdb->get_row(
112 $wpdb->prepare( $sql, $report_id),
113 'ARRAY_A'
114 );
115
116 return $result;
117 }
118
119 public static function get_answer_by_id( $id ) {
120 global $wpdb;
121
122 $answ_id = absint(sanitize_text_field($id));
123 $answ_table = esc_sql($wpdb->prefix."ayspoll_answers");
124
125 $sql = "SELECT * FROM ".$answ_table." WHERE id=%d";
126
127 $result = $wpdb->get_row(
128 $wpdb->prepare( $sql, $answ_id),
129 'ARRAY_A'
130 );
131
132 return $result;
133 }
134
135 public function get_polls() {
136 global $wpdb;
137
138 $poll_table = esc_sql($wpdb->prefix."ayspoll_polls");
139 $sql = "SELECT * FROM ".$poll_table;
140
141 $result = $wpdb->get_results($sql, 'ARRAY_A');
142
143 return $result;
144 }
145
146 /**
147 * Delete a customer record.
148 *
149 * @param int $id customer ID
150 */
151 public static function delete_reports( $id ) {
152
153 global $wpdb;
154
155 $wpdb->update(
156 "{$wpdb->prefix}ayspoll_answers",
157 array(
158 "votes" => 0
159 ),
160 array(
161 'poll_id' => $id
162 )
163 );
164
165 $sql = "DELETE r FROM {$wpdb->prefix}ayspoll_reports as r
166 JOIN {$wpdb->prefix}ayspoll_answers ON {$wpdb->prefix}ayspoll_answers.id = r.answer_id
167 WHERE {$wpdb->prefix}ayspoll_answers.poll_id = $id";
168 $res = $wpdb->query($sql);
169
170 return $res > 0;
171 }
172
173 /**
174 * Returns the count of records in the database.
175 *
176 * @return null|string
177 */
178 public static function record_count() {
179 global $wpdb;
180
181 $sql = "SELECT COUNT(*) FROM {$wpdb->prefix}ayspoll_polls";
182 return $wpdb->get_var($sql);
183 }
184
185 /** Text displayed when no customer data is available */
186 public function no_items() {
187 _e('There are no results yet.', "poll-maker");
188 }
189
190 /**
191 * Render a column when no column specific method exist.
192 *
193 * @param array $item
194 * @param string $column_name
195 *
196 * @return mixed
197 */
198 public function column_default( $item, $column_name ) {
199 switch ( $column_name ) {
200 case 'poll_title':
201 case 'voted':
202 case 'unread':
203 case 'id':
204 return $item[$column_name];
205 break;
206 default:
207 return print_r($item, true); //Show the whole array for troubleshooting purposes
208 }
209 }
210
211 /**
212 * Render the bulk edit checkbox
213 *
214 * @param array $item
215 *
216 * @return string
217 */
218 function column_cb( $item ) {
219 return sprintf(
220 '<input type="checkbox" name="bulk-action[]" value="%s">', $item['id']
221 );
222 }
223
224 function column_poll_title( $item ) {
225 global $wpdb;
226 $delete_nonce = wp_create_nonce($this->plugin_name . '-delete-result');
227
228 $res = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}ayspoll_polls WHERE id={$item['id']}", "ARRAY_A");
229
230 $restitle = Poll_Maker_Ays_Admin::ays_restriction_string("word",stripcslashes($res['title']), $this->title_length);
231 $title = sprintf('<a href="?page=%s-each&poll=%d&title=%s">' . $restitle . '</a>', esc_attr($_REQUEST['page']), absint($item['id']), stripslashes($res['title']));
232 $actions = [
233 'delete' => sprintf('<a href="?page=%s&action=%s&result=%s&_wpnonce=%s">Delete</a>', esc_attr($_REQUEST['page']), 'delete', absint($item['id']), $delete_nonce),
234 ];
235
236 return $title . $this->row_actions($actions);
237 }
238
239 function get_poll_title( $item ) {
240 global $wpdb;
241 $delete_nonce = wp_create_nonce($this->plugin_name . '-delete-result');
242
243 $result = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}ayspoll_answers WHERE id={$item['answer_id']}", "ARRAY_A");
244
245 $res = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}ayspoll_polls WHERE id={$result['poll_id']}", "ARRAY_A");
246
247 return stripslashes($res['title']);
248 }
249
250 function column_answer( $item ) {
251 global $wpdb;
252 $delete_nonce = wp_create_nonce($this->plugin_name . '-delete-result');
253
254 $result = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}ayspoll_answers WHERE id={$item['answer_id']}", "ARRAY_A");
255
256 $poll_type = $this->get_poll_type($result['poll_id']);
257
258 $result['answer'] = $poll_type['type'] == 'voting' && $result['answer'] == '1' ? 'Like' : $result['answer'];
259 $result['answer'] = $poll_type['type'] == 'voting' && $result['answer'] == '-1' ? 'Dislike' : $result['answer'];
260
261 return stripslashes($result['answer']);
262 }
263
264 function column_voted( $item ) {
265 global $wpdb;
266 $sql = "SELECT
267 COUNT({$wpdb->prefix}ayspoll_reports.id)
268 FROM
269 {$wpdb->prefix}ayspoll_reports
270 JOIN {$wpdb->prefix}ayspoll_answers ON {$wpdb->prefix}ayspoll_answers.id = {$wpdb->prefix}ayspoll_reports.answer_id
271 WHERE {$wpdb->prefix}ayspoll_answers.poll_id = {$item['id']}
272 GROUP BY {$wpdb->prefix}ayspoll_answers.poll_id";
273 $res = $wpdb->get_var($sql);
274
275 return $res ? $res : 0;
276 }
277
278 function column_unread( $item ) {
279 global $wpdb;
280 $sql = "SELECT
281 COUNT({$wpdb->prefix}ayspoll_reports.unread)
282 FROM
283 {$wpdb->prefix}ayspoll_reports
284 JOIN {$wpdb->prefix}ayspoll_answers ON {$wpdb->prefix}ayspoll_answers.id = {$wpdb->prefix}ayspoll_reports.answer_id
285 WHERE {$wpdb->prefix}ayspoll_answers.poll_id = {$item['id']} AND {$wpdb->prefix}ayspoll_reports.unread = 1
286 GROUP BY {$wpdb->prefix}ayspoll_answers.poll_id";
287 $res = $wpdb->get_var($sql);
288 $result = $res ? $res : 0;
289 $unread = $res ? 'ays_poll_unread' : '';
290 $count = sprintf('<span class="%s">' . $result . '</span>', $unread);
291 return $count;
292 }
293
294 /**
295 * Associative array of columns
296 *
297 * @return array
298 */
299 function get_columns() {
300 $columns = array(
301 'cb' => '<input type="checkbox" />',
302 'id' => __('ID',"poll-maker"),
303 'poll_title' => __('Poll',"poll-maker"),
304 'voted' => __('Voters count',"poll-maker"),
305 'unread' => __('New results count',"poll-maker")
306 );
307
308 return $columns;
309 }
310
311 /**
312 * Columns to make sortable.
313 *
314 * @return array
315 */
316 public function get_sortable_columns() {
317 $sortable_columns = array(
318 'vote_date' => array('vote_date', true),
319 'id' => array('id', true),
320 );
321
322 return $sortable_columns;
323 }
324
325 /**
326 * Returns an associative array containing the bulk action
327 *
328 * @return array
329 */
330 public function get_bulk_actions() {
331 $actions = array(
332 'bulk-read' => __('Mark as read', "poll-maker"),
333 'bulk-delete' => __('Delete', "poll-maker"),
334 );
335
336 return $actions;
337 }
338
339 /**
340 * Handles data query and filter, sorting, and pagination.
341 */
342 public function prepare_items() {
343
344 $this->_column_headers = $this->get_column_info();
345
346 /** Process bulk action */
347 $this->process_bulk_action();
348
349 $per_page = $this->get_items_per_page('poll_results_per_page', 50);
350
351 $current_page = $this->get_pagenum();
352 $total_items = self::record_count();
353
354 $this->set_pagination_args(array(
355 'total_items' => $total_items, //WE have to calculate the total number of items
356 'per_page' => $per_page, //WE have to determine how many items to show on a page
357 ));
358
359 $this->items = self::get_reports($per_page, $current_page);
360 }
361
362 public function process_bulk_action() {
363 //Detect when a bulk action is being triggered...
364
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-result')) {
371 die('Go get a life script kiddies');
372 } else {
373 self::delete_reports(absint($_GET['result']));
374
375 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
376 // add_query_arg() return the current url
377 $message = 'deleted';
378 $url = esc_url_raw(remove_query_arg([
379 'action',
380 'result',
381 '_wpnonce'
382 ])) . '&status=' . $message;
383 wp_redirect($url);
384 }
385
386 }
387
388 // If the delete bulk action is triggered
389 if ((isset($_POST['action']) && 'bulk-delete' == $_POST['action'])
390 || (isset($_POST['action2']) && 'bulk-delete' == $_POST['action2'])
391 ) {
392 $delete_ids = esc_sql($_POST['bulk-action']);
393
394 // loop over the array of record IDs and delete them
395 foreach ( $delete_ids as $id ) {
396 self::delete_reports($id);
397 }
398
399 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
400 // add_query_arg() return the current url
401
402 $message = 'deleted';
403 $url = esc_url_raw(remove_query_arg(['action', 'result', '_wpnonce'])) . '&status=' . $message;
404 wp_redirect($url);
405 } elseif ((isset($_POST['action']) && 'bulk-read' == $_POST['action'])
406 || (isset($_POST['action2']) && 'bulk-read' == $_POST['action2'])
407 ) {
408
409 $read_ids = esc_sql($_POST['bulk-action']);
410
411 // loop over the array of record IDs and mark as readed them
412 foreach ( $read_ids as $id ) {
413 echo $id . "<br>";
414 self::mark_as_read_reports($id);
415 }
416
417 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
418 // add_query_arg() return the current url
419
420 $message = 'read';
421 $url = esc_url_raw(remove_query_arg(['action', 'result', '_wpnonce'])) . '&status=' . $message;
422 wp_redirect($url);
423 }
424 }
425
426 /**
427 * Mark as read a result record.
428 *
429 * @param int $id result ID
430 */
431 public static function mark_as_read_reports( $id ) {
432 global $wpdb;
433 $sql = "UPDATE {$wpdb->prefix}ayspoll_reports
434 JOIN {$wpdb->prefix}ayspoll_answers ON {$wpdb->prefix}ayspoll_answers.id = {$wpdb->prefix}ayspoll_reports.answer_id
435 SET {$wpdb->prefix}ayspoll_reports.unread = 0
436 WHERE {$wpdb->prefix}ayspoll_answers.poll_id = $id";
437 $res = $wpdb->query($sql);
438
439 return $res > 0 ? true : false;
440 }
441
442 public function results_notices() {
443 $status = (isset($_REQUEST['status'])) ? sanitize_text_field($_REQUEST['status']) : '';
444
445 if (empty($status)) {
446 return;
447 }
448
449 if ('deleted' == $status) {
450 $updated_message = esc_html(__('Result deleted.', "poll-maker"));
451 }
452
453 if (empty($updated_message)) {
454 return;
455 }
456
457 ?>
458 <div class="notice notice-success is-dismissible">
459 <p> <?php echo $updated_message; ?> </p>
460 </div>
461 <?php
462 }
463
464 public function get_poll_type($item) {
465 global $wpdb;
466
467 $id = absint(sanitize_text_field($item));
468
469 $poll_table = esc_sql($wpdb->prefix."ayspoll_polls");
470 $sql = "SELECT type FROM ".$poll_table." WHERE id=%d";
471
472 $result = $wpdb->get_row(
473 $wpdb->prepare( $sql, $id),
474 'ARRAY_A'
475 );
476
477 return $result;
478 }
479
480 public function ays_category_filter_content(){
481 $poll_cats = $this->get_categories();
482 $content = "";
483 if(isset($poll_cats)){
484 $content = '<label for="bulk-action-selector-top-cat" class="screen-reader-text">Select Filter Type</label>
485 <select name="orderbycat" id="bulk-action-selector-top-cat">
486 <option value="0" selected>' .__("Select Category", "poll-maker"). '</option>';
487 $selected = "";
488 $this_cat_id = 0;
489 foreach ($poll_cats as $cat_key => $cat_value) {
490 $selected = (isset($_REQUEST['orderbycat']) && $_REQUEST['orderbycat'] == $cat_value['id'] ) ? 'selected' : '';
491 $this_cat_id = isset($_REQUEST['orderbycat']) ? $_REQUEST['orderbycat'] : $this_cat_id;
492 $cat_id = isset($cat_value['id']) && $cat_value['id'] != "" ? esc_attr($cat_value['id']) : "";
493 $cat_value = isset($cat_value['title']) && $cat_value['title'] != "" ? esc_attr($cat_value['title']) : "";
494 $content .= '<option value="'.$cat_id.'" '.$selected.'>'.$cat_value.'</option>';
495 }
496 $content .= '</select>';
497 $content .= '<input type="submit" id="doactioncat" name="filter_by_cat" class="button action" value="'.__("Filter", "poll-maker").'" style="width: 3.7rem;margin-left: 5px;">';
498 if(isset($_REQUEST['filter_by_cat'])){
499 $new_url = remove_query_arg("orderbycat")."&orderbycat=".$this_cat_id;
500 wp_redirect($new_url);
501 }
502 }
503 echo $content;
504 }
505
506 public function get_categories() {
507 global $wpdb;
508 $category_table = $wpdb->prefix . 'ayspoll_categories';
509
510 $sql = "SELECT id,title FROM ".$category_table." ORDER BY title";
511 $results = $wpdb->get_results($sql , "ARRAY_A");
512
513 if(isset($results) && !empty($results)){
514 return $results;
515 }
516 else{
517 return array();
518 }
519 }
520
521 // Get answers modified
522 public function get_poll_answers($poll_id){
523 global $wpdb;
524
525 $sql = "SELECT * FROM `{$wpdb->prefix}ayspoll_reports` WHERE poll_id=$poll_id";
526 $result = $wpdb->get_results($sql, "ARRAY_A");
527
528 $multivote_answers_ids = array();
529 $multivote_answers = array();
530
531 foreach ($result as $r_key => $r_value) {
532
533 $r_value['multi_answer_id'] = json_decode($r_value['multi_answer_ids'], true);
534
535 $multivote_res = false;
536 if (isset($r_value['multi_answer_id']) && count($r_value['multi_answer_id']) > 0) {
537 $multivote_res = true;
538 }
539
540 if ($multivote_res) {
541 foreach ($r_value['multi_answer_id'] as $m_key => $m_val) {
542 $multivote_answers_ids[] = $m_val;
543 $multi_answer = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}ayspoll_answers WHERE id=".$m_val, "ARRAY_A");
544 $multivote_answers[ $m_val ] = $multi_answer['answer'];
545 }
546 $answ_poll_id = $multi_answer['poll_id'];
547 } else {
548 $answer = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}ayspoll_answers WHERE id={$r_value['answer_id']}", "ARRAY_A");
549 $multivote_answers_ids[] = $r_value['answer_id'];
550 $multivote_answers[ $r_value['answer_id'] ] = $answer['answer'];
551 $answ_poll_id = $answer['poll_id'];
552 }
553 }
554
555 $multivote_answers_count_arr = array_count_values( $multivote_answers_ids );
556
557 $res = array();
558 foreach ($multivote_answers_count_arr as $key => $value) {
559 $res[] = array(
560 "votes" => $value,
561 "answer" => $multivote_answers[ $key ],
562 );
563 }
564
565 if ( is_null( $res ) || empty( $res ) ) {
566 $res = array();
567 }
568
569 return $res;
570 }
571
572 }
573