PluginProbe
Poll Maker by AYS – Versus Polls, Anonymous Polls, Image Polls / 5.6.5
Poll Maker by AYS – Versus Polls, Anonymous Polls, Image Polls v5.6.5
6.5.1 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 All 154 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.5, at includes/lists/class-poll-maker-results-list-table.php

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