-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Comments: add comment history endpoint (#8222)
* Comments: add comment history endpoint * Sync with latest dotcom changes
- Loading branch information
1 parent
1f532fb
commit e19c3e9
Showing
2 changed files
with
54 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
53 changes: 53 additions & 0 deletions
53
json-endpoints/class.wpcom-json-api-get-comment-history-endpoint.php
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,53 @@ | ||
<?php | ||
|
||
new WPCOM_JSON_API_GET_Comment_History_Endpoint( array( | ||
'description' => 'Get the audit history for given comment', | ||
'group' => 'comments', | ||
'stat' => 'comments:1:comment-history', | ||
'method' => 'GET', | ||
'path' => '/sites/%s/comment-history/%d', | ||
'path_labels' => array( | ||
'$site' => '(int|string) Site ID or domain', | ||
'$comment_ID' => '(int) The comment ID' | ||
), | ||
|
||
'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/comment-history/11', | ||
|
||
'response_format' => array( | ||
'comment_history' => '(array) Array of arrays representing the comment history objects.' | ||
) | ||
) ); | ||
|
||
class WPCOM_JSON_API_GET_Comment_History_Endpoint extends WPCOM_JSON_API_Endpoint { | ||
|
||
// /sites/%s/comment-history/%d | ||
public function callback( $path = '', $blog_id = 0, $comment_id = 0 ) { | ||
$blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) ); | ||
|
||
if ( is_wp_error( $blog_id ) ) { | ||
return $blog_id; | ||
} | ||
|
||
if ( ! get_current_user_id() ) { | ||
return new WP_Error( 'authorization_required', 'An active access token must be used to retrieve comment history.', 403 ); | ||
} | ||
|
||
if ( ! current_user_can_for_blog( $blog_id, 'edit_posts' ) ) { | ||
return new WP_Error( 'authorization_required', 'You are not authorized to view comment history on this blog.', 403 ); | ||
} | ||
|
||
if ( ! method_exists( 'Akismet', 'get_comment_history' ) ) { | ||
return new WP_Error( 'akismet_required', 'Akismet plugin must be active for this feature to work', 503 ); | ||
} | ||
|
||
$comment_history = Akismet::get_comment_history( $comment_id ); | ||
|
||
foreach ( $comment_history as &$item ) { | ||
// Times are stored as floating point values in microseconds. | ||
// We don't need that precision on the client so let's get rid of the decimal part. | ||
$item['time'] = intval( $item['time'] ); | ||
} | ||
|
||
return array( 'comment_history' => $comment_history ); | ||
} | ||
} |