Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from WP-API/users
Support manipulating users
- Loading branch information
Showing
4 changed files
with
287 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
<?php | ||
/** | ||
* Base object | ||
* | ||
* @package WordPress API Client | ||
* @subpackage Entities | ||
*/ | ||
|
||
/** | ||
* Base object | ||
* | ||
* @package WordPress API Client | ||
* @subpackage Entities | ||
*/ | ||
abstract class WPAPI_Object { | ||
/** | ||
* API handler | ||
* | ||
* @var WPAPI | ||
*/ | ||
protected $api; | ||
|
||
/** | ||
* Data container | ||
* | ||
* @var array | ||
*/ | ||
protected $data; | ||
|
||
/** | ||
* Keys that have been changed since last update | ||
* | ||
* @var array | ||
*/ | ||
protected $changed = array(); | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param WPAPI $api API object | ||
* @param array $data Data to initialise the object with | ||
*/ | ||
public function __construct($api, $data = array()) { | ||
$this->api = $api; | ||
$this->data = (array) $data; | ||
} | ||
|
||
/** | ||
* Get a property | ||
* | ||
* See the specification for data keys/values returned by the API. | ||
* | ||
* @param string $key Key to retrieve | ||
* @return mixed data for the key | ||
*/ | ||
public function __get($key) { | ||
if (!isset($this->data[$key])) { | ||
return null; | ||
} | ||
return $this->data[$key]; | ||
} | ||
|
||
/** | ||
* Set a property | ||
* | ||
* @param string $key Key to replace | ||
* @param mixed $value Value for the key | ||
*/ | ||
public function __set($key, $value) { | ||
$this->data[$key] = $value; | ||
$this->changed[$key] = true; | ||
} | ||
|
||
/** | ||
* Get the raw internal post data | ||
* | ||
* Avoid use in favour of accessing via the properties instead. | ||
* | ||
* @return array Raw data from the API | ||
*/ | ||
public function getRawData() { | ||
return $this->data; | ||
} | ||
|
||
/** | ||
* Update the object | ||
* | ||
* @throws Requests_Exception Failed to update the object | ||
* @throws Exception Failed to decode JSON | ||
* @param array $data Data to update, in addition to already changed data | ||
* @param boolean $use_json Use a JSON body rather than form-encoded data | ||
* @param boolean $check_modification Should we change the last modified date? Avoids editing conflicts | ||
* @return boolean Was the update successful? | ||
*/ | ||
public function update($data = array(), $use_json = true, $check_modification = true) { | ||
$keys = array_keys( $this->changed ); | ||
$values = array(); | ||
|
||
if ( ! empty( $data ) ) { | ||
$values = array_merge( $values, array_diff_assoc($data, $this->data) ); | ||
} | ||
|
||
// Don't send the ID with the data | ||
unset($values['ID']); | ||
|
||
$headers = array(); | ||
|
||
if ($use_json) { | ||
$body = json_encode($values); | ||
$headers['Content-Type'] = 'application/json'; | ||
} | ||
else { | ||
$body = array( | ||
'data' => $values, | ||
); | ||
} | ||
|
||
if ($check_modification) { | ||
$headers['If-Unmodified-Since'] = date( DateTime::RFC1123, strtotime( $this->modified ) ); | ||
} | ||
$response = Requests::put($this->meta['links']['self'], $headers, $body, $this->api->getDefaultOptions()); | ||
$response->throw_for_status(); | ||
|
||
$this->data = json_decode($response->body, true); | ||
$this->changed = array(); | ||
|
||
return true; | ||
} | ||
|
||
/** | ||
* Delete the object | ||
* | ||
* @throws Requests_Exception Failed to delete object | ||
* @return boolean Was the deletion successful? | ||
*/ | ||
public function delete($permanent = false) { | ||
$url = $this->meta['links']['self']; | ||
if ($permanent) { | ||
if (strpos($url, '?') !== false) { | ||
$url .= '&force=true'; | ||
} | ||
else { | ||
$url .= '?force=true'; | ||
} | ||
} | ||
$response = Requests::delete($url, array(), $this->api->getDefaultOptions()); | ||
$response->throw_for_status(); | ||
|
||
$this->data = array(); | ||
$this->changed = array(); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
/** | ||
* User entity object | ||
* | ||
* @package WordPress API Client | ||
* @subpackage Entities | ||
*/ | ||
class WPAPI_User extends WPAPI_Object { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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 ); | ||
} | ||
} |