PluginProbe
Poll Maker by AYS – Versus Polls, Anonymous Polls, Image Polls / 6.4.9
Poll Maker by AYS – Versus Polls, Anonymous Polls, Image Polls v6.4.9
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-categories-list-table.php

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

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