class-api-v2.php
10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
<?php
namespace um\core\rest;
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
if ( ! class_exists( 'um\core\rest\API_v2' ) ) {
/**
* Class API_v2
* @package um\core\rest
*/
class API_v2 extends API {
/**
*
*/
const VERSION = '2.0';
/**
* REST_API constructor.
*/
public function __construct() {
parent::__construct();
add_filter( 'query_vars', array( $this, 'query_vars' ) );
}
/**
* Registers query vars for API access
*
* @param $vars
*
* @return array
*/
public function query_vars( $vars ) {
$vars[] = 'um_key';
$vars[] = 'um_token';
$vars[] = 'um_format';
$vars[] = 'um_query';
$vars[] = 'um_type';
$vars[] = 'um_data';
$vars[] = 'um_fields';
$vars[] = 'um_value';
$vars[] = 'um_number';
$vars[] = 'um_id';
$vars[] = 'um_email';
$vars[] = 'um_orderby';
$vars[] = 'um_order';
$vars[] = 'um_include';
$vars[] = 'um_exclude';
$this->vars = $vars;
return $vars;
}
/**
* Validate the API request
*/
protected function validate_request() {
global $wp_query;
$this->override = false;
// Make sure we have both user and api key
if ( ! empty( $wp_query->query_vars['um-api'] ) ) {
if ( empty( $wp_query->query_vars['um_token'] ) || empty( $wp_query->query_vars['um_key'] ) ) {
$this->missing_auth();
}
// Retrieve the user by public API key and ensure they exist
if ( ! ( $user = $this->get_user( $wp_query->query_vars['um_key'] ) ) ) {
$this->invalid_key();
} else {
$token = urldecode( $wp_query->query_vars['um_token'] );
$secret = get_user_meta( $user, 'um_user_secret_key', true );
$public = urldecode( $wp_query->query_vars['um_key'] );
if ( hash_equals( md5( $secret . $public ), $token ) ) {
$this->is_valid_request = true;
} else {
$this->invalid_auth();
}
}
}
}
/**
* Retrieve the user ID based on the public key provided
*
* @param string $key
*
* @return bool|mixed|null|string
*/
public function get_user( $key = '' ) {
global $wpdb, $wp_query;
if ( empty( $key ) ) {
$key = urldecode( $wp_query->query_vars['um_key'] );
}
if ( empty( $key ) ) {
return false;
}
$user = get_transient( md5( 'um_api_user_' . $key ) );
if ( false === $user ) {
$user = $wpdb->get_var( $wpdb->prepare(
"SELECT user_id
FROM $wpdb->usermeta
WHERE meta_key = 'um_user_public_key' AND
meta_value = %s
LIMIT 1",
$key
) );
set_transient( md5( 'um_api_user_' . $key ) , $user, DAY_IN_SECONDS );
}
if ( $user != NULL ) {
$this->user_id = $user;
return $user;
}
return false;
}
/**
* Process Get users API Request
*
* @param array $args
*
* @return array
*/
public function get_users( $args ) {
$response = array();
$number = array_key_exists( 'um_number', $args ) && is_numeric( $args['um_number'] ) ? absint( $args['um_number'] ) : 10;
$orderby = array_key_exists( 'um_orderby', $args ) ? sanitize_key( $args['um_orderby'] ) : 'user_registered';
$order = array_key_exists( 'um_order', $args ) ? sanitize_key( $args['um_order'] ) : 'desc';
$loop_a = array(
'number' => $number,
'orderby' => $orderby,
'order' => $order,
);
if ( array_key_exists( 'um_include', $args ) ) {
$include = explode( ',', sanitize_text_field( $args['um_include'] ) );
$loop_a['include'] = $include;
}
if ( array_key_exists( 'um_exclude', $args ) ) {
$exclude = explode( ',', sanitize_text_field( $args['um_exclude'] ) );
$loop_a['exclude'] = $exclude;
}
$loop = get_users( $loop_a );
foreach ( $loop as $user ) {
unset( $user->data->user_status, $user->data->user_activation_key, $user->data->user_pass );
um_fetch_user( $user->ID );
foreach ( $user as $key => $val ) {
if ( 'data' !== $key ) {
continue;
}
$val->roles = $user->roles;
$val->first_name = um_user( 'first_name' );
$val->last_name = um_user( 'last_name' );
$val->account_status = um_user( 'account_status' );
$val->profile_pic_original = um_get_user_avatar_url( '', 'original' );
$val->profile_pic_normal = um_get_user_avatar_url( '', 200 );
$val->profile_pic_small = um_get_user_avatar_url( '', 40 );
$val->cover_photo = $this->getsrc( um_user( 'cover_photo', 1000 ) );
/** This filter is documented in includes/core/rest/class-api-v1.php */
$response[ $user->ID ] = apply_filters( 'um_rest_userdata', $val, $user->ID );
}
}
return $response;
}
/**
* Update user API query
*
* @param $args
*
* @return array
*/
public function update_user( $args ) {
$response = array();
$error = array();
if ( empty( $args['um_id'] ) ) {
$error['error'] = __( 'You must provide a user ID', 'ultimate-member' );
return $error;
}
if ( empty( $args['um_data'] ) ) {
$error['error'] = __( 'You need to provide data to update', 'ultimate-member' );
return $error;
}
if ( ! array_key_exists( 'um_value', $args ) ) {
$error['error'] = __( 'You need to provide value to update', 'ultimate-member' );
return $error;
}
$id = absint( $args['um_id'] );
$data = sanitize_text_field( $args['um_data'] );
$value = sanitize_text_field( $args['um_value'] );
um_fetch_user( $id );
switch ( $data ) {
case 'status':
UM()->user()->set_status( $value );
$response['success'] = __( 'User status has been changed.', 'ultimate-member' );
break;
case 'role':
$wp_user_object = new \WP_User( $id );
$old_roles = $wp_user_object->roles;
$wp_user_object->set_role( $value );
/** This action is documented in includes/core/class-user.php */
do_action( 'um_after_member_role_upgrade', array( $value ), $old_roles, $id );
$response['success'] = __( 'User role has been changed.', 'ultimate-member' );
break;
default:
update_user_meta( $id, $data, $value );
$response['success'] = __( 'User meta has been changed.', 'ultimate-member' );
break;
}
return $response;
}
/**
* Process delete user via API.
*
* @param array $args
*
* @return array
*/
public function delete_user( $args ) {
$response = array();
$error = array();
if ( empty( $args['um_id'] ) ) {
$error['error'] = __( 'You must provide a user ID', 'ultimate-member' );
return $error;
}
$id = absint( $args['um_id'] );
$user = get_userdata( $id );
if ( ! $user ) {
$error['error'] = __( 'Invalid user specified', 'ultimate-member' );
return $error;
}
um_fetch_user( $id );
UM()->user()->delete();
$response['success'] = __( 'User has been successfully deleted.', 'ultimate-member' );
return $response;
}
/**
* Process Get user API Request
*
* @param $args
*
* @return array
*/
public function get_auser( $args ) {
$response = array();
$error = array();
if ( empty( $args['um_id'] ) ) {
$error['error'] = __( 'You must provide a user ID', 'ultimate-member' );
return $error;
}
$id = absint( $args['um_id'] );
$user = get_userdata( $id );
if ( ! $user ) {
$error['error'] = __( 'Invalid user specified', 'ultimate-member' );
return $error;
}
unset( $user->data->user_status, $user->data->user_activation_key, $user->data->user_pass );
um_fetch_user( $user->ID );
if ( array_key_exists( 'um_fields', $args ) ) {
$fields = explode( ',', sanitize_text_field( $args['um_fields'] ) );
$response['ID'] = $user->ID;
$response['username'] = $user->user_login;
foreach ( $fields as $field ) {
switch ( $field ) {
default:
$profile_data = um_profile( $field );
$response[ $field ] = $profile_data ? $profile_data : '';
/** This filter is documented in includes/core/rest/class-api-v1.php */
$response = apply_filters( 'um_rest_get_auser', $response, $field, $user->ID );
break;
case 'cover_photo':
$response['cover_photo'] = $this->getsrc( um_user( 'cover_photo', 1000 ) );
break;
case 'profile_pic':
$response['profile_pic_original'] = um_get_user_avatar_url( '', 'original' );
$response['profile_pic_normal'] = um_get_user_avatar_url( '', 200 );
$response['profile_pic_small'] = um_get_user_avatar_url( '', 40 );
break;
case 'status':
$response['status'] = um_user( 'account_status' );
break;
case 'role':
//get priority role here
$response['role'] = um_user( 'role' );
break;
case 'email':
case 'user_email':
$response['email'] = um_user( 'user_email' );
break;
}
}
} else {
foreach ( $user as $key => $val ) {
if ( 'data' !== $key ) {
continue;
}
$val->roles = $user->roles;
$val->first_name = um_user( 'first_name' );
$val->last_name = um_user( 'last_name' );
$val->account_status = um_user( 'account_status' );
$val->profile_pic_original = um_get_user_avatar_url( '', 'original' );
$val->profile_pic_normal = um_get_user_avatar_url( '', 200 );
$val->profile_pic_small = um_get_user_avatar_url( '', 40 );
$val->cover_photo = $this->getsrc( um_user( 'cover_photo', 1000 ) );
/** This filter is documented in includes/core/rest/class-api-v1.php */
$response = apply_filters( 'um_rest_userdata', $val, $user->ID );
}
}
return $response;
}
/**
* Get source
*
* @param $image
*
* @return string
*/
public function getsrc( $image ) {
if ( preg_match( '/<img.+?src(?: )*=(?: )*[\'"](.*?)[\'"]/si', $image, $arr_result ) ) {
return $arr_result[1];
}
return '';
}
/**
* Retrieve the output format
*/
public function get_output_format() {
global $wp_query;
$format = isset( $wp_query->query_vars['um_format'] ) ? $wp_query->query_vars['um_format'] : 'json';
/** This filter is documented in includes/core/rest/class-api-v1.php */
return apply_filters( 'um_api_output_format', $format );
}
}
}