Skip to content

Commit

Permalink
Merge pull request #2 from WP-API/users
Browse files Browse the repository at this point in the history
Support manipulating users
  • Loading branch information
rmccue committed May 27, 2014
2 parents eac5440 + 645ef01 commit 105f2b9
Show file tree
Hide file tree
Showing 4 changed files with 287 additions and 0 deletions.
4 changes: 4 additions & 0 deletions library/WPAPI.php
Expand Up @@ -37,6 +37,9 @@ class WPAPI {
const ROUTE_POST = '/posts/%d';
const ROUTE_MEDIA = '/media';
const ROUTE_MEDIA_SINGLE = '/media/%d';
const ROUTE_USERS = '/users';
const ROUTE_USER = '/users/%d';
const ROUTE_USER_CURRENT = '/users/me';

/**
* Constructor
Expand Down Expand Up @@ -87,6 +90,7 @@ public function __get($key) {
$classes = array(
'index' => 'WPAPI_Index',
'posts' => 'WPAPI_Posts',
'users' => 'WPAPI_Users',
);

if (!isset($classes[$key])) {
Expand Down
153 changes: 153 additions & 0 deletions library/WPAPI/Object.php
@@ -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;
}
}
10 changes: 10 additions & 0 deletions library/WPAPI/User.php
@@ -0,0 +1,10 @@
<?php

/**
* User entity object
*
* @package WordPress API Client
* @subpackage Entities
*/
class WPAPI_User extends WPAPI_Object {
}
120 changes: 120 additions & 0 deletions library/WPAPI/Users.php
@@ -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 );
}
}

0 comments on commit 105f2b9

Please sign in to comment.