| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
|
| 6 |
global $current_user, $wppmfunction ,$wpdb; |
| 7 |
if ( check_ajax_referer( 'wppm_set_delete_task', '_ajax_nonce', false ) != 1 ) { |
| 8 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 9 |
} |
| 10 |
$task_id = isset($_POST['task_id']) ? intval(sanitize_text_field(wp_unslash($_POST['task_id']))) : 0 ; |
| 11 |
|
| 12 |
$task_data = $wppmfunction->get_task($task_id); |
| 13 |
$project_data = $wppmfunction->get_project($task_data['project']); |
| 14 |
$wppm_current_user_capability = get_user_meta( $current_user->ID, 'wppm_capability', true ); |
| 15 |
if(!(($current_user->ID && $current_user->has_cap('manage_options')) || ($wppmfunction->has_permission('delete_task',$task_id)) || $wppm_current_user_capability == 'wppm_admin'|| $project_data['created_by']==$current_user->ID )){ |
| 16 |
exit; |
| 17 |
} |
| 18 |
$sql=$wpdb->prepare("SELECT attachment_ids FROM {$wpdb->prefix}wppm_task_comment WHERE task_id = %d",$task_id); |
| 19 |
$thread_attachment_ids= $wpdb->get_results( $sql ); |
| 20 |
/***************************Code for deleting attachment files****************************************************/ |
| 21 |
if(!empty($thread_attachment_ids)){ |
| 22 |
foreach ($thread_attachment_ids as $thread_attachment_id){ |
| 23 |
$attachment_ids_temp = array(); |
| 24 |
if($thread_attachment_id->attachment_ids){ |
| 25 |
$attachment_ids_temp = explode(',', $thread_attachment_id->attachment_ids); |
| 26 |
} |
| 27 |
if(!empty($attachment_ids_temp)){ |
| 28 |
foreach ($attachment_ids_temp as $attachment_id){ |
| 29 |
$sql=$wpdb->prepare("SELECT * FROM {$wpdb->prefix}wppm_attachments WHERE id = %d",$attachment_id); |
| 30 |
$result=$wpdb->get_row($sql); |
| 31 |
if(!empty($result)){ |
| 32 |
$sql_query=$wpdb->prepare("SELECT file_path FROM {$wpdb->prefix}wppm_attachments WHERE file_path = %s",$result->file_path); |
| 33 |
$attach_result=$wpdb->get_results($sql_query); |
| 34 |
$result_count = count($attach_result); |
| 35 |
if(file_exists($result->file_path) && $result_count < 2) |
| 36 |
{ |
| 37 |
wp_delete_file($result->file_path); |
| 38 |
} |
| 39 |
$wpdb->delete($wpdb->prefix.'wppm_attachments', array( 'id' => absint($attachment_id))); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
} |
| 45 |
/***************************Code for deleting checklists****************************************************/ |
| 46 |
$sql=$wpdb->prepare("SELECT id FROM {$wpdb->prefix}wppm_checklist WHERE task_id = %d",$task_id); |
| 47 |
$checklists = $wpdb->get_results( $sql ); |
| 48 |
if(!empty($checklists)){ |
| 49 |
foreach($checklists as $checklist){ |
| 50 |
$chk_array = (array) $checklist; |
| 51 |
$chk_array_id = absint($chk_array['id']); |
| 52 |
$sql=$wpdb->prepare("SELECT id FROM {$wpdb->prefix}wppm_checklist_items WHERE checklist_id = %d",$chk_array_id); |
| 53 |
$checklist_items = $wpdb->get_results( $sql ); |
| 54 |
if(!empty($checklist_items)){ |
| 55 |
foreach($checklist_items as $ch_item){ |
| 56 |
$chk_items_array = (array) $ch_item; |
| 57 |
$chk_items_array_id = absint($chk_items_array['id']); |
| 58 |
$wpdb->delete($wpdb->prefix.'wppm_checklist_items',array('id'=>$chk_items_array_id)); |
| 59 |
} |
| 60 |
} |
| 61 |
$wpdb->delete($wpdb->prefix.'wppm_checklist',array('id'=>$chk_array_id)); |
| 62 |
} |
| 63 |
} |
| 64 |
$wpdb->delete($wpdb->prefix.'wppm_task',array('id'=>$task_id)); |
| 65 |
$wpdb->delete($wpdb->prefix.'wppm_task_meta',array('task_id'=>$task_id)); |
| 66 |
$wpdb->delete($wpdb->prefix.'wppm_task_comment',array('task_id'=>$task_id)) ; |
| 67 |
$wpdb->delete($wpdb->prefix.'wppm_task_comment_meta',array('task_id'=>$task_id)); |
| 68 |
do_action('wppm_after_delete_task',$task_id); |