| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
global $current_user, $wppmfunction ,$wpdb; |
| 7 |
$task_id = isset($_POST['task_id']) ? intval(sanitize_text_field($_POST['task_id'])) : 0 ; |
| 8 |
$thread_id = isset($_POST['comment_id']) ? intval(sanitize_text_field($_POST['comment_id'])) : 0 ; |
| 9 |
$task_comment = $wppmfunction->get_task_comment($thread_id); |
| 10 |
if (!(($current_user->ID && $current_user->has_cap('manage_options')) || $wppmfunction->has_comment_permission('delete_task_thread',$task_id,$thread_id))) {exit;} |
| 11 |
if ( check_ajax_referer( 'wppm_set_delete_thread', '_ajax_nonce', false ) != 1 ) { |
| 12 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 13 |
} |
| 14 |
$thread_id = esc_sql($thread_id); |
| 15 |
$task_id = esc_sql($task_id); |
| 16 |
$sql="SELECT attachment_ids FROM {$wpdb->prefix}wppm_task_comment WHERE id ='$thread_id'"; |
| 17 |
$thread_attachment_ids = $wpdb->get_results( $sql ); |
| 18 |
if(!empty($thread_attachment_ids) ){ |
| 19 |
foreach ($thread_attachment_ids as $thread_attachment_id){ |
| 20 |
$attachment_ids_temp=array(); |
| 21 |
if($thread_attachment_id->attachment_ids){ |
| 22 |
$attachment_ids_temp = explode(',', $thread_attachment_id->attachment_ids); |
| 23 |
} |
| 24 |
$attachment_ids=$attachment_ids_temp; |
| 25 |
if(!empty($attachment_ids_temp)){ |
| 26 |
foreach ($attachment_ids_temp as $attachment_id){ |
| 27 |
$attachment_id = esc_sql($attachment_id); |
| 28 |
$sql="SELECT * FROM {$wpdb->prefix}wppm_attachments WHERE id = '$attachment_id'"; |
| 29 |
$result=$wpdb->get_row($sql); |
| 30 |
if(!empty($result)){ |
| 31 |
$sql_query="SELECT file_path FROM {$wpdb->prefix}wppm_attachments WHERE file_path ='".esc_sql($result->file_path)."'"; |
| 32 |
$attach_result=$wpdb->get_results($sql_query); |
| 33 |
$result_count = count($attach_result); |
| 34 |
if(file_exists($result->file_path) && $result_count < 2) |
| 35 |
{ |
| 36 |
unlink($result->file_path); |
| 37 |
} |
| 38 |
$wpdb->delete($wpdb->prefix.'wppm_attachments', array( 'id' => $attachment_id)); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
$cur_user = get_userdata($current_user->ID); |
| 45 |
$cur_user_display_name = $cur_user->display_name; |
| 46 |
$log_values = array('task_id'=>"$task_id",'body'=>'This comment was deleted by '.$cur_user_display_name,'attachment_ids'=>"" ); |
| 47 |
$wpdb->update($wpdb->prefix.'wppm_task_comment', $log_values, array('id'=>"$thread_id")); |
| 48 |
$task_log_values = array('task_id'=>"$task_id",'comment_id'=>"$thread_id",'comment_type'=>'delete_task_comment'); |
| 49 |
$comment_meta = $wpdb->get_row("SELECT * FROM {$wpdb->prefix}wppm_task_comment_meta WHERE comment_id='$thread_id' "); |
| 50 |
if(!empty($comment_meta)){ |
| 51 |
$comment_meta_id = esc_sql($comment_meta->id); |
| 52 |
$wpdb->update($wpdb->prefix . 'wppm_task_comment_meta',$task_log_values,array('id'=>"$comment_meta_id")); |
| 53 |
}else{ |
| 54 |
$wpdb->insert($wpdb->prefix . 'wppm_task_comment_meta',$task_log_values); |
| 55 |
|
| 56 |
} |
| 57 |
?> |