class.wpcom-json-api-get-comment-history-endpoint.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. <?php
  2. new WPCOM_JSON_API_GET_Comment_History_Endpoint( array(
  3. 'description' => 'Get the audit history for given comment',
  4. 'group' => 'comments',
  5. 'stat' => 'comments:1:comment-history',
  6. 'method' => 'GET',
  7. 'path' => '/sites/%s/comment-history/%d',
  8. 'path_labels' => array(
  9. '$site' => '(int|string) Site ID or domain',
  10. '$comment_ID' => '(int) The comment ID'
  11. ),
  12. 'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/comment-history/11',
  13. 'response_format' => array(
  14. 'comment_history' => '(array) Array of arrays representing the comment history objects.'
  15. )
  16. ) );
  17. class WPCOM_JSON_API_GET_Comment_History_Endpoint extends WPCOM_JSON_API_Endpoint {
  18. // /sites/%s/comment-history/%d
  19. public function callback( $path = '', $blog_id = 0, $comment_id = 0 ) {
  20. $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
  21. if ( is_wp_error( $blog_id ) ) {
  22. return $blog_id;
  23. }
  24. if ( ! get_current_user_id() ) {
  25. return new WP_Error( 'authorization_required', 'An active access token must be used to retrieve comment history.', 403 );
  26. }
  27. if ( ! current_user_can_for_blog( $blog_id, 'edit_posts' ) ) {
  28. return new WP_Error( 'authorization_required', 'You are not authorized to view comment history on this blog.', 403 );
  29. }
  30. if ( ! method_exists( 'Akismet', 'get_comment_history' ) ) {
  31. return new WP_Error( 'akismet_required', 'Akismet plugin must be active for this feature to work', 503 );
  32. }
  33. $comment_history = Akismet::get_comment_history( $comment_id );
  34. foreach ( $comment_history as &$item ) {
  35. // Times are stored as floating point values in microseconds.
  36. // We don't need that precision on the client so let's get rid of the decimal part.
  37. $item['time'] = intval( $item['time'] );
  38. }
  39. return array( 'comment_history' => $comment_history );
  40. }
  41. }