| 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_remove_thread_attachment', '_ajax_nonce', false ) != 1 ) { |
| 8 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 9 |
} |
| 10 |
$attachment = isset($_POST['attachment']) ? intval(sanitize_text_field($_POST['attachment'])) : '0' ; |
| 11 |
$comment_id = isset($_POST['comment_id']) ? intval(sanitize_text_field($_POST['comment_id'])) : '0' ; |
| 12 |
$task_id = isset($_POST['task_id']) ? intval(sanitize_text_field($_POST['task_id'])) : '0' ; |
| 13 |
$task_comment = $wpdb->get_row( "SELECT * FROM {$wpdb->prefix}wppm_task_comment where id = $comment_id AND task_id=$task_id"); |
| 14 |
if(!(((!empty($task_comment)) && ($task_comment->created_by == $current_user->ID)) || ($current_user->has_cap('manage_options')) || ($wppmfunction->has_comment_permission('edit_task_comment',$task_id,$comment_id)))){ |
| 15 |
exit; |
| 16 |
} |
| 17 |
$sql="SELECT * FROM {$wpdb->prefix}wppm_attachments WHERE id =".$attachment; |
| 18 |
$result=$wpdb->get_row($sql); |
| 19 |
$sql_query="SELECT file_path FROM {$wpdb->prefix}wppm_attachments WHERE file_path ='".$result->file_path."'"; |
| 20 |
$attach_result=$wpdb->get_results($sql_query); |
| 21 |
$result_count = count($attach_result); |
| 22 |
if(file_exists($result->file_path) && $result_count < 2) |
| 23 |
{ |
| 24 |
unlink($result->file_path); |
| 25 |
} |
| 26 |
$success = $wpdb->delete( |
| 27 |
$wpdb->prefix . 'wppm_attachments', |
| 28 |
array( 'id' => $attachment ) |
| 29 |
); |
| 30 |
if( !$success ) return false; |
| 31 |
$task_attachment = $wpdb->get_var("SELECT attachment_ids FROM {$wpdb->prefix}wppm_task_comment WHERE id=$comment_id "); |
| 32 |
$task_attachment = explode(",",$task_attachment); |
| 33 |
if(!empty($task_attachment)){ |
| 34 |
foreach($task_attachment as $key=>$val){ |
| 35 |
if($val == $attachment){ |
| 36 |
unset($task_attachment[$key]); |
| 37 |
} |
| 38 |
} |
| 39 |
} |
| 40 |
$tattachment = implode(',',$task_attachment); |
| 41 |
$values=array( |
| 42 |
'attachment_ids'=>$tattachment |
| 43 |
); |
| 44 |
$wpdb->update($wpdb->prefix.'wppm_task_comment', $values, array('id'=>$comment_id)); |
| 45 |
$attachment = apply_filters('wppm_after_delete_comment_attachment',$attachment,$comment_id,$task_id); |