2016-11-28 21:52:15 -08:00
|
|
|
<?php
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
if (!defined('UPDRAFTCENTRAL_CLIENT_DIR')) die('No access.');
|
2016-11-28 21:52:15 -08:00
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
/**
|
|
|
|
* Handles Users Commands
|
|
|
|
*/
|
2016-11-28 21:52:15 -08:00
|
|
|
class UpdraftCentral_Users_Commands extends UpdraftCentral_Commands {
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
/**
|
|
|
|
* Compares two user object whether one is lesser than, equal to, greater than the other
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @param array $a First user in the comparison
|
|
|
|
* @param array $b Second user in the comparison
|
|
|
|
* @return integer Comparison results (0 = equal, -1 = less than, 1 = greater than)
|
|
|
|
*/
|
|
|
|
private function compare_user_id($a, $b) {
|
|
|
|
if ($a->ID === $b->ID) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ($a->ID < $b->ID) ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Searches users based from the keyword submitted
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @param array $query Parameter array containing the filter and keyword fields
|
|
|
|
* @return array Contains the list of users found as well as the total users count
|
|
|
|
*/
|
|
|
|
private function _search_users($query) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$this->_admin_include('user.php');
|
2018-01-26 15:50:15 +01:00
|
|
|
$query1 = new WP_User_Query(array(
|
2016-11-28 21:52:15 -08:00
|
|
|
'orderby' => 'ID',
|
|
|
|
'order' => 'ASC',
|
|
|
|
'role'=> $query["role"],
|
2018-01-26 15:50:15 +01:00
|
|
|
'search' => '*' . esc_attr($query["search"]) . '*',
|
|
|
|
'search_columns' => array('user_login', 'user_email')
|
2016-11-28 21:52:15 -08:00
|
|
|
));
|
2018-01-26 15:50:15 +01:00
|
|
|
$query2 = new WP_User_Query(array(
|
2016-11-28 21:52:15 -08:00
|
|
|
'orderby' => 'ID',
|
|
|
|
'order' => 'ASC',
|
|
|
|
'role'=> $query["role"],
|
|
|
|
'meta_query'=>array(
|
|
|
|
'relation' => 'OR',
|
|
|
|
array(
|
2018-01-26 15:50:15 +01:00
|
|
|
'key' => 'first_name',
|
|
|
|
'value' => $query["search"],
|
|
|
|
'compare' => 'LIKE'
|
2016-11-28 21:52:15 -08:00
|
|
|
),
|
|
|
|
array(
|
2018-01-26 15:50:15 +01:00
|
|
|
'key' => 'last_name',
|
|
|
|
'value' => $query["search"],
|
|
|
|
'compare' => 'LIKE'
|
2016-11-28 21:52:15 -08:00
|
|
|
),
|
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
if (empty($query1->results) && empty($query2->results)) {
|
2016-11-28 21:52:15 -08:00
|
|
|
return array("message" => "users_not_found");
|
2018-01-26 15:50:15 +01:00
|
|
|
} else {
|
|
|
|
$found_users = array_merge($query1->results, $query2->results);
|
|
|
|
$temp = array();
|
|
|
|
foreach ($found_users as $new_user) {
|
|
|
|
if (!isset($temp[$new_user->ID])) {
|
|
|
|
$temp[$new_user->ID] = $new_user;
|
2016-11-28 21:52:15 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
$users = array_values($temp);
|
2016-11-28 21:52:15 -08:00
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
// Sort users:
|
|
|
|
usort($users, array($this, 'compare_user_id'));
|
|
|
|
$offset = (intval($query['page_no']) * intval($query['per_page'])) - intval($query['per_page']);
|
|
|
|
$user_list = array_slice($users, $offset, $query['per_page']);
|
2016-11-28 21:52:15 -08:00
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
return array(
|
|
|
|
'users' => $user_list,
|
|
|
|
'total_users' => count($users)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates the number of pages needed to construct the pagination links
|
|
|
|
*
|
|
|
|
* @internal
|
|
|
|
* @param array $query
|
|
|
|
* @param array $total_users The total number of users found from the WP_User_Query query
|
|
|
|
* @return array Contains information needed to construct the pagination links
|
|
|
|
*/
|
|
|
|
private function _calculate_pages($query, $total_users) {
|
|
|
|
|
|
|
|
$per_page_options = array(10, 20, 30, 40, 50);
|
|
|
|
|
|
|
|
if (!empty($query)) {
|
2016-11-28 21:52:15 -08:00
|
|
|
|
|
|
|
$pages = array();
|
2018-01-26 15:50:15 +01:00
|
|
|
$page_count = ceil($total_users / $query["per_page"]);
|
|
|
|
if ($page_count > 1) {
|
2016-11-28 21:52:15 -08:00
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
for ($i = 0; $i < $page_count; $i++) {
|
|
|
|
if ($i + 1 == $query['page_no']) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$paginator_item = array(
|
|
|
|
"value"=>$i+1,
|
|
|
|
"setting"=>"disabled"
|
|
|
|
);
|
2018-01-26 15:50:15 +01:00
|
|
|
} else {
|
2016-11-28 21:52:15 -08:00
|
|
|
$paginator_item = array(
|
|
|
|
"value"=>$i+1
|
|
|
|
);
|
|
|
|
}
|
|
|
|
array_push($pages, $paginator_item);
|
|
|
|
};
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
if ($query['page_no'] >= $page_count) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$page_next = array(
|
|
|
|
"value"=>$page_count,
|
|
|
|
"setting"=>"disabled"
|
|
|
|
);
|
2018-01-26 15:50:15 +01:00
|
|
|
} else {
|
2016-11-28 21:52:15 -08:00
|
|
|
$page_next = array(
|
2018-01-26 15:50:15 +01:00
|
|
|
"value"=>$query['page_no'] + 1
|
2016-11-28 21:52:15 -08:00
|
|
|
);
|
|
|
|
};
|
2018-01-26 15:50:15 +01:00
|
|
|
if (1 === $query['page_no']) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$page_prev = array(
|
|
|
|
"value"=>1,
|
|
|
|
"setting"=>"disabled"
|
|
|
|
);
|
2018-01-26 15:50:15 +01:00
|
|
|
} else {
|
2016-11-28 21:52:15 -08:00
|
|
|
$page_prev = array(
|
2018-01-26 15:50:15 +01:00
|
|
|
"value"=>$query['page_no'] - 1
|
2016-11-28 21:52:15 -08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
return array(
|
2018-01-26 15:50:15 +01:00
|
|
|
"page_no" => $query['page_no'],
|
2016-11-28 21:52:15 -08:00
|
|
|
"per_page" => $query["per_page"],
|
|
|
|
"page_count" => $page_count,
|
|
|
|
"pages" => $pages,
|
|
|
|
"page_next" => $page_next,
|
|
|
|
"page_prev" => $page_prev,
|
2018-01-26 15:50:15 +01:00
|
|
|
"total_results" => $total_users,
|
2016-11-28 21:52:15 -08:00
|
|
|
"per_page_options" => $per_page_options
|
|
|
|
);
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
} else {
|
2016-11-28 21:52:15 -08:00
|
|
|
return array(
|
2018-01-26 15:50:15 +01:00
|
|
|
"page_no" => $query['page_no'],
|
2016-11-28 21:52:15 -08:00
|
|
|
"per_page" => $query["per_page"],
|
|
|
|
"page_count" => $page_count,
|
2018-01-26 15:50:15 +01:00
|
|
|
"total_results" => $total_users,
|
2016-11-28 21:52:15 -08:00
|
|
|
"per_page_options" => $per_page_options
|
|
|
|
);
|
|
|
|
}
|
2018-01-26 15:50:15 +01:00
|
|
|
} else {
|
2016-11-28 21:52:15 -08:00
|
|
|
return array(
|
|
|
|
"per_page_options" => $per_page_options
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
/**
|
|
|
|
* Validates whether the username exists
|
|
|
|
*
|
|
|
|
* @param array $params Contains the user name to check and validate
|
|
|
|
* @return array An array containing the result of the current process
|
|
|
|
*/
|
|
|
|
public function check_username($params) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$this->_admin_include('user.php');
|
2018-01-26 15:50:15 +01:00
|
|
|
$username = $params['user_name'];
|
|
|
|
|
|
|
|
$blog_id = get_current_blog_id();
|
|
|
|
if (!empty($params['site_id'])) {
|
|
|
|
$blog_id = $params['site_id'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Here, we're switching to the actual blog that we need
|
|
|
|
// to pull users from.
|
|
|
|
|
|
|
|
$switched = function_exists('switch_to_blog') ? switch_to_blog($blog_id) : false;
|
|
|
|
|
|
|
|
if (username_exists($username) && is_user_member_of_blog(username_exists($username), $blog_id)) {
|
|
|
|
$result = array("valid" => false, "message" => 'username_exists');
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
2018-01-26 15:50:15 +01:00
|
|
|
if (!validate_username($username)) {
|
|
|
|
$result = array("valid" => false, "message" => 'username_invalid');
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
|
|
|
|
// Here, we're restoring to the current (default) blog before we
|
|
|
|
// do the switched.
|
|
|
|
|
|
|
|
if (function_exists('restore_current_blog') && $switched) {
|
|
|
|
restore_current_blog();
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = array("valid" => true, "message" => 'username_valid');
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
/**
|
|
|
|
* Pulls blog sites available
|
|
|
|
* for the current WP instance.
|
|
|
|
* If the site is a multisite, then sites under the network
|
|
|
|
* will be pulled, otherwise, it will return an empty array.
|
|
|
|
*
|
|
|
|
* @return Array - an array of sites
|
|
|
|
*/
|
|
|
|
private function _get_blog_sites() {
|
|
|
|
|
|
|
|
if (!is_multisite()) return array();
|
|
|
|
|
|
|
|
// Initialize array container
|
|
|
|
$sites = $network_sites = array();
|
|
|
|
|
|
|
|
// Check to see if latest get_sites (available on WP version >= 4.6) function is
|
|
|
|
// available to pull any available sites from the current WP instance. If not, then
|
|
|
|
// we're going to use the fallback function wp_get_sites (for older version).
|
|
|
|
if (function_exists('get_sites') && class_exists('WP_Site_Query')) {
|
|
|
|
$network_sites = get_sites();
|
|
|
|
} else {
|
|
|
|
if (function_exists('wp_get_sites')) {
|
|
|
|
$network_sites = wp_get_sites();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We only process if sites array is not empty, otherwise, bypass
|
|
|
|
// the next block.
|
|
|
|
if (!empty($network_sites)) {
|
|
|
|
foreach ($network_sites as $site) {
|
|
|
|
|
|
|
|
// Here we're checking if the site type is an array, because
|
|
|
|
// we're pulling the blog_id property based on the type of
|
|
|
|
// site returned.
|
|
|
|
// get_sites returns an array of object, whereas the wp_get_sites
|
|
|
|
// function returns an array of array.
|
|
|
|
$blog_id = is_array($site) ? $site['blog_id'] : $site->blog_id;
|
|
|
|
|
|
|
|
|
|
|
|
// We're saving the blog_id and blog name as an associative item
|
|
|
|
// into the sites array, that will be used as "Sites" option in
|
|
|
|
// the frontend.
|
|
|
|
$sites[$blog_id] = get_blog_details($blog_id)->blogname;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $sites;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validates whether the email exists
|
|
|
|
*
|
|
|
|
* @param array $params Contains the email to check and validate
|
|
|
|
* @return array An array containing the result of the current process
|
|
|
|
*/
|
|
|
|
public function check_email($params) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$this->_admin_include('user.php');
|
2018-01-26 15:50:15 +01:00
|
|
|
$email = $params['email'];
|
|
|
|
|
|
|
|
$blog_id = get_current_blog_id();
|
|
|
|
if (isset($params['site_id']) && 0 !== $params['site_id']) {
|
|
|
|
$blog_id = $params['site_id'];
|
|
|
|
}
|
|
|
|
|
2016-11-28 21:52:15 -08:00
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
// Here, we're switching to the actual blog that we need
|
|
|
|
// to pull users from.
|
|
|
|
|
|
|
|
$switched = false;
|
|
|
|
if (function_exists('switch_to_blog')) {
|
|
|
|
$switched = switch_to_blog($blog_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_email($email) === false) {
|
|
|
|
$result = array("valid" => false, "message" => 'email_invalid');
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
if (email_exists($email) && is_user_member_of_blog(email_exists($email), $blog_id)) {
|
|
|
|
$result = array("valid" => false, "message" => 'email_exists');
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
// Here, we're restoring to the current (default) blog before we
|
|
|
|
// do the switched.
|
|
|
|
|
|
|
|
if (function_exists('restore_current_blog') && $switched) {
|
|
|
|
restore_current_blog();
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = array("valid" => true, "message" => 'email_valid');
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
/**
|
|
|
|
* The get_users function pull all the users from the database
|
|
|
|
* based on the current search parameters/filters. Please see _search_users
|
|
|
|
* for the breakdown of these parameters.
|
|
|
|
*
|
|
|
|
* @param array $query Parameter array containing the filter and keyword fields
|
|
|
|
* @return array An array containing the result of the current process
|
|
|
|
*/
|
2016-11-28 21:52:15 -08:00
|
|
|
public function get_users($query) {
|
|
|
|
$this->_admin_include('user.php');
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
// Here, we're getting the current blog id. If blog id
|
|
|
|
// is passed along with the parameters then we override
|
|
|
|
// that current (default) value with the parameter blog id value.
|
|
|
|
$blog_id = get_current_blog_id();
|
|
|
|
if (isset($query['site_id']) && 0 !== $query['site_id']) $blog_id = $query['site_id'];
|
2016-11-28 21:52:15 -08:00
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
|
|
|
|
// Here, we're switching to the actual blog that we need
|
|
|
|
// to pull users from.
|
|
|
|
|
|
|
|
$switched = false;
|
|
|
|
if (function_exists('switch_to_blog')) {
|
|
|
|
$switched = switch_to_blog($blog_id);
|
2016-11-28 21:52:15 -08:00
|
|
|
}
|
2018-01-26 15:50:15 +01:00
|
|
|
|
|
|
|
// Set default:
|
|
|
|
if (empty($query["per_page"])) {
|
|
|
|
$query["per_page"] = 10;
|
|
|
|
}
|
|
|
|
if (empty($query['page_no'])) {
|
|
|
|
$query['page_no'] = 1;
|
|
|
|
}
|
|
|
|
if (empty($query["role"])) {
|
|
|
|
$query["role"] = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
$users = array();
|
|
|
|
$total_users = 0;
|
|
|
|
|
|
|
|
if (!empty($query["search"])) {
|
|
|
|
$search_results = $this->_search_users($query);
|
|
|
|
|
|
|
|
if (isset($search_results['users'])) {
|
|
|
|
$users = $search_results['users'];
|
|
|
|
$total_users = $search_results['total_users'];
|
|
|
|
}
|
|
|
|
} else {
|
2016-11-28 21:52:15 -08:00
|
|
|
$user_query = new WP_User_Query(array(
|
|
|
|
'orderby' => 'ID',
|
|
|
|
'order' => 'ASC',
|
|
|
|
'number' => $query["per_page"],
|
2018-01-26 15:50:15 +01:00
|
|
|
'paged'=> $query['page_no'],
|
2016-11-28 21:52:15 -08:00
|
|
|
'role'=> $query["role"]
|
|
|
|
));
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
if (empty($user_query->results)) {
|
|
|
|
$result = array("message" => 'users_not_found');
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
$users = $user_query->results;
|
2018-01-26 15:50:15 +01:00
|
|
|
$total_users = $user_query->get_total();
|
2016-11-28 21:52:15 -08:00
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
foreach ($users as &$user) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$user_object = get_userdata($user->ID);
|
2018-01-26 15:50:15 +01:00
|
|
|
if (method_exists($user_object, 'to_array')) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$user = $user_object->to_array();
|
|
|
|
$user["roles"] = $user_object->roles;
|
|
|
|
$user["first_name"] = $user_object->first_name;
|
|
|
|
$user["last_name"] = $user_object->last_name;
|
|
|
|
$user["description"] = $user_object->description;
|
2018-01-26 15:50:15 +01:00
|
|
|
} else {
|
2016-11-28 21:52:15 -08:00
|
|
|
$user = $user_object;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$result = array(
|
|
|
|
"users"=>$users,
|
2018-01-26 15:50:15 +01:00
|
|
|
"paging" => $this->_calculate_pages($query, $total_users)
|
2016-11-28 21:52:15 -08:00
|
|
|
);
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
// Here, we're restoring to the current (default) blog before we
|
|
|
|
// do the switched.
|
|
|
|
|
|
|
|
if (function_exists('restore_current_blog') && $switched) {
|
|
|
|
restore_current_blog();
|
|
|
|
}
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
2018-01-26 15:50:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates new user for the current blog
|
|
|
|
*
|
|
|
|
* @param array $user User information to add
|
|
|
|
* @return array An array containing the result of the current process
|
|
|
|
*/
|
|
|
|
public function add_user($user) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$this->_admin_include('user.php');
|
2018-01-26 15:50:15 +01:00
|
|
|
// Here, we're getting the current blog id. If blog id
|
|
|
|
// is passed along with the parameters then we override
|
|
|
|
// that current (default) value with the parameter blog id value.
|
|
|
|
|
|
|
|
|
|
|
|
$blog_id = get_current_blog_id();
|
|
|
|
if (isset($user['site_id']) && 0 !== $user['site_id']) $blog_id = $user['site_id'];
|
|
|
|
|
2016-11-28 21:52:15 -08:00
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
// Here, we're switching to the actual blog that we need
|
|
|
|
// to pull users from.
|
|
|
|
|
|
|
|
$switched = false;
|
|
|
|
if (function_exists('switch_to_blog')) {
|
|
|
|
$switched = switch_to_blog($blog_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!current_user_can('create_users') && !is_super_admin()) {
|
|
|
|
$result = array('error' => true, 'message' => 'user_create_no_permission', 'data' => array('multisite' => is_multisite()));
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
2018-01-26 15:50:15 +01:00
|
|
|
}
|
|
|
|
if (is_email($user["user_email"]) === false) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$result = array("error" => true, "message" => "email_invalid");
|
2018-01-26 15:50:15 +01:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
if (email_exists($user["user_email"]) && is_user_member_of_blog(email_exists($user["user_email"]), $blog_id)) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$result = array("error" => true, "message" => "email_exists");
|
2018-01-26 15:50:15 +01:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
if (username_exists($user["user_login"]) && is_user_member_of_blog(username_exists($user["user_login"]), $blog_id)) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$result = array("error" => true, "message" => "username_exists");
|
2018-01-26 15:50:15 +01:00
|
|
|
return $this->_response($result);
|
2016-11-28 21:52:15 -08:00
|
|
|
}
|
2018-01-26 15:50:15 +01:00
|
|
|
if (!validate_username($user["user_login"])) {
|
|
|
|
$result = array("error" => true, "message" => 'username_invalid');
|
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
if (isset($user['site_id']) && !current_user_can('manage_network_users')) {
|
|
|
|
$result = array("error" => true, "message" => 'user_create_no_permission');
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
if (email_exists($user["user_email"]) && !is_user_member_of_blog(email_exists($user["user_email"]), $blog_id)) {
|
|
|
|
$user_id = email_exists($user["user_email"]);
|
|
|
|
} else {
|
|
|
|
$user_id = wp_insert_user($user);
|
|
|
|
}
|
|
|
|
$role = $user['role'];
|
|
|
|
if (is_multisite()) {
|
|
|
|
add_existing_user_to_blog(array('user_id' => $user_id, 'role' => $role));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Here, we're restoring to the current (default) blog before we
|
|
|
|
// do the switched.
|
|
|
|
|
|
|
|
if (function_exists('restore_current_blog') && $switched) {
|
|
|
|
restore_current_blog();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($user_id > 0) {
|
|
|
|
$result = array("error" => false, "message" => "user_created_with_user_name", "values" => array($user['user_login']));
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
2018-01-26 15:50:15 +01:00
|
|
|
} else {
|
|
|
|
$result = array("error" => true, "message" => "user_create_failed", "values" => array($user));
|
2016-11-28 21:52:15 -08:00
|
|
|
}
|
2018-01-26 15:50:15 +01:00
|
|
|
|
|
|
|
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* [delete_user - UCP: users.delete_user]
|
|
|
|
*
|
|
|
|
* This function is used to check to make sure the user_id is valid and that it has has user delete permissions.
|
|
|
|
* If there are no issues, the user is deleted.
|
|
|
|
*
|
|
|
|
* current_user_can: This check the user permissons from UCP
|
|
|
|
* get_userdata: This get the user data on the data from user_id in the $user_id array
|
|
|
|
* wp_delete_user: Deleting users on the User ID (user_id) and, IF Specified, the Assigner ID (assign_user_id).
|
2018-01-26 15:50:15 +01:00
|
|
|
*
|
|
|
|
* @param [type] $params [description] THis is an Array of params sent over from UpdraftCentral
|
2016-11-28 21:52:15 -08:00
|
|
|
* @return [type] Array [description] This will send back an error array along with message if there are any issues with the user_id
|
|
|
|
*/
|
2018-01-26 15:50:15 +01:00
|
|
|
public function delete_user($params) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$this->_admin_include('user.php');
|
2018-01-26 15:50:15 +01:00
|
|
|
$user_id = $params['user_id'];
|
|
|
|
$assign_user_id = $params["assign_user_id"];
|
|
|
|
// Here, we're getting the current blog id. If blog id
|
|
|
|
// is passed along with the parameters then we override
|
|
|
|
// that current (default) value with the parameter blog id value.
|
|
|
|
|
|
|
|
$blog_id = get_current_blog_id();
|
|
|
|
if (isset($params['site_id']) && 0 !== $params['site_id']) $blog_id = $params['site_id'];
|
2016-11-28 21:52:15 -08:00
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
$switched = false;
|
|
|
|
if (function_exists('switch_to_blog')) {
|
|
|
|
$switched = switch_to_blog($blog_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!current_user_can('delete_users') && !is_super_admin()) {
|
|
|
|
$result = array('error' => true, 'message' => 'user_delete_no_permission', 'data' => array('multisite' => is_multisite()));
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
2018-01-26 15:50:15 +01:00
|
|
|
if (get_userdata($user_id) === false) {
|
|
|
|
$result = array("error" => true, "message" => "user_not_found");
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
2018-01-26 15:50:15 +01:00
|
|
|
}
|
2016-11-28 21:52:15 -08:00
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
if (wp_delete_user($user_id, $assign_user_id)) {
|
|
|
|
$result = array("error" => false, "message" => "user_deleted");
|
|
|
|
} else {
|
|
|
|
$result = array("error" => true, "message" => "user_delete_failed");
|
2016-11-28 21:52:15 -08:00
|
|
|
}
|
2018-01-26 15:50:15 +01:00
|
|
|
|
|
|
|
// Here, we're restoring to the current (default) blog before we
|
|
|
|
// do the switched.
|
|
|
|
|
|
|
|
if (function_exists('restore_current_blog') && $switched) {
|
|
|
|
restore_current_blog();
|
|
|
|
}
|
|
|
|
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
/**
|
|
|
|
* Edits existing user information
|
|
|
|
*
|
|
|
|
* @param array $user User information to save
|
|
|
|
* @return array An array containing the result of the current process
|
|
|
|
*/
|
|
|
|
public function edit_user($user) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$this->_admin_include('user.php');
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
// Here, we're getting the current blog id. If blog id
|
|
|
|
// is passed along with the parameters then we override
|
|
|
|
// that current (default) value with the parameter blog id value.
|
|
|
|
|
|
|
|
$blog_id = get_current_blog_id();
|
|
|
|
if (isset($user['site_id']) && 0 !== $user['site_id']) $blog_id = $user['site_id'];
|
|
|
|
|
|
|
|
// Here, we're switching to the actual blog that we need
|
|
|
|
// to apply our changes.
|
|
|
|
|
|
|
|
$switched = false;
|
|
|
|
if (function_exists('switch_to_blog')) {
|
|
|
|
$switched = switch_to_blog($blog_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!current_user_can('edit_users') && !is_super_admin() && get_current_user_id() !== $user["ID"]) {
|
|
|
|
$result = array('error' => true, 'message' => 'user_edit_no_permission', 'data' => array('multisite' => is_multisite()));
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
if (false === get_userdata($user["ID"])) {
|
|
|
|
$result = array("error" => true, "message" => "user_not_found");
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
2018-01-26 15:50:15 +01:00
|
|
|
if (get_current_user_id() == $user["ID"]) {
|
|
|
|
unset($user["role"]);
|
2016-11-28 21:52:15 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Validate Username*/
|
2018-01-26 15:50:15 +01:00
|
|
|
if (!validate_username($user["user_login"])) {
|
|
|
|
$result = array("error" => true, "message" => 'username_invalid');
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
/* Validate Email if not the same*/
|
|
|
|
|
|
|
|
$remote_user = get_userdata($user["ID"]);
|
|
|
|
$old_email = $remote_user->user_email;
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
if ($user['user_email'] !== $old_email) {
|
|
|
|
if (is_email($user['user_email']) === false) {
|
|
|
|
$result = array("error" => true, "message" => 'email_invalid');
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
if (email_exists($user['user_email'])) {
|
|
|
|
$result = array("error" => true, "message" => 'email_exists');
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
$user_id = wp_update_user($user);
|
|
|
|
if (is_wp_error($user_id)) {
|
2016-11-28 21:52:15 -08:00
|
|
|
$result = array("error" => true, "message" => "user_edit_failed_with_error", "values" => array($user_id));
|
|
|
|
} else {
|
|
|
|
$result = array("error" => false, "message" => "user_edited_with_user_name", "values" => array($user["user_login"]));
|
|
|
|
}
|
2018-01-26 15:50:15 +01:00
|
|
|
|
|
|
|
// Here, we're restoring to the current (default) blog before we
|
|
|
|
// do the switched.
|
|
|
|
|
|
|
|
if (function_exists('restore_current_blog') && $switched) {
|
|
|
|
restore_current_blog();
|
|
|
|
}
|
|
|
|
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
/**
|
|
|
|
* Retrieves available roles to be used as filter options
|
|
|
|
*
|
|
|
|
* @return array An array containing all available roles
|
|
|
|
*/
|
|
|
|
public function get_roles() {
|
2016-11-28 21:52:15 -08:00
|
|
|
$this->_admin_include('user.php');
|
2018-01-26 15:50:15 +01:00
|
|
|
$roles = array_reverse(get_editable_roles());
|
2016-11-28 21:52:15 -08:00
|
|
|
return $this->_response($roles);
|
|
|
|
}
|
|
|
|
|
2018-01-26 15:50:15 +01:00
|
|
|
/**
|
|
|
|
* Retrieves information to be use as filters
|
|
|
|
*
|
|
|
|
* @return array An array containing the filter fields and their data
|
|
|
|
*/
|
|
|
|
public function get_user_filters() {
|
2016-11-28 21:52:15 -08:00
|
|
|
$this->_admin_include('user.php');
|
2018-01-26 15:50:15 +01:00
|
|
|
|
|
|
|
// Pull sites options if available.
|
|
|
|
$sites = $this->_get_blog_sites();
|
|
|
|
|
2016-11-28 21:52:15 -08:00
|
|
|
$result = array(
|
2018-01-26 15:50:15 +01:00
|
|
|
"sites" => $sites,
|
2016-11-28 21:52:15 -08:00
|
|
|
"roles" => array_reverse(get_editable_roles()),
|
2018-01-26 15:50:15 +01:00
|
|
|
"paging" => $this->_calculate_pages(null, 0),
|
2016-11-28 21:52:15 -08:00
|
|
|
);
|
|
|
|
return $this->_response($result);
|
|
|
|
}
|
|
|
|
}
|