This repository has been archived by the owner on Apr 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Added User Meta Endpoints #3
Merged
danielbachhuber
merged 11 commits into
WP-API:master
from
kjbenk:add/user-meta-endpoints
Feb 25, 2016
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d75b058
Added User Meta Endpoints
kjbenk 561af3c
Fixed Permissions Check
kjbenk 577117c
Added Unit Tests
kjbenk 4ed1829
Fixed User Authentication Bug
kjbenk cf8a2d4
Added More Unit Tests
kjbenk ae64817
Removed references to posts
kjbenk b89e515
Added Support for Multisite
kjbenk 7521a83
Removed Whitespace
kjbenk 75f9209
Added User Meta Unit Test
kjbenk 0d73db6
Fixed Line Indent
kjbenk f1a9a7c
Improved code readability
kjbenk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<?php | ||
|
||
class WP_REST_Meta_Users_Controller extends WP_REST_Meta_Controller { | ||
|
||
/** | ||
* Associated object type. | ||
* | ||
* @var string "user" | ||
*/ | ||
protected $parent_type = 'user'; | ||
|
||
/** | ||
* Base path for parent meta type endpoints. | ||
* | ||
* @var string "users" | ||
*/ | ||
protected $parent_base = 'users'; | ||
|
||
/** | ||
* User controller class object. | ||
* | ||
* @var WP_REST_Users_Controller | ||
*/ | ||
protected $parent_controller; | ||
|
||
public function __construct() { | ||
$this->parent_controller = new WP_REST_Users_Controller(); | ||
$this->namespace = 'wp/v2'; | ||
$this->rest_base = 'meta'; | ||
} | ||
|
||
/** | ||
* Check if a given request has access to get meta for a user. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function get_items_permissions_check( $request ) { | ||
$user = get_user_by( 'id', (int) $request['parent_id'] ); | ||
|
||
if ( empty( $user ) || empty( $user->ID ) ) { | ||
return new WP_Error( 'rest_user_invalid_id', __( 'Invalid user id.' ), array( 'status' => 404 ) ); | ||
} | ||
|
||
if ( ! current_user_can( 'edit_user', $user->ID ) ) { | ||
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot view the meta for this user.' ), array( 'status' => rest_authorization_required_code() ) ); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Check if a given request has access to get a specific meta entry for a user. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function get_item_permissions_check( $request ) { | ||
return $this->get_items_permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Check if a given request has access to create a meta entry for a user. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function create_item_permissions_check( $request ) { | ||
return $this->get_items_permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Check if a given request has access to update a meta entry for a user. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function update_item_permissions_check( $request ) { | ||
return $this->get_items_permissions_check( $request ); | ||
} | ||
|
||
/** | ||
* Check if a given request has access to delete meta for a user. | ||
* | ||
* @param WP_REST_Request $request Full details about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function delete_item_permissions_check( $request ) { | ||
$user = get_user_by( 'id', (int) $request['parent_id'] ); | ||
|
||
if ( empty( $user ) || empty( $user->ID ) ) { | ||
return new WP_Error( 'rest_user_invalid_id', __( 'Invalid user id.' ), array( 'status' => 404 ) ); | ||
} | ||
|
||
if ( ! current_user_can( 'delete_user', $user->ID ) ) { | ||
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot delete the meta for this user.' ), array( 'status' => rest_authorization_required_code() ) ); | ||
} | ||
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same two comments from
get_items_permissions_check
are applicable here.