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

451 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(*) 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'])) . "'";
283
284 $result = $wpdb->get_var($sql);
285
286 if ( isset($result) && $result > 0 ) {
287 $result = sprintf( '<a href="?page=%s&filterby=%d" target="_blank">%s</a>', 'poll-maker-ays', $item['id'], $result );
288 }
289
290 return "<p style='font-size:14px;'>" . $result . "</p>";
291 }
292
293 /**
294 * Associative array of columns
295 *
296 * @return array
297 */
298 function get_columns() {
299 $columns = array(
300 'cb' => '<input type="checkbox">',
301 'title' => __('Title', "poll-maker"),
302 'description' => __('Description', "poll-maker"),
303 'shortcode' => __('Shortcode', "poll-maker"),
304 'polls' => __('Polls', "poll-maker"),
305 'id' => __('ID', "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 'title' => array('title', 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-delete' => __('Delete', "poll-maker"),
333 );
334
335 return $actions;
336 }
337
338 /**
339 * Handles data query and filter, sorting, and pagination.
340 */
341 public function prepare_items() {
342
343 $this->_column_headers = $this->get_column_info();
344
345 /** Process bulk action */
346 $this->process_bulk_action();
347
348 $per_page = $this->get_items_per_page('poll_cats_per_page', 20);
349 $current_page = $this->get_pagenum();
350 $total_items = self::record_count();
351
352 $this->set_pagination_args(array(
353 'total_items' => $total_items, //WE have to calculate the total number of items
354 'per_page' => $per_page, //WE have to determine how many items to show on a page
355 ));
356
357 $search = ( isset( $_REQUEST['s'] ) ) ? sanitize_text_field( $_REQUEST['s'] ) : false;
358
359 $do_search = ( $search ) ? sprintf(" title LIKE '%%%s%%' ", $search ) : '';
360
361 $this->items = self::get_poll_categories($per_page, $current_page, $do_search);
362
363 }
364
365 public function process_bulk_action() {
366 //Detect when a bulk action is being triggered...
367 if ('delete' === $this->current_action()) {
368
369 // In our file that handles the request, verify the nonce.
370 $nonce = esc_attr($_REQUEST['_wpnonce']);
371
372 if (!wp_verify_nonce($nonce, $this->plugin_name . '-delete-poll-category')) {
373 die('Go get a life script kiddies');
374 } else {
375 self::delete_poll_categories(absint($_GET['poll_category']));
376
377 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
378 // add_query_arg() return the current url
379
380 $url = esc_url_raw(remove_query_arg(['action', 'poll_category', '_wpnonce'])) . '&status=deleted';
381 wp_redirect($url);
382 }
383
384 }
385
386 // If the delete bulk action is triggered
387 if ((isset($_POST['action']) && 'bulk-delete' == $_POST['action'])
388 || (isset($_POST['action2']) && 'bulk-delete' == $_POST['action2'])
389 ) {
390
391 $delete_ids = ( isset( $_POST['bulk-delete'] ) && ! empty( $_POST['bulk-delete'] ) ) ? esc_sql( $_POST['bulk-delete'] ) : array();
392
393 // loop over the array of record IDs and delete them
394 foreach ( $delete_ids as $id ) {
395 self::delete_poll_categories($id);
396
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 $url = esc_url_raw(remove_query_arg(['action', 'poll_category', '_wpnonce'])) . '&status=deleted';
402 wp_redirect($url);
403 }
404 }
405
406 public function poll_category_notices() {
407 $status = (isset($_REQUEST['status'])) ? sanitize_text_field($_REQUEST['status']) : '';
408
409 if (empty($status)) {
410 return;
411 }
412
413 if ('created' == $status) {
414 $updated_message = esc_html(__('Poll category created.', "poll-maker"));
415 } elseif ('updated' == $status) {
416 $updated_message = esc_html(__('Poll category saved.', "poll-maker"));
417 } elseif ('deleted' == $status) {
418 $updated_message = esc_html(__('Poll category deleted.', "poll-maker"));
419 }
420
421 if (empty($updated_message)) {
422 return;
423 }
424
425 ?>
426 <div class="notice notice-success is-dismissible">
427 <p> <?php echo $updated_message; ?> </p>
428 </div>
429 <?php
430 }
431
432 public static function get_where_condition(){
433 global $wpdb;
434
435 $where = array();
436 $sql = '';
437
438 // Search by title
439 $search = ( isset( $_REQUEST['s'] ) ) ? esc_sql( sanitize_text_field( $_REQUEST['s'] ) ) : false;
440 if( $search ){
441 $where[] = sprintf("title LIKE '%%%s%%'", esc_sql( $wpdb->esc_like( $search ) ) );
442 }
443
444 if( !empty($where) ){
445 $sql = " WHERE " . implode( " AND ", $where );
446 }
447
448 return $sql;
449 }
450
451 }