PluginProbe
UpdraftCentral Dashboard / trunk
UpdraftCentral Dashboard vtrunk
0.8.33 0.7.2 0.7.3 0.7.4 0.8.0 0.8.1 0.8.10 0.8.11 0.8.12 0.8.13 0.8.14 0.8.15 0.8.16 0.8.17 0.8.18 0.8.19 0.8.2 0.8.20 0.8.21 0.8.22 0.8.23 0.8.24 0.8.25 0.8.26 0.8.27 All 51 releases
updraftcentral / classes / class-rest-users-controller.php

class-rest-users-controller.php in UpdraftCentral Dashboard trunk, at classes/class-rest-users-controller.php

137 lines 3.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) die('Access denied.');
3
4 if (!class_exists('WP_REST_Users_Controller')) {
5 include_once ABSPATH . 'wp-includes/rest-api/endpoints/class-wp-rest-users-controller.php';
6 }
7
8 /**
9 * UpdraftCentral_REST_Users_Controller class. Used to override some basic response
10 * details to integrate the remote user information.
11 *
12 * Originally copied and edited from WP_REST_Users_Controller (WordPress 5.0.3). Updated
13 * to support higher versions up to WordPress 5.4.
14 *
15 * @see WP_REST_Users_Controller
16 */
17 class UpdraftCentral_REST_Users_Controller extends WP_REST_Users_Controller {
18
19 public function __construct() {
20 parent::__construct();
21 }
22
23 /**
24 * Prepares a single user output for response.
25 *
26 * @since 4.7.0
27 *
28 * @param object $user_obj A simple (stdClass) object containing basic (public) properties of a remote user object.
29 * @param WP_REST_Request $request Request object.
30 * @param array $misc Array containing remote user data.
31 * @return WP_REST_Response Response object.
32 */
33 public function prepare_remote_item_for_response($user_obj, $request, $misc) {
34
35 // Make sure that we have a WP_User object before proceeding
36 // with the rest of the process below.
37 $user = new WP_User();
38 foreach (get_object_vars($user_obj) as $key => $value) {
39 $user->{$key} = $value;
40 }
41
42 $data = array();
43 $fields = $this->get_fields_for_response($request);
44
45 if (in_array('id', $fields, true)) {
46 $data['id'] = $user->ID;
47 }
48
49 if (in_array('username', $fields, true)) {
50 $data['username'] = $user->user_login;
51 }
52
53 if (in_array('name', $fields, true)) {
54 $data['name'] = $user->display_name;
55 }
56
57 if (in_array('first_name', $fields, true)) {
58 $data['first_name'] = $user->first_name;
59 }
60
61 if (in_array('last_name', $fields, true)) {
62 $data['last_name'] = $user->last_name;
63 }
64
65 if (in_array('email', $fields, true)) {
66 $data['email'] = $user->user_email;
67 }
68
69 if (in_array('url', $fields, true)) {
70 $data['url'] = $user->user_url;
71 }
72
73 if (in_array('description', $fields, true)) {
74 $data['description'] = $user->description;
75 }
76
77 if (in_array('link', $fields, true)) {
78 $data['link'] = $misc['link'];
79 }
80
81 if (in_array('locale', $fields, true)) {
82 $data['locale'] = $misc['locale'];
83 }
84
85 if (in_array('nickname', $fields, true)) {
86 $data['nickname'] = !empty($user->nickname) ? $user->nickname : '';
87 }
88
89 if (in_array('slug', $fields, true)) {
90 $data['slug'] = $user->user_nicename;
91 }
92
93 if (in_array('roles', $fields, true)) {
94 $roles = $user->roles;
95 // Just in case other plugins were messing around with the roles array.
96 if (is_object($roles)) {
97 $filtered_roles = array_filter(get_object_vars($roles), 'is_string');
98 $roles = ($filtered_roles && !empty($filtered_roles)) ? $filtered_roles : array();
99 }
100
101 // Defensively call array_values() to ensure an array is returned.
102 $data['roles'] = array_values($roles);
103 }
104
105 if (in_array('registered_date', $fields, true)) {
106 $data['registered_date'] = $misc['registered_date'];
107 }
108
109 if (in_array('capabilities', $fields, true)) {
110 $data['capabilities'] = (object) $user->allcaps;
111 }
112
113 if (in_array('extra_capabilities', $fields, true)) {
114 $data['extra_capabilities'] = (object) $user->caps;
115 }
116
117 if (in_array('avatar_urls', $fields, true)) {
118 $data['avatar_urls'] = rest_get_avatar_urls($user->user_email);
119 }
120
121 if (in_array('meta', $fields, true)) {
122 $data['meta'] = $this->meta->get_value($user->ID, $request);
123 }
124
125 $context = !empty($request['context']) ? $request['context'] : 'embed';
126
127 $data = $this->add_additional_fields_to_object($data, $request);
128 $data = $this->filter_response_by_context($data, $context);
129
130 // Wrap the data in a response object.
131 $response = rest_ensure_response($data);
132 $response->add_links($this->prepare_links($user));
133
134 return $response;
135 }
136 }
137