/
Users.php
120 lines (105 loc) · 2.81 KB
/
Users.php
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
<?php
/**
* Users collection
*
* @package WordPress API Client
* @subpackage Collections
*/
/**
* Users collection
*
* @package WordPress API Client
* @subpackage Collections
*/
class WPAPI_Users implements WPAPI_Collection {
/**
* API object
*
* @var WPAPI
*/
protected $api;
/**
* Constructor
*
* @param WPAPI $api API handler object
*/
public function __construct($api) {
$this->api = $api;
}
/**
* Get all users
*
* @return array List of WPAPI_User objects
*/
public function getAll() {
$response = $this->api->get( WPAPI::ROUTE_USERS );
$users = json_decode( $response->body, true );
foreach ( $users as &$user ) {
$user = new WPAPI_User( $this->api, $user );
}
return $users;
}
/**
* Get a single user
*
* @throws Requests_Exception Failed to retrieve the user
* @throws Exception Failed to decode JSON
* @param int $id User ID
* @return WPAPI_User
*/
public function get($id, $will_edit = false) {
$url = sprintf( WPAPI::ROUTE_USER, $id );
if ($will_edit) {
$url .= '?context=edit';
}
$response = $this->api->get( $url );
$response->throw_for_status();
$data = json_decode( $response->body, true );
$has_error = ( function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE );
if ( ( ! $has_error && $data === null ) || $has_error ) {
throw new Exception( $response->body );
}
return new WPAPI_User( $this->api, $data );
}
/**
* Get the current user
*
* @throws Requests_Exception Failed to retrieve the user
* @throws Exception Failed to decode JSON
* @return WPAPI_User
*/
public function getCurrent($will_edit = false) {
$url = WPAPI::ROUTE_USER_CURRENT;
if ($will_edit) {
$url .= '?context=edit';
}
$response = $this->api->get( $url );
$response->throw_for_status();
$data = json_decode( $response->body, true );
$has_error = ( function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE );
if ( ( ! $has_error && $data === null ) || $has_error ) {
throw new Exception( $response->body );
}
return new WPAPI_User( $this->api, $data );
}
/**
* Create a new user
*
* @throws Requests_Exception Failed to retrieve the user
* @throws Exception Failed to decode JSON
* @param array $data User data to create
* @return WPAPI_User
*/
public function create( $data ) {
$data = json_encode( $data );
$headers = array( 'Content-Type' => 'application/json' );
$response = $this->api->post( WPAPI::ROUTE_USERS, $headers, $data );
$response->throw_for_status();
$data = json_decode( $response->body, true );
$has_error = ( function_exists('json_last_error') && json_last_error() !== JSON_ERROR_NONE );
if ( ( ! $has_error && $data === null ) || $has_error ) {
throw new Exception( $response->body );
}
return new WPAPI_User( $this->api, $data );
}
}