# taskbuilder/6.0.6/includes/admin/projects/wppm_set_bulk_project_users.php

Taskbuilder – Project Management &amp; Task Management Tool With Kanban Board, version 6.0.6. 142 lines.

- Page: https://pluginprobe.com/plugins/taskbuilder/6.0.6/code/includes/admin/projects/wppm_set_bulk_project_users.php
- Raw: https://pluginprobe.com/plugins/taskbuilder/6.0.6/raw/includes/admin/projects/wppm_set_bulk_project_users.php
- Modified: 2026-08-26T12:08:58+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/taskbuilder/6.0.6/code/includes/admin/projects/wppm_set_bulk_project_users.php#L10-L20`.

```php
<?php
if ( ! defined( 'ABSPATH' ) ) {
  exit; // Exit if accessed directly
}
global $wpdb,$wppmfunction,$current_user;
if ( check_ajax_referer( 'wppm_set_bulk_project_users', '_ajax_nonce', false ) != 1 ) {
    wp_send_json_error( 'Unauthorised request!', 401 );
}
$wppm_current_user_capability = get_user_meta( $current_user->ID, 'wppm_capability', true );
$proj_ids = isset( $_POST['proj_ids'] ) ? array_filter( array_map( 'intval', explode( ',', sanitize_text_field( wp_unslash( $_POST['proj_ids'] ) ) ) ) ) : array();
if ( ! $proj_ids ) {
    wp_send_json_error( 'Missing project ids', 400 );
}
$users = isset( $_POST['user_names']) ? $wppmfunction->sanitize_array($_POST['user_names']) : ''; 
if(!empty($proj_ids)){
    foreach($proj_ids as $proj_id){
        $project_data = $wppmfunction->get_project($proj_id);
        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')))) {
            $wppm_users_role = get_option('wppm_user_role');
            $prev_assign_users = $project_data['users'];
            if(!empty($prev_assign_users )){
                $prev_assign_users = explode(",",$prev_assign_users);
            }   
            $users = (!empty($_POST['user_names']))? $wppmfunction->sanitize_array($_POST['user_names']):"";
            $prev_assgn_user_meta = $wppmfunction->get_project_meta($proj_id,'prev_assigned_users');
            $wppmfunction->delete_project_meta($proj_id,'prev_assigned_users');
            $task_users = $wpdb->get_results(
                $wpdb->prepare(
                    "SELECT id, users
                    FROM {$wpdb->prefix}wppm_task
                    WHERE project = %d",
                    absint($proj_id)
                )
            );
            if(!empty($prev_assign_users)) {
                foreach($prev_assign_users as $puser){
                    $wppmfunction->add_project_meta($proj_id,'prev_assigned_users',$puser);
                    if(!empty($users) && !in_array($puser,$users)){
                        if(!empty($task_users)){
                            foreach($task_users as $tuser){
                                if(!empty($tuser)){
                                    $wppm_tuser = explode(',',$tuser->users);
                                    if (($key = array_search($puser, $wppm_tuser)) !== false) {
                                        unset($wppm_tuser[$key]);
                                        if(!empty($wppm_tuser)){
                                            $wppmtuser = implode(',',$wppm_tuser);
                                        $value=array(
                                            'users'=>  absint($wppmtuser)
                                        );
                                        } elseif(empty($wppm_tuser)){
                                        $value=array(
                                            'users'=> ''
                                        );
                                        }
                                        $wpdb->update($wpdb->prefix.'wppm_task', $value, array('id'=>absint($tuser->id)));
                                    }
                                }
                            }
                        }
                    }
                    elseif(empty($users) && !empty($task_users)){
                        foreach($task_users as $tuser){
                            if(!empty($tuser)){
                                $wppm_tuser = explode(',',$tuser->users);
                                if (($key = array_search($puser, $wppm_tuser)) !== false) {
                                    unset($wppm_tuser[$key]);
                                    if(!empty($wppm_tuser)){
                                        $wppmtuser = implode(',',$wppm_tuser);
                                        $value=array(
                                            'users'=> absint($wppmtuser)
                                        );
                                    } elseif(empty($wppm_tuser)){
                                        $value=array(
                                            'users'=> ''
                                        );
                                    }
                                    $wpdb->update($wpdb->prefix.'wppm_task', $value, array('id'=>absint($tuser->id)));
                                }
                            }
                        }
                    }
                }
            }
            if(!empty($users)){
                $users = array_unique($users);
                foreach($users as $user){
                    if(!empty($user)){
                        $user_role = sanitize_text_field(wp_unslash($_POST['wppm_select_user_role_'.$user]));
                        $project_user = $wpdb->get_row(
                            $wpdb->prepare(
                                "SELECT *
                                FROM {$wpdb->prefix}wppm_project_users
                                WHERE proj_id = %d
                                AND user_id = %d",
                                absint($proj_id),
                                absint($user)
                            )
                        );
                        if(!empty($project_user)){
                            $values=array(
                                'role_id'=> absint($user_role),
                                'assigned_by'=> absint($current_user->ID)
                            );
                            $wpdb->update($wpdb->prefix.'wppm_project_users', $values, array('id'=>absint($project_user->id)));
                        }else{
                            $wpdb->insert( 
                                $wpdb->prefix . 'wppm_project_users', 
                                array(
                                'proj_id' => absint($proj_id),
                                'user_id' => absint($user),
                                'role_id' => absint($user_role),
                                'assigned_by' => absint($current_user->ID)
                            ) );
                        }
                    }
                }
                $pusers = implode(",",$users);
                if($project_data['users'] != $pusers){
                    $pvalues=array(
                        'users'=> esc_sql($pusers)
                    );
                    $wpdb->update($wpdb->prefix.'wppm_project', $pvalues, array('id'=>absint($proj_id)));
                }
            }
            else{
                $pvalues = array(
                    'users'=>''
                );
                $wpdb->update($wpdb->prefix.'wppm_project', $pvalues, array('id'=>absint($proj_id)));
            }
            $change_assign_user_value = array('prev_assign_user'=>"$project_data[users]",'new_assign_user'=>"$pusers");
            $change_assign_user_obj = serialize($change_assign_user_value);
            $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));
            $wpdb->insert($wpdb->prefix . 'wppm_project_comment',$log_values);
            $log_id = absint($wpdb->insert_id);
            $proj_log_values = array('proj_id'=>absint($proj_id),'comment_id'=>$log_id,'comment_type'=>'change_assign_user');
            $wpdb->insert($wpdb->prefix . 'wppm_project_comment_meta',$proj_log_values);
            do_action('wppm_set_bulk_project_users', $proj_id);
        } 
    }
}
do_action('wppm_after_set_bulk_project_users',$proj_ids, $users);
```
