| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; // Exit if accessed directly |
| 4 |
} |
| 5 |
global $wpdb,$wppmfunction,$current_user; |
| 6 |
if ( check_ajax_referer( 'wppm_set_bulk_project_users', '_ajax_nonce', false ) != 1 ) { |
| 7 |
wp_send_json_error( 'Unauthorised request!', 401 ); |
| 8 |
} |
| 9 |
$wppm_current_user_capability = get_user_meta( $current_user->ID, 'wppm_capability', true ); |
| 10 |
$proj_ids = isset( $_POST['proj_ids'] ) ? array_filter( array_map( 'intval', explode( ',', sanitize_text_field( wp_unslash( $_POST['proj_ids'] ) ) ) ) ) : array(); |
| 11 |
if ( ! $proj_ids ) { |
| 12 |
wp_send_json_error( 'Missing project ids', 400 ); |
| 13 |
} |
| 14 |
$users = isset( $_POST['user_names']) ? $wppmfunction->sanitize_array($_POST['user_names']) : ''; |
| 15 |
if(!empty($proj_ids)){ |
| 16 |
foreach($proj_ids as $proj_id){ |
| 17 |
$project_data = $wppmfunction->get_project($proj_id); |
| 18 |
if ((($current_user->ID && $current_user->has_cap('manage_options')) || $wppmfunction->has_project_permission('assign_project_users',$proj_id) || ($project_data['created_by']==$current_user->ID && $wppm_current_user_capability == 'wppm_manager') || ($current_user->ID && $current_user->has_cap('wppm_admin')))) { |
| 19 |
$wppm_users_role = get_option('wppm_user_role'); |
| 20 |
$prev_assign_users = $project_data['users']; |
| 21 |
if(!empty($prev_assign_users )){ |
| 22 |
$prev_assign_users = explode(",",$prev_assign_users); |
| 23 |
} |
| 24 |
$users = (!empty($_POST['user_names']))? $wppmfunction->sanitize_array($_POST['user_names']):""; |
| 25 |
$prev_assgn_user_meta = $wppmfunction->get_project_meta($proj_id,'prev_assigned_users'); |
| 26 |
$wppmfunction->delete_project_meta($proj_id,'prev_assigned_users'); |
| 27 |
$task_users = $wpdb->get_results( |
| 28 |
$wpdb->prepare( |
| 29 |
"SELECT id, users |
| 30 |
FROM {$wpdb->prefix}wppm_task |
| 31 |
WHERE project = %d", |
| 32 |
absint($proj_id) |
| 33 |
) |
| 34 |
); |
| 35 |
if(!empty($prev_assign_users)) { |
| 36 |
foreach($prev_assign_users as $puser){ |
| 37 |
$wppmfunction->add_project_meta($proj_id,'prev_assigned_users',$puser); |
| 38 |
if(!empty($users) && !in_array($puser,$users)){ |
| 39 |
if(!empty($task_users)){ |
| 40 |
foreach($task_users as $tuser){ |
| 41 |
if(!empty($tuser)){ |
| 42 |
$wppm_tuser = explode(',',$tuser->users); |
| 43 |
if (($key = array_search($puser, $wppm_tuser)) !== false) { |
| 44 |
unset($wppm_tuser[$key]); |
| 45 |
if(!empty($wppm_tuser)){ |
| 46 |
$wppmtuser = implode(',',$wppm_tuser); |
| 47 |
$value=array( |
| 48 |
'users'=> absint($wppmtuser) |
| 49 |
); |
| 50 |
} elseif(empty($wppm_tuser)){ |
| 51 |
$value=array( |
| 52 |
'users'=> '' |
| 53 |
); |
| 54 |
} |
| 55 |
$wpdb->update($wpdb->prefix.'wppm_task', $value, array('id'=>absint($tuser->id))); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
} |
| 61 |
elseif(empty($users) && !empty($task_users)){ |
| 62 |
foreach($task_users as $tuser){ |
| 63 |
if(!empty($tuser)){ |
| 64 |
$wppm_tuser = explode(',',$tuser->users); |
| 65 |
if (($key = array_search($puser, $wppm_tuser)) !== false) { |
| 66 |
unset($wppm_tuser[$key]); |
| 67 |
if(!empty($wppm_tuser)){ |
| 68 |
$wppmtuser = implode(',',$wppm_tuser); |
| 69 |
$value=array( |
| 70 |
'users'=> absint($wppmtuser) |
| 71 |
); |
| 72 |
} elseif(empty($wppm_tuser)){ |
| 73 |
$value=array( |
| 74 |
'users'=> '' |
| 75 |
); |
| 76 |
} |
| 77 |
$wpdb->update($wpdb->prefix.'wppm_task', $value, array('id'=>absint($tuser->id))); |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
} |
| 84 |
if(!empty($users)){ |
| 85 |
$users = array_unique($users); |
| 86 |
foreach($users as $user){ |
| 87 |
if(!empty($user)){ |
| 88 |
$user_role = sanitize_text_field(wp_unslash($_POST['wppm_select_user_role_'.$user])); |
| 89 |
$project_user = $wpdb->get_row( |
| 90 |
$wpdb->prepare( |
| 91 |
"SELECT * |
| 92 |
FROM {$wpdb->prefix}wppm_project_users |
| 93 |
WHERE proj_id = %d |
| 94 |
AND user_id = %d", |
| 95 |
absint($proj_id), |
| 96 |
absint($user) |
| 97 |
) |
| 98 |
); |
| 99 |
if(!empty($project_user)){ |
| 100 |
$values=array( |
| 101 |
'role_id'=> absint($user_role), |
| 102 |
'assigned_by'=> absint($current_user->ID) |
| 103 |
); |
| 104 |
$wpdb->update($wpdb->prefix.'wppm_project_users', $values, array('id'=>absint($project_user->id))); |
| 105 |
}else{ |
| 106 |
$wpdb->insert( |
| 107 |
$wpdb->prefix . 'wppm_project_users', |
| 108 |
array( |
| 109 |
'proj_id' => absint($proj_id), |
| 110 |
'user_id' => absint($user), |
| 111 |
'role_id' => absint($user_role), |
| 112 |
'assigned_by' => absint($current_user->ID) |
| 113 |
) ); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
$pusers = implode(",",$users); |
| 118 |
if($project_data['users'] != $pusers){ |
| 119 |
$pvalues=array( |
| 120 |
'users'=> esc_sql($pusers) |
| 121 |
); |
| 122 |
$wpdb->update($wpdb->prefix.'wppm_project', $pvalues, array('id'=>absint($proj_id))); |
| 123 |
} |
| 124 |
} |
| 125 |
else{ |
| 126 |
$pvalues = array( |
| 127 |
'users'=>'' |
| 128 |
); |
| 129 |
$wpdb->update($wpdb->prefix.'wppm_project', $pvalues, array('id'=>absint($proj_id))); |
| 130 |
} |
| 131 |
$change_assign_user_value = array('prev_assign_user'=>"$project_data[users]",'new_assign_user'=>"$pusers"); |
| 132 |
$change_assign_user_obj = serialize($change_assign_user_value); |
| 133 |
$log_values = array('proj_id'=>absint($proj_id),'body'=>$change_assign_user_obj,'attachment_ids'=>"",'create_time'=>wp_date("Y-m-d h:i:sa"),'created_by'=>absint($current_user->ID)); |
| 134 |
$wpdb->insert($wpdb->prefix . 'wppm_project_comment',$log_values); |
| 135 |
$log_id = absint($wpdb->insert_id); |
| 136 |
$proj_log_values = array('proj_id'=>absint($proj_id),'comment_id'=>$log_id,'comment_type'=>'change_assign_user'); |
| 137 |
$wpdb->insert($wpdb->prefix . 'wppm_project_comment_meta',$proj_log_values); |
| 138 |
do_action('wppm_set_bulk_project_users', $proj_id); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
do_action('wppm_after_set_bulk_project_users',$proj_ids, $users); |