Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_JSON_API_GET_Comment_History_Endpoint
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
0.00% covered (danger)
0.00%
0 / 1
 callback
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
42
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Endpoint: /sites/%s/comment-history/%d
4 */
5
6if ( ! defined( 'ABSPATH' ) ) {
7    exit( 0 );
8}
9
10new WPCOM_JSON_API_GET_Comment_History_Endpoint(
11    array(
12        'description'     => 'Get the audit history for given comment',
13        'group'           => 'comments',
14        'stat'            => 'comments:1:comment-history',
15        'method'          => 'GET',
16        'path'            => '/sites/%s/comment-history/%d',
17        'path_labels'     => array(
18            '$site'       => '(int|string) Site ID or domain',
19            '$comment_ID' => '(int) The comment ID',
20        ),
21
22        'example_request' => 'https://public-api.wordpress.com/rest/v1/sites/en.blog.wordpress.com/comment-history/11',
23
24        'response_format' => array(
25            'comment_history' => '(array) Array of arrays representing the comment history objects.',
26        ),
27    )
28);
29/**
30 * GET Comment History endpoint.
31 *
32 * @phan-constructor-used-for-side-effects
33 */
34class WPCOM_JSON_API_GET_Comment_History_Endpoint extends WPCOM_JSON_API_Endpoint {
35    /**
36     *
37     * API callback.
38     *
39     * @param string $path - the path.
40     * @param int    $blog_id - the blog ID.
41     * @param int    $comment_id - the comment ID.
42     */
43    public function callback( $path = '', $blog_id = 0, $comment_id = 0 ) {
44        $blog_id = $this->api->switch_to_blog_and_validate_user( $this->api->get_blog_id( $blog_id ) );
45
46        if ( is_wp_error( $blog_id ) ) {
47            return $blog_id;
48        }
49
50        if ( ! get_current_user_id() ) {
51            return new WP_Error( 'authorization_required', 'An active access token must be used to retrieve comment history.', 403 );
52        }
53
54        if ( ! current_user_can_for_site( $blog_id, 'edit_posts' ) ) {
55            return new WP_Error( 'authorization_required', 'You are not authorized to view comment history on this blog.', 403 );
56        }
57
58        if ( ! method_exists( 'Akismet', 'get_comment_history' ) ) {
59            return new WP_Error( 'akismet_required', 'Akismet plugin must be active for this feature to work', 503 );
60        }
61
62        $comment_history = Akismet::get_comment_history( $comment_id );
63
64        foreach ( $comment_history as &$item ) {
65            // Times are stored as floating point values in microseconds.
66            // We don't need that precision on the client so let's get rid of the decimal part.
67            $item['time'] = (int) $item['time'];
68        }
69
70        return array( 'comment_history' => $comment_history );
71    }
72}