/*************************************************************
* Copyright (c) 2011 Prelovac Media
**************************************************************/
class MMB_User extends MMB_Core
public function __construct()
public function get_users($args)
$user_roles = isset($args['user_roles']) ? $args['user_roles'] : array();
$username_filter = isset($args['username_filter']) ? $args['username_filter'] : '';
$level_strings = array();
foreach ($user_roles as $user_role) {
switch (strtolower($user_role)) {
$level_strings[] = $user_role;
$level_strings[] = $user_role;
$level_strings[] = $user_role;
$level_strings[] = $user_role;
$level_strings[] = $user_role;
$userlevel_qry = "('".implode("','", $userlevels)."')";
if (!empty($level_strings)) {
foreach ($level_strings as $level) {
$queryOR .= "meta_value LIKE '%{$level}%'";
$field = $wpdb->prefix."capabilities";
$field2 = $wpdb->prefix."user_level";
$metaQuery = "SELECT * from {$wpdb->usermeta} WHERE meta_key = '{$field}' AND ({$queryOR})";
$user_metas = $wpdb->get_results($metaQuery);
if ($user_metas == false || empty($user_metas)) {
$metaQuery = "SELECT * from {$wpdb->usermeta} WHERE meta_key = '{$field2}' AND meta_value IN {$userlevel_qry}";
$user_metas = $wpdb->get_results($metaQuery);
$include = array(0 => 0);
if (is_array($user_metas) && !empty($user_metas)) {
foreach ($user_metas as $user_meta) {
$include[] = $user_meta->user_id;
$args['include'] = $include;
$args['fields'] = 'all_with_meta';
if (!empty($username_filter)) {
$args['search'] = $username_filter;
$temp_users = get_users($args);
foreach ((array) $temp_users as $temp) {
$user['user_id'] = $temp->ID;
$user['user_login'] = $temp->user_login;
$user['wp_capabilities'] = array_keys($temp->$field);
return array('users' => $users);
public function add_user($args)
if (!function_exists('username_exists') || !function_exists('email_exists')) {
include_once ABSPATH.WPINC.'/registration.php';
if (username_exists($args['user_login'])) {
return array('error' => 'Username already exists');
if (email_exists($args['user_email'])) {
return array('error' => 'Email already exists');
if (!function_exists('wp_insert_user')) {
include_once ABSPATH.'wp-admin/includes/user.php';
$user_id = wp_insert_user($args);
if ($args['email_notify']) {
//require_once ABSPATH . WPINC . '/pluggable.php';
wp_new_user_notification($user_id, $args['user_pass']);
return array('error' => 'User not added. Please try again.');
public function edit_users($args)
if (!function_exists('get_user_to_edit')) {
include_once ABSPATH.'wp-admin/includes/user.php';
if (!function_exists('wp_update_user')) {
include_once ABSPATH.WPINC.'/user.php';
//$args: $users, $new_role, $new_password, $user_edit_action
// if action is edit-user $args are: $users, $new_role, $new_password, $user_edit_action, $new_first_name, $new_last_name, $new_user_email, $new_description, $new_user_url
foreach ($users as $user) {
$user_obj = $this->mmb_get_user_info($user);
if ($user_obj != false) {
switch ($user_edit_action) {
$userdata['user_pass'] = $new_password;
$userdata['ID'] = $user_obj->ID;
$result = wp_update_user($userdata);
$result = array('error' => 'No password provided.');
if ($user != $username) {
if (!$this->last_admin($user_obj)) {
$userdata['ID'] = $user_obj->ID;
$userdata['role'] = strtolower($new_role);
$result = wp_update_user($userdata);
$result = array('error' => 'Cannot change role to the only one left admin user.');
$result = array('error' => 'Cannot change role to user assigned for ManageWP.');
$result = array('error' => 'No role provided.');
case 'change-description':
$userdata['ID'] = $user_obj->ID;
$userdata['description'] = trim($change_description);
$result = wp_update_user($userdata);
if ($user != $username) {
if (!$this->last_admin($user_obj)) {
$to_user = $this->mmb_get_user_info($reassign_user);
$result = wp_delete_user($user_obj->ID, $to_user->ID);
$result = array('error' => 'User not deleted. User to reassign posts doesn\'t exist.');
$result = wp_delete_user($user_obj->ID);
$result = array('error' => 'Cannot delete the only one left admin user.');
$result = array('error' => 'Cannot delete user assigned for ManageWP.');
$result = array('error' => 'No email provided.');
$result = array('error' => 'No role provided.');
if ($user == $username) {
$result = array('error' => 'Cannot change role to user assigned for ManageWP.');
if ($this->last_admin($user_obj) && $new_role != 'administrator') {
$result = array('error' => 'Cannot change role to the only one left admin user.');
$userdata['ID'] = $user_obj->ID;
$userdata['user_pass'] = $new_password;
$userdata['first_name'] = $new_first_name;
$userdata['last_name'] = $new_last_name;
$userdata['user_email'] = $new_user_email;
$userdata['role'] = strtolower($new_role);
$userdata['description'] = trim($new_description);
$userdata['user_url'] = $new_user_url;
$result = wp_update_user($userdata);
$result = array('error' => 'Wrong action provided. Please try again.');
$result = array('error' => 'User not found.');
if (is_wp_error($result)) {
$result = array('error' => $result->get_error_message());
$return[$user] = $result;
//Check if user is the only one admin on the site
public function last_admin($user_obj)
$field = $wpdb->prefix."capabilities";
$capabilities = array_map('strtolower', array_keys($user_obj->$field));
if (in_array('administrator', $capabilities)) {
if (!function_exists('count_users')) {
include_once ABSPATH.WPINC.'/user.php';
if ($result['avail_roles']['administrator'] == 1) {