From e19c3e977664154357146c547ae5aa08518d0724 Mon Sep 17 00:00:00 2001 From: Marko Andrijasevic Date: Mon, 27 Nov 2017 17:54:06 +0100 Subject: [PATCH] Comments: add comment history endpoint (#8222) * Comments: add comment history endpoint * Sync with latest dotcom changes --- json-endpoints.php | 1 + ...-json-api-get-comment-history-endpoint.php | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 json-endpoints/class.wpcom-json-api-get-comment-history-endpoint.php diff --git a/json-endpoints.php b/json-endpoints.php index 859955332f1ba..b9b210cfc1b18 100644 --- a/json-endpoints.php +++ b/json-endpoints.php @@ -22,6 +22,7 @@ require_once( $json_endpoints_dir . 'class.wpcom-json-api-delete-media-endpoint.php' ); require_once( $json_endpoints_dir . 'class.wpcom-json-api-get-comment-endpoint.php' ); require_once( $json_endpoints_dir . 'class.wpcom-json-api-get-comments-tree-endpoint.php' ); +require_once( $json_endpoints_dir . 'class.wpcom-json-api-get-comment-history-endpoint.php' ); require_once( $json_endpoints_dir . 'class.wpcom-json-api-get-media-endpoint.php' ); require_once( $json_endpoints_dir . 'class.wpcom-json-api-get-post-endpoint.php' ); require_once( $json_endpoints_dir . 'class.wpcom-json-api-render-shortcode-endpoint.php' ); diff --git a/json-endpoints/class.wpcom-json-api-get-comment-history-endpoint.php b/json-endpoints/class.wpcom-json-api-get-comment-history-endpoint.php new file mode 100644 index 0000000000000..37aa9206cb466 --- /dev/null +++ b/json-endpoints/class.wpcom-json-api-get-comment-history-endpoint.php @@ -0,0 +1,53 @@ + '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 ); + } +}