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: Comment Meta Endpoints #7
Merged
danielbachhuber
merged 5 commits into
WP-API:master
from
kjbenk:add/comment-meta-endpoints-2
Feb 26, 2016
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
fe6d58b
Added Comment Meta Endpoints
kjbenk e4c16be
Fixed Permissions Check and Unit Tests
kjbenk 5f7992d
Merge remote-tracking branch 'WP-API/master' into add/comment-meta-en…
kjbenk 83d5c56
Merge remote-tracking branch 'WP-API/master' into add/comment-meta-en…
kjbenk f68134f
Updated `delete_item_permissions_check`
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,100 @@ | ||
<?php | ||
|
||
class WP_REST_Meta_Comments_Controller extends WP_REST_Meta_Controller { | ||
/** | ||
* Associated object type. | ||
* | ||
* @var string Type "comment" | ||
*/ | ||
protected $parent_type = 'comment'; | ||
|
||
/** | ||
* Associated comment controller class object. | ||
* | ||
* @var WP_REST_Comments_Controller | ||
*/ | ||
protected $parent_controller; | ||
|
||
/** | ||
* Base path for parent meta type endpoints. | ||
* | ||
* @var string "comments" | ||
*/ | ||
protected $parent_base = 'comments'; | ||
|
||
public function __construct() { | ||
$this->parent_controller = new WP_REST_Comments_Controller(); | ||
$this->namespace = 'wp/v2'; | ||
$this->rest_base = 'meta'; | ||
} | ||
|
||
/** | ||
* Check if a given request has access to get meta for a comment. | ||
* | ||
* @param WP_REST_Request $request Full data about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function get_items_permissions_check( $request ) { | ||
$comment_id = (int) $request['parent_id']; | ||
$comment = get_comment( $comment_id ); | ||
|
||
if ( empty( $comment ) || empty( $comment->comment_ID ) ) { | ||
return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) ); | ||
} | ||
|
||
if ( ! current_user_can( 'edit_comment', $comment->comment_ID ) ) { | ||
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot view the meta for this comment.' ), array( 'status' => rest_authorization_required_code() ) ); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Check if a given request has access to get a specific meta entry for a comment. | ||
* | ||
* @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 comment. | ||
* | ||
* @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 comment. | ||
* | ||
* @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 comment. | ||
* | ||
* @param WP_REST_Request $request Full details about the request. | ||
* @return WP_Error|boolean | ||
*/ | ||
public function delete_item_permissions_check( $request ) { | ||
$comment_id = (int) $request['parent_id']; | ||
$comment = get_comment( $comment_id ); | ||
|
||
if ( empty( $comment ) || empty( $comment->comment_ID ) ) { | ||
return new WP_Error( 'rest_comment_invalid_id', __( 'Invalid comment id.' ), array( 'status' => 404 ) ); | ||
} | ||
|
||
if ( ! current_user_can( 'moderate_comments' ) ) { | ||
return new WP_Error( 'rest_forbidden', __( 'Sorry, you cannot delete the meta for this comment.' ), 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
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.
Actually, I'd think this check should be
edit_comment
again, no?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.
Maybe since the
edit_comment
cap is used when trying to delete a comment anyways.https://github.com/WordPress/WordPress/blob/06225cc394ee72035b3afa6ef380b847f1e98912/wp-admin/comment.php#L89