| 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 action" 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 action"><?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 |
$survey_allowed_html = Survey_Maker_Data::ays_survey_custom_allowed_html(); |
| 238 |
|
| 239 |
// Save type |
| 240 |
$save_type = (isset($data['save_type'])) ? $data['save_type'] : ''; |
| 241 |
|
| 242 |
// Id of item |
| 243 |
$id = isset( $data['id'] ) ? absint( intval( $data['id'] ) ) : 0; |
| 244 |
|
| 245 |
// Title |
| 246 |
$title = isset( $data[ $name_prefix . 'title' ] ) && $data[ $name_prefix . 'title' ] != '' ? stripslashes( sanitize_text_field( $data[ $name_prefix . 'title' ] ) ) : ''; |
| 247 |
|
| 248 |
if($title == ''){ |
| 249 |
$url = esc_url_raw( remove_query_arg( false ) ); |
| 250 |
wp_redirect( $url ); |
| 251 |
} |
| 252 |
|
| 253 |
// Description |
| 254 |
// Sanitize description to allow only safe HTML tags and remove scripts. |
| 255 |
$description = isset( $data[ $name_prefix . 'description' ] ) && $data[ $name_prefix . 'description' ] != '' ? wp_kses( $data[ $name_prefix . 'description' ], $survey_allowed_html ) : ''; |
| 256 |
|
| 257 |
// Status |
| 258 |
$status = isset( $data[ $name_prefix . 'status' ] ) && $data[ $name_prefix . 'status' ] != '' ? stripslashes( sanitize_text_field($data[ $name_prefix . 'status' ]) ) : 'published'; |
| 259 |
|
| 260 |
// Date created |
| 261 |
$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' ); |
| 262 |
|
| 263 |
// Date modified |
| 264 |
$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' ); |
| 265 |
|
| 266 |
// Options |
| 267 |
$options = array( |
| 268 |
|
| 269 |
); |
| 270 |
|
| 271 |
$message = ''; |
| 272 |
if( $id == 0 ){ |
| 273 |
$result = $wpdb->insert( |
| 274 |
$table, |
| 275 |
array( |
| 276 |
'title' => $title, |
| 277 |
'description' => $description, |
| 278 |
'status' => $status, |
| 279 |
'date_created' => $date_created, |
| 280 |
'date_modified' => $date_modified, |
| 281 |
'options' => json_encode( $options ), |
| 282 |
), |
| 283 |
array( |
| 284 |
'%s', // title |
| 285 |
'%s', // description |
| 286 |
'%s', // status |
| 287 |
'%s', // date_created |
| 288 |
'%s', // date_modified |
| 289 |
'%s', // options |
| 290 |
) |
| 291 |
); |
| 292 |
|
| 293 |
$inserted_id = $wpdb->insert_id; |
| 294 |
$message = 'created'; |
| 295 |
}else{ |
| 296 |
$result = $wpdb->update( |
| 297 |
$table, |
| 298 |
array( |
| 299 |
'title' => $title, |
| 300 |
'description' => $description, |
| 301 |
'status' => $status, |
| 302 |
'date_modified' => $date_modified, |
| 303 |
'options' => json_encode( $options ), |
| 304 |
), |
| 305 |
array( 'id' => $id ), |
| 306 |
array( |
| 307 |
'%s', // title |
| 308 |
'%s', // description |
| 309 |
'%s', // status |
| 310 |
'%s', // date_modified |
| 311 |
'%s', // options |
| 312 |
), |
| 313 |
array( '%d' ) |
| 314 |
); |
| 315 |
|
| 316 |
$inserted_id = $id; |
| 317 |
$message = 'updated'; |
| 318 |
} |
| 319 |
|
| 320 |
if( $result >= 0 ) { |
| 321 |
if($save_type == 'apply'){ |
| 322 |
if($id == 0){ |
| 323 |
$url = esc_url_raw( add_query_arg( array( |
| 324 |
"action" => "edit", |
| 325 |
"id" => $inserted_id, |
| 326 |
"status" => $message |
| 327 |
) ) ); |
| 328 |
}else{ |
| 329 |
$url = esc_url_raw( add_query_arg( array( |
| 330 |
"status" => $message |
| 331 |
) ) ); |
| 332 |
} |
| 333 |
wp_redirect( $url ); |
| 334 |
}elseif($save_type == 'save_new'){ |
| 335 |
$url = remove_query_arg( array('id') ); |
| 336 |
$url = esc_url_raw( add_query_arg( array( |
| 337 |
"action" => "add", |
| 338 |
"status" => $message |
| 339 |
), $url ) ); |
| 340 |
wp_redirect( $url ); |
| 341 |
}else{ |
| 342 |
$url = remove_query_arg( array('action', 'id') ); |
| 343 |
$url = esc_url_raw( add_query_arg( array( |
| 344 |
"status" => $message |
| 345 |
), $url ) ); |
| 346 |
wp_redirect( $url ); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
|
| 352 |
public function duplicate_survey_categories( $id ){ |
| 353 |
global $wpdb; |
| 354 |
|
| 355 |
if ( is_null( $id ) || empty($id) || $id == 0 ) { |
| 356 |
return; |
| 357 |
} |
| 358 |
|
| 359 |
$survey_category_table = $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories"; |
| 360 |
$survey_category_data = self::get_item_by_id( $id ); |
| 361 |
|
| 362 |
$survey_allowed_html = Survey_Maker_Data::ays_survey_custom_allowed_html(); |
| 363 |
|
| 364 |
$title = (isset($survey_category_data['title']) && $survey_category_data['title'] != "") ? stripslashes( sanitize_text_field( $survey_category_data['title'] ) ) : __("Copy", 'survey-maker'); |
| 365 |
|
| 366 |
$description = (isset( $survey_category_data['description'] ) && $survey_category_data['description'] != "") ? wp_kses( $survey_category_data['description'], $survey_allowed_html ) : ""; |
| 367 |
|
| 368 |
$status = (isset($survey_category_data['status']) && $survey_category_data['status'] != "") ? sanitize_text_field( $survey_category_data['status'] ) : 'published'; |
| 369 |
$trash_status = (isset($survey_category_data['trash_status']) && $survey_category_data['trash_status'] != "") ? sanitize_text_field( $survey_category_data['trash_status'] ) : ''; |
| 370 |
$date_created = current_time( 'mysql' ); |
| 371 |
$date_modified = $date_created; |
| 372 |
|
| 373 |
$result = $wpdb->insert( |
| 374 |
$survey_category_table, |
| 375 |
array( |
| 376 |
'title' => "Copy - " . $title, |
| 377 |
'description' => $description, |
| 378 |
'status' => $status, |
| 379 |
'trash_status' => $trash_status, |
| 380 |
'date_created' => $date_created, |
| 381 |
'date_modified' => $date_modified, |
| 382 |
), |
| 383 |
array( |
| 384 |
'%s', // title |
| 385 |
'%s', // description |
| 386 |
'%s', // status |
| 387 |
'%s', // trash_status |
| 388 |
'%s', // date_created |
| 389 |
'%s', // date_modified |
| 390 |
) |
| 391 |
); |
| 392 |
|
| 393 |
if( $result >= 0 ){ |
| 394 |
$message = "duplicated"; |
| 395 |
$url = esc_url_raw( remove_query_arg(array('action', 'id', '_wpnonce') ) ) . '&status=' . $message; |
| 396 |
wp_redirect( $url ); |
| 397 |
} |
| 398 |
} |
| 399 |
|
| 400 |
/** |
| 401 |
* Delete a customer record. |
| 402 |
* |
| 403 |
* @param int $id customer ID |
| 404 |
*/ |
| 405 |
public static function delete_items( $id ) { |
| 406 |
global $wpdb; |
| 407 |
$wpdb->delete( |
| 408 |
$wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories", |
| 409 |
array( 'id' => $id ), |
| 410 |
array( '%d' ) |
| 411 |
); |
| 412 |
} |
| 413 |
|
| 414 |
/** |
| 415 |
* Move to trash a customer record. |
| 416 |
* |
| 417 |
* @param int $id customer ID |
| 418 |
*/ |
| 419 |
public static function trash_items( $id ) { |
| 420 |
global $wpdb; |
| 421 |
$db_item = self::get_item_by_id( $id ); |
| 422 |
$wpdb->update( |
| 423 |
$wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories", |
| 424 |
array( |
| 425 |
'status' => 'trashed', |
| 426 |
'trash_status' => $db_item['status'], |
| 427 |
), |
| 428 |
array( 'id' => $id ), |
| 429 |
array( '%s', '%s' ), |
| 430 |
array( '%d' ) |
| 431 |
); |
| 432 |
} |
| 433 |
|
| 434 |
/** |
| 435 |
* Restore a customer record. |
| 436 |
* |
| 437 |
* @param int $id customer ID |
| 438 |
*/ |
| 439 |
public static function restore_items( $id ) { |
| 440 |
global $wpdb; |
| 441 |
$db_item = self::get_item_by_id( $id ); |
| 442 |
|
| 443 |
$wpdb->update( |
| 444 |
$wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories", |
| 445 |
array( |
| 446 |
'status' => $db_item['trash_status'], |
| 447 |
'trash_status' => '', |
| 448 |
), |
| 449 |
array( 'id' => $id ), |
| 450 |
array( '%s', '%s' ), |
| 451 |
array( '%d' ) |
| 452 |
); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
/** |
| 457 |
* Returns the count of records in the database. |
| 458 |
* |
| 459 |
* @return null|string |
| 460 |
*/ |
| 461 |
public static function record_count($do_search) { |
| 462 |
global $wpdb; |
| 463 |
$sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories "; |
| 464 |
$where = array(); |
| 465 |
$where[] = "status != 'trashed'"; |
| 466 |
|
| 467 |
if( isset( $_GET['filterbyDescription'] ) && sanitize_text_field( $_GET['filterbyDescription'] ) != ""){ |
| 468 |
$description_key = sanitize_text_field( $_GET['filterbyDescription'] ); |
| 469 |
|
| 470 |
switch ( $description_key ) { |
| 471 |
case 'with': |
| 472 |
$where[] = ' `description` != "" '; |
| 473 |
break; |
| 474 |
case 'without': |
| 475 |
default: |
| 476 |
$where[] = ' `description` = "" '; |
| 477 |
break; |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
if( $do_search != '' ){ |
| 482 |
$where[] = $do_search; |
| 483 |
} |
| 484 |
|
| 485 |
if ( ! empty( $where ) ){ |
| 486 |
$sql .= ' WHERE ' . implode( ' AND ', $where ); |
| 487 |
} |
| 488 |
return $wpdb->get_var( $sql ); |
| 489 |
} |
| 490 |
|
| 491 |
public static function all_record_count() { |
| 492 |
global $wpdb; |
| 493 |
|
| 494 |
$sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories "; |
| 495 |
$where = array(); |
| 496 |
$where[] = "status != 'trashed'"; |
| 497 |
|
| 498 |
if( isset( $_GET['filterbyDescription'] ) && sanitize_text_field( $_GET['filterbyDescription'] ) != ""){ |
| 499 |
$description_key = sanitize_text_field( $_GET['filterbyDescription'] ); |
| 500 |
|
| 501 |
switch ( $description_key ) { |
| 502 |
case 'with': |
| 503 |
$where[] = ' `description` != "" '; |
| 504 |
break; |
| 505 |
case 'without': |
| 506 |
default: |
| 507 |
$where[] = ' `description` = "" '; |
| 508 |
break; |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
if ( ! empty( $where ) ){ |
| 513 |
$sql .= ' WHERE ' . implode( ' AND ', $where ); |
| 514 |
} |
| 515 |
|
| 516 |
return $wpdb->get_var( $sql ); |
| 517 |
} |
| 518 |
|
| 519 |
public static function get_statused_record_count( $status ) { |
| 520 |
global $wpdb; |
| 521 |
|
| 522 |
$sql = "SELECT COUNT(*) FROM " . $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories "; |
| 523 |
|
| 524 |
$where = array(); |
| 525 |
$where[] = "status='" . esc_sql( $status ) . "'"; |
| 526 |
|
| 527 |
if( isset( $_GET['filterbyDescription'] ) && sanitize_text_field( $_GET['filterbyDescription'] ) != ""){ |
| 528 |
$description_key = sanitize_text_field( $_GET['filterbyDescription'] ); |
| 529 |
|
| 530 |
switch ( $description_key ) { |
| 531 |
case 'with': |
| 532 |
$where[] = ' `description` != "" '; |
| 533 |
break; |
| 534 |
case 'without': |
| 535 |
default: |
| 536 |
$where[] = ' `description` = "" '; |
| 537 |
break; |
| 538 |
} |
| 539 |
} |
| 540 |
|
| 541 |
if ( ! empty( $where ) ){ |
| 542 |
$sql .= ' WHERE ' . implode( ' AND ', $where ); |
| 543 |
} |
| 544 |
|
| 545 |
return $wpdb->get_var( $sql ); |
| 546 |
} |
| 547 |
|
| 548 |
|
| 549 |
/** Text displayed when no customer data is available */ |
| 550 |
public function no_items() { |
| 551 |
Survey_Maker_Data::survey_no_items_list_tables(); |
| 552 |
} |
| 553 |
|
| 554 |
|
| 555 |
/** |
| 556 |
* Render a column when no column specific method exist. |
| 557 |
* |
| 558 |
* @param array $item |
| 559 |
* @param string $column_name |
| 560 |
* |
| 561 |
* @return mixed |
| 562 |
*/ |
| 563 |
public function column_default( $item, $column_name ) { |
| 564 |
switch ( $column_name ) { |
| 565 |
case 'title': |
| 566 |
case 'description': |
| 567 |
case 'items_count': |
| 568 |
case 'status': |
| 569 |
case 'id': |
| 570 |
return $item[ $column_name ]; |
| 571 |
break; |
| 572 |
default: |
| 573 |
return print_r( $item, true ); //Show the whole array for troubleshooting purposes |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* Render the bulk edit checkbox |
| 579 |
* |
| 580 |
* @param array $item |
| 581 |
* |
| 582 |
* @return string |
| 583 |
*/ |
| 584 |
function column_cb( $item ) { |
| 585 |
|
| 586 |
if(intval($item['id']) === 1){ |
| 587 |
return; |
| 588 |
} |
| 589 |
|
| 590 |
return sprintf( |
| 591 |
'<input type="checkbox" name="bulk-delete[]" value="%s" />', $item['id'] |
| 592 |
); |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
/** |
| 597 |
* Method for name column |
| 598 |
* |
| 599 |
* @param array $item an array of DB data |
| 600 |
* |
| 601 |
* @return string |
| 602 |
*/ |
| 603 |
function column_title( $item ) { |
| 604 |
if($item['status'] == 'trashed'){ |
| 605 |
$delete_nonce = wp_create_nonce( $this->plugin_name . '-delete-survey-category' ); |
| 606 |
}else{ |
| 607 |
$delete_nonce = wp_create_nonce( $this->plugin_name . '-trash-survey-category' ); |
| 608 |
} |
| 609 |
|
| 610 |
$survey_cat_title = (isset($item['title']) && $item['title'] != "") ? esc_attr(stripcslashes($item['title'])) : ""; |
| 611 |
|
| 612 |
$restitle = Survey_Maker_Admin::ays_restriction_string( "word", stripcslashes( $survey_cat_title ), $this->title_length); |
| 613 |
|
| 614 |
$fstatus = ''; |
| 615 |
if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){ |
| 616 |
$fstatus = '&fstatus=' . sanitize_text_field( $_GET['fstatus'] ); |
| 617 |
} |
| 618 |
$actions = array(); |
| 619 |
if($item['status'] == 'trashed'){ |
| 620 |
$title = sprintf( '<a><strong>%s</strong></a>', $restitle ); |
| 621 |
$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 ); |
| 622 |
}else{ |
| 623 |
$title = sprintf( '<a href="?page=%s&action=%s&id=%d"><strong>%s</strong></a>', sanitize_text_field( $_REQUEST['page'] ), 'edit', absint( $item['id'] ), $restitle ); |
| 624 |
$actions['edit'] = sprintf( '<a href="?page=%s&action=%s&id=%d">'. __('Edit', "survey-maker") .'</a>', sanitize_text_field( $_REQUEST['page'] ), 'edit', absint( $item['id'] ) ); |
| 625 |
} |
| 626 |
|
| 627 |
if(intval($item['id']) !== 1){ |
| 628 |
if($item['status'] == 'trashed'){ |
| 629 |
$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 ); |
| 630 |
}else{ |
| 631 |
$dup_nonce = wp_create_nonce( $this->plugin_name . '-duplicate-survey-category' ); |
| 632 |
$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 ); |
| 633 |
$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 ); |
| 634 |
} |
| 635 |
} |
| 636 |
|
| 637 |
return $title . $this->row_actions( $actions ); |
| 638 |
} |
| 639 |
|
| 640 |
function column_items_count( $item ) { |
| 641 |
global $wpdb; |
| 642 |
$surveys_table = $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "surveys"; |
| 643 |
$categories_table = $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "survey_categories"; |
| 644 |
|
| 645 |
$sql = "SELECT COUNT(*) |
| 646 |
FROM " . $categories_table . " c |
| 647 |
JOIN " . $surveys_table . " s |
| 648 |
ON FIND_IN_SET(c.id, s.category_ids ) AND c.status = 'published' AND s.status = 'published' |
| 649 |
WHERE c.id = " . esc_sql( absint( $item['id'] ) ); |
| 650 |
$result = $wpdb->get_var($sql); |
| 651 |
|
| 652 |
if ( isset($result) && $result > 0 ) { |
| 653 |
$result = sprintf( '<a href="?page=%s&filterby=%d" target="_blank">%s</a>', 'survey-maker', $item['id'], $result ); |
| 654 |
} |
| 655 |
|
| 656 |
return "<p style='font-size:14px;'>" . $result . "</p>"; |
| 657 |
} |
| 658 |
|
| 659 |
function column_status( $item ) { |
| 660 |
global $wpdb; |
| 661 |
$status = ucfirst( esc_attr($item['status']) ); |
| 662 |
$date = date( 'Y/m/d', strtotime( esc_attr($item['date_modified']) ) ); |
| 663 |
$title_date = date( 'l jS \of F Y h:i:s A', strtotime( $item['date_modified'] ) ); |
| 664 |
$html = "<p style='font-size:14px;margin:0;'>" . esc_html($status) . "</p>"; |
| 665 |
$html .= "<p style=';font-size:14px;margin:0;text-decoration: dotted underline;' title='" . esc_attr($title_date) . "'>" . esc_attr($date) . "</p>"; |
| 666 |
return $html; |
| 667 |
} |
| 668 |
|
| 669 |
function column_description( $item ){ |
| 670 |
return (isset($item['description']) && $item['description']) ? esc_attr($item['description']) : ''; |
| 671 |
} |
| 672 |
|
| 673 |
/** |
| 674 |
* Associative array of columns |
| 675 |
* |
| 676 |
* @return array |
| 677 |
*/ |
| 678 |
function get_columns() { |
| 679 |
$columns = array( |
| 680 |
'cb' => '<input type="checkbox" />', |
| 681 |
'title' => __( 'Title', "survey-maker" ), |
| 682 |
'description' => __( 'Description', "survey-maker" ), |
| 683 |
'items_count' => __( 'Surveys', "survey-maker" ), |
| 684 |
'status' => __( 'Status', "survey-maker" ), |
| 685 |
'id' => __( 'ID', "survey-maker" ), |
| 686 |
); |
| 687 |
|
| 688 |
if( isset( $_GET['action'] ) && ( $_GET['action'] == 'add' || $_GET['action'] == 'edit' ) ){ |
| 689 |
return array(); |
| 690 |
} |
| 691 |
|
| 692 |
return $columns; |
| 693 |
} |
| 694 |
|
| 695 |
|
| 696 |
/** |
| 697 |
* Columns to make sortable. |
| 698 |
* |
| 699 |
* @return array |
| 700 |
*/ |
| 701 |
public function get_sortable_columns() { |
| 702 |
$sortable_columns = array( |
| 703 |
'title' => array( 'title', true ), |
| 704 |
'id' => array( 'id', true ), |
| 705 |
); |
| 706 |
|
| 707 |
return $sortable_columns; |
| 708 |
} |
| 709 |
|
| 710 |
/** |
| 711 |
* Returns an associative array containing the bulk action |
| 712 |
* |
| 713 |
* @return array |
| 714 |
*/ |
| 715 |
public function get_bulk_actions() { |
| 716 |
$actions = array( |
| 717 |
'bulk-trash' => __( 'Move to trash', "survey-maker" ), |
| 718 |
); |
| 719 |
|
| 720 |
if(isset($_GET['fstatus']) && $_GET['fstatus'] == 'trashed'){ |
| 721 |
$actions = array( |
| 722 |
'bulk-restore' => __( 'Restore', "survey-maker" ), |
| 723 |
'bulk-delete' => __( 'Delete Permanently', "survey-maker" ), |
| 724 |
); |
| 725 |
} |
| 726 |
|
| 727 |
return $actions; |
| 728 |
} |
| 729 |
|
| 730 |
/** |
| 731 |
* Handles data query and filter, sorting, and pagination. |
| 732 |
*/ |
| 733 |
public function prepare_items() { |
| 734 |
global $wpdb; |
| 735 |
$this->_column_headers = $this->get_column_info(); |
| 736 |
|
| 737 |
$search = ( isset( $_REQUEST['s'] ) ) ? sanitize_text_field( $_REQUEST['s'] ) : false; |
| 738 |
|
| 739 |
$do_search = ( $search ) ? sprintf( " title LIKE '%%%s%%' ", esc_sql( $wpdb->esc_like( $search ) ) ) : ''; |
| 740 |
|
| 741 |
/** Process bulk action */ |
| 742 |
$this->process_bulk_action(); |
| 743 |
|
| 744 |
$per_page = $this->get_items_per_page( 'survey_categories_per_page', 20 ); |
| 745 |
$current_page = $this->get_pagenum(); |
| 746 |
$total_items = self::record_count($do_search); |
| 747 |
|
| 748 |
$this->set_pagination_args( array( |
| 749 |
'total_items' => $total_items, //WE have to calculate the total number of items |
| 750 |
'per_page' => $per_page //WE have to determine how many items to show on a page |
| 751 |
) ); |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
$this->items = self::get_items( $per_page, $current_page, $do_search ); |
| 756 |
} |
| 757 |
|
| 758 |
public function process_bulk_action() { |
| 759 |
|
| 760 |
//Detect when a bulk action is being triggered... |
| 761 |
if ( 'delete' === $this->current_action() ) { |
| 762 |
|
| 763 |
// In our file that handles the request, verify the nonce. |
| 764 |
$nonce = sanitize_text_field( $_REQUEST['_wpnonce'] ); |
| 765 |
|
| 766 |
if ( ! wp_verify_nonce( $nonce, $this->plugin_name . '-delete-survey-category' ) ) { |
| 767 |
die( 'Go get a life script kiddies' ); |
| 768 |
} |
| 769 |
else { |
| 770 |
self::delete_items( absint( sanitize_text_field($_GET['id']) ) ); |
| 771 |
|
| 772 |
// esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 773 |
// add_query_arg() return the current url |
| 774 |
|
| 775 |
$add_query_args = array( |
| 776 |
"status" => 'deleted' |
| 777 |
); |
| 778 |
if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){ |
| 779 |
$add_query_args['fstatus'] = sanitize_text_field( $_GET['fstatus'] ); |
| 780 |
} |
| 781 |
$url = remove_query_arg( array('action', 'id', '_wpnonce') ); |
| 782 |
$url = esc_url_raw( add_query_arg( $add_query_args, $url ) ); |
| 783 |
wp_redirect( $url ); |
| 784 |
} |
| 785 |
|
| 786 |
} |
| 787 |
|
| 788 |
//Detect when a bulk action is being triggered... |
| 789 |
if ( 'trash' === $this->current_action() ) { |
| 790 |
|
| 791 |
// In our file that handles the request, verify the nonce. |
| 792 |
$nonce = sanitize_text_field( $_REQUEST['_wpnonce'] ); |
| 793 |
|
| 794 |
if ( ! wp_verify_nonce( $nonce, $this->plugin_name . '-trash-survey-category' ) ) { |
| 795 |
die( 'Go get a life script kiddies' ); |
| 796 |
} |
| 797 |
else { |
| 798 |
self::trash_items( absint( sanitize_text_field($_GET['id']) ) ); |
| 799 |
|
| 800 |
// esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 801 |
// add_query_arg() return the current url |
| 802 |
|
| 803 |
$add_query_args = array( |
| 804 |
"status" => 'trashed' |
| 805 |
); |
| 806 |
if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){ |
| 807 |
$add_query_args['fstatus'] = sanitize_text_field( $_GET['fstatus'] ); |
| 808 |
} |
| 809 |
$url = remove_query_arg( array('action', 'id', '_wpnonce') ); |
| 810 |
$url = esc_url_raw( add_query_arg( $add_query_args, $url ) ); |
| 811 |
wp_redirect( $url ); |
| 812 |
} |
| 813 |
|
| 814 |
} |
| 815 |
|
| 816 |
//Detect when a bulk action is being triggered... |
| 817 |
if ( 'restore' === $this->current_action() ) { |
| 818 |
|
| 819 |
// In our file that handles the request, verify the nonce. |
| 820 |
$nonce = sanitize_text_field( $_REQUEST['_wpnonce'] ); |
| 821 |
|
| 822 |
if ( ! wp_verify_nonce( $nonce, $this->plugin_name . '-delete-survey-category' ) ) { |
| 823 |
die( 'Go get a life script kiddies' ); |
| 824 |
} |
| 825 |
else { |
| 826 |
self::restore_items( absint( sanitize_text_field($_GET['id']) ) ); |
| 827 |
|
| 828 |
// esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 829 |
// add_query_arg() return the current url |
| 830 |
|
| 831 |
$add_query_args = array( |
| 832 |
"status" => 'restored' |
| 833 |
); |
| 834 |
if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){ |
| 835 |
$add_query_args['fstatus'] = sanitize_text_field( $_GET['fstatus'] ); |
| 836 |
} |
| 837 |
$url = remove_query_arg( array('action', 'id', '_wpnonce') ); |
| 838 |
$url = esc_url_raw( add_query_arg( $add_query_args, $url ) ); |
| 839 |
wp_redirect( $url ); |
| 840 |
} |
| 841 |
|
| 842 |
} |
| 843 |
|
| 844 |
// If the delete bulk action is triggered |
| 845 |
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-delete' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-delete' ) ) { |
| 846 |
|
| 847 |
$delete_ids = ( isset( $_POST['bulk-delete'] ) && ! empty( $_POST['bulk-delete'] ) ) ? esc_sql( $_POST['bulk-delete'] ) : array(); |
| 848 |
|
| 849 |
// loop over the array of record IDs and delete them |
| 850 |
foreach ( $delete_ids as $id ) { |
| 851 |
self::delete_items( $id ); |
| 852 |
} |
| 853 |
|
| 854 |
// esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 855 |
// add_query_arg() return the current url |
| 856 |
$add_query_args = array( |
| 857 |
"status" => 'deleted' |
| 858 |
); |
| 859 |
if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){ |
| 860 |
$add_query_args['fstatus'] = sanitize_text_field( $_GET['fstatus'] ); |
| 861 |
} |
| 862 |
$url = remove_query_arg( array('action', 'id', '_wpnonce') ); |
| 863 |
$url = esc_url_raw( add_query_arg( $add_query_args, $url ) ); |
| 864 |
wp_redirect( $url ); |
| 865 |
} |
| 866 |
|
| 867 |
// If the delete bulk action is triggered |
| 868 |
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-trash' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-trash' ) ) { |
| 869 |
|
| 870 |
$trash_ids = esc_sql( $_POST['bulk-delete'] ); |
| 871 |
|
| 872 |
// loop over the array of record IDs and delete them |
| 873 |
foreach ( $trash_ids as $id ) { |
| 874 |
self::trash_items( $id ); |
| 875 |
} |
| 876 |
|
| 877 |
// esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 878 |
// add_query_arg() return the current url |
| 879 |
$add_query_args = array( |
| 880 |
"status" => 'trashed' |
| 881 |
); |
| 882 |
if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){ |
| 883 |
$add_query_args['fstatus'] = sanitize_text_field( $_GET['fstatus'] ); |
| 884 |
} |
| 885 |
$url = remove_query_arg( array('action', 'id', '_wpnonce') ); |
| 886 |
$url = esc_url_raw( add_query_arg( $add_query_args, $url ) ); |
| 887 |
wp_redirect( $url ); |
| 888 |
} |
| 889 |
|
| 890 |
// If the delete bulk action is triggered |
| 891 |
if ( ( isset( $_POST['action'] ) && $_POST['action'] == 'bulk-restore' ) || ( isset( $_POST['action2'] ) && $_POST['action2'] == 'bulk-restore' ) ) { |
| 892 |
|
| 893 |
$restore_ids = esc_sql( $_POST['bulk-delete'] ); |
| 894 |
|
| 895 |
// loop over the array of record IDs and delete them |
| 896 |
foreach ( $restore_ids as $id ) { |
| 897 |
self::restore_items( $id ); |
| 898 |
} |
| 899 |
|
| 900 |
// esc_url_raw() is used to prevent converting ampersand in url to "#038;" |
| 901 |
// add_query_arg() return the current url |
| 902 |
$add_query_args = array( |
| 903 |
"status" => 'restored' |
| 904 |
); |
| 905 |
if( isset( $_GET['fstatus'] ) && $_GET['fstatus'] != '' ){ |
| 906 |
$add_query_args['fstatus'] = sanitize_text_field( $_GET['fstatus'] ); |
| 907 |
} |
| 908 |
$url = remove_query_arg( array('action', 'id', '_wpnonce') ); |
| 909 |
$url = esc_url_raw( add_query_arg( $add_query_args, $url ) ); |
| 910 |
wp_redirect( $url ); |
| 911 |
} |
| 912 |
} |
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
public function survey_category_notices(){ |
| 917 |
$status = (isset($_REQUEST['status'])) ? sanitize_text_field( $_REQUEST['status'] ) : ''; |
| 918 |
|
| 919 |
if ( empty( $status ) ) |
| 920 |
return; |
| 921 |
|
| 922 |
if ( 'created' == $status ) |
| 923 |
$updated_message = __( 'Survey category created.', "survey-maker" ); |
| 924 |
elseif ( 'updated' == $status ) |
| 925 |
$updated_message = __( 'Survey category saved.', "survey-maker" ); |
| 926 |
elseif ( 'deleted' == $status ) |
| 927 |
$updated_message = __( 'Survey category deleted.', "survey-maker" ); |
| 928 |
elseif ( 'duplicated' == $status ) |
| 929 |
$updated_message = __( 'Survey category duplicated.', "survey-maker" ); |
| 930 |
|
| 931 |
if ( empty( $updated_message ) ) |
| 932 |
return; |
| 933 |
|
| 934 |
?> |
| 935 |
<div class="ays-survey-admin-notice notice notice-success is-dismissible"> |
| 936 |
<p> <?php echo esc_html($updated_message); ?> </p> |
| 937 |
</div> |
| 938 |
<?php |
| 939 |
} |
| 940 |
} |
| 941 |
|