| 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_proj_thread_attachment', '_ajax_nonce', false ) != 1 ) { |
| 8 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 9 |
} |
| 10 |
$attachment = isset($_POST['attachment']) ? absint(wp_unslash($_POST['attachment'])) : '0' ; |
| 11 |
$attachment = esc_sql($attachment); |
| 12 |
$comment_id = isset($_POST['comment_id']) ? absint(wp_unslash($_POST['comment_id'])) : '0' ; |
| 13 |
$comment_id = esc_sql($comment_id ); |
| 14 |
$proj_id = isset($_POST['proj_id']) ? absint(wp_unslash($_POST['proj_id'])) : '0' ; |
| 15 |
$proj_id = esc_sql($proj_id ); |
| 16 |
$proj_comment = $wpdb->get_row( $wpdb->prepare("SELECT * FROM {$wpdb->prefix}wppm_project_comment WHERE id = %d AND proj_id = %d",absint($comment_id), absint($proj_id))); |
| 17 |
if(!(((!empty($proj_comment)) && ($proj_comment->created_by == $current_user->ID)) || ($current_user->has_cap('manage_options')) || ($wppmfunction->has_proj_comment_permission('edit_proj_comment',$proj_id,$comment_id)))){ |
| 18 |
exit; |
| 19 |
} |
| 20 |
$result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}wppm_attachments WHERE id = %d", $attachment)); |
| 21 |
$sql_query = $wpdb->prepare( "SELECT file_path FROM {$wpdb->prefix}wppm_attachments WHERE file_path = %s", $result->file_path ); |
| 22 |
$attach_result=$wpdb->get_results($sql_query); |
| 23 |
$result_count = count($attach_result); |
| 24 |
if(file_exists($result->file_path) && $result_count < 2) |
| 25 |
{ |
| 26 |
wp_delete_file($result->file_path); |
| 27 |
} |
| 28 |
$success = $wpdb->delete( |
| 29 |
$wpdb->prefix . 'wppm_attachments', |
| 30 |
array( 'id' => $attachment ) |
| 31 |
); |
| 32 |
if( !$success ) return false; |
| 33 |
$proj_attachment = $wpdb->get_var( $wpdb->prepare( "SELECT attachment_ids FROM {$wpdb->prefix}wppm_project_comment WHERE id = %d", $comment_id )); |
| 34 |
$proj_attachment = explode(",",$proj_attachment); |
| 35 |
if(!empty($proj_attachment)){ |
| 36 |
foreach($proj_attachment as $key=>$val){ |
| 37 |
if($val == $attachment){ |
| 38 |
unset($proj_attachment[$key]); |
| 39 |
} |
| 40 |
} |
| 41 |
} |
| 42 |
$pattachment = implode(',',$proj_attachment); |
| 43 |
$values=array( |
| 44 |
'attachment_ids'=>$pattachment |
| 45 |
); |
| 46 |
$wpdb->update($wpdb->prefix.'wppm_project_comment', $values, array('id'=>"$comment_id")); |
| 47 |
$attachment = apply_filters('wppm_after_delete_proj_comment_attachment',$attachment,$comment_id,$proj_id); |