PluginProbe
Poll Maker by AYS – Versus Polls, Anonymous Polls, Image Polls / 5.6.3
Poll Maker by AYS – Versus Polls, Anonymous Polls, Image Polls v5.6.3
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 5.6.3, at includes/lists/class-poll-maker-categories-list-table.php

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