Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
Jetpack_JSON_API_Get_Comment_Backup_Endpoint
0.00% covered (danger)
0.00%
0 / 31
0.00% covered (danger)
0.00%
0 / 2
42
0.00% covered (danger)
0.00%
0 / 1
 validate_input
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 result
0.00% covered (danger)
0.00%
0 / 27
0.00% covered (danger)
0.00%
0 / 1
12
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2
3if ( ! defined( 'ABSPATH' ) ) {
4    exit( 0 );
5}
6
7/**
8 * The Get comment backup endpoint class.
9 *
10 * /sites/%s/comments/%d/backup -> $blog_id, $comment_id
11 *
12 * @phan-constructor-used-for-side-effects
13 */
14class Jetpack_JSON_API_Get_Comment_Backup_Endpoint extends Jetpack_JSON_API_Endpoint {
15
16    /**
17     * Needed capabilities.
18     *
19     * @var array
20     */
21    protected $needed_capabilities = array(); // This endpoint is only accessible using a site token
22
23    /**
24     * The comment ID.
25     *
26     * @var int
27     */
28    protected $comment_id;
29
30    /**
31     * Validate input
32     *
33     * @param int $comment_id - the comment ID.
34     *
35     * @return bool|WP_Error
36     */
37    public function validate_input( $comment_id ) {
38        if ( empty( $comment_id ) || ! is_numeric( $comment_id ) ) {
39            return new WP_Error( 'comment_id_not_specified', __( 'You must specify a Comment ID', 'jetpack' ), 400 );
40        }
41
42        $this->comment_id = (int) $comment_id;
43
44        return true;
45    }
46
47    /**
48     * The result.
49     *
50     * @return array|WP_Error
51     */
52    protected function result() {
53        // Disable Sync as this is a read-only operation and triggered by sync activity.
54        \Automattic\Jetpack\Sync\Actions::mark_sync_read_only();
55
56        $comment = get_comment( $this->comment_id );
57        if ( empty( $comment ) ) {
58            return new WP_Error( 'comment_not_found', __( 'Comment not found', 'jetpack' ), 404 );
59        }
60
61        $allowed_keys = array(
62            'comment_ID',
63            'comment_post_ID',
64            'comment_author',
65            'comment_author_email',
66            'comment_author_url',
67            'comment_author_IP',
68            'comment_date',
69            'comment_date_gmt',
70            'comment_content',
71            'comment_karma',
72            'comment_approved',
73            'comment_agent',
74            'comment_type',
75            'comment_parent',
76            'user_id',
77        );
78
79        $comment      = array_intersect_key( $comment->to_array(), array_flip( $allowed_keys ) );
80        $comment_meta = get_comment_meta( $comment['comment_ID'] );
81
82        return array(
83            'comment' => $comment,
84            'meta'    => is_array( $comment_meta ) ? $comment_meta : array(),
85        );
86    }
87}