You are here

function drush_delete_all_delete_users in Delete all 8

Same name and namespace in other branches
  1. 2.x drush/delete_all.drush.inc \drush_delete_all_delete_users()

Drush callback to delete users.

File

drush/delete_all.drush.inc, line 112
delete all Drush command

Code

function drush_delete_all_delete_users() {

  // Initialize $roles as FALSE to specify that all users should be deleted.
  // This will be overriden if user provides/choses a role.
  $input_roles = FALSE;
  $deleteUser = new UserDeleteController();

  // Check for presence of '--roles' in drush command.
  if (drush_get_option('role')) {

    // func_get_args collects all keywords separated by space in an array.
    // To get the roles, we join all the keywords in a string and then use
    // 'comma' to separate them.
    $types = func_get_args();
    if ($types) {
      $input_roles = implode(' ', $types);
      if (strpos($input_roles, ',')) {
        $input_roles = explode(',', $input_roles);
      }
      else {
        $input_roles = [
          $input_roles,
        ];
      }
    }
    else {
      $choices = [];

      // Output all roles on screen and ask user to choose one.
      $roles = user_roles();
      foreach ($roles as $key => $value) {
        $choices[$key] = $value
          ->label();
      }
      $role = drush_choice($choices, dt("Choose a role to delete."));

      // Return if no role is chosen.
      if ($role === 0 || $role === FALSE) {
        drush_user_abort();
        return;
      }
      $input_roles = [
        $role,
      ];
    }
  }
  if (drush_confirm('Are you sure you want to delete the users?')) {

    // Get users to delete.
    $users_to_delete = $deleteUser
      ->getUserToDelete($input_roles);

    // Get batch array.
    $batch = $deleteUser
      ->getUserDeleteBatch($users_to_delete);

    // Initialize the batch.
    batch_set($batch);

    // Start the batch process.
    drush_backend_batch_process();
  }
  else {
    drush_user_abort();
  }
}