PluginProbe
Survey Maker by AYS / 5.2.0.8
Survey Maker by AYS v5.2.0.8
5.2.4.1 5.2.4.0 5.2.3.9 5.2.3.8 5.2.3.7 5.2.3.6 5.2.3.5 5.2.3.4 5.2.3.3 5.2.3.2 5.2.3.1 5.2.3.0 5.2.2.9 5.2.2.8 5.2.2.7 5.2.2.6 5.2.2.5 5.2.2.4 5.2.2.3 5.2.2.2 5.2.2.1 5.2.2.0 5.2.1.9 5.2.1.8 5.2.0.8 All 154 releases
survey-maker / includes / lists / class-survey-maker-survey-categories-list-table.php

class-survey-maker-survey-categories-list-table.php in Survey Maker by AYS 5.2.0.8, at includes/lists/class-survey-maker-survey-categories-list-table.php

935 lines 35.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 ob_start();
3 class Survey_Categories_List_Table extends WP_List_Table {
4
5 /**
6 * The ID of this plugin.
7 *
8 * @since 1.0.0
9 * @access private
10 * @var string $plugin_name The ID of this plugin.
11 */
12 private $plugin_name;
13
14 /**
15 * The table name in database of the survey categories.
16 *
17 * @since 1.0.0
18 * @access private
19 * @var string $table_name The table name in database of the survey categories.
20 */
21 private $table_name;
22
23 private $title_length;
24
25 /** Class constructor */
26 public function __construct( $plugin_name ) {
27 global $wpdb;
28
29 $this->plugin_name = $plugin_name;
30
31 $this->table_name = $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories";
32
33 $this->title_length = Survey_Maker_Data::get_listtables_title_length('survey_categories');
34
35 parent::__construct( array(
36 'singular' => __( 'Survey Category', "survey-maker" ), //singular name of the listed records
37 'plural' => __( 'Survey Categories', "survey-maker" ), //plural name of the listed records
38 'ajax' => false //does this table support ajax?
39 ) );
40
41 add_action( 'admin_notices', array( $this, 'survey_category_notices' ) );
42 }
43
44 /**
45 * Override of table nav to avoid breaking with bulk actions & according nonce field
46 */
47 public function display_tablenav( $which ) {
48 ?>
49 <div class="tablenav <?php echo esc_attr( $which ); ?>">
50
51 <div class="alignleft actions">
52 <?php $this->bulk_actions( $which ); ?>
53 </div>
54
55 <?php
56 $this->extra_tablenav( $which );
57 $this->pagination( $which );
58 ?>
59 <br class="clear" />
60 </div>
61 <?php
62 }
63
64 /**
65 * Disables the views for 'side' context as there's not enough free space in the UI
66 * Only displays them on screen/browser refresh. Else we'd have to do this via an AJAX DB update.
67 *
68 * @see WP_List_Table::extra_tablenav()
69 */
70 public function extra_tablenav($which) {
71
72 $survey_cat_description = array(
73 "with" => __( "With description", "survey-maker"),
74 "without" => __( "Without description", "survey-maker"),
75 );
76
77 $description_key = null;
78
79 if( isset( $_GET['filterbyDescription'] ) && sanitize_text_field( $_GET['filterbyDescription'] ) != ""){
80 $description_key = sanitize_text_field( $_GET['filterbyDescription'] );
81 }
82
83 ?>
84
85 <div id="survey-filter-div-<?php echo esc_attr( $which ); ?>" class="alignleft actions bulkactions">
86
87 <select name="filterbyDescription-<?php echo esc_attr( $which ); ?>" id="bulk-action-survey-cat-description-selector-<?php echo esc_attr( $which ); ?>">
88 <option value=""><?php echo esc_html__('With/without description',"survey-maker"); ?></option>
89 <?php
90 foreach($survey_cat_description as $key => $cat_description) {
91 $selected = "";
92 if( $description_key === sanitize_text_field($key) ) {
93 $selected = "selected";
94 }
95 echo "<option ".esc_attr($selected)." value='".esc_attr( $key )."'>".esc_html($cat_description)."</option>";
96 }
97 ?>
98 </select>
99 <input type="button" id="doaction-survey-<?php echo esc_attr( $which ); ?>" class="ays-survey-question-tab-all-filter-button-<?php echo esc_attr( $which ); ?> button" value="<?php echo esc_html__( "Filter", "survey-maker" ); ?>">
100 </div>
101
102 <a style="" href="?page=<?php echo esc_attr( sanitize_text_field( $_REQUEST['page'] ) ); ?>" class="button"><?php echo esc_html__( "Clear filters", "survey-maker" ); ?></a>
103 <?php
104 }
105
106
107 protected function get_views() {
108 $published_count = $this->get_statused_record_count( 'published' );
109 $draft_count = $this->get_statused_record_count( 'draft' );
110 $trashed_count = $this->get_statused_record_count( 'trashed' );
111 $all_count = $this->all_record_count();
112 $selected_all = "";
113 $selected_published = "";
114 $selected_draft = "";
115 $selected_trashed = "";
116 if( isset( $_GET['fstatus'] ) ){
117 switch( sanitize_text_field( $_GET['fstatus'] ) ){
118 case "published":
119 $selected_published = " style='font-weight:bold;' ";
120 break;
121 case "draft":
122 $selected_draft = " style='font-weight:bold;' ";
123 break;
124 case "trashed":
125 $selected_trashed = " style='font-weight:bold;' ";
126 break;
127 default:
128 $selected_all = " style='font-weight:bold;' ";
129 break;
130 }
131 }else{
132 $selected_all = " style='font-weight:bold;' ";
133 }
134 $status_links = array(
135 "all" => "<a ".$selected_all." href='?page=".sanitize_text_field( $_REQUEST['page'] )."'>" . __( "All", "survey-maker" ) . " (".esc_attr($all_count).")</a>",
136 );
137 if( intval( $published_count ) > 0 ){
138 $status_links["published"] = "<a ".$selected_published." href='?page=".sanitize_text_field( $_REQUEST['page'] )."&fstatus=published'>" . __( "Published", "survey-maker" ) . " (".esc_attr($published_count).")</a>";
139 }
140 if( intval( $draft_count ) > 0 ){
141 $status_links["draft"] = "<a ".$selected_draft." href='?page=".sanitize_text_field( $_REQUEST['page'] )."&fstatus=draft'>" . __( "Draft", "survey-maker" ) . " (".esc_attr($draft_count).")</a>";
142 }
143 if( intval( $trashed_count ) > 0 ){
144 $status_links["trashed"] = "<a ".$selected_trashed." href='?page=".sanitize_text_field( $_REQUEST['page'] )."&fstatus=trashed'>" . __( "Trash", "survey-maker" ) . " (".esc_attr($trashed_count).")</a>";
145 }
146 return $status_links;
147 }
148
149
150 /**
151 * Retrieve customers data from the database
152 *
153 * @param int $per_page
154 * @param int $page_number
155 *
156 * @return mixed
157 */
158 public static function get_items( $per_page = 20, $page_number = 1, $search = "" ) {
159
160 global $wpdb;
161
162 $sql = "SELECT * FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories";
163
164 $where = array();
165
166 if ( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != ''){
167 $where[] = ' status = "' . esc_sql( sanitize_text_field( $_GET['fstatus'] ) ) . '" ';
168 }else{
169 $where[] = ' status != "trashed" ';
170 }
171
172 if( isset( $_GET['filterbyDescription'] ) && sanitize_text_field( $_GET['filterbyDescription'] ) != ""){
173 $description_key = sanitize_text_field( $_GET['filterbyDescription'] );
174
175 switch ( $description_key ) {
176 case 'with':
177 $where[] = ' `description` != "" ';
178 break;
179 case 'without':
180 default:
181 $where[] = ' `description` = "" ';
182 break;
183 }
184 }
185
186 if( $search != '' ){
187 $where[] = $search;
188 }
189
190 if ( ! empty( $where ) ){
191 $sql .= ' WHERE ' . implode( ' AND ', $where );
192 }
193
194 if ( ! empty( $_REQUEST['orderby'] ) ) {
195 $order_by = ( isset( $_REQUEST['orderby'] ) && sanitize_text_field( $_REQUEST['orderby'] ) != '' ) ? sanitize_text_field( $_REQUEST['orderby'] ) : 'id';
196 $order_by .= ( ! empty( $_REQUEST['order'] ) && strtolower( $_REQUEST['order'] ) == 'asc' ) ? ' ASC' : ' DESC';
197
198 $sql_orderby = sanitize_sql_orderby( $order_by );
199
200 if ( $sql_orderby ) {
201 $sql .= ' ORDER BY ' . $sql_orderby;
202 } else {
203 $sql .= ' ORDER BY id DESC';
204 }
205 }else{
206 $sql .= ' ORDER BY id DESC';
207 }
208
209 $sql .= " LIMIT $per_page";
210 $sql .= ' OFFSET ' . ( $page_number - 1 ) * $per_page;
211
212
213 $result = $wpdb->get_results( $sql, 'ARRAY_A' );
214
215 return $result;
216 }
217
218 public static function get_item_by_id( $id ) {
219 global $wpdb;
220
221 $sql = "SELECT * FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories WHERE id=" . esc_sql( absint( $id ) );
222
223 $result = $wpdb->get_row( $sql, 'ARRAY_A' );
224
225 return $result;
226 }
227
228 public function add_or_edit_item( $data ){
229 global $wpdb;
230 $table = $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories";
231
232
233 if( isset( $data["survey_category_action"] ) && wp_verify_nonce( $data["survey_category_action"], 'survey_category_action' ) ){
234
235 $name_prefix = 'ays_';
236
237 // Save type
238 $save_type = (isset($data['save_type'])) ? $data['save_type'] : '';
239
240 // Id of item
241 $id = isset( $data['id'] ) ? absint( intval( $data['id'] ) ) : 0;
242
243 // Title
244 $title = isset( $data[ $name_prefix . 'title' ] ) && $data[ $name_prefix . 'title' ] != '' ? stripslashes( sanitize_text_field( $data[ $name_prefix . 'title' ] ) ) : '';
245
246 if($title == ''){
247 $url = esc_url_raw( remove_query_arg( false ) );
248 wp_redirect( $url );
249 }
250
251 // Description
252 // Sanitize description to allow only safe HTML tags and remove scripts.
253 $description = isset( $data[ $name_prefix . 'description' ] ) && $data[ $name_prefix . 'description' ] != '' ? stripslashes( wp_kses_post( $data[ $name_prefix . 'description' ] ) ) : '';
254
255 // Status
256 $status = isset( $data[ $name_prefix . 'status' ] ) && $data[ $name_prefix . 'status' ] != '' ? stripslashes( sanitize_text_field($data[ $name_prefix . 'status' ]) ) : 'published';
257
258 // Date created
259 $date_created = isset( $data[ $name_prefix . 'date_created' ] ) && Survey_Maker_Admin::validateDate( $data[ $name_prefix . 'date_created' ] ) ? stripslashes( sanitize_text_field($data[ $name_prefix . 'date_created' ]) ) : current_time( 'mysql' );
260
261 // Date modified
262 $date_modified = isset( $data[ $name_prefix . 'date_modified' ] ) && Survey_Maker_Admin::validateDate( $data[ $name_prefix . 'date_modified' ] ) ? stripslashes( sanitize_text_field($data[ $name_prefix . 'date_modified' ]) ) : current_time( 'mysql' );
263
264 // Options
265 $options = array(
266
267 );
268
269 $message = '';
270 if( $id == 0 ){
271 $result = $wpdb->insert(
272 $table,
273 array(
274 'title' => $title,
275 'description' => $description,
276 'status' => $status,
277 'date_created' => $date_created,
278 'date_modified' => $date_modified,
279 'options' => json_encode( $options ),
280 ),
281 array(
282 '%s', // title
283 '%s', // description
284 '%s', // status
285 '%s', // date_created
286 '%s', // date_modified
287 '%s', // options
288 )
289 );
290
291 $inserted_id = $wpdb->insert_id;
292 $message = 'created';
293 }else{
294 $result = $wpdb->update(
295 $table,
296 array(
297 'title' => $title,
298 'description' => $description,
299 'status' => $status,
300 'date_modified' => $date_modified,
301 'options' => json_encode( $options ),
302 ),
303 array( 'id' => $id ),
304 array(
305 '%s', // title
306 '%s', // description
307 '%s', // status
308 '%s', // date_modified
309 '%s', // options
310 ),
311 array( '%d' )
312 );
313
314 $inserted_id = $id;
315 $message = 'updated';
316 }
317
318 if( $result >= 0 ) {
319 if($save_type == 'apply'){
320 if($id == 0){
321 $url = esc_url_raw( add_query_arg( array(
322 "action" => "edit",
323 "id" => $inserted_id,
324 "status" => $message
325 ) ) );
326 }else{
327 $url = esc_url_raw( add_query_arg( array(
328 "status" => $message
329 ) ) );
330 }
331 wp_redirect( $url );
332 }elseif($save_type == 'save_new'){
333 $url = remove_query_arg( array('id') );
334 $url = esc_url_raw( add_query_arg( array(
335 "action" => "add",
336 "status" => $message
337 ), $url ) );
338 wp_redirect( $url );
339 }else{
340 $url = remove_query_arg( array('action', 'id') );
341 $url = esc_url_raw( add_query_arg( array(
342 "status" => $message
343 ), $url ) );
344 wp_redirect( $url );
345 }
346 }
347 }
348 }
349
350 public function duplicate_survey_categories( $id ){
351 global $wpdb;
352
353 if ( is_null( $id ) || empty($id) || $id == 0 ) {
354 return;
355 }
356
357 $survey_category_table = $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories";
358 $survey_category_data = self::get_item_by_id( $id );
359
360 $title = (isset($survey_category_data['title']) && $survey_category_data['title'] != "") ? stripslashes( sanitize_text_field( $survey_category_data['title'] ) ) : __("Copy", 'survey-maker');
361 $description = (isset($survey_category_data['description']) && $survey_category_data['description'] != "") ? wp_kses_post( $survey_category_data['description'] ) : "";
362 $status = (isset($survey_category_data['status']) && $survey_category_data['status'] != "") ? sanitize_text_field( $survey_category_data['status'] ) : 'published';
363 $trash_status = (isset($survey_category_data['trash_status']) && $survey_category_data['trash_status'] != "") ? sanitize_text_field( $survey_category_data['trash_status'] ) : '';
364 $date_created = current_time( 'mysql' );
365 $date_modified = $date_created;
366
367 $result = $wpdb->insert(
368 $survey_category_table,
369 array(
370 'title' => "Copy - " . $title,
371 'description' => $description,
372 'status' => $status,
373 'trash_status' => $trash_status,
374 'date_created' => $date_created,
375 'date_modified' => $date_modified,
376 ),
377 array(
378 '%s', // title
379 '%s', // description
380 '%s', // status
381 '%s', // trash_status
382 '%s', // date_created
383 '%s', // date_modified
384 )
385 );
386
387 if( $result >= 0 ){
388 $message = "duplicated";
389 $url = esc_url_raw( remove_query_arg(array('action', 'id', '_wpnonce') ) ) . '&status=' . $message;
390 wp_redirect( $url );
391 }
392 }
393
394 /**
395 * Delete a customer record.
396 *
397 * @param int $id customer ID
398 */
399 public static function delete_items( $id ) {
400 global $wpdb;
401 $wpdb->delete(
402 $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories",
403 array( 'id' => $id ),
404 array( '%d' )
405 );
406 }
407
408 /**
409 * Move to trash a customer record.
410 *
411 * @param int $id customer ID
412 */
413 public static function trash_items( $id ) {
414 global $wpdb;
415 $db_item = self::get_item_by_id( $id );
416 $wpdb->update(
417 $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories",
418 array(
419 'status' => 'trashed',
420 'trash_status' => $db_item['status'],
421 ),
422 array( 'id' => $id ),
423 array( '%s', '%s' ),
424 array( '%d' )
425 );
426 }
427
428 /**
429 * Restore a customer record.
430 *
431 * @param int $id customer ID
432 */
433 public static function restore_items( $id ) {
434 global $wpdb;
435 $db_item = self::get_item_by_id( $id );
436
437 $wpdb->update(
438 $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories",
439 array(
440 'status' => $db_item['trash_status'],
441 'trash_status' => '',
442 ),
443 array( 'id' => $id ),
444 array( '%s', '%s' ),
445 array( '%d' )
446 );
447 }
448
449
450 /**
451 * Returns the count of records in the database.
452 *
453 * @return null|string
454 */
455 public static function record_count($do_search) {
456 global $wpdb;
457 $sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories ";
458 $where = array();
459 $where[] = "status != 'trashed'";
460
461 if( isset( $_GET['filterbyDescription'] ) && sanitize_text_field( $_GET['filterbyDescription'] ) != ""){
462 $description_key = sanitize_text_field( $_GET['filterbyDescription'] );
463
464 switch ( $description_key ) {
465 case 'with':
466 $where[] = ' `description` != "" ';
467 break;
468 case 'without':
469 default:
470 $where[] = ' `description` = "" ';
471 break;
472 }
473 }
474
475 if( $do_search != '' ){
476 $where[] = $do_search;
477 }
478
479 if ( ! empty( $where ) ){
480 $sql .= ' WHERE ' . implode( ' AND ', $where );
481 }
482 return $wpdb->get_var( $sql );
483 }
484
485 public static function all_record_count() {
486 global $wpdb;
487
488 $sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories ";
489 $where = array();
490 $where[] = "status != 'trashed'";
491
492 if( isset( $_GET['filterbyDescription'] ) && sanitize_text_field( $_GET['filterbyDescription'] ) != ""){
493 $description_key = sanitize_text_field( $_GET['filterbyDescription'] );
494
495 switch ( $description_key ) {
496 case 'with':
497 $where[] = ' `description` != "" ';
498 break;
499 case 'without':
500 default:
501 $where[] = ' `description` = "" ';
502 break;
503 }
504 }
505
506 if ( ! empty( $where ) ){
507 $sql .= ' WHERE ' . implode( ' AND ', $where );
508 }
509
510 return $wpdb->get_var( $sql );
511 }
512
513 public static function get_statused_record_count( $status ) {
514 global $wpdb;
515
516 $sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories ";
517
518 $where = array();
519 $where[] = "status='" . esc_sql( $status ) . "'";
520
521 if( isset( $_GET['filterbyDescription'] ) && sanitize_text_field( $_GET['filterbyDescription'] ) != ""){
522 $description_key = sanitize_text_field( $_GET['filterbyDescription'] );
523
524 switch ( $description_key ) {
525 case 'with':
526 $where[] = ' `description` != "" ';
527 break;
528 case 'without':
529 default:
530 $where[] = ' `description` = "" ';
531 break;
532 }
533 }
534
535 if ( ! empty( $where ) ){
536 $sql .= ' WHERE ' . implode( ' AND ', $where );
537 }
538
539 return $wpdb->get_var( $sql );
540 }
541
542
543 /** Text displayed when no customer data is available */
544 public function no_items() {
545 Survey_Maker_Data::survey_no_items_list_tables();
546 }
547
548
549 /**
550 * Render a column when no column specific method exist.
551 *
552 * @param array $item
553 * @param string $column_name
554 *
555 * @return mixed
556 */
557 public function column_default( $item, $column_name ) {
558 switch ( $column_name ) {
559 case 'title':
560 case 'description':
561 case 'items_count':
562 case 'status':
563 case 'id':
564 return $item[ $column_name ];
565 break;
566 default:
567 return print_r( $item, true ); //Show the whole array for troubleshooting purposes
568 }
569 }
570
571 /**
572 * Render the bulk edit checkbox
573 *
574 * @param array $item
575 *
576 * @return string
577 */
578 function column_cb( $item ) {
579
580 if(intval($item['id']) === 1){
581 return;
582 }
583
584 return sprintf(
585 '<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['id']
586 );
587 }
588
589
590 /**
591 * Method for name column
592 *
593 * @param array $item an array of DB data
594 *
595 * @return string
596 */
597 function column_title( $item ) {
598 if($item['status'] == 'trashed'){
599 $delete_nonce = wp_create_nonce( $this->plugin_name . '-delete-survey-category' );
600 }else{
601 $delete_nonce = wp_create_nonce( $this->plugin_name . '-trash-survey-category' );
602 }
603
604 $survey_cat_title = (isset($item['title']) && $item['title'] != "") ? esc_attr(stripcslashes($item['title'])) : "";
605
606 $restitle = Survey_Maker_Admin::ays_restriction_string( "word", stripcslashes( $survey_cat_title ), $this->title_length);
607
608 $fstatus = '';
609 if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){
610 $fstatus = '&fstatus=' . sanitize_text_field( $_GET['fstatus'] );
611 }
612 $actions = array();
613 if($item['status'] == 'trashed'){
614 $title = sprintf( '<a><strong>%s</strong></a>', $restitle );
615 $actions['restore'] = sprintf( '<a href="?page=%s&action=%s&id=%d&_wpnonce=%s'.$fstatus.'">'. __('Restore', "survey-maker") .'</a>', sanitize_text_field( $_REQUEST['page'] ), 'restore', absint( $item['id'] ), $delete_nonce );
616 }else{
617 $title = sprintf( '<a href="?page=%s&action=%s&id=%d"><strong>%s</strong></a>', sanitize_text_field( $_REQUEST['page'] ), 'edit', absint( $item['id'] ), $restitle );
618 $actions['edit'] = sprintf( '<a href="?page=%s&action=%s&id=%d">'. __('Edit', "survey-maker") .'</a>', sanitize_text_field( $_REQUEST['page'] ), 'edit', absint( $item['id'] ) );
619 }
620
621 if(intval($item['id']) !== 1){
622 if($item['status'] == 'trashed'){
623 $actions['delete'] = sprintf( '<a class="ays_confirm_del" data-message="%s" href="?page=%s&action=%s&id=%s&_wpnonce=%s'.$fstatus.'">'. __('Delete Permanently', "survey-maker") .'</a>', $restitle, sanitize_text_field( $_REQUEST['page'] ), 'delete', absint( $item['id'] ), $delete_nonce );
624 }else{
625 $dup_nonce = wp_create_nonce( $this->plugin_name . '-duplicate-survey-category' );
626 $actions['duplicate'] = sprintf( '<a href="?page=%s&action=%s&id=%s&_wpnonce=%s'.$fstatus.'">'. __('Duplicate', "survey-maker") .'</a>', sanitize_text_field( $_REQUEST['page'] ), 'duplicate', absint( $item['id'] ), $dup_nonce );
627 $actions['trash'] = sprintf( '<a href="?page=%s&action=%s&id=%s&_wpnonce=%s'.$fstatus.'">'. __('Move to trash', "survey-maker") .'</a>', sanitize_text_field( $_REQUEST['page'] ), 'trash', absint( $item['id'] ), $delete_nonce );
628 }
629 }
630
631 return $title . $this->row_actions( $actions );
632 }
633
634 function column_items_count( $item ) {
635 global $wpdb;
636 $surveys_table = $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "surveys";
637 $categories_table = $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories";
638
639 $sql = "SELECT COUNT(*)
640 FROM " . $categories_table . " c
641 JOIN " . $surveys_table . " s
642 ON FIND_IN_SET(c.id, s.category_ids ) AND c.status = 'published' AND s.status = 'published'
643 WHERE c.id = " . esc_sql( absint( $item['id'] ) );
644 $result = $wpdb->get_var($sql);
645
646 if ( isset($result) && $result > 0 ) {
647 $result = sprintf( '<a href="?page=%s&filterby=%d" target="_blank">%s</a>', 'survey-maker', $item['id'], $result );
648 }
649
650 return "<p style='font-size:14px;'>" . $result . "</p>";
651 }
652
653 function column_status( $item ) {
654 global $wpdb;
655 $status = ucfirst( esc_attr($item['status']) );
656 $date = date( 'Y/m/d', strtotime( esc_attr($item['date_modified']) ) );
657 $title_date = date( 'l jS \of F Y h:i:s A', strtotime( $item['date_modified'] ) );
658 $html = "<p style='font-size:14px;margin:0;'>" . esc_html($status) . "</p>";
659 $html .= "<p style=';font-size:14px;margin:0;text-decoration: dotted underline;' title='" . esc_attr($title_date) . "'>" . esc_attr($date) . "</p>";
660 return $html;
661 }
662
663 function column_description( $item ){
664 return (isset($item['description']) && $item['description']) ? esc_attr($item['description']) : '';
665 }
666
667 /**
668 * Associative array of columns
669 *
670 * @return array
671 */
672 function get_columns() {
673 $columns = array(
674 'cb' => '<input type="checkbox" />',
675 'title' => __( 'Title', "survey-maker" ),
676 'description' => __( 'Description', "survey-maker" ),
677 'items_count' => __( 'Surveys', "survey-maker" ),
678 'status' => __( 'Status', "survey-maker" ),
679 'id' => __( 'ID', "survey-maker" ),
680 );
681
682 if( isset( $_GET['action'] ) && ( $_GET['action'] == 'add' || $_GET['action'] == 'edit' ) ){
683 return array();
684 }
685
686 return $columns;
687 }
688
689
690 /**
691 * Columns to make sortable.
692 *
693 * @return array
694 */
695 public function get_sortable_columns() {
696 $sortable_columns = array(
697 'title' => array( 'title', true ),
698 'id' => array( 'id', true ),
699 );
700
701 return $sortable_columns;
702 }
703
704 /**
705 * Returns an associative array containing the bulk action
706 *
707 * @return array
708 */
709 public function get_bulk_actions() {
710 $actions = array(
711 'bulk-trash' => __( 'Move to trash', "survey-maker" ),
712 );
713
714 if(isset($_GET['fstatus']) && $_GET['fstatus'] == 'trashed'){
715 $actions = array(
716 'bulk-restore' => __( 'Restore', "survey-maker" ),
717 'bulk-delete' => __( 'Delete Permanently', "survey-maker" ),
718 );
719 }
720
721 return $actions;
722 }
723
724 /**
725 * Handles data query and filter, sorting, and pagination.
726 */
727 public function prepare_items() {
728 global $wpdb;
729 $this->_column_headers = $this->get_column_info();
730
731 $search = ( isset( $_REQUEST['s'] ) ) ? sanitize_text_field( $_REQUEST['s'] ) : false;
732
733 $do_search = ( $search ) ? sprintf( " title LIKE '%%%s%%' ", esc_sql( $wpdb->esc_like( $search ) ) ) : '';
734
735 /** Process bulk action */
736 $this->process_bulk_action();
737
738 $per_page = $this->get_items_per_page( 'survey_categories_per_page', 20 );
739 $current_page = $this->get_pagenum();
740 $total_items = self::record_count($do_search);
741
742 $this->set_pagination_args( array(
743 'total_items' => $total_items, //WE have to calculate the total number of items
744 'per_page' => $per_page //WE have to determine how many items to show on a page
745 ) );
746
747
748
749 $this->items = self::get_items( $per_page, $current_page, $do_search );
750 }
751
752 public function process_bulk_action() {
753
754 //Detect when a bulk action is being triggered...
755 if ( 'delete' === $this->current_action() ) {
756
757 // In our file that handles the request, verify the nonce.
758 $nonce = sanitize_text_field( $_REQUEST['_wpnonce'] );
759
760 if ( ! wp_verify_nonce( $nonce, $this->plugin_name . '-delete-survey-category' ) ) {
761 die( 'Go get a life script kiddies' );
762 }
763 else {
764 self::delete_items( absint( sanitize_text_field($_GET['id']) ) );
765
766 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
767 // add_query_arg() return the current url
768
769 $add_query_args = array(
770 "status" => 'deleted'
771 );
772 if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){
773 $add_query_args['fstatus'] = sanitize_text_field( $_GET['fstatus'] );
774 }
775 $url = remove_query_arg( array('action', 'id', '_wpnonce') );
776 $url = esc_url_raw( add_query_arg( $add_query_args, $url ) );
777 wp_redirect( $url );
778 }
779
780 }
781
782 //Detect when a bulk action is being triggered...
783 if ( 'trash' === $this->current_action() ) {
784
785 // In our file that handles the request, verify the nonce.
786 $nonce = sanitize_text_field( $_REQUEST['_wpnonce'] );
787
788 if ( ! wp_verify_nonce( $nonce, $this->plugin_name . '-trash-survey-category' ) ) {
789 die( 'Go get a life script kiddies' );
790 }
791 else {
792 self::trash_items( absint( sanitize_text_field($_GET['id']) ) );
793
794 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
795 // add_query_arg() return the current url
796
797 $add_query_args = array(
798 "status" => 'trashed'
799 );
800 if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){
801 $add_query_args['fstatus'] = sanitize_text_field( $_GET['fstatus'] );
802 }
803 $url = remove_query_arg( array('action', 'id', '_wpnonce') );
804 $url = esc_url_raw( add_query_arg( $add_query_args, $url ) );
805 wp_redirect( $url );
806 }
807
808 }
809
810 //Detect when a bulk action is being triggered...
811 if ( 'restore' === $this->current_action() ) {
812
813 // In our file that handles the request, verify the nonce.
814 $nonce = sanitize_text_field( $_REQUEST['_wpnonce'] );
815
816 if ( ! wp_verify_nonce( $nonce, $this->plugin_name . '-delete-survey-category' ) ) {
817 die( 'Go get a life script kiddies' );
818 }
819 else {
820 self::restore_items( absint( sanitize_text_field($_GET['id']) ) );
821
822 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
823 // add_query_arg() return the current url
824
825 $add_query_args = array(
826 "status" => 'restored'
827 );
828 if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){
829 $add_query_args['fstatus'] = sanitize_text_field( $_GET['fstatus'] );
830 }
831 $url = remove_query_arg( array('action', 'id', '_wpnonce') );
832 $url = esc_url_raw( add_query_arg( $add_query_args, $url ) );
833 wp_redirect( $url );
834 }
835
836 }
837
838 // If the delete bulk action is triggered
839 if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' ) ) {
840
841 $delete_ids = ( isset( $_POST['bulk-delete'] ) && ! empty( $_POST['bulk-delete'] ) ) ? esc_sql( $_POST['bulk-delete'] ) : array();
842
843 // loop over the array of record IDs and delete them
844 foreach ( $delete_ids as $id ) {
845 self::delete_items( $id );
846 }
847
848 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
849 // add_query_arg() return the current url
850 $add_query_args = array(
851 "status" => 'deleted'
852 );
853 if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){
854 $add_query_args['fstatus'] = sanitize_text_field( $_GET['fstatus'] );
855 }
856 $url = remove_query_arg( array('action', 'id', '_wpnonce') );
857 $url = esc_url_raw( add_query_arg( $add_query_args, $url ) );
858 wp_redirect( $url );
859 }
860
861 // If the delete bulk action is triggered
862 if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-trash' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-trash' ) ) {
863
864 $trash_ids = esc_sql( $_POST['bulk-delete'] );
865
866 // loop over the array of record IDs and delete them
867 foreach ( $trash_ids as $id ) {
868 self::trash_items( $id );
869 }
870
871 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
872 // add_query_arg() return the current url
873 $add_query_args = array(
874 "status" => 'trashed'
875 );
876 if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){
877 $add_query_args['fstatus'] = sanitize_text_field( $_GET['fstatus'] );
878 }
879 $url = remove_query_arg( array('action', 'id', '_wpnonce') );
880 $url = esc_url_raw( add_query_arg( $add_query_args, $url ) );
881 wp_redirect( $url );
882 }
883
884 // If the delete bulk action is triggered
885 if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-restore' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-restore' ) ) {
886
887 $restore_ids = esc_sql( $_POST['bulk-delete'] );
888
889 // loop over the array of record IDs and delete them
890 foreach ( $restore_ids as $id ) {
891 self::restore_items( $id );
892 }
893
894 // esc_url_raw() is used to prevent converting ampersand in url to "#038;"
895 // add_query_arg() return the current url
896 $add_query_args = array(
897 "status" => 'restored'
898 );
899 if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){
900 $add_query_args['fstatus'] = sanitize_text_field( $_GET['fstatus'] );
901 }
902 $url = remove_query_arg( array('action', 'id', '_wpnonce') );
903 $url = esc_url_raw( add_query_arg( $add_query_args, $url ) );
904 wp_redirect( $url );
905 }
906 }
907
908
909
910 public function survey_category_notices(){
911 $status = (isset($_REQUEST['status'])) ? sanitize_text_field( $_REQUEST['status'] ) : '';
912
913 if ( empty( $status ) )
914 return;
915
916 if ( 'created' == $status )
917 $updated_message = __( 'Survey category created.', "survey-maker" );
918 elseif ( 'updated' == $status )
919 $updated_message = __( 'Survey category saved.', "survey-maker" );
920 elseif ( 'deleted' == $status )
921 $updated_message = __( 'Survey category deleted.', "survey-maker" );
922 elseif ( 'duplicated' == $status )
923 $updated_message = __( 'Survey category duplicated.', "survey-maker" );
924
925 if ( empty( $updated_message ) )
926 return;
927
928 ?>
929 <div class="ays-survey-admin-notice notice notice-success is-dismissible">
930 <p> <?php echo esc_html($updated_message); ?> </p>
931 </div>
932 <?php
933 }
934 }
935