PluginProbe
UpdraftPlus: WP Backup & Migration Plugin / 1.16.5
UpdraftPlus: WP Backup & Migration Plugin v1.16.5
1.26.7 1.26.6 1.26.5 1.26.4 1.26.3 1.9.19 1.9.25 1.9.26 1.9.30 1.9.31 1.9.32 1.9.4 1.9.40 1.9.41 1.9.42 1.9.43 1.9.44 1.9.45 1.9.46 1.9.5 1.9.50 1.9.51 1.9.60 1.9.62 1.9.63 All 371 releases
updraftplus / central / modules / users.php

users.php in UpdraftPlus: WP Backup & Migration Plugin 1.16.5, at central/modules/users.php

633 lines 18.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('UPDRAFTCENTRAL_CLIENT_DIR')) die('No access.');
4
5 /**
6 * Handles Users Commands
7 */
8 class UpdraftCentral_Users_Commands extends UpdraftCentral_Commands {
9
10 /**
11 * Compares two user object whether one is lesser than, equal to, greater than the other
12 *
13 * @internal
14 * @param array $a First user in the comparison
15 * @param array $b Second user in the comparison
16 * @return integer Comparison results (0 = equal, -1 = less than, 1 = greater than)
17 */
18 private function compare_user_id($a, $b) {
19 if ($a->ID === $b->ID) {
20 return 0;
21 }
22
23 return ($a->ID < $b->ID) ? -1 : 1;
24 }
25
26 /**
27 * Searches users based from the keyword submitted
28 *
29 * @internal
30 * @param array $query Parameter array containing the filter and keyword fields
31 * @return array Contains the list of users found as well as the total users count
32 */
33 private function _search_users($query) {
34 $this->_admin_include('user.php');
35 $query1 = new WP_User_Query(array(
36 'orderby' => 'ID',
37 'order' => 'ASC',
38 'role'=> $query["role"],
39 'search' => '*' . esc_attr($query["search"]) . '*',
40 'search_columns' => array('user_login', 'user_email')
41 ));
42 $query2 = new WP_User_Query(array(
43 'orderby' => 'ID',
44 'order' => 'ASC',
45 'role'=> $query["role"],
46 'meta_query'=>array(
47 'relation' => 'OR',
48 array(
49 'key' => 'first_name',
50 'value' => $query["search"],
51 'compare' => 'LIKE'
52 ),
53 array(
54 'key' => 'last_name',
55 'value' => $query["search"],
56 'compare' => 'LIKE'
57 ),
58 )
59 ));
60
61 if (empty($query1->results) && empty($query2->results)) {
62 return array("message" => "users_not_found");
63 } else {
64 $found_users = array_merge($query1->results, $query2->results);
65 $temp = array();
66 foreach ($found_users as $new_user) {
67 if (!isset($temp[$new_user->ID])) {
68 $temp[$new_user->ID] = $new_user;
69 }
70 };
71
72 $users = array_values($temp);
73
74 // Sort users:
75 usort($users, array($this, 'compare_user_id'));
76 $offset = (intval($query['page_no']) * intval($query['per_page'])) - intval($query['per_page']);
77 $user_list = array_slice($users, $offset, $query['per_page']);
78
79 return array(
80 'users' => $user_list,
81 'total_users' => count($users)
82 );
83 }
84 }
85
86 /**
87 * Calculates the number of pages needed to construct the pagination links
88 *
89 * @internal
90 * @param array $query
91 * @param array $total_users The total number of users found from the WP_User_Query query
92 * @return array Contains information needed to construct the pagination links
93 */
94 private function _calculate_pages($query, $total_users) {
95
96 $per_page_options = array(10, 20, 30, 40, 50);
97
98 if (!empty($query)) {
99
100 $pages = array();
101 $page_count = ceil($total_users / $query["per_page"]);
102 if ($page_count > 1) {
103
104 for ($i = 0; $i < $page_count; $i++) {
105 if ($i + 1 == $query['page_no']) {
106 $paginator_item = array(
107 "value"=>$i+1,
108 "setting"=>"disabled"
109 );
110 } else {
111 $paginator_item = array(
112 "value"=>$i+1
113 );
114 }
115 array_push($pages, $paginator_item);
116 };
117
118 if ($query['page_no'] >= $page_count) {
119 $page_next = array(
120 "value"=>$page_count,
121 "setting"=>"disabled"
122 );
123 } else {
124 $page_next = array(
125 "value"=>$query['page_no'] + 1
126 );
127 };
128 if (1 === $query['page_no']) {
129 $page_prev = array(
130 "value"=>1,
131 "setting"=>"disabled"
132 );
133 } else {
134 $page_prev = array(
135 "value"=>$query['page_no'] - 1
136 );
137 };
138
139 return array(
140 "page_no" => $query['page_no'],
141 "per_page" => $query["per_page"],
142 "page_count" => $page_count,
143 "pages" => $pages,
144 "page_next" => $page_next,
145 "page_prev" => $page_prev,
146 "total_results" => $total_users,
147 "per_page_options" => $per_page_options
148 );
149
150 } else {
151 return array(
152 "page_no" => $query['page_no'],
153 "per_page" => $query["per_page"],
154 "page_count" => $page_count,
155 "total_results" => $total_users,
156 "per_page_options" => $per_page_options
157 );
158 }
159 } else {
160 return array(
161 "per_page_options" => $per_page_options
162 );
163 }
164 }
165
166 /**
167 * Validates whether the username exists
168 *
169 * @param array $params Contains the user name to check and validate
170 * @return array An array containing the result of the current process
171 */
172 public function check_username($params) {
173 $this->_admin_include('user.php');
174 $username = $params['user_name'];
175
176 $blog_id = get_current_blog_id();
177 if (!empty($params['site_id'])) {
178 $blog_id = $params['site_id'];
179 }
180
181
182 // Here, we're switching to the actual blog that we need
183 // to pull users from.
184
185 $switched = function_exists('switch_to_blog') ? switch_to_blog($blog_id) : false;
186
187 if (username_exists($username) && is_user_member_of_blog(username_exists($username), $blog_id)) {
188 $result = array("valid" => false, "message" => 'username_exists');
189 return $this->_response($result);
190 }
191 if (!validate_username($username)) {
192 $result = array("valid" => false, "message" => 'username_invalid');
193 return $this->_response($result);
194 }
195
196
197 // Here, we're restoring to the current (default) blog before we
198 // do the switched.
199
200 if (function_exists('restore_current_blog') && $switched) {
201 restore_current_blog();
202 }
203
204 $result = array("valid" => true, "message" => 'username_valid');
205 return $this->_response($result);
206 }
207
208 /**
209 * Pulls blog sites available
210 * for the current WP instance.
211 * If the site is a multisite, then sites under the network
212 * will be pulled, otherwise, it will return an empty array.
213 *
214 * @return Array - an array of sites
215 */
216 private function _get_blog_sites() {
217
218 if (!is_multisite()) return array();
219
220 // Initialize array container
221 $sites = $network_sites = array();
222
223 // Check to see if latest get_sites (available on WP version >= 4.6) function is
224 // available to pull any available sites from the current WP instance. If not, then
225 // we're going to use the fallback function wp_get_sites (for older version).
226 if (function_exists('get_sites') && class_exists('WP_Site_Query')) {
227 $network_sites = get_sites();
228 } else {
229 if (function_exists('wp_get_sites')) {
230 $network_sites = wp_get_sites();
231 }
232 }
233
234 // We only process if sites array is not empty, otherwise, bypass
235 // the next block.
236 if (!empty($network_sites)) {
237 foreach ($network_sites as $site) {
238
239 // Here we're checking if the site type is an array, because
240 // we're pulling the blog_id property based on the type of
241 // site returned.
242 // get_sites returns an array of object, whereas the wp_get_sites
243 // function returns an array of array.
244 $blog_id = is_array($site) ? $site['blog_id'] : $site->blog_id;
245
246
247 // We're saving the blog_id and blog name as an associative item
248 // into the sites array, that will be used as "Sites" option in
249 // the frontend.
250 $sites[$blog_id] = get_blog_details($blog_id)->blogname;
251 }
252 }
253
254 return $sites;
255 }
256
257 /**
258 * Validates whether the email exists
259 *
260 * @param array $params Contains the email to check and validate
261 * @return array An array containing the result of the current process
262 */
263 public function check_email($params) {
264 $this->_admin_include('user.php');
265 $email = $params['email'];
266
267 $blog_id = get_current_blog_id();
268 if (isset($params['site_id']) && 0 !== $params['site_id']) {
269 $blog_id = $params['site_id'];
270 }
271
272
273 // Here, we're switching to the actual blog that we need
274 // to pull users from.
275
276 $switched = false;
277 if (function_exists('switch_to_blog')) {
278 $switched = switch_to_blog($blog_id);
279 }
280
281 if (is_email($email) === false) {
282 $result = array("valid" => false, "message" => 'email_invalid');
283 return $this->_response($result);
284 }
285
286 if (email_exists($email) && is_user_member_of_blog(email_exists($email), $blog_id)) {
287 $result = array("valid" => false, "message" => 'email_exists');
288 return $this->_response($result);
289 }
290
291 // Here, we're restoring to the current (default) blog before we
292 // do the switched.
293
294 if (function_exists('restore_current_blog') && $switched) {
295 restore_current_blog();
296 }
297
298 $result = array("valid" => true, "message" => 'email_valid');
299 return $this->_response($result);
300 }
301
302 /**
303 * The get_users function pull all the users from the database
304 * based on the current search parameters/filters. Please see _search_users
305 * for the breakdown of these parameters.
306 *
307 * @param array $query Parameter array containing the filter and keyword fields
308 * @return array An array containing the result of the current process
309 */
310 public function get_users($query) {
311 $this->_admin_include('user.php');
312
313 // Here, we're getting the current blog id. If blog id
314 // is passed along with the parameters then we override
315 // that current (default) value with the parameter blog id value.
316 $blog_id = get_current_blog_id();
317 if (isset($query['site_id']) && 0 !== $query['site_id']) $blog_id = $query['site_id'];
318
319
320 // Here, we're switching to the actual blog that we need
321 // to pull users from.
322
323 $switched = false;
324 if (function_exists('switch_to_blog')) {
325 $switched = switch_to_blog($blog_id);
326 }
327
328 // Set default:
329 if (empty($query["per_page"])) {
330 $query["per_page"] = 10;
331 }
332 if (empty($query['page_no'])) {
333 $query['page_no'] = 1;
334 }
335 if (empty($query["role"])) {
336 $query["role"] = "";
337 }
338
339 $users = array();
340 $total_users = 0;
341
342 if (!empty($query["search"])) {
343 $search_results = $this->_search_users($query);
344
345 if (isset($search_results['users'])) {
346 $users = $search_results['users'];
347 $total_users = $search_results['total_users'];
348 }
349 } else {
350 $user_query = new WP_User_Query(array(
351 'orderby' => 'ID',
352 'order' => 'ASC',
353 'number' => $query["per_page"],
354 'paged'=> $query['page_no'],
355 'role'=> $query["role"]
356 ));
357
358 if (empty($user_query->results)) {
359 $result = array("message" => 'users_not_found');
360 return $this->_response($result);
361 }
362
363 $users = $user_query->results;
364 $total_users = $user_query->get_total();
365 }
366
367 foreach ($users as &$user) {
368 $user_object = get_userdata($user->ID);
369 if (method_exists($user_object, 'to_array')) {
370 $user = $user_object->to_array();
371 $user["roles"] = $user_object->roles;
372 $user["first_name"] = $user_object->first_name;
373 $user["last_name"] = $user_object->last_name;
374 $user["description"] = $user_object->description;
375 } else {
376 $user = $user_object;
377 }
378 }
379
380 $result = array(
381 "users"=>$users,
382 "paging" => $this->_calculate_pages($query, $total_users)
383 );
384
385 // Here, we're restoring to the current (default) blog before we
386 // do the switched.
387
388 if (function_exists('restore_current_blog') && $switched) {
389 restore_current_blog();
390 }
391 return $this->_response($result);
392 }
393
394 /**
395 * Creates new user for the current blog
396 *
397 * @param array $user User information to add
398 * @return array An array containing the result of the current process
399 */
400 public function add_user($user) {
401 $this->_admin_include('user.php');
402 // Here, we're getting the current blog id. If blog id
403 // is passed along with the parameters then we override
404 // that current (default) value with the parameter blog id value.
405
406
407 $blog_id = get_current_blog_id();
408 if (isset($user['site_id']) && 0 !== $user['site_id']) $blog_id = $user['site_id'];
409
410
411 // Here, we're switching to the actual blog that we need
412 // to pull users from.
413
414 $switched = false;
415 if (function_exists('switch_to_blog')) {
416 $switched = switch_to_blog($blog_id);
417 }
418
419 if (!current_user_can('create_users') && !is_super_admin()) {
420 $result = array('error' => true, 'message' => 'user_create_no_permission', 'data' => array('multisite' => is_multisite()));
421 return $this->_response($result);
422 }
423 if (is_email($user["user_email"]) === false) {
424 $result = array("error" => true, "message" => "email_invalid");
425 return $this->_response($result);
426 }
427 if (email_exists($user["user_email"]) && is_user_member_of_blog(email_exists($user["user_email"]), $blog_id)) {
428 $result = array("error" => true, "message" => "email_exists");
429 return $this->_response($result);
430 }
431 if (username_exists($user["user_login"]) && is_user_member_of_blog(username_exists($user["user_login"]), $blog_id)) {
432 $result = array("error" => true, "message" => "username_exists");
433 return $this->_response($result);
434 }
435 if (!validate_username($user["user_login"])) {
436 $result = array("error" => true, "message" => 'username_invalid');
437 return $this->_response($result);
438 }
439 if (isset($user['site_id']) && !current_user_can('manage_network_users')) {
440 $result = array("error" => true, "message" => 'user_create_no_permission');
441 return $this->_response($result);
442 }
443
444 if (email_exists($user["user_email"]) && !is_user_member_of_blog(email_exists($user["user_email"]), $blog_id)) {
445 $user_id = email_exists($user["user_email"]);
446 } else {
447 $user_id = wp_insert_user($user);
448 }
449 $role = $user['role'];
450 if (is_multisite()) {
451 add_existing_user_to_blog(array('user_id' => $user_id, 'role' => $role));
452 }
453
454 // Here, we're restoring to the current (default) blog before we
455 // do the switched.
456
457 if (function_exists('restore_current_blog') && $switched) {
458 restore_current_blog();
459 }
460
461 if ($user_id > 0) {
462 $result = array("error" => false, "message" => "user_created_with_user_name", "values" => array($user['user_login']));
463 return $this->_response($result);
464 } else {
465 $result = array("error" => true, "message" => "user_create_failed", "values" => array($user));
466 }
467
468
469 return $this->_response($result);
470 }
471
472 /**
473 * [delete_user - UCP: users.delete_user]
474 *
475 * This function is used to check to make sure the user_id is valid and that it has has user delete permissions.
476 * If there are no issues, the user is deleted.
477 *
478 * current_user_can: This check the user permissons from UCP
479 * get_userdata: This get the user data on the data from user_id in the $user_id array
480 * wp_delete_user: Deleting users on the User ID (user_id) and, IF Specified, the Assigner ID (assign_user_id).
481 *
482 * @param [type] $params [description] THis is an Array of params sent over from UpdraftCentral
483 * @return [type] Array [description] This will send back an error array along with message if there are any issues with the user_id
484 */
485 public function delete_user($params) {
486 $this->_admin_include('user.php');
487 $user_id = $params['user_id'];
488 $assign_user_id = $params["assign_user_id"];
489 // Here, we're getting the current blog id. If blog id
490 // is passed along with the parameters then we override
491 // that current (default) value with the parameter blog id value.
492
493 $blog_id = get_current_blog_id();
494 if (isset($params['site_id']) && 0 !== $params['site_id']) $blog_id = $params['site_id'];
495
496 $switched = false;
497 if (function_exists('switch_to_blog')) {
498 $switched = switch_to_blog($blog_id);
499 }
500
501 if (!current_user_can('delete_users') && !is_super_admin()) {
502 $result = array('error' => true, 'message' => 'user_delete_no_permission', 'data' => array('multisite' => is_multisite()));
503 return $this->_response($result);
504 }
505 if (get_userdata($user_id) === false) {
506 $result = array("error" => true, "message" => "user_not_found");
507 return $this->_response($result);
508 }
509
510 if (wp_delete_user($user_id, $assign_user_id)) {
511 $result = array("error" => false, "message" => "user_deleted");
512 } else {
513 $result = array("error" => true, "message" => "user_delete_failed");
514 }
515
516 // Here, we're restoring to the current (default) blog before we
517 // do the switched.
518
519 if (function_exists('restore_current_blog') && $switched) {
520 restore_current_blog();
521 }
522
523 return $this->_response($result);
524 }
525
526 /**
527 * Edits existing user information
528 *
529 * @param array $user User information to save
530 * @return array An array containing the result of the current process
531 */
532 public function edit_user($user) {
533 $this->_admin_include('user.php');
534
535 // Here, we're getting the current blog id. If blog id
536 // is passed along with the parameters then we override
537 // that current (default) value with the parameter blog id value.
538
539 $blog_id = get_current_blog_id();
540 if (isset($user['site_id']) && 0 !== $user['site_id']) $blog_id = $user['site_id'];
541
542 // Here, we're switching to the actual blog that we need
543 // to apply our changes.
544
545 $switched = false;
546 if (function_exists('switch_to_blog')) {
547 $switched = switch_to_blog($blog_id);
548 }
549
550 if (!current_user_can('edit_users') && !is_super_admin() && get_current_user_id() !== $user["ID"]) {
551 $result = array('error' => true, 'message' => 'user_edit_no_permission', 'data' => array('multisite' => is_multisite()));
552 return $this->_response($result);
553 }
554
555 if (false === get_userdata($user["ID"])) {
556 $result = array("error" => true, "message" => "user_not_found");
557 return $this->_response($result);
558 }
559 if (get_current_user_id() == $user["ID"]) {
560 unset($user["role"]);
561 }
562
563 /* Validate Username*/
564 if (!validate_username($user["user_login"])) {
565 $result = array("error" => true, "message" => 'username_invalid');
566 return $this->_response($result);
567 }
568 /* Validate Email if not the same*/
569
570 $remote_user = get_userdata($user["ID"]);
571 $old_email = $remote_user->user_email;
572
573 if ($user['user_email'] !== $old_email) {
574 if (is_email($user['user_email']) === false) {
575 $result = array("error" => true, "message" => 'email_invalid');
576 return $this->_response($result);
577 }
578
579 if (email_exists($user['user_email'])) {
580 $result = array("error" => true, "message" => 'email_exists');
581 return $this->_response($result);
582 }
583 }
584
585
586 $user_id = wp_update_user($user);
587 if (is_wp_error($user_id)) {
588 $result = array("error" => true, "message" => "user_edit_failed_with_error", "values" => array($user_id));
589 } else {
590 $result = array("error" => false, "message" => "user_edited_with_user_name", "values" => array($user["user_login"]));
591 }
592
593 // Here, we're restoring to the current (default) blog before we
594 // do the switched.
595
596 if (function_exists('restore_current_blog') && $switched) {
597 restore_current_blog();
598 }
599
600 return $this->_response($result);
601 }
602
603 /**
604 * Retrieves available roles to be used as filter options
605 *
606 * @return array An array containing all available roles
607 */
608 public function get_roles() {
609 $this->_admin_include('user.php');
610 $roles = array_reverse(get_editable_roles());
611 return $this->_response($roles);
612 }
613
614 /**
615 * Retrieves information to be use as filters
616 *
617 * @return array An array containing the filter fields and their data
618 */
619 public function get_user_filters() {
620 $this->_admin_include('user.php');
621
622 // Pull sites options if available.
623 $sites = $this->_get_blog_sites();
624
625 $result = array(
626 "sites" => $sites,
627 "roles" => array_reverse(get_editable_roles()),
628 "paging" => $this->_calculate_pages(null, 0),
629 );
630 return $this->_response($result);
631 }
632 }
633