Edit File by line
/home/barbar84/public_h.../wp-conte.../plugins/worker/src/MMB
File: User.php
<?php
[0] Fix | Delete
[1] Fix | Delete
/*************************************************************
[2] Fix | Delete
* user.class.php
[3] Fix | Delete
* Add Users
[4] Fix | Delete
* Copyright (c) 2011 Prelovac Media
[5] Fix | Delete
* www.prelovac.com
[6] Fix | Delete
**************************************************************/
[7] Fix | Delete
class MMB_User extends MMB_Core
[8] Fix | Delete
{
[9] Fix | Delete
public function __construct()
[10] Fix | Delete
{
[11] Fix | Delete
parent::__construct();
[12] Fix | Delete
}
[13] Fix | Delete
[14] Fix | Delete
public function get_users($args)
[15] Fix | Delete
{
[16] Fix | Delete
global $wpdb;
[17] Fix | Delete
[18] Fix | Delete
//$args: $user_roles;
[19] Fix | Delete
if (empty($args)) {
[20] Fix | Delete
return false;
[21] Fix | Delete
}
[22] Fix | Delete
[23] Fix | Delete
$user_roles = isset($args['user_roles']) ? $args['user_roles'] : array();
[24] Fix | Delete
$username_filter = isset($args['username_filter']) ? $args['username_filter'] : '';
[25] Fix | Delete
[26] Fix | Delete
$userlevels = array();
[27] Fix | Delete
$level_strings = array();
[28] Fix | Delete
foreach ($user_roles as $user_role) {
[29] Fix | Delete
switch (strtolower($user_role)) {
[30] Fix | Delete
case 'subscriber' :
[31] Fix | Delete
$userlevels[] = 0;
[32] Fix | Delete
$level_strings[] = $user_role;
[33] Fix | Delete
break;
[34] Fix | Delete
case 'contributor' :
[35] Fix | Delete
$userlevels[] = 1;
[36] Fix | Delete
$level_strings[] = $user_role;
[37] Fix | Delete
break;
[38] Fix | Delete
case 'author' :
[39] Fix | Delete
$userlevels[] = 2;
[40] Fix | Delete
$level_strings[] = $user_role;
[41] Fix | Delete
break;
[42] Fix | Delete
case 'editor' :
[43] Fix | Delete
$userlevels[] = 7;
[44] Fix | Delete
$level_strings[] = $user_role;
[45] Fix | Delete
break;
[46] Fix | Delete
case 'administrator' :
[47] Fix | Delete
$userlevels[] = 10;
[48] Fix | Delete
$level_strings[] = $user_role;
[49] Fix | Delete
break;
[50] Fix | Delete
default:
[51] Fix | Delete
break;
[52] Fix | Delete
}
[53] Fix | Delete
}
[54] Fix | Delete
[55] Fix | Delete
$users = array();
[56] Fix | Delete
$userlevel_qry = "('".implode("','", $userlevels)."')";
[57] Fix | Delete
$queryOR = '';
[58] Fix | Delete
if (!empty($level_strings)) {
[59] Fix | Delete
foreach ($level_strings as $level) {
[60] Fix | Delete
if (!empty($queryOR)) {
[61] Fix | Delete
$queryOR .= ' OR ';
[62] Fix | Delete
}
[63] Fix | Delete
$queryOR .= "meta_value LIKE '%{$level}%'";
[64] Fix | Delete
}
[65] Fix | Delete
}
[66] Fix | Delete
$field = $wpdb->prefix."capabilities";
[67] Fix | Delete
$field2 = $wpdb->prefix."user_level";
[68] Fix | Delete
[69] Fix | Delete
$metaQuery = "SELECT * from {$wpdb->usermeta} WHERE meta_key = '{$field}' AND ({$queryOR})";
[70] Fix | Delete
$user_metas = $wpdb->get_results($metaQuery);
[71] Fix | Delete
[72] Fix | Delete
if ($user_metas == false || empty($user_metas)) {
[73] Fix | Delete
$metaQuery = "SELECT * from {$wpdb->usermeta} WHERE meta_key = '{$field2}' AND meta_value IN {$userlevel_qry}";
[74] Fix | Delete
$user_metas = $wpdb->get_results($metaQuery);
[75] Fix | Delete
}
[76] Fix | Delete
[77] Fix | Delete
$include = array(0 => 0);
[78] Fix | Delete
if (is_array($user_metas) && !empty($user_metas)) {
[79] Fix | Delete
foreach ($user_metas as $user_meta) {
[80] Fix | Delete
$include[] = $user_meta->user_id;
[81] Fix | Delete
}
[82] Fix | Delete
}
[83] Fix | Delete
[84] Fix | Delete
$args = array(0, 0);
[85] Fix | Delete
$args['include'] = $include;
[86] Fix | Delete
$args['fields'] = 'all_with_meta';
[87] Fix | Delete
if (!empty($username_filter)) {
[88] Fix | Delete
$args['search'] = $username_filter;
[89] Fix | Delete
}
[90] Fix | Delete
$temp_users = get_users($args);
[91] Fix | Delete
$user = array();
[92] Fix | Delete
foreach ((array) $temp_users as $temp) {
[93] Fix | Delete
$user['user_id'] = $temp->ID;
[94] Fix | Delete
$user['user_login'] = $temp->user_login;
[95] Fix | Delete
$user['wp_capabilities'] = array_keys($temp->$field);
[96] Fix | Delete
$users[] = $user;
[97] Fix | Delete
}
[98] Fix | Delete
[99] Fix | Delete
return array('users' => $users);
[100] Fix | Delete
}
[101] Fix | Delete
[102] Fix | Delete
public function add_user($args)
[103] Fix | Delete
{
[104] Fix | Delete
if (!function_exists('username_exists') || !function_exists('email_exists')) {
[105] Fix | Delete
include_once ABSPATH.WPINC.'/registration.php';
[106] Fix | Delete
}
[107] Fix | Delete
[108] Fix | Delete
if (username_exists($args['user_login'])) {
[109] Fix | Delete
return array('error' => 'Username already exists');
[110] Fix | Delete
}
[111] Fix | Delete
[112] Fix | Delete
if (email_exists($args['user_email'])) {
[113] Fix | Delete
return array('error' => 'Email already exists');
[114] Fix | Delete
}
[115] Fix | Delete
[116] Fix | Delete
if (!function_exists('wp_insert_user')) {
[117] Fix | Delete
include_once ABSPATH.'wp-admin/includes/user.php';
[118] Fix | Delete
}
[119] Fix | Delete
[120] Fix | Delete
$user_id = wp_insert_user($args);
[121] Fix | Delete
[122] Fix | Delete
if ($user_id) {
[123] Fix | Delete
if ($args['email_notify']) {
[124] Fix | Delete
//require_once ABSPATH . WPINC . '/pluggable.php';
[125] Fix | Delete
wp_new_user_notification($user_id, $args['user_pass']);
[126] Fix | Delete
}
[127] Fix | Delete
[128] Fix | Delete
return $user_id;
[129] Fix | Delete
} else {
[130] Fix | Delete
return array('error' => 'User not added. Please try again.');
[131] Fix | Delete
}
[132] Fix | Delete
}
[133] Fix | Delete
[134] Fix | Delete
public function edit_users($args)
[135] Fix | Delete
{
[136] Fix | Delete
if (empty($args)) {
[137] Fix | Delete
return false;
[138] Fix | Delete
}
[139] Fix | Delete
if (!function_exists('get_user_to_edit')) {
[140] Fix | Delete
include_once ABSPATH.'wp-admin/includes/user.php';
[141] Fix | Delete
}
[142] Fix | Delete
if (!function_exists('wp_update_user')) {
[143] Fix | Delete
include_once ABSPATH.WPINC.'/user.php';
[144] Fix | Delete
}
[145] Fix | Delete
[146] Fix | Delete
extract($args);
[147] Fix | Delete
//$args: $users, $new_role, $new_password, $user_edit_action
[148] Fix | Delete
// 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
[149] Fix | Delete
[150] Fix | Delete
$return = array();
[151] Fix | Delete
if (count($users)) {
[152] Fix | Delete
foreach ($users as $user) {
[153] Fix | Delete
$result = '';
[154] Fix | Delete
$user_obj = $this->mmb_get_user_info($user);
[155] Fix | Delete
if ($user_obj != false) {
[156] Fix | Delete
switch ($user_edit_action) {
[157] Fix | Delete
case 'change-password':
[158] Fix | Delete
if ($new_password) {
[159] Fix | Delete
$user_data = array();
[160] Fix | Delete
$userdata['user_pass'] = $new_password;
[161] Fix | Delete
$userdata['ID'] = $user_obj->ID;
[162] Fix | Delete
$result = wp_update_user($userdata);
[163] Fix | Delete
} else {
[164] Fix | Delete
$result = array('error' => 'No password provided.');
[165] Fix | Delete
}
[166] Fix | Delete
break;
[167] Fix | Delete
case 'change-role':
[168] Fix | Delete
if ($new_role) {
[169] Fix | Delete
if ($user != $username) {
[170] Fix | Delete
if (!$this->last_admin($user_obj)) {
[171] Fix | Delete
$user_data = array();
[172] Fix | Delete
$userdata['ID'] = $user_obj->ID;
[173] Fix | Delete
$userdata['role'] = strtolower($new_role);
[174] Fix | Delete
$result = wp_update_user($userdata);
[175] Fix | Delete
} else {
[176] Fix | Delete
$result = array('error' => 'Cannot change role to the only one left admin user.');
[177] Fix | Delete
}
[178] Fix | Delete
} else {
[179] Fix | Delete
$result = array('error' => 'Cannot change role to user assigned for ManageWP.');
[180] Fix | Delete
}
[181] Fix | Delete
} else {
[182] Fix | Delete
$result = array('error' => 'No role provided.');
[183] Fix | Delete
}
[184] Fix | Delete
break;
[185] Fix | Delete
case 'change-description':
[186] Fix | Delete
$userdata = array();
[187] Fix | Delete
$userdata['ID'] = $user_obj->ID;
[188] Fix | Delete
$userdata['description'] = trim($change_description);
[189] Fix | Delete
$result = wp_update_user($userdata);
[190] Fix | Delete
break;
[191] Fix | Delete
case 'delete-user':
[192] Fix | Delete
if ($user != $username) {
[193] Fix | Delete
if (!$this->last_admin($user_obj)) {
[194] Fix | Delete
if ($reassign_user) {
[195] Fix | Delete
$to_user = $this->mmb_get_user_info($reassign_user);
[196] Fix | Delete
if ($to_user != false) {
[197] Fix | Delete
$result = wp_delete_user($user_obj->ID, $to_user->ID);
[198] Fix | Delete
} else {
[199] Fix | Delete
$result = array('error' => 'User not deleted. User to reassign posts doesn\'t exist.');
[200] Fix | Delete
}
[201] Fix | Delete
} else {
[202] Fix | Delete
$result = wp_delete_user($user_obj->ID);
[203] Fix | Delete
}
[204] Fix | Delete
} else {
[205] Fix | Delete
$result = array('error' => 'Cannot delete the only one left admin user.');
[206] Fix | Delete
}
[207] Fix | Delete
} else {
[208] Fix | Delete
$result = array('error' => 'Cannot delete user assigned for ManageWP.');
[209] Fix | Delete
}
[210] Fix | Delete
[211] Fix | Delete
break;
[212] Fix | Delete
case 'edit-user':
[213] Fix | Delete
if (!$new_user_email) {
[214] Fix | Delete
$result = array('error' => 'No email provided.');
[215] Fix | Delete
break;
[216] Fix | Delete
}
[217] Fix | Delete
[218] Fix | Delete
if (!$new_role) {
[219] Fix | Delete
$result = array('error' => 'No role provided.');
[220] Fix | Delete
break;
[221] Fix | Delete
}
[222] Fix | Delete
[223] Fix | Delete
if ($user == $username) {
[224] Fix | Delete
$result = array('error' => 'Cannot change role to user assigned for ManageWP.');
[225] Fix | Delete
break;
[226] Fix | Delete
}
[227] Fix | Delete
[228] Fix | Delete
if ($this->last_admin($user_obj) && $new_role != 'administrator') {
[229] Fix | Delete
$result = array('error' => 'Cannot change role to the only one left admin user.');
[230] Fix | Delete
break;
[231] Fix | Delete
}
[232] Fix | Delete
[233] Fix | Delete
$userdata = array();
[234] Fix | Delete
$userdata['ID'] = $user_obj->ID;
[235] Fix | Delete
[236] Fix | Delete
if ($new_password) {
[237] Fix | Delete
$userdata['user_pass'] = $new_password;
[238] Fix | Delete
}
[239] Fix | Delete
[240] Fix | Delete
$userdata['first_name'] = $new_first_name;
[241] Fix | Delete
$userdata['last_name'] = $new_last_name;
[242] Fix | Delete
$userdata['user_email'] = $new_user_email;
[243] Fix | Delete
$userdata['role'] = strtolower($new_role);
[244] Fix | Delete
$userdata['description'] = trim($new_description);
[245] Fix | Delete
$userdata['user_url'] = $new_user_url;
[246] Fix | Delete
$result = wp_update_user($userdata);
[247] Fix | Delete
break;
[248] Fix | Delete
default:
[249] Fix | Delete
$result = array('error' => 'Wrong action provided. Please try again.');
[250] Fix | Delete
break;
[251] Fix | Delete
}
[252] Fix | Delete
} else {
[253] Fix | Delete
$result = array('error' => 'User not found.');
[254] Fix | Delete
}
[255] Fix | Delete
[256] Fix | Delete
if (is_wp_error($result)) {
[257] Fix | Delete
$result = array('error' => $result->get_error_message());
[258] Fix | Delete
}
[259] Fix | Delete
[260] Fix | Delete
$return[$user] = $result;
[261] Fix | Delete
}
[262] Fix | Delete
}
[263] Fix | Delete
[264] Fix | Delete
return $return;
[265] Fix | Delete
}
[266] Fix | Delete
[267] Fix | Delete
//Check if user is the only one admin on the site
[268] Fix | Delete
public function last_admin($user_obj)
[269] Fix | Delete
{
[270] Fix | Delete
global $wpdb;
[271] Fix | Delete
$field = $wpdb->prefix."capabilities";
[272] Fix | Delete
$capabilities = array_map('strtolower', array_keys($user_obj->$field));
[273] Fix | Delete
$result = count_users();
[274] Fix | Delete
if (in_array('administrator', $capabilities)) {
[275] Fix | Delete
if (!function_exists('count_users')) {
[276] Fix | Delete
include_once ABSPATH.WPINC.'/user.php';
[277] Fix | Delete
}
[278] Fix | Delete
[279] Fix | Delete
$result = count_users();
[280] Fix | Delete
if ($result['avail_roles']['administrator'] == 1) {
[281] Fix | Delete
return true;
[282] Fix | Delete
}
[283] Fix | Delete
}
[284] Fix | Delete
[285] Fix | Delete
return false;
[286] Fix | Delete
}
[287] Fix | Delete
}
[288] Fix | Delete
[289] Fix | Delete
It is recommended that you Edit text format, this type of Fix handles quite a lot in one request
Function